Skip to content

Commit 086a3e4

Browse files
authored
Feat: Provide simple API that sets only Access-Control-Allow-Origin (#4) (#6)
* chore: bump version to 0.3.0 * feat: add APIs to allow origins only - Adds the following functions that configure only `Access-Control-Allow-Origin` headers: - `makeIntegrationResponsesAllowOrigin`: for `IntegrationResponse`s - `makeMethodResponsesAllowOrigin`: for `MethodResponse`s issue #4 * docs: update API documentation * docs: update README - Bumps version to v0.3.0 in the installation instruction.
1 parent ff86110 commit 086a3e4

10 files changed

+145
-12
lines changed

README.ja.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ CDKでAPI Gatewayに繰り返しCORS設定をするのが面倒でしょうが
1010

1111
## 背景
1212

13-
API GatewayでREST APIを記述するとき、私はいつもCORSを扱うためのユーティリティ関数をいくつか書いています
13+
API GatewayでREST APIを記述するとき、いつもCORSを扱うためのユーティリティ関数をいくつか書いています
1414
これらの関数はとてもシンプルですが、新しいプロジェクトを始めるたびにもう毎回準備したくありません。
1515

1616
## インストール方法
1717

1818
このレポジトリをNodeの依存関係に追加してください。
1919

2020
```sh
21-
npm install https://github.com/codemonger-io/cdk-cors-utils.git#v0.2.0
21+
npm install https://github.com/codemonger-io/cdk-cors-utils.git#v0.3.0
2222
```
2323

2424
インストールしたモジュールは`cdk2-cors-utils`として以下のように利用できます。

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ These functions are quite simple though, I no longer want to prepare them everyt
1818
Please add this repository to your Node dependencies.
1919

2020
```sh
21-
npm install https://github.com/codemonger-io/cdk-cors-utils.git#v0.2.0
21+
npm install https://github.com/codemonger-io/cdk-cors-utils.git#v0.3.0
2222
```
2323

2424
The installed module will be available as `cdk2-cors-utils` like the following,

api-docs/cdk2-cors-utils.api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import { aws_apigateway } from 'aws-cdk-lib';
99
// @beta
1010
export function makeIntegrationResponsesAllowCors<T extends aws_apigateway.IntegrationResponse>(responses: T[]): T[];
1111

12+
// @beta
13+
export function makeIntegrationResponsesAllowOrigin<T extends aws_apigateway.IntegrationResponse>(responses: T[], origin: string): T[];
14+
1215
// @beta
1316
export function makeMethodResponsesAllowCors<T extends aws_apigateway.MethodResponse>(responses: T[]): T[];
1417

18+
// @beta
19+
export function makeMethodResponsesAllowOrigin<T extends aws_apigateway.MethodResponse>(responses: T[]): T[];
20+
1521
```

api-docs/markdown/cdk2-cors-utils.makeintegrationresponsesallowcors.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
## makeIntegrationResponsesAllowCors() function
66

7-
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
7+
> This API is provided as a beta preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
88
>
99
1010
Makes given [IntegrationResponse](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IntegrationResponse.html)<!-- -->s allow CORS.
1111

12-
<b>Signature:</b>
12+
**Signature:**
1313

1414
```typescript
1515
export declare function makeIntegrationResponsesAllowCors<T extends apigateway.IntegrationResponse>(responses: T[]): T[];
@@ -21,7 +21,7 @@ export declare function makeIntegrationResponsesAllowCors<T extends apigateway.I
2121
| --- | --- | --- |
2222
| responses | T\[\] | Integration response settings to allow CORS. |
2323

24-
<b>Returns:</b>
24+
**Returns:**
2525

2626
T\[\]
2727

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [cdk2-cors-utils](./cdk2-cors-utils.md) &gt; [makeIntegrationResponsesAllowOrigin](./cdk2-cors-utils.makeintegrationresponsesalloworigin.md)
4+
5+
## makeIntegrationResponsesAllowOrigin() function
6+
7+
> This API is provided as a beta preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
8+
>
9+
10+
Makes given [IntegrationResponse](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IntegrationResponse.html)<!-- -->s allow an origin.
11+
12+
**Signature:**
13+
14+
```typescript
15+
export declare function makeIntegrationResponsesAllowOrigin<T extends apigateway.IntegrationResponse>(responses: T[], origin: string): T[];
16+
```
17+
18+
## Parameters
19+
20+
| Parameter | Type | Description |
21+
| --- | --- | --- |
22+
| responses | T\[\] | Integration response settings to allow an origin. |
23+
| origin | string | Origin to allow. If it does not start with a quotation mark, this function surrounds it with quotation marks. |
24+
25+
**Returns:**
26+
27+
T\[\]
28+
29+
`responses` with updated [responseParameters](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IntegrationResponse.html#responseparameters)<!-- -->.
30+
31+
## Remarks
32+
33+
Configures `Access-Control-Allow-Origin` response header to accept `origin`<!-- -->.
34+

api-docs/markdown/cdk2-cors-utils.makemethodresponsesallowcors.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
## makeMethodResponsesAllowCors() function
66

7-
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
7+
> This API is provided as a beta preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
88
>
99
1010
Makes given [MethodResponse](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.MethodResponse.html)<!-- -->s allow CORS.
1111

12-
<b>Signature:</b>
12+
**Signature:**
1313

1414
```typescript
1515
export declare function makeMethodResponsesAllowCors<T extends apigateway.MethodResponse>(responses: T[]): T[];
@@ -21,7 +21,7 @@ export declare function makeMethodResponsesAllowCors<T extends apigateway.Method
2121
| --- | --- | --- |
2222
| responses | T\[\] | Method response settings to allow CORS. |
2323

24-
<b>Returns:</b>
24+
**Returns:**
2525

2626
T\[\]
2727

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [cdk2-cors-utils](./cdk2-cors-utils.md) &gt; [makeMethodResponsesAllowOrigin](./cdk2-cors-utils.makemethodresponsesalloworigin.md)
4+
5+
## makeMethodResponsesAllowOrigin() function
6+
7+
> This API is provided as a beta preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
8+
>
9+
10+
Makes given [MethodResponse](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.MethodResponse.html)<!-- -->s allow an origin.
11+
12+
**Signature:**
13+
14+
```typescript
15+
export declare function makeMethodResponsesAllowOrigin<T extends apigateway.MethodResponse>(responses: T[]): T[];
16+
```
17+
18+
## Parameters
19+
20+
| Parameter | Type | Description |
21+
| --- | --- | --- |
22+
| responses | T\[\] | Method response settings to allow an origin. |
23+
24+
**Returns:**
25+
26+
T\[\]
27+
28+
`responses` with updated [responseParameters](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.MethodResponse.html#responseparameters)<!-- -->.
29+
30+
## Remarks
31+
32+
Marks `Access-Control-Allow-Origin` response header included.
33+

api-docs/markdown/cdk2-cors-utils.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Utilities to configure CORS for [Amazon API Gateway](https://docs.aws.amazon.com
1010

1111
| Function | Description |
1212
| --- | --- |
13-
| [makeIntegrationResponsesAllowCors(responses)](./cdk2-cors-utils.makeintegrationresponsesallowcors.md) | <b><i>(BETA)</i></b> Makes given [IntegrationResponse](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IntegrationResponse.html)<!-- -->s allow CORS. |
14-
| [makeMethodResponsesAllowCors(responses)](./cdk2-cors-utils.makemethodresponsesallowcors.md) | <b><i>(BETA)</i></b> Makes given [MethodResponse](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.MethodResponse.html)<!-- -->s allow CORS. |
13+
| [makeIntegrationResponsesAllowCors(responses)](./cdk2-cors-utils.makeintegrationresponsesallowcors.md) | **_(BETA)_** Makes given [IntegrationResponse](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IntegrationResponse.html)<!-- -->s allow CORS. |
14+
| [makeIntegrationResponsesAllowOrigin(responses, origin)](./cdk2-cors-utils.makeintegrationresponsesalloworigin.md) | **_(BETA)_** Makes given [IntegrationResponse](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IntegrationResponse.html)<!-- -->s allow an origin. |
15+
| [makeMethodResponsesAllowCors(responses)](./cdk2-cors-utils.makemethodresponsesallowcors.md) | **_(BETA)_** Makes given [MethodResponse](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.MethodResponse.html)<!-- -->s allow CORS. |
16+
| [makeMethodResponsesAllowOrigin(responses)](./cdk2-cors-utils.makemethodresponsesalloworigin.md) | **_(BETA)_** Makes given [MethodResponse](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.MethodResponse.html)<!-- -->s allow an origin. |
1517

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cdk2-cors-utils",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "CORS utilities for CDK v2",
55
"main": "./dist/index.js",
66
"typings": "./dist/index.d.ts",

src/allow-cors.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,64 @@ export function makeMethodResponsesAllowCors<T extends apigateway.MethodResponse
9595
return responses;
9696
}
9797

98+
/**
99+
* Makes given {@link https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IntegrationResponse.html | IntegrationResponse}s allow an origin.
100+
*
101+
* @remarks
102+
*
103+
* Configures `Access-Control-Allow-Origin` response header to accept `origin`.
104+
*
105+
* @param responses -
106+
*
107+
* Integration response settings to allow an origin.
108+
*
109+
* @param origin -
110+
*
111+
* Origin to allow.
112+
* If it does not start with a quotation mark, this function surrounds it with
113+
* quotation marks.
114+
*
115+
* @returns
116+
*
117+
* `responses` with updated
118+
* {@link https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IntegrationResponse.html#responseparameters | responseParameters}.
119+
*
120+
* @beta
121+
*/
122+
export function makeIntegrationResponsesAllowOrigin<T extends apigateway.IntegrationResponse>(
123+
responses: T[],
124+
origin: string,
125+
): T[] {
126+
if (!origin.startsWith("'")) {
127+
origin = `'${origin}'`;
128+
}
129+
return setResponseParameters(responses, ALLOW_ORIGIN, origin);
130+
}
131+
132+
/**
133+
* Makes given {@link https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.MethodResponse.html | MethodResponse}s allow an origin.
134+
*
135+
* @remarks
136+
*
137+
* Marks `Access-Control-Allow-Origin` response header included.
138+
*
139+
* @param responses -
140+
*
141+
* Method response settings to allow an origin.
142+
*
143+
* @returns
144+
*
145+
* `responses` with updated
146+
* {@link https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.MethodResponse.html#responseparameters | responseParameters}.
147+
*
148+
* @beta
149+
*/
150+
export function makeMethodResponsesAllowOrigin<T extends apigateway.MethodResponse>(
151+
responses: T[],
152+
): T[] {
153+
return setResponseParameters(responses, ALLOW_ORIGIN, true);
154+
}
155+
98156
// Sets the value of a given response header.
99157
function setResponseParameters<T, U extends Response<T>>(
100158
responses: U[],

0 commit comments

Comments
 (0)