@digitraffic/common 2024.3.14-1 → 2024.3.22-1
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.
@@ -1,4 +1,4 @@
|
|
1
|
-
import { type IResource, type JsonSchema, Resource, RestApi, type RestApiProps } from "aws-cdk-lib/aws-apigateway";
|
1
|
+
import { type IResource, type JsonSchema, Resource, type ResourceOptions, RestApi, type RestApiProps } from "aws-cdk-lib/aws-apigateway";
|
2
2
|
import { PolicyDocument } from "aws-cdk-lib/aws-iam";
|
3
3
|
import { Construct } from "constructs";
|
4
4
|
import type { ModelWithReference } from "../../types/model-with-reference.mjs";
|
@@ -16,6 +16,7 @@ export declare class DigitrafficRestApi extends RestApi {
|
|
16
16
|
private getModelWithReference;
|
17
17
|
private addDocumentationPart;
|
18
18
|
documentResource(resource: Resource, ...documentationPart: DocumentationPart[]): void;
|
19
|
+
addResourceWithCorsOptionsSubTree(resource: Resource, pathPart: string, config?: ResourceOptions): Resource;
|
19
20
|
/**
|
20
21
|
* Add support for HTTP OPTIONS to an API GW resource,
|
21
22
|
* this is required for preflight CORS requests made by browsers.
|
@@ -45,3 +46,4 @@ export declare function setReturnCodeForMissingAuthenticationToken(returnCode: n
|
|
45
46
|
export declare function createRestApi(stack: Construct, apiId: string, apiName: string, allowFromIpAddresses?: string[] | undefined): RestApi;
|
46
47
|
export declare function createDefaultPolicyDocument(): PolicyDocument;
|
47
48
|
export declare function createIpRestrictionPolicyDocument(allowFromIpAddresses: string[]): PolicyDocument;
|
49
|
+
export declare const PUBLIC_REST_API_CORS_CONFIG: Partial<ResourceOptions>;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { CfnDocumentationPart, EndpointType, GatewayResponse, MethodLoggingLevel,
|
1
|
+
import { CfnDocumentationPart, Cors, EndpointType, GatewayResponse, MethodLoggingLevel, Model, Resource, ResponseType, RestApi, } from "aws-cdk-lib/aws-apigateway";
|
2
2
|
import { AnyPrincipal, Effect, PolicyDocument, PolicyStatement, } from "aws-cdk-lib/aws-iam";
|
3
3
|
import { Construct } from "constructs";
|
4
4
|
import { getModelReference } from "../../../utils/api-model.mjs";
|
@@ -82,38 +82,20 @@ export class DigitrafficRestApi extends RestApi {
|
|
82
82
|
console.info("Skipping documentation for %s", resource.path);
|
83
83
|
}
|
84
84
|
}
|
85
|
+
addResourceWithCorsOptionsSubTree(resource, pathPart, config) {
|
86
|
+
const mergedConfig = {
|
87
|
+
...PUBLIC_REST_API_CORS_CONFIG,
|
88
|
+
...config
|
89
|
+
};
|
90
|
+
return resource.addResource(pathPart, mergedConfig);
|
91
|
+
}
|
85
92
|
/**
|
86
93
|
* Add support for HTTP OPTIONS to an API GW resource,
|
87
94
|
* this is required for preflight CORS requests made by browsers.
|
88
95
|
* @param apiResource
|
89
96
|
*/
|
90
97
|
addCorsOptions(apiResource) {
|
91
|
-
apiResource.
|
92
|
-
integrationResponses: [
|
93
|
-
{
|
94
|
-
statusCode: "200",
|
95
|
-
responseParameters: {
|
96
|
-
"method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,Digitraffic-User'",
|
97
|
-
"method.response.header.Access-Control-Allow-Origin": "'*'",
|
98
|
-
"method.response.header.Access-Control-Allow-Methods": "'OPTIONS,GET,HEAD'",
|
99
|
-
},
|
100
|
-
},
|
101
|
-
],
|
102
|
-
requestTemplates: {
|
103
|
-
"application/json": '{"statusCode": 200}',
|
104
|
-
},
|
105
|
-
}), {
|
106
|
-
methodResponses: [
|
107
|
-
{
|
108
|
-
statusCode: "200",
|
109
|
-
responseParameters: {
|
110
|
-
"method.response.header.Access-Control-Allow-Headers": true,
|
111
|
-
"method.response.header.Access-Control-Allow-Methods": true,
|
112
|
-
"method.response.header.Access-Control-Allow-Origin": true,
|
113
|
-
},
|
114
|
-
},
|
115
|
-
],
|
116
|
-
});
|
98
|
+
apiResource.addCorsPreflight(PUBLIC_REST_API_CORS_CONFIG.defaultCorsPreflightOptions);
|
117
99
|
}
|
118
100
|
}
|
119
101
|
/**
|
@@ -206,4 +188,11 @@ export function createIpRestrictionPolicyDocument(allowFromIpAddresses) {
|
|
206
188
|
],
|
207
189
|
});
|
208
190
|
}
|
191
|
+
export const PUBLIC_REST_API_CORS_CONFIG = {
|
192
|
+
defaultCorsPreflightOptions: {
|
193
|
+
allowOrigins: Cors.ALL_ORIGINS,
|
194
|
+
allowHeaders: ["Content-Type", "X-Amz-Date", "Authorization", "X-Api-Key", "X-Amz-Security-Token", "Digitraffic-User"],
|
195
|
+
allowMethods: ["OPTIONS", "GET", "HEAD"]
|
196
|
+
}
|
197
|
+
};
|
209
198
|
//# sourceMappingURL=rest_apis.mjs.map
|
package/dist/utils/utils.d.mts
CHANGED
@@ -75,6 +75,12 @@ export declare function getEnvVariableOr<T>(key: string, fn: () => T): string |
|
|
75
75
|
* @param orElse Alternative value
|
76
76
|
*/
|
77
77
|
export declare function getEnvVariableOrElse<T>(key: string, orElse: T): string | T;
|
78
|
+
/**
|
79
|
+
* Gets variable from environment or from an AWS Secrets Manager secret if not found in the environment.
|
80
|
+
* @param Environment key
|
81
|
+
* @param Secret id in Secrets Manager
|
82
|
+
*/
|
83
|
+
export declare function getFromEnvOrSecret(key: string, secretId: string): Promise<string>;
|
78
84
|
export declare function setSecretOverideAwsRegionEnv(region: string): void;
|
79
85
|
/**
|
80
86
|
* ESLint won't allow to call Object.prototype builtin methods.
|
package/dist/utils/utils.mjs
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
import { EnvKeys } from "../aws/runtime/environment.mjs";
|
2
|
+
import { getSecret, } from "../aws/runtime/secrets/secret.mjs";
|
2
3
|
/**
|
3
4
|
* Check if arrays have only elements that also exists also in other array.
|
4
5
|
* Individual element count doesn't matter.
|
@@ -130,6 +131,23 @@ export function getEnvVariableOr(key, fn) {
|
|
130
131
|
export function getEnvVariableOrElse(key, orElse) {
|
131
132
|
return getEnvVariableOr(key, () => orElse);
|
132
133
|
}
|
134
|
+
/**
|
135
|
+
* Gets variable from environment or from an AWS Secrets Manager secret if not found in the environment.
|
136
|
+
* @param Environment key
|
137
|
+
* @param Secret id in Secrets Manager
|
138
|
+
*/
|
139
|
+
export async function getFromEnvOrSecret(key, secretId) {
|
140
|
+
const envValue = getEnvVariableSafe(key);
|
141
|
+
if (envValue.result === "ok") {
|
142
|
+
return envValue.value;
|
143
|
+
}
|
144
|
+
const secret = await getSecret(secretId);
|
145
|
+
const secretValue = secret[key];
|
146
|
+
if (secretValue !== undefined) {
|
147
|
+
return secretValue;
|
148
|
+
}
|
149
|
+
throw new Error(`Cannot get value with key ${key} from env or secret`);
|
150
|
+
}
|
133
151
|
export function setSecretOverideAwsRegionEnv(region) {
|
134
152
|
setEnvVariable(EnvKeys.SECRET_OVERRIDE_AWS_REGION, region);
|
135
153
|
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@digitraffic/common",
|
3
|
-
"version": "2024.3.
|
3
|
+
"version": "2024.3.22-1",
|
4
4
|
"description": "",
|
5
5
|
"type": "module",
|
6
6
|
"repository": {
|
@@ -109,7 +109,7 @@
|
|
109
109
|
"@aws-sdk/client-api-gateway": "^3.533.0",
|
110
110
|
"@aws-sdk/client-sns": "^3.533.0",
|
111
111
|
"@types/geojson": "^7946.0.14",
|
112
|
-
"aws-cdk-lib": "^2.
|
112
|
+
"aws-cdk-lib": "^2.133.0",
|
113
113
|
"change-case": "5.0.0",
|
114
114
|
"constructs": "^10.3.0",
|
115
115
|
"date-fns": "~2.30.0",
|
@@ -137,7 +137,7 @@
|
|
137
137
|
"@rushstack/heft-typescript-plugin": "^0.3.20",
|
138
138
|
"@types/aws-lambda": "8.10.136",
|
139
139
|
"@types/etag": "1.8.3",
|
140
|
-
"@types/geojson": "7946.0.14",
|
140
|
+
"@types/geojson": "^7946.0.14",
|
141
141
|
"@types/geojson-validation": "^1.0.3",
|
142
142
|
"@types/jest": "29.5.12",
|
143
143
|
"@types/lodash": "4.14.202",
|
@@ -145,7 +145,7 @@
|
|
145
145
|
"@types/sinon": "17.0.3",
|
146
146
|
"@typescript-eslint/eslint-plugin": "~6.18.1",
|
147
147
|
"@typescript-eslint/parser": "^6.20.0",
|
148
|
-
"aws-cdk-lib": "
|
148
|
+
"aws-cdk-lib": "^2.133.0",
|
149
149
|
"change-case": "5.3.0",
|
150
150
|
"constructs": "10.3.0",
|
151
151
|
"date-fns": "~2.30.0",
|