@goldstack/utils-aws-http-api-local 0.1.3
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/README.md +3 -0
- package/dist/src/expressRequestToHttpApiGatewayEvent.d.ts +18 -0
- package/dist/src/expressRequestToHttpApiGatewayEvent.d.ts.map +1 -0
- package/dist/src/expressRequestToHttpApiGatewayEvent.js +171 -0
- package/dist/src/expressRequestToHttpApiGatewayEvent.js.map +1 -0
- package/dist/src/expressRoutes.d.ts +10 -0
- package/dist/src/expressRoutes.d.ts.map +1 -0
- package/dist/src/expressRoutes.js +65 -0
- package/dist/src/expressRoutes.js.map +1 -0
- package/dist/src/expressRoutes.spec.d.ts +2 -0
- package/dist/src/expressRoutes.spec.d.ts.map +1 -0
- package/dist/src/expressRoutes.spec.js +11 -0
- package/dist/src/expressRoutes.spec.js.map +1 -0
- package/dist/src/utilsAwsHttpApiLocal.d.ts +15 -0
- package/dist/src/utilsAwsHttpApiLocal.d.ts.map +1 -0
- package/dist/src/utilsAwsHttpApiLocal.js +44 -0
- package/dist/src/utilsAwsHttpApiLocal.js.map +1 -0
- package/dist/src/utilsAwsHttpApiLocal.spec.d.ts +2 -0
- package/dist/src/utilsAwsHttpApiLocal.spec.d.ts.map +1 -0
- package/dist/src/utilsAwsHttpApiLocal.spec.js +69 -0
- package/dist/src/utilsAwsHttpApiLocal.spec.js.map +1 -0
- package/package.json +70 -0
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Request, Response } from 'express';
|
|
2
|
+
import { APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2, Context } from 'aws-lambda';
|
|
3
|
+
import { LambdaConfig } from '@goldstack/utils-aws-lambda';
|
|
4
|
+
export interface CovertToGatewayEventParams {
|
|
5
|
+
req: Request;
|
|
6
|
+
lambdaConfig: LambdaConfig;
|
|
7
|
+
}
|
|
8
|
+
export interface StringMap {
|
|
9
|
+
[key: string]: string;
|
|
10
|
+
}
|
|
11
|
+
export declare const sortPropertiesByAppearanceInPath: (params: CovertToGatewayEventParams, pathParams: StringMap) => [string, string][];
|
|
12
|
+
export declare const convertToGatewayEvent: (params: CovertToGatewayEventParams) => APIGatewayProxyEventV2;
|
|
13
|
+
export interface CreateContextParams {
|
|
14
|
+
functionName: string;
|
|
15
|
+
}
|
|
16
|
+
export declare const createContext: (params: CreateContextParams) => Context;
|
|
17
|
+
export declare const injectGatewayResultIntoResponse: (result: APIGatewayProxyStructuredResultV2 | string | any, resp: Response) => void;
|
|
18
|
+
//# sourceMappingURL=expressRequestToHttpApiGatewayEvent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expressRequestToHttpApiGatewayEvent.d.ts","sourceRoot":"","sources":["../../src/expressRequestToHttpApiGatewayEvent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAE5C,OAAO,EACL,sBAAsB,EACtB,iCAAiC,EACjC,OAAO,EACR,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAE3D,MAAM,WAAW,0BAA0B;IACzC,GAAG,EAAE,OAAO,CAAC;IACb,YAAY,EAAE,YAAY,CAAC;CAC5B;AAED,MAAM,WAAW,SAAS;IACxB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB;AAED,eAAO,MAAM,gCAAgC,WACnC,0BAA0B,cACtB,SAAS,KACpB,CAAC,MAAM,EAAE,MAAM,CAAC,EAUlB,CAAC;AAEF,eAAO,MAAM,qBAAqB,WACxB,0BAA0B,KACjC,sBA+GF,CAAC;AAEF,MAAM,WAAW,mBAAmB;IAClC,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,eAAO,MAAM,aAAa,WAAY,mBAAmB,KAAG,OAiB3D,CAAC;AAEF,eAAO,MAAM,+BAA+B,WAClC,iCAAiC,GAAG,MAAM,GAAG,GAAG,QAClD,QAAQ,KACb,IAwCF,CAAC"}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.injectGatewayResultIntoResponse = exports.createContext = exports.convertToGatewayEvent = exports.sortPropertiesByAppearanceInPath = void 0;
|
|
7
|
+
const cookie_1 = __importDefault(require("cookie"));
|
|
8
|
+
const sortPropertiesByAppearanceInPath = (params, pathParams) => {
|
|
9
|
+
const pathElements = Object.entries(pathParams)
|
|
10
|
+
.filter((e) => e[0] !== '0')
|
|
11
|
+
.sort((a, b) => {
|
|
12
|
+
return (params.lambdaConfig.path.indexOf(a[0]) -
|
|
13
|
+
params.lambdaConfig.path.indexOf(b[0]));
|
|
14
|
+
});
|
|
15
|
+
return pathElements;
|
|
16
|
+
};
|
|
17
|
+
exports.sortPropertiesByAppearanceInPath = sortPropertiesByAppearanceInPath;
|
|
18
|
+
const convertToGatewayEvent = (params) => {
|
|
19
|
+
const incomingHeaders = params.req.headers;
|
|
20
|
+
const normalisedHeaders = Object.entries(incomingHeaders).reduce((prev, curr) => {
|
|
21
|
+
if (typeof curr[1] === 'string') {
|
|
22
|
+
prev[curr[0]] = curr[1];
|
|
23
|
+
return prev;
|
|
24
|
+
}
|
|
25
|
+
else if (curr[1]) {
|
|
26
|
+
prev[curr[0]] = curr[1].join(',');
|
|
27
|
+
return prev;
|
|
28
|
+
}
|
|
29
|
+
return prev;
|
|
30
|
+
}, {});
|
|
31
|
+
const expressQuery = params.req.query;
|
|
32
|
+
const lambdaQuery = Object.entries(expressQuery)
|
|
33
|
+
.map((e) => `${e[0]}=${e[1]}`)
|
|
34
|
+
.join('&');
|
|
35
|
+
const queryStringParameters = Object.entries(expressQuery).reduce((prev, curr) => {
|
|
36
|
+
if (typeof curr[1] === 'string') {
|
|
37
|
+
prev[curr[0]] = curr[1];
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
prev[curr[0]] = curr[1].join(',');
|
|
41
|
+
}
|
|
42
|
+
return prev;
|
|
43
|
+
}, {});
|
|
44
|
+
const pathParams = { ...params.req.params };
|
|
45
|
+
// some complicated stuff happening
|
|
46
|
+
// if there was a regex/ greedy path match
|
|
47
|
+
// since express does not provide the full path
|
|
48
|
+
// but rather the 'matches' of the regex
|
|
49
|
+
if (pathParams['0']) {
|
|
50
|
+
const sortedPathParams = (0, exports.sortPropertiesByAppearanceInPath)(params, pathParams);
|
|
51
|
+
if (sortedPathParams.length > 0) {
|
|
52
|
+
const lastPathParam = sortedPathParams[sortedPathParams.length - 1];
|
|
53
|
+
const lastPathParamObject = Object.entries(pathParams).find((e) => {
|
|
54
|
+
return e[0].indexOf(lastPathParam[0]) === 0;
|
|
55
|
+
});
|
|
56
|
+
if (lastPathParamObject) {
|
|
57
|
+
// here we append the regex match to the path
|
|
58
|
+
// so the path is the full path that was matched
|
|
59
|
+
pathParams[lastPathParamObject[0]] = `${lastPathParamObject[1]}${pathParams['0']}`;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
version: '2.0',
|
|
65
|
+
routeKey: params.lambdaConfig.route,
|
|
66
|
+
rawPath: params.req.url.split('?')[0],
|
|
67
|
+
rawQueryString: lambdaQuery,
|
|
68
|
+
queryStringParameters,
|
|
69
|
+
pathParameters: pathParams,
|
|
70
|
+
cookies: params.req.cookies,
|
|
71
|
+
headers: {
|
|
72
|
+
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
|
|
73
|
+
'accept-encoding': 'gzip, deflate, br',
|
|
74
|
+
'accept-language': 'en-US,en;q=0.9,de;q=0.8',
|
|
75
|
+
'content-length': '0',
|
|
76
|
+
dnt: '1',
|
|
77
|
+
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
|
|
78
|
+
'sec-ch-ua-mobile': '?0',
|
|
79
|
+
'sec-ch-ua-platform': '"Windows"',
|
|
80
|
+
'sec-fetch-dest': 'document',
|
|
81
|
+
'sec-fetch-mode': 'navigate',
|
|
82
|
+
'sec-fetch-site': 'none',
|
|
83
|
+
'sec-fetch-user': '?1',
|
|
84
|
+
'upgrade-insecure-requests': '1',
|
|
85
|
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
|
|
86
|
+
'x-amzn-trace-id': 'Root=1-61ce5e14-5d8a4b4e22d2e69f3af9eb20',
|
|
87
|
+
'x-forwarded-for': '120.148.16.26',
|
|
88
|
+
'x-forwarded-port': '443',
|
|
89
|
+
'x-forwarded-proto': 'https',
|
|
90
|
+
...normalisedHeaders,
|
|
91
|
+
},
|
|
92
|
+
requestContext: {
|
|
93
|
+
accountId: '111111128371',
|
|
94
|
+
apiId: 'd5811h14p0',
|
|
95
|
+
domainName: 'localhost',
|
|
96
|
+
domainPrefix: 'localhost',
|
|
97
|
+
http: {
|
|
98
|
+
method: params.req.method,
|
|
99
|
+
path: params.req.url,
|
|
100
|
+
protocol: 'HTTP/1.1',
|
|
101
|
+
sourceIp: '120.148.16.26',
|
|
102
|
+
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
|
|
103
|
+
},
|
|
104
|
+
requestId: 'LMOjOiBiPHcEMkQ=',
|
|
105
|
+
routeKey: params.lambdaConfig.route,
|
|
106
|
+
stage: '$default',
|
|
107
|
+
time: '31/Dec/2021:01:34:12 +0000',
|
|
108
|
+
timeEpoch: new Date().getTime(),
|
|
109
|
+
},
|
|
110
|
+
isBase64Encoded: false,
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
exports.convertToGatewayEvent = convertToGatewayEvent;
|
|
114
|
+
const createContext = (params) => {
|
|
115
|
+
return {
|
|
116
|
+
callbackWaitsForEmptyEventLoop: true,
|
|
117
|
+
getRemainingTimeInMillis: () => {
|
|
118
|
+
return 0;
|
|
119
|
+
},
|
|
120
|
+
done: () => { },
|
|
121
|
+
fail: () => { },
|
|
122
|
+
succeed: () => { },
|
|
123
|
+
functionVersion: '$LATEST',
|
|
124
|
+
functionName: params.functionName,
|
|
125
|
+
memoryLimitInMB: '2048',
|
|
126
|
+
logGroupName: `/aws/lambda/${params.functionName}`,
|
|
127
|
+
logStreamName: '2021/12/31/[$LATEST]11f0a4a770034f1c822a5c4c69490bb1',
|
|
128
|
+
invokedFunctionArn: `arn:aws:lambda:us-west-2:475629728374:function:${params.functionName}`,
|
|
129
|
+
awsRequestId: '8bb853aa-6c00-42cb-9877-0963bd56ae01',
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
exports.createContext = createContext;
|
|
133
|
+
const injectGatewayResultIntoResponse = (result, resp) => {
|
|
134
|
+
// result can have three different structures https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
|
|
135
|
+
// 1: string
|
|
136
|
+
if (typeof result === 'string') {
|
|
137
|
+
resp.status(200);
|
|
138
|
+
resp.contentType('application/json');
|
|
139
|
+
resp.json(JSON.parse(result));
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
// 2: structured output
|
|
143
|
+
if (result.body) {
|
|
144
|
+
const structuredResult = result;
|
|
145
|
+
resp.status(structuredResult.statusCode || 200);
|
|
146
|
+
if (structuredResult.headers) {
|
|
147
|
+
resp.contentType(structuredResult.headers['content-type'] ||
|
|
148
|
+
'application/json');
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
resp.contentType('application/json');
|
|
152
|
+
}
|
|
153
|
+
if (structuredResult.cookies) {
|
|
154
|
+
structuredResult.cookies.forEach((val) => {
|
|
155
|
+
const obj = cookie_1.default.parse(val);
|
|
156
|
+
Object.entries(obj).forEach((element) => {
|
|
157
|
+
resp.cookie(element[0], element[1]);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
resp.json(JSON.parse(structuredResult.body || '"parsing error"'));
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
// 3: json object
|
|
165
|
+
resp.status(200);
|
|
166
|
+
resp.contentType('application/json');
|
|
167
|
+
resp.json(result);
|
|
168
|
+
return;
|
|
169
|
+
};
|
|
170
|
+
exports.injectGatewayResultIntoResponse = injectGatewayResultIntoResponse;
|
|
171
|
+
//# sourceMappingURL=expressRequestToHttpApiGatewayEvent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expressRequestToHttpApiGatewayEvent.js","sourceRoot":"","sources":["../../src/expressRequestToHttpApiGatewayEvent.ts"],"names":[],"mappings":";;;;;;AAQA,oDAA4B;AAYrB,MAAM,gCAAgC,GAAG,CAC9C,MAAkC,EAClC,UAAqB,EACD,EAAE;IACtB,MAAM,YAAY,GAAG,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC;SAC5C,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC;SAC3B,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QACb,OAAO,CACL,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YACtC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CACvC,CAAC;IACJ,CAAC,CAAC,CAAC;IACL,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAbW,QAAA,gCAAgC,oCAa3C;AAEK,MAAM,qBAAqB,GAAG,CACnC,MAAkC,EACV,EAAE;IAC1B,MAAM,eAAe,GAAG,MAAM,CAAC,GAA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M,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBAC9B,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;oBACtC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;gBACtC,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;SACJ;QACD,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,IAAI,IAAI,iBAAiB,CAAC,CAAC,CAAC;QAClE,OAAO;KACR;IAED,iBAAiB;IACjB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACjB,IAAI,CAAC,WAAW,CAAC,kBAAkB,CAAC,CAAC;IACrC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAClB,OAAO;AACT,CAAC,CAAC;AA3CW,QAAA,+BAA+B,mCA2C1C"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { LambdaConfig } from '@goldstack/utils-aws-lambda';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
export interface InjectRoutesParam {
|
|
4
|
+
lambdaConfigs: LambdaConfig[];
|
|
5
|
+
app: express.Application;
|
|
6
|
+
}
|
|
7
|
+
export declare const sortRoutesBySpecificity: (configs: LambdaConfig[]) => LambdaConfig[];
|
|
8
|
+
export declare const gatewayRouteToExpressPath: (route: string) => string;
|
|
9
|
+
export declare const injectRoutes: (params: InjectRoutesParam) => void;
|
|
10
|
+
//# sourceMappingURL=expressRoutes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expressRoutes.d.ts","sourceRoot":"","sources":["../../src/expressRoutes.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EAEb,MAAM,6BAA6B,CAAC;AAErC,OAAO,OAAO,MAAM,SAAS,CAAC;AAQ9B,MAAM,WAAW,iBAAiB;IAChC,aAAa,EAAE,YAAY,EAAE,CAAC;IAC9B,GAAG,EAAE,OAAO,CAAC,WAAW,CAAC;CAC1B;AAKD,eAAO,MAAM,uBAAuB,YACzB,YAAY,EAAE,KACtB,YAAY,EA+Bd,CAAC;AAEF,eAAO,MAAM,yBAAyB,UAAW,MAAM,KAAG,MAUzD,CAAC;AAEF,eAAO,MAAM,YAAY,WAAY,iBAAiB,KAAG,IAsBxD,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.injectRoutes = exports.gatewayRouteToExpressPath = exports.sortRoutesBySpecificity = void 0;
|
|
4
|
+
const utils_aws_lambda_1 = require("@goldstack/utils-aws-lambda");
|
|
5
|
+
const expressRequestToHttpApiGatewayEvent_1 = require("./expressRequestToHttpApiGatewayEvent");
|
|
6
|
+
/*
|
|
7
|
+
Ensure more generic routes are handled later
|
|
8
|
+
*/
|
|
9
|
+
const sortRoutesBySpecificity = (configs) => {
|
|
10
|
+
const res = [...configs];
|
|
11
|
+
res.sort((first, second) => {
|
|
12
|
+
if (first.path === '$default') {
|
|
13
|
+
return 1;
|
|
14
|
+
}
|
|
15
|
+
if (second.path === '$default') {
|
|
16
|
+
return -1;
|
|
17
|
+
}
|
|
18
|
+
if (first.route.indexOf('+}') !== -1 && second.route.indexOf('+}') === -1) {
|
|
19
|
+
return 1;
|
|
20
|
+
}
|
|
21
|
+
if (second.route.indexOf('+}') !== -1 && first.route.indexOf('+}') === -1) {
|
|
22
|
+
return -1;
|
|
23
|
+
}
|
|
24
|
+
if (first.route.indexOf('}') !== -1 && second.route.indexOf('}') === -1) {
|
|
25
|
+
return 1;
|
|
26
|
+
}
|
|
27
|
+
if (second.route.indexOf('}') !== -1 && first.route.indexOf('}') === -1) {
|
|
28
|
+
return -1;
|
|
29
|
+
}
|
|
30
|
+
// ensure longer routes come first
|
|
31
|
+
return (second.route.replace(/\{(.*?)\}/g, '').length -
|
|
32
|
+
first.route.replace(/\{(.*?)\}/g, '').length);
|
|
33
|
+
});
|
|
34
|
+
return res;
|
|
35
|
+
};
|
|
36
|
+
exports.sortRoutesBySpecificity = sortRoutesBySpecificity;
|
|
37
|
+
const gatewayRouteToExpressPath = (route) => {
|
|
38
|
+
if (route === '$default') {
|
|
39
|
+
return '*';
|
|
40
|
+
}
|
|
41
|
+
// see https://expressjs.com/en/guide/routing.html
|
|
42
|
+
let path = route;
|
|
43
|
+
// see https://stackoverflow.com/a/16829917/270662
|
|
44
|
+
path = path.replace(/\{(.*?)\+\}/g, ':$1*');
|
|
45
|
+
path = path.replace(/\{(.*?)\}/g, ':$1');
|
|
46
|
+
return path;
|
|
47
|
+
};
|
|
48
|
+
exports.gatewayRouteToExpressPath = gatewayRouteToExpressPath;
|
|
49
|
+
const injectRoutes = (params) => {
|
|
50
|
+
const sortedConfigs = (0, exports.sortRoutesBySpecificity)(params.lambdaConfigs);
|
|
51
|
+
for (const lambdaConfig of sortedConfigs) {
|
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
53
|
+
const script = require(lambdaConfig.absoluteFilePath);
|
|
54
|
+
const handler = script.handler;
|
|
55
|
+
const expressPath = (0, exports.gatewayRouteToExpressPath)(lambdaConfig.path);
|
|
56
|
+
params.app.all(expressPath, async (req, resp) => {
|
|
57
|
+
const result = await handler((0, expressRequestToHttpApiGatewayEvent_1.convertToGatewayEvent)({ req: req, lambdaConfig: lambdaConfig }), (0, expressRequestToHttpApiGatewayEvent_1.createContext)({
|
|
58
|
+
functionName: (0, utils_aws_lambda_1.generateFunctionName)('local', lambdaConfig),
|
|
59
|
+
}));
|
|
60
|
+
(0, expressRequestToHttpApiGatewayEvent_1.injectGatewayResultIntoResponse)(result, resp);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
exports.injectRoutes = injectRoutes;
|
|
65
|
+
//# sourceMappingURL=expressRoutes.js.map
|
|
@@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AAE,GAAG,EAAE,YAAY,EAAE,YAAY,EAAE,CAAC,EAC/D,IAAA,mDAAa,EAAC;gBACZ,YAAY,EAAE,IAAA,uCAAoB,EAAC,OAAO,EAAE,YAAY,CAAC;aAC1D,CAAC,CACH,CAAC;YACF,IAAA,qEAA+B,EAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAChD,CAAC,CACF,CAAC;KACH;AACH,CAAC,CAAC;AAtBW,QAAA,YAAY,gBAsBvB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expressRoutes.spec.d.ts","sourceRoot":"","sources":["../../src/expressRoutes.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const expressRoutes_1 = require("./expressRoutes");
|
|
4
|
+
describe('Should create express routes correctly', () => {
|
|
5
|
+
it('Should deal with parameters', () => {
|
|
6
|
+
expect((0, expressRoutes_1.gatewayRouteToExpressPath)('/hello/{param1}')).toEqual('/hello/:param1');
|
|
7
|
+
expect((0, expressRoutes_1.gatewayRouteToExpressPath)('/hello/{param1}/furtherpath')).toEqual('/hello/:param1/furtherpath');
|
|
8
|
+
expect((0, expressRoutes_1.gatewayRouteToExpressPath)('/hello/{greedy+}')).toEqual('/hello/:greedy*');
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
//# sourceMappingURL=expressRoutes.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expressRoutes.spec.js","sourceRoot":"","sources":["../../src/expressRoutes.spec.ts"],"names":[],"mappings":";;AAAA,mDAA4D;AAE5D,QAAQ,CAAC,wCAAwC,EAAE,GAAG,EAAE;IACtD,EAAE,CAAC,6BAA6B,EAAE,GAAG,EAAE;QACrC,MAAM,CAAC,IAAA,yCAAyB,EAAC,iBAAiB,CAAC,CAAC,CAAC,OAAO,CAC1D,gBAAgB,CACjB,CAAC;QACF,MAAM,CAAC,IAAA,yCAAyB,EAAC,6BAA6B,CAAC,CAAC,CAAC,OAAO,CACtE,4BAA4B,CAC7B,CAAC;QACF,MAAM,CAAC,IAAA,yCAAyB,EAAC,kBAAkB,CAAC,CAAC,CAAC,OAAO,CAC3D,iBAAiB,CAClB,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Server } from 'http';
|
|
3
|
+
import { LambdaConfig } from '@goldstack/utils-aws-lambda';
|
|
4
|
+
export interface LocalHttpAPIOptions {
|
|
5
|
+
port: string;
|
|
6
|
+
routesDir: string;
|
|
7
|
+
cors?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface StartServerResult {
|
|
10
|
+
shutdown: () => Promise<void>;
|
|
11
|
+
server: Server;
|
|
12
|
+
lambdaConfig: LambdaConfig[];
|
|
13
|
+
}
|
|
14
|
+
export declare const startServer: (options: LocalHttpAPIOptions) => Promise<StartServerResult>;
|
|
15
|
+
//# sourceMappingURL=utilsAwsHttpApiLocal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utilsAwsHttpApiLocal.d.ts","sourceRoot":"","sources":["../../src/utilsAwsHttpApiLocal.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAE9B,OAAO,EAAoB,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAG7E,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,YAAY,EAAE,CAAC;CAC9B;AAED,eAAO,MAAM,WAAW,YACb,mBAAmB,KAC3B,QAAQ,iBAAiB,CAmC3B,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.startServer = void 0;
|
|
7
|
+
const express_1 = __importDefault(require("express"));
|
|
8
|
+
const cors_1 = __importDefault(require("cors"));
|
|
9
|
+
const utils_aws_lambda_1 = require("@goldstack/utils-aws-lambda");
|
|
10
|
+
const expressRoutes_1 = require("./expressRoutes");
|
|
11
|
+
const startServer = async (options) => {
|
|
12
|
+
const app = (0, express_1.default)();
|
|
13
|
+
app.use(express_1.default.json());
|
|
14
|
+
if (options.cors) {
|
|
15
|
+
app.use((0, cors_1.default)({ origin: options.cors, credentials: true }));
|
|
16
|
+
}
|
|
17
|
+
const lambdaConfig = (0, utils_aws_lambda_1.readLambdaConfig)(options.routesDir);
|
|
18
|
+
(0, expressRoutes_1.injectRoutes)({
|
|
19
|
+
app: app,
|
|
20
|
+
lambdaConfigs: lambdaConfig,
|
|
21
|
+
});
|
|
22
|
+
const result = await new Promise((resolve) => {
|
|
23
|
+
const server = app.listen(parseInt(options.port), function () {
|
|
24
|
+
resolve(server);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
return {
|
|
28
|
+
server: result,
|
|
29
|
+
lambdaConfig: lambdaConfig,
|
|
30
|
+
shutdown: () => {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
result.close((err) => {
|
|
33
|
+
if (err) {
|
|
34
|
+
reject(err);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
resolve();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
exports.startServer = startServer;
|
|
44
|
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AE;wBACP,MAAM,CAAC,GAAG,CAAC,CAAC;wBACZ,OAAO;qBACR;oBACD,OAAO,EAAE,CAAC;gBACZ,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC,CAAC;AArCW,QAAA,WAAW,eAqCtB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utilsAwsHttpApiLocal.spec.d.ts","sourceRoot":"","sources":["../../src/utilsAwsHttpApiLocal.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const find_free_port_1 = __importDefault(require("find-free-port"));
|
|
7
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
8
|
+
const utilsAwsHttpApiLocal_1 = require("./utilsAwsHttpApiLocal");
|
|
9
|
+
describe('Should create API', () => {
|
|
10
|
+
let port = undefined;
|
|
11
|
+
let server = undefined;
|
|
12
|
+
beforeAll(async () => {
|
|
13
|
+
port = await new Promise((resolve, reject) => {
|
|
14
|
+
(0, find_free_port_1.default)(50121, (err, p1) => {
|
|
15
|
+
if (err) {
|
|
16
|
+
reject(err);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
resolve(p1);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
server = await (0, utilsAwsHttpApiLocal_1.startServer)({
|
|
23
|
+
port: `${port}`,
|
|
24
|
+
routesDir: './testData/routes',
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
test('Should receive response and support parameters', async () => {
|
|
28
|
+
const res = await (0, node_fetch_1.default)(`http://localhost:${port}/echo?message=abc`);
|
|
29
|
+
expect(await res.text()).toContain('abc');
|
|
30
|
+
});
|
|
31
|
+
test('Should support fallback', async () => {
|
|
32
|
+
const res = await (0, node_fetch_1.default)(`http://localhost:${port}/unknownEndpoint`);
|
|
33
|
+
expect(await res.text()).toContain('Unknown endpoint');
|
|
34
|
+
});
|
|
35
|
+
test('Should support path parameters', async () => {
|
|
36
|
+
const res = await (0, node_fetch_1.default)(`http://localhost:${port}/cart/123456/items`);
|
|
37
|
+
expect(await res.text()).toContain('cart [123456]');
|
|
38
|
+
});
|
|
39
|
+
test('Should support path parameters at end of path', async () => {
|
|
40
|
+
const res = await (0, node_fetch_1.default)(`http://localhost:${port}/order/abcd`);
|
|
41
|
+
const response = await res.json();
|
|
42
|
+
expect(response.message).toContain('order [abcd]');
|
|
43
|
+
});
|
|
44
|
+
test('Should support index', async () => {
|
|
45
|
+
const res = await (0, node_fetch_1.default)(`http://localhost:${port}/userRepo`);
|
|
46
|
+
expect(await res.json()).toHaveProperty('users');
|
|
47
|
+
});
|
|
48
|
+
test('Should support greedy paths', async () => {
|
|
49
|
+
const res = await (0, node_fetch_1.default)(`http://localhost:${port}/admin/my/nested/path`);
|
|
50
|
+
const response = await res.json();
|
|
51
|
+
expect(response.message).toContain('[my/nested/path]');
|
|
52
|
+
});
|
|
53
|
+
test('Should support greedy paths', async () => {
|
|
54
|
+
const res = await (0, node_fetch_1.default)(`http://localhost:${port}/admin/short`);
|
|
55
|
+
const response = await res.json();
|
|
56
|
+
expect(response.message).toContain('[short]');
|
|
57
|
+
});
|
|
58
|
+
test('Should not match root for greedy paths', async () => {
|
|
59
|
+
const res = await (0, node_fetch_1.default)(`http://localhost:${port}/admin`);
|
|
60
|
+
const response = await res.json();
|
|
61
|
+
expect(response.message).toContain('Unknown endpoint');
|
|
62
|
+
});
|
|
63
|
+
afterAll(async () => {
|
|
64
|
+
if (server) {
|
|
65
|
+
await server.shutdown();
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
//# sourceMappingURL=utilsAwsHttpApiLocal.spec.js.map
|
|
@@ -0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G,MAAM,IAAA,oBAAK,EAAC,oBAAoB,IAAI,cAAc,CAAC,CAAC;QAChE,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;QAClC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;IAChD,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,wCAAwC,EAAE,KAAK,IAAI,EAAE;QACxD,MAAM,GAAG,GAAG,MAAM,IAAA,oBAAK,EAAC,oBAAoB,IAAI,QAAQ,CAAC,CAAC;QAC1D,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;QAClC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,SAAS,CAAC,kBAAkB,CAAC,CAAC;IACzD,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,KAAK,IAAI,EAAE;QAClB,IAAI,MAAM,EAAE;YACV,MAAM,MAAM,CAAC,QAAQ,EAAE,CAAC;SACzB;IACH,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@goldstack/utils-aws-http-api-local",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Starts a local AWS HTTP API for testing",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"goldstack",
|
|
7
|
+
"utility",
|
|
8
|
+
"infrastructure",
|
|
9
|
+
"cli",
|
|
10
|
+
"docker",
|
|
11
|
+
"aws",
|
|
12
|
+
"lambda",
|
|
13
|
+
"aws sdk",
|
|
14
|
+
"http api",
|
|
15
|
+
"api gateway",
|
|
16
|
+
"testing",
|
|
17
|
+
"IaC",
|
|
18
|
+
"configuration"
|
|
19
|
+
],
|
|
20
|
+
"homepage": "https://goldstack.party",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/goldstack/goldstack/issues"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/goldstack/goldstack.git"
|
|
27
|
+
},
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"author": "Max Rohde",
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"main": "dist/src/utilsAwsHttpApiLocal.js",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "yarn clean && yarn compile",
|
|
34
|
+
"build:watch": "yarn clean && yarn compile-watch",
|
|
35
|
+
"clean": "rm -rf ./dist",
|
|
36
|
+
"compile": "tsc -p tsconfig.json",
|
|
37
|
+
"compile-watch": "tsc -p tsconfig.json --watch",
|
|
38
|
+
"compile-watch:light": "nodemon --watch ./src/ -e '*' --exec 'yarn compile'",
|
|
39
|
+
"coverage": "jest --collect-coverage --passWithNoTests --config=./jest.config.js",
|
|
40
|
+
"prepublishOnly": "yarn run build",
|
|
41
|
+
"publish": "utils-git changed --exec \"yarn npm publish $@\"",
|
|
42
|
+
"test-ci": "jest --passWithNoTests --config=./jest.config.js",
|
|
43
|
+
"test-watch": "jest --watch --passWithNoTests --config=./jest.config.js",
|
|
44
|
+
"version:apply": "utils-git changed --exec \"yarn version $@ && yarn version apply\"",
|
|
45
|
+
"version:apply:force": "yarn version $@ && yarn version apply"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@goldstack/utils-aws-lambda": "0.1.4",
|
|
49
|
+
"@types/aws-lambda": "^8.10.88",
|
|
50
|
+
"cookie": "^0.4.1",
|
|
51
|
+
"express": "^4.17.1"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@goldstack/utils-git": "0.1.29",
|
|
55
|
+
"@types/cookie": "^0",
|
|
56
|
+
"@types/express": "^4.17.13",
|
|
57
|
+
"@types/jest": "^27.0.2",
|
|
58
|
+
"@types/node": "^16.11.0",
|
|
59
|
+
"@types/node-fetch": "2",
|
|
60
|
+
"cors": "^2.8.5",
|
|
61
|
+
"find-free-port": "^2.0.0",
|
|
62
|
+
"jest": "^26.6.3",
|
|
63
|
+
"node-fetch": "2",
|
|
64
|
+
"ts-jest": "^26.5.4",
|
|
65
|
+
"typescript": "^4.4.3"
|
|
66
|
+
},
|
|
67
|
+
"publishConfig": {
|
|
68
|
+
"main": "dist/src/utilsAwsHttpApiLocal.js"
|
|
69
|
+
}
|
|
70
|
+
}
|