@aws/nx-plugin 0.82.1 → 0.82.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws/nx-plugin",
3
- "version": "0.82.1",
3
+ "version": "0.82.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/awslabs/nx-plugin-for-aws.git",
@@ -21,7 +21,7 @@ exports[`infra generator > should add required dependencies to package.json > de
21
21
  "@swc/core": "~1.15.5",
22
22
  "@swc/helpers": "~0.5.18",
23
23
  "@types/node": "22.19.13",
24
- "@vitest/coverage-v8": "^4.0.0",
24
+ "@vitest/coverage-v8": "4.0.18",
25
25
  "eslint": "^9.8.0",
26
26
  "eslint-config-prettier": "^10.0.0",
27
27
  "eslint-plugin-prettier": "5.5.5",
@@ -32,7 +32,7 @@ exports[`infra generator > should add required dependencies to package.json > de
32
32
  "typescript": "~5.9.2",
33
33
  "typescript-eslint": "^8.40.0",
34
34
  "vite": "^7.0.0",
35
- "vitest": "^4.0.8",
35
+ "vitest": "4.0.18",
36
36
  }
37
37
  `;
38
38
 
@@ -55,7 +55,7 @@ exports[`infra generator > should add required dependencies to package.json > pa
55
55
  "@swc/core": "~1.15.5",
56
56
  "@swc/helpers": "~0.5.18",
57
57
  "@types/node": "22.19.13",
58
- "@vitest/coverage-v8": "^4.0.0",
58
+ "@vitest/coverage-v8": "4.0.18",
59
59
  "eslint": "^9.8.0",
60
60
  "eslint-config-prettier": "^10.0.0",
61
61
  "eslint-plugin-prettier": "5.5.5",
@@ -66,7 +66,7 @@ exports[`infra generator > should add required dependencies to package.json > pa
66
66
  "typescript": "~5.9.2",
67
67
  "typescript-eslint": "^8.40.0",
68
68
  "vite": "^7.0.0",
69
- "vitest": "^4.0.8",
69
+ "vitest": "4.0.18",
70
70
  },
71
71
  "name": "@proj/source",
72
72
  "type": "module",
@@ -529,21 +529,28 @@ export class TestApi<
529
529
  }
530
530
 
531
531
  /**
532
- * Restricts CORS to the website CloudFront distribution domains
532
+ * Restricts CORS to the provided origins
533
533
  *
534
- * Configures the CloudFront distribution domains as the only permitted CORS origins
535
- * (other than local host with default ports) in the API gateway
536
- * The CORS origins are not configured within the AWS Lambda integrations since
537
- * the associated header is controlled by API Gateway v2
534
+ * Configures the provided CloudFront distribution domains or origin strings
535
+ * as the only permitted CORS origins
536
+ * in the API gateway. The CORS origins are not configured within the AWS Lambda
537
+ * integrations since the associated header is controlled by API Gateway v2.
538
538
  *
539
- * @param cloudFrontDistribution - The CloudFront distribution to grant CORS from
539
+ * @param origins - The origin strings, CloudFront distributions, or objects containing a CloudFront distribution to grant CORS from
540
540
  */
541
541
  public restrictCorsTo(
542
- ...websites: { cloudFrontDistribution: Distribution }[]
542
+ ...origins: (
543
+ | string
544
+ | Distribution
545
+ | { cloudFrontDistribution: Distribution }
546
+ )[]
543
547
  ) {
544
- const allowedOrigins = websites.map(
545
- ({ cloudFrontDistribution }) =>
546
- \`https://\${cloudFrontDistribution.distributionDomainName}\`,
548
+ const allowedOrigins = origins.map((origin) =>
549
+ typeof origin === 'string'
550
+ ? origin
551
+ : 'cloudFrontDistribution' in origin
552
+ ? \`https://\${origin.cloudFrontDistribution.distributionDomainName}\`
553
+ : \`https://\${origin.distributionDomainName}\`,
547
554
  );
548
555
 
549
556
  const cfnApi = this.api.node.defaultChild;
@@ -554,11 +561,7 @@ export class TestApi<
554
561
  }
555
562
 
556
563
  cfnApi.corsConfiguration = {
557
- allowOrigins: [
558
- 'http://localhost:4200',
559
- 'http://localhost:4300',
560
- ...allowedOrigins,
561
- ],
564
+ allowOrigins: allowedOrigins,
562
565
  allowMethods: [CorsHttpMethod.ANY],
563
566
  allowHeaders: [
564
567
  'authorization',
@@ -936,13 +939,16 @@ export class IntegrationBuilder<
936
939
  `;
937
940
 
938
941
  exports[`fastapi project generator > should set up shared constructs for rest > rest-api.ts 1`] = `
939
- "import { Construct } from 'constructs';
942
+ "import { Construct, IConstruct } from 'constructs';
940
943
  import {
944
+ Cors,
945
+ Resource,
941
946
  RestApi as _RestApi,
942
947
  RestApiProps as _RestApiProps,
943
948
  IResource,
944
949
  Stage,
945
950
  } from 'aws-cdk-lib/aws-apigateway';
951
+ import { IAspect } from 'aws-cdk-lib';
946
952
  import { RuntimeConfig } from '../runtime-config.js';
947
953
  import {
948
954
  ApiIntegrations,
@@ -1041,7 +1047,7 @@ export class RestApi<
1041
1047
  };
1042
1048
 
1043
1049
  // Create API resources and methods for each operation
1044
- (Object.entries(operations) as [TOperation, OperationDetails][]).map(
1050
+ (Object.entries(operations) as [TOperation, OperationDetails][]).forEach(
1045
1051
  ([op, details]) => {
1046
1052
  const integration = resolveIntegration(op);
1047
1053
  const resource = this.getOrCreateResource(
@@ -1085,6 +1091,21 @@ export class RestApi<
1085
1091
  return this.getOrCreateResource(childResource, pathParts);
1086
1092
  }
1087
1093
  }
1094
+
1095
+ export class AddCorsPreflightAspect implements IAspect {
1096
+ private getAllowedOrigins: () => readonly string[];
1097
+ constructor(getAllowedOrigins: () => readonly string[]) {
1098
+ this.getAllowedOrigins = getAllowedOrigins;
1099
+ }
1100
+ public visit(node: IConstruct): void {
1101
+ if (node instanceof Resource) {
1102
+ node.addCorsPreflight({
1103
+ allowOrigins: [...this.getAllowedOrigins()],
1104
+ allowMethods: Cors.ALL_METHODS,
1105
+ });
1106
+ }
1107
+ }
1108
+ }
1088
1109
  "
1089
1110
  `;
1090
1111
 
@@ -1103,11 +1124,10 @@ import {
1103
1124
  } from 'aws-cdk-lib/aws-lambda';
1104
1125
  import {
1105
1126
  AuthorizationType,
1106
- Cors,
1107
1127
  LambdaIntegration,
1108
1128
  ResponseTransferMode,
1109
1129
  } from 'aws-cdk-lib/aws-apigateway';
1110
- import { Duration, Stack } from 'aws-cdk-lib';
1130
+ import { Aspects, Duration, Stack } from 'aws-cdk-lib';
1111
1131
  import {
1112
1132
  PolicyDocument,
1113
1133
  PolicyStatement,
@@ -1121,7 +1141,7 @@ import {
1121
1141
  IntegrationBuilder,
1122
1142
  RestApiIntegration,
1123
1143
  } from '../../core/api/utils.js';
1124
- import { RestApi } from '../../core/api/rest-api.js';
1144
+ import { AddCorsPreflightAspect, RestApi } from '../../core/api/rest-api.js';
1125
1145
  import {
1126
1146
  OPERATION_DETAILS,
1127
1147
  Operations,
@@ -1149,6 +1169,8 @@ export interface TestApiProps<
1149
1169
  export class TestApi<
1150
1170
  TIntegrations extends ApiIntegrations<Operations, RestApiIntegration>,
1151
1171
  > extends RestApi<Operations, TIntegrations> {
1172
+ private allowedOrigins: readonly string[] = ['*'];
1173
+
1152
1174
  /**
1153
1175
  * Creates default integrations for all operations, which implement each operation as
1154
1176
  * its own individual lambda function.
@@ -1211,10 +1233,6 @@ export class TestApi<
1211
1233
  defaultMethodOptions: {
1212
1234
  authorizationType: AuthorizationType.IAM,
1213
1235
  },
1214
- defaultCorsPreflightOptions: {
1215
- allowOrigins: Cors.ALL_ORIGINS,
1216
- allowMethods: Cors.ALL_METHODS,
1217
- },
1218
1236
  deployOptions: {
1219
1237
  tracingEnabled: true,
1220
1238
  },
@@ -1232,32 +1250,42 @@ export class TestApi<
1232
1250
  operations: OPERATION_DETAILS,
1233
1251
  ...props,
1234
1252
  });
1253
+ Aspects.of(this).add(new AddCorsPreflightAspect(() => this.allowedOrigins));
1235
1254
  }
1236
1255
 
1237
1256
  /**
1238
- * Restricts CORS to the website CloudFront distribution domains
1257
+ * Restricts CORS to the provided origins
1239
1258
  *
1240
- * Configures the CloudFront distribution domains as the only permitted CORS origins
1241
- * (other than local host) in the AWS Lambda integrations
1259
+ * Configures the provided CloudFront distribution domains or origin strings
1260
+ * as the only permitted CORS origins in API Gateway preflight responses and the
1261
+ * AWS Lambda integrations.
1242
1262
  *
1243
- * Note that this restriction is not applied to preflight OPTIONS
1244
- *
1245
- * @param websites - The CloudFront distribution to grant CORS from
1263
+ * @param origins - The origin strings, CloudFront distributions, or objects containing a CloudFront distribution to grant CORS from
1246
1264
  */
1247
1265
  public restrictCorsTo(
1248
- ...websites: { cloudFrontDistribution: Distribution }[]
1266
+ ...origins: (
1267
+ | string
1268
+ | Distribution
1269
+ | { cloudFrontDistribution: Distribution }
1270
+ )[]
1249
1271
  ) {
1250
- const allowedOrigins = websites
1251
- .map(
1252
- ({ cloudFrontDistribution }) =>
1253
- \`https://\${cloudFrontDistribution.distributionDomainName}\`,
1254
- )
1255
- .join(',');
1272
+ const allowedOrigins = origins.map((origin) =>
1273
+ typeof origin === 'string'
1274
+ ? origin
1275
+ : 'cloudFrontDistribution' in origin
1276
+ ? \`https://\${origin.cloudFrontDistribution.distributionDomainName}\`
1277
+ : \`https://\${origin.distributionDomainName}\`,
1278
+ );
1279
+
1280
+ this.allowedOrigins = allowedOrigins;
1256
1281
 
1257
1282
  // Set ALLOWED_ORIGINS environment variable for all Lambda integrations
1258
1283
  Object.values(this.integrations).forEach((integration) => {
1259
1284
  if ('handler' in integration && integration.handler instanceof Function) {
1260
- integration.handler.addEnvironment('ALLOWED_ORIGINS', allowedOrigins);
1285
+ integration.handler.addEnvironment(
1286
+ 'ALLOWED_ORIGINS',
1287
+ allowedOrigins.join(','),
1288
+ );
1261
1289
  }
1262
1290
  });
1263
1291
  }
@@ -44,11 +44,10 @@ import {
44
44
  } from 'aws-cdk-lib/aws-lambda';
45
45
  import {
46
46
  AuthorizationType,
47
- Cors,
48
47
  LambdaIntegration,
49
48
  CognitoUserPoolsAuthorizer,
50
49
  } from 'aws-cdk-lib/aws-apigateway';
51
- import { Duration } from 'aws-cdk-lib';
50
+ import { Aspects, Duration } from 'aws-cdk-lib';
52
51
  import {
53
52
  PolicyDocument,
54
53
  PolicyStatement,
@@ -61,7 +60,7 @@ import {
61
60
  IntegrationBuilder,
62
61
  RestApiIntegration,
63
62
  } from '../../core/api/utils.js';
64
- import { RestApi } from '../../core/api/rest-api.js';
63
+ import { AddCorsPreflightAspect, RestApi } from '../../core/api/rest-api.js';
65
64
  import {
66
65
  OPERATION_DETAILS,
67
66
  Operations,
@@ -95,6 +94,8 @@ export interface TestApiProps<
95
94
  export class TestApi<
96
95
  TIntegrations extends ApiIntegrations<Operations, RestApiIntegration>,
97
96
  > extends RestApi<Operations, TIntegrations> {
97
+ private allowedOrigins: readonly string[] = ['*'];
98
+
98
99
  /**
99
100
  * Creates default integrations for all operations, which implement each operation as
100
101
  * its own individual lambda function.
@@ -146,10 +147,6 @@ export class TestApi<
146
147
  cognitoUserPools: [props.identity.userPool],
147
148
  }),
148
149
  },
149
- defaultCorsPreflightOptions: {
150
- allowOrigins: Cors.ALL_ORIGINS,
151
- allowMethods: Cors.ALL_METHODS,
152
- },
153
150
  deployOptions: {
154
151
  tracingEnabled: true,
155
152
  },
@@ -167,32 +164,42 @@ export class TestApi<
167
164
  operations: OPERATION_DETAILS,
168
165
  ...props,
169
166
  });
167
+ Aspects.of(this).add(new AddCorsPreflightAspect(() => this.allowedOrigins));
170
168
  }
171
169
 
172
170
  /**
173
- * Restricts CORS to the website CloudFront distribution domains
174
- *
175
- * Configures the CloudFront distribution domains as the only permitted CORS origins
176
- * (other than local host) in the AWS Lambda integrations
171
+ * Restricts CORS to the provided origins
177
172
  *
178
- * Note that this restriction is not applied to preflight OPTIONS
173
+ * Configures the provided CloudFront distribution domains or origin strings
174
+ * as the only permitted CORS origins in API Gateway preflight responses and the
175
+ * AWS Lambda integrations.
179
176
  *
180
- * @param websites - The CloudFront distribution to grant CORS from
177
+ * @param origins - The origin strings, CloudFront distributions, or objects containing a CloudFront distribution to grant CORS from
181
178
  */
182
179
  public restrictCorsTo(
183
- ...websites: { cloudFrontDistribution: Distribution }[]
180
+ ...origins: (
181
+ | string
182
+ | Distribution
183
+ | { cloudFrontDistribution: Distribution }
184
+ )[]
184
185
  ) {
185
- const allowedOrigins = websites
186
- .map(
187
- ({ cloudFrontDistribution }) =>
188
- \`https://\${cloudFrontDistribution.distributionDomainName}\`,
189
- )
190
- .join(',');
186
+ const allowedOrigins = origins.map((origin) =>
187
+ typeof origin === 'string'
188
+ ? origin
189
+ : 'cloudFrontDistribution' in origin
190
+ ? \`https://\${origin.cloudFrontDistribution.distributionDomainName}\`
191
+ : \`https://\${origin.distributionDomainName}\`,
192
+ );
193
+
194
+ this.allowedOrigins = allowedOrigins;
191
195
 
192
196
  // Set ALLOWED_ORIGINS environment variable for all Lambda integrations
193
197
  Object.values(this.integrations).forEach((integration) => {
194
198
  if ('handler' in integration && integration.handler instanceof Function) {
195
- integration.handler.addEnvironment('ALLOWED_ORIGINS', allowedOrigins);
199
+ integration.handler.addEnvironment(
200
+ 'ALLOWED_ORIGINS',
201
+ allowedOrigins.join(','),
202
+ );
196
203
  }
197
204
  });
198
205
  }
@@ -213,10 +220,9 @@ import {
213
220
  } from 'aws-cdk-lib/aws-lambda';
214
221
  import {
215
222
  AuthorizationType,
216
- Cors,
217
223
  LambdaIntegration,
218
224
  } from 'aws-cdk-lib/aws-apigateway';
219
- import { Duration } from 'aws-cdk-lib';
225
+ import { Aspects, Duration } from 'aws-cdk-lib';
220
226
  import {
221
227
  PolicyDocument,
222
228
  PolicyStatement,
@@ -230,7 +236,7 @@ import {
230
236
  IntegrationBuilder,
231
237
  RestApiIntegration,
232
238
  } from '../../core/api/utils.js';
233
- import { RestApi } from '../../core/api/rest-api.js';
239
+ import { AddCorsPreflightAspect, RestApi } from '../../core/api/rest-api.js';
234
240
  import {
235
241
  OPERATION_DETAILS,
236
242
  Operations,
@@ -258,6 +264,8 @@ export interface TestApiProps<
258
264
  export class TestApi<
259
265
  TIntegrations extends ApiIntegrations<Operations, RestApiIntegration>,
260
266
  > extends RestApi<Operations, TIntegrations> {
267
+ private allowedOrigins: readonly string[] = ['*'];
268
+
261
269
  /**
262
270
  * Creates default integrations for all operations, which implement each operation as
263
271
  * its own individual lambda function.
@@ -306,10 +314,6 @@ export class TestApi<
306
314
  defaultMethodOptions: {
307
315
  authorizationType: AuthorizationType.IAM,
308
316
  },
309
- defaultCorsPreflightOptions: {
310
- allowOrigins: Cors.ALL_ORIGINS,
311
- allowMethods: Cors.ALL_METHODS,
312
- },
313
317
  deployOptions: {
314
318
  tracingEnabled: true,
315
319
  },
@@ -327,32 +331,42 @@ export class TestApi<
327
331
  operations: OPERATION_DETAILS,
328
332
  ...props,
329
333
  });
334
+ Aspects.of(this).add(new AddCorsPreflightAspect(() => this.allowedOrigins));
330
335
  }
331
336
 
332
337
  /**
333
- * Restricts CORS to the website CloudFront distribution domains
334
- *
335
- * Configures the CloudFront distribution domains as the only permitted CORS origins
336
- * (other than local host) in the AWS Lambda integrations
338
+ * Restricts CORS to the provided origins
337
339
  *
338
- * Note that this restriction is not applied to preflight OPTIONS
340
+ * Configures the provided CloudFront distribution domains or origin strings
341
+ * as the only permitted CORS origins in API Gateway preflight responses and the
342
+ * AWS Lambda integrations.
339
343
  *
340
- * @param websites - The CloudFront distribution to grant CORS from
344
+ * @param origins - The origin strings, CloudFront distributions, or objects containing a CloudFront distribution to grant CORS from
341
345
  */
342
346
  public restrictCorsTo(
343
- ...websites: { cloudFrontDistribution: Distribution }[]
347
+ ...origins: (
348
+ | string
349
+ | Distribution
350
+ | { cloudFrontDistribution: Distribution }
351
+ )[]
344
352
  ) {
345
- const allowedOrigins = websites
346
- .map(
347
- ({ cloudFrontDistribution }) =>
348
- \`https://\${cloudFrontDistribution.distributionDomainName}\`,
349
- )
350
- .join(',');
353
+ const allowedOrigins = origins.map((origin) =>
354
+ typeof origin === 'string'
355
+ ? origin
356
+ : 'cloudFrontDistribution' in origin
357
+ ? \`https://\${origin.cloudFrontDistribution.distributionDomainName}\`
358
+ : \`https://\${origin.distributionDomainName}\`,
359
+ );
360
+
361
+ this.allowedOrigins = allowedOrigins;
351
362
 
352
363
  // Set ALLOWED_ORIGINS environment variable for all Lambda integrations
353
364
  Object.values(this.integrations).forEach((integration) => {
354
365
  if ('handler' in integration && integration.handler instanceof Function) {
355
- integration.handler.addEnvironment('ALLOWED_ORIGINS', allowedOrigins);
366
+ integration.handler.addEnvironment(
367
+ 'ALLOWED_ORIGINS',
368
+ allowedOrigins.join(','),
369
+ );
356
370
  }
357
371
  });
358
372
  }