@tsed/platform-serverless 6.87.2 → 6.90.0
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/lib/{builder → cjs/builder}/PlatformServerless.d.ts +0 -0
- package/lib/cjs/builder/PlatformServerless.js +156 -0
- package/lib/cjs/builder/PlatformServerless.js.map +1 -0
- package/lib/{builder → cjs/builder}/PlatformServerlessHandler.d.ts +0 -0
- package/lib/cjs/builder/PlatformServerlessHandler.js +93 -0
- package/lib/cjs/builder/PlatformServerlessHandler.js.map +1 -0
- package/lib/{domain → cjs/domain}/ServerlessContext.d.ts +0 -0
- package/lib/cjs/domain/ServerlessContext.js +26 -0
- package/lib/cjs/domain/ServerlessContext.js.map +1 -0
- package/lib/{domain → cjs/domain}/ServerlessRequest.d.ts +0 -0
- package/lib/cjs/domain/ServerlessRequest.js +80 -0
- package/lib/cjs/domain/ServerlessRequest.js.map +1 -0
- package/lib/{domain → cjs/domain}/ServerlessResponse.d.ts +0 -0
- package/lib/cjs/domain/ServerlessResponse.js +193 -0
- package/lib/cjs/domain/ServerlessResponse.js.map +1 -0
- package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
- package/lib/cjs/index.js +23 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/{services → cjs/services}/PlatformServerlessTest.d.ts +0 -0
- package/lib/cjs/services/PlatformServerlessTest.js +196 -0
- package/lib/cjs/services/PlatformServerlessTest.js.map +1 -0
- package/lib/{utils → cjs/utils}/getRequestId.d.ts +0 -0
- package/lib/cjs/utils/getRequestId.js +13 -0
- package/lib/cjs/utils/getRequestId.js.map +1 -0
- package/lib/{utils → cjs/utils}/setResponseHeaders.d.ts +0 -0
- package/lib/cjs/utils/setResponseHeaders.js +35 -0
- package/lib/cjs/utils/setResponseHeaders.js.map +1 -0
- package/lib/esm/builder/PlatformServerless.js +133 -0
- package/lib/esm/builder/PlatformServerless.js.map +1 -0
- package/lib/esm/builder/PlatformServerlessHandler.js +90 -0
- package/lib/esm/builder/PlatformServerlessHandler.js.map +1 -0
- package/lib/esm/domain/ServerlessContext.js +22 -0
- package/lib/esm/domain/ServerlessContext.js.map +1 -0
- package/lib/esm/domain/ServerlessRequest.js +76 -0
- package/lib/esm/domain/ServerlessRequest.js.map +1 -0
- package/lib/esm/domain/ServerlessResponse.js +189 -0
- package/lib/esm/domain/ServerlessResponse.js.map +1 -0
- package/lib/esm/index.js +11 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/services/PlatformServerlessTest.js +191 -0
- package/lib/esm/services/PlatformServerlessTest.js.map +1 -0
- package/lib/esm/utils/getRequestId.js +9 -0
- package/lib/esm/utils/getRequestId.js.map +1 -0
- package/lib/esm/utils/setResponseHeaders.js +31 -0
- package/lib/esm/utils/setResponseHeaders.js.map +1 -0
- package/lib/types/builder/PlatformServerless.d.ts +31 -0
- package/lib/types/builder/PlatformServerlessHandler.d.ts +19 -0
- package/lib/types/domain/ServerlessContext.d.ts +19 -0
- package/lib/types/domain/ServerlessRequest.d.ts +55 -0
- package/lib/types/domain/ServerlessResponse.d.ts +105 -0
- package/lib/types/index.d.ts +9 -0
- package/lib/types/services/PlatformServerlessTest.d.ts +46 -0
- package/lib/types/utils/getRequestId.d.ts +2 -0
- package/lib/types/utils/setResponseHeaders.d.ts +5 -0
- package/package.json +17 -13
- package/readme.md +52 -28
- package/tsconfig.compile.esm.json +23 -0
- package/lib/index-a6c91998.js +0 -955
- package/lib/index-a6c91998.js.map +0 -1
- package/lib/index-a7574150.js +0 -975
- package/lib/index-a7574150.js.map +0 -1
- package/lib/index-b255dcf0.js +0 -2350
- package/lib/index-b255dcf0.js.map +0 -1
- package/lib/index-eeb85309.js +0 -2354
- package/lib/index-eeb85309.js.map +0 -1
- package/lib/index.js +0 -90
- package/lib/index.js.map +0 -1
- package/lib/index.modern.js +0 -12
- package/lib/index.modern.js.map +0 -1
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { getValue } from "@tsed/core";
|
|
2
|
+
/**
|
|
3
|
+
* @platform
|
|
4
|
+
*/
|
|
5
|
+
export class ServerlessRequest {
|
|
6
|
+
constructor({ event }) {
|
|
7
|
+
this.raw = event;
|
|
8
|
+
}
|
|
9
|
+
get secure() {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
get host() {
|
|
13
|
+
return this.get("host");
|
|
14
|
+
}
|
|
15
|
+
get protocol() {
|
|
16
|
+
return "https";
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Get the url of the request.
|
|
20
|
+
*
|
|
21
|
+
* Is equivalent of `express.response.originalUrl || express.response.url`.
|
|
22
|
+
*/
|
|
23
|
+
get url() {
|
|
24
|
+
return this.raw.path;
|
|
25
|
+
}
|
|
26
|
+
get headers() {
|
|
27
|
+
return this.raw.headers;
|
|
28
|
+
}
|
|
29
|
+
get method() {
|
|
30
|
+
return this.raw.httpMethod;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Contains key-value pairs of data submitted in the request body. By default, it is `undefined`, and is populated when you use
|
|
34
|
+
* `body-parsing` middleware such as `express.json()` or `express.urlencoded()`.
|
|
35
|
+
*/
|
|
36
|
+
get body() {
|
|
37
|
+
try {
|
|
38
|
+
return this.raw.body ? JSON.parse(this.raw.body) : {};
|
|
39
|
+
}
|
|
40
|
+
catch (er) {
|
|
41
|
+
return this.raw.body;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
get rawBody() {
|
|
45
|
+
return this.raw.body;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* This property is an object containing properties mapped to the named route `parameters`.
|
|
49
|
+
* For example, if you have the route `/user/:name`, then the `name` property is available as `req.params.name`.
|
|
50
|
+
* This object defaults to `{}`.
|
|
51
|
+
*/
|
|
52
|
+
get params() {
|
|
53
|
+
return this.raw.pathParameters || {};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* This property is an object containing a property for each query string parameter in the route.
|
|
57
|
+
* When query parser is set to disabled, it is an empty object `{}`, otherwise it is the result of the configured query parser.
|
|
58
|
+
*/
|
|
59
|
+
get query() {
|
|
60
|
+
return this.raw.queryStringParameters || {};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Returns the HTTP request header specified by field. The match is case-insensitive.
|
|
64
|
+
*
|
|
65
|
+
* ```typescript
|
|
66
|
+
* request.get('Content-Type') // => "text/plain"
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @param name
|
|
70
|
+
*/
|
|
71
|
+
get(name) {
|
|
72
|
+
return getValue(this.raw.headers, name);
|
|
73
|
+
}
|
|
74
|
+
destroy() { }
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=ServerlessRequest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServerlessRequest.js","sourceRoot":"","sources":["../../../src/domain/ServerlessRequest.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;AAMpC;;GAEG;AACH,MAAM,OAAO,iBAAiB;IAG5B,YAAY,EAAC,KAAK,EAA2B;QAC3C,IAAI,CAAC,GAAG,GAAG,KAAK,CAAC;IACnB,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC1B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,OAAO,CAAC;IACjB,CAAC;IAED;;;;OAIG;IACH,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;IACvB,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC;IAC1B,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC;IAC7B,CAAC;IAED;;;OAGG;IACH,IAAI,IAAI;QACN,IAAI;YACF,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;SACvD;QAAC,OAAO,EAAE,EAAE;YACX,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;SACtB;IACH,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,cAAc,IAAI,EAAE,CAAC;IACvC,CAAC;IAED;;;OAGG;IACH,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,GAAG,CAAC,qBAAqB,IAAI,EAAE,CAAC;IAC9C,CAAC;IAED;;;;;;;;OAQG;IACH,GAAG,CAAC,IAAY;QACd,OAAO,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAC1C,CAAC;IAED,OAAO,KAAI,CAAC;CACb","sourcesContent":["import type {APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEventBase, Context} from \"aws-lambda\";\nimport {getValue} from \"@tsed/core\";\n\nexport interface ServerlessRequestOptions {\n event: APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;\n context: Context;\n}\n/**\n * @platform\n */\nexport class ServerlessRequest {\n readonly raw: APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;\n\n constructor({event}: ServerlessRequestOptions) {\n this.raw = event;\n }\n\n get secure(): boolean {\n return true;\n }\n\n get host(): string {\n return this.get(\"host\");\n }\n\n get protocol(): string {\n return \"https\";\n }\n\n /**\n * Get the url of the request.\n *\n * Is equivalent of `express.response.originalUrl || express.response.url`.\n */\n get url(): string {\n return this.raw.path;\n }\n\n get headers() {\n return this.raw.headers;\n }\n\n get method(): string {\n return this.raw.httpMethod;\n }\n\n /**\n * Contains key-value pairs of data submitted in the request body. By default, it is `undefined`, and is populated when you use\n * `body-parsing` middleware such as `express.json()` or `express.urlencoded()`.\n */\n get body(): any {\n try {\n return this.raw.body ? JSON.parse(this.raw.body) : {};\n } catch (er) {\n return this.raw.body;\n }\n }\n\n get rawBody(): any {\n return this.raw.body;\n }\n\n /**\n * This property is an object containing properties mapped to the named route `parameters`.\n * For example, if you have the route `/user/:name`, then the `name` property is available as `req.params.name`.\n * This object defaults to `{}`.\n */\n get params(): {[key: string]: any} {\n return this.raw.pathParameters || {};\n }\n\n /**\n * This property is an object containing a property for each query string parameter in the route.\n * When query parser is set to disabled, it is an empty object `{}`, otherwise it is the result of the configured query parser.\n */\n get query(): {[key: string]: any} {\n return this.raw.queryStringParameters || {};\n }\n\n /**\n * Returns the HTTP request header specified by field. The match is case-insensitive.\n *\n * ```typescript\n * request.get('Content-Type') // => \"text/plain\"\n * ```\n *\n * @param name\n */\n get(name: string) {\n return getValue(this.raw.headers, name);\n }\n\n destroy() {}\n}\n"]}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
var _ServerlessResponse_status, _ServerlessResponse_body, _ServerlessResponse_headers, _ServerlessResponse_locals, _ServerlessResponse_isHeadersSent, _ServerlessResponse_endListeners;
|
|
2
|
+
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
|
+
import { getValue } from "@tsed/core";
|
|
4
|
+
import { getStatusMessage } from "@tsed/schema";
|
|
5
|
+
import encodeUrl from "encodeurl";
|
|
6
|
+
import * as mime from "mime";
|
|
7
|
+
/**
|
|
8
|
+
* @platform
|
|
9
|
+
*/
|
|
10
|
+
export class ServerlessResponse {
|
|
11
|
+
constructor({ event, request }) {
|
|
12
|
+
_ServerlessResponse_status.set(this, 200);
|
|
13
|
+
_ServerlessResponse_body.set(this, undefined);
|
|
14
|
+
_ServerlessResponse_headers.set(this, {});
|
|
15
|
+
_ServerlessResponse_locals.set(this, {});
|
|
16
|
+
_ServerlessResponse_isHeadersSent.set(this, false);
|
|
17
|
+
_ServerlessResponse_endListeners.set(this, []);
|
|
18
|
+
this.event = event;
|
|
19
|
+
this.request = request;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Get the current statusCode
|
|
23
|
+
*/
|
|
24
|
+
get statusCode() {
|
|
25
|
+
return __classPrivateFieldGet(this, _ServerlessResponse_status, "f");
|
|
26
|
+
}
|
|
27
|
+
set statusCode(status) {
|
|
28
|
+
__classPrivateFieldSet(this, _ServerlessResponse_status, status, "f");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any). Otherwise, this property is identical to app.locals.
|
|
32
|
+
*
|
|
33
|
+
* This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on.
|
|
34
|
+
*/
|
|
35
|
+
get locals() {
|
|
36
|
+
return __classPrivateFieldGet(this, _ServerlessResponse_locals, "f");
|
|
37
|
+
}
|
|
38
|
+
get isHeadersSent() {
|
|
39
|
+
return __classPrivateFieldGet(this, _ServerlessResponse_isHeadersSent, "f");
|
|
40
|
+
}
|
|
41
|
+
getStatus() {
|
|
42
|
+
return __classPrivateFieldGet(this, _ServerlessResponse_status, "f");
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Sets the HTTP status for the response.
|
|
46
|
+
*
|
|
47
|
+
* @param status
|
|
48
|
+
*/
|
|
49
|
+
status(status) {
|
|
50
|
+
__classPrivateFieldSet(this, _ServerlessResponse_status, status, "f");
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
set(name, value) {
|
|
54
|
+
return this.setHeader(name, value);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns the HTTP response header specified by field. The match is case-insensitive.
|
|
58
|
+
*
|
|
59
|
+
* ```typescript
|
|
60
|
+
* response.get('Content-Type') // => "text/plain"
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @param name
|
|
64
|
+
*/
|
|
65
|
+
get(name) {
|
|
66
|
+
return getValue(__classPrivateFieldGet(this, _ServerlessResponse_headers, "f"), name.toLowerCase());
|
|
67
|
+
}
|
|
68
|
+
getHeaders() {
|
|
69
|
+
return {
|
|
70
|
+
...__classPrivateFieldGet(this, _ServerlessResponse_headers, "f")
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
hasStatus() {
|
|
74
|
+
return __classPrivateFieldGet(this, _ServerlessResponse_status, "f") !== 200;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Set header `field` to `val`, or pass
|
|
78
|
+
* an object of header fields.
|
|
79
|
+
*
|
|
80
|
+
* Examples:
|
|
81
|
+
* ```typescript
|
|
82
|
+
* response.setHeaders({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* Aliased as `res.header()`.
|
|
86
|
+
*/
|
|
87
|
+
setHeaders(headers) {
|
|
88
|
+
// apply headers
|
|
89
|
+
Object.entries(headers).forEach(([key, item]) => {
|
|
90
|
+
this.setHeader(key, item);
|
|
91
|
+
});
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
setHeader(key, item) {
|
|
95
|
+
if (item !== null && item !== undefined) {
|
|
96
|
+
key = key.toLowerCase();
|
|
97
|
+
if (key === "location") {
|
|
98
|
+
// "back" is an alias for the referrer
|
|
99
|
+
if (item === "back") {
|
|
100
|
+
item = this.request.get("Referrer") || "/";
|
|
101
|
+
}
|
|
102
|
+
item = encodeUrl(String(item));
|
|
103
|
+
}
|
|
104
|
+
__classPrivateFieldSet(this, _ServerlessResponse_headers, {
|
|
105
|
+
...__classPrivateFieldGet(this, _ServerlessResponse_headers, "f"),
|
|
106
|
+
[key.toLowerCase()]: item
|
|
107
|
+
}, "f");
|
|
108
|
+
}
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Set `Content-Type` response header with `type` through `mime.lookup()`
|
|
113
|
+
* when it does not contain "/", or set the Content-Type to `type` otherwise.
|
|
114
|
+
*
|
|
115
|
+
* Examples:
|
|
116
|
+
*
|
|
117
|
+
* res.type('.html');
|
|
118
|
+
* res.type('html');
|
|
119
|
+
* res.type('json');
|
|
120
|
+
* res.type('application/json');
|
|
121
|
+
* res.type('png');
|
|
122
|
+
*/
|
|
123
|
+
contentType(contentType) {
|
|
124
|
+
this.setHeader("Content-Type", mime.getType(contentType));
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
contentLength(length) {
|
|
128
|
+
this.setHeader("Content-Length", length);
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
getContentLength() {
|
|
132
|
+
if (this.get("Content-Length")) {
|
|
133
|
+
return parseInt(this.get("Content-Length"), 10) || 0;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
getContentType() {
|
|
137
|
+
return String(this.get("Content-Type") || "").split(";")[0];
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an [HTTP status code](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).
|
|
141
|
+
* If not specified, status defaults to `302 Found`.
|
|
142
|
+
*
|
|
143
|
+
* @param status
|
|
144
|
+
* @param url
|
|
145
|
+
*/
|
|
146
|
+
redirect(status, url) {
|
|
147
|
+
// Set location header
|
|
148
|
+
const address = this.location(url).get("Location");
|
|
149
|
+
const body = `${getStatusMessage(status)}. Redirecting to ${address}`;
|
|
150
|
+
this.status(status).set("Content-Length", Buffer.byteLength(body)).body(body);
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Sets the response Location HTTP header to the specified path parameter.
|
|
155
|
+
*
|
|
156
|
+
* @param url
|
|
157
|
+
*/
|
|
158
|
+
location(url) {
|
|
159
|
+
return this.set("Location", url);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Send any data to your consumer.
|
|
163
|
+
*
|
|
164
|
+
* This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data.
|
|
165
|
+
* It choose the better way to send the data.
|
|
166
|
+
*
|
|
167
|
+
* @param body
|
|
168
|
+
*/
|
|
169
|
+
body(body) {
|
|
170
|
+
__classPrivateFieldSet(this, _ServerlessResponse_body, body, "f");
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
getBody() {
|
|
174
|
+
return __classPrivateFieldGet(this, _ServerlessResponse_body, "f");
|
|
175
|
+
}
|
|
176
|
+
isDone() {
|
|
177
|
+
return __classPrivateFieldGet(this, _ServerlessResponse_isHeadersSent, "f");
|
|
178
|
+
}
|
|
179
|
+
onEnd(cb) {
|
|
180
|
+
__classPrivateFieldGet(this, _ServerlessResponse_endListeners, "f").push(cb);
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
destroy() {
|
|
184
|
+
__classPrivateFieldGet(this, _ServerlessResponse_endListeners, "f").forEach((cb) => cb());
|
|
185
|
+
__classPrivateFieldSet(this, _ServerlessResponse_isHeadersSent, true, "f");
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
_ServerlessResponse_status = new WeakMap(), _ServerlessResponse_body = new WeakMap(), _ServerlessResponse_headers = new WeakMap(), _ServerlessResponse_locals = new WeakMap(), _ServerlessResponse_isHeadersSent = new WeakMap(), _ServerlessResponse_endListeners = new WeakMap();
|
|
189
|
+
//# sourceMappingURL=ServerlessResponse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServerlessResponse.js","sourceRoot":"","sources":["../../../src/domain/ServerlessResponse.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;AACpC,OAAO,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AAE9C,OAAO,SAAS,MAAM,WAAW,CAAC;AAClC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAW7B;;GAEG;AACH,MAAM,OAAO,kBAAkB;IAW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,CAAC,EAAE,EAAE,CAAC,CAAC;QACnD,uBAAA,IAAI,qCAAkB,IAAI,MAAA,CAAC;IAC7B,CAAC;CACF","sourcesContent":["import {getValue} from \"@tsed/core\";\nimport {getStatusMessage} from \"@tsed/schema\";\nimport type {APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEventBase, Context} from \"aws-lambda\";\nimport encodeUrl from \"encodeurl\";\nimport * as mime from \"mime\";\nimport type {ServerlessRequest} from \"./ServerlessRequest\";\n\nexport interface ServerlessResponseOptions {\n event: APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;\n context: Context;\n request: ServerlessRequest;\n}\n\nexport type HeaderValue = boolean | number | string;\n\n/**\n * @platform\n */\nexport class ServerlessResponse {\n readonly event: APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;\n readonly request: ServerlessRequest;\n\n #status: number = 200;\n #body: any = undefined;\n #headers: Record<string, HeaderValue> = {};\n #locals: Record<string, any> = {};\n #isHeadersSent = false;\n #endListeners: Function[] = [];\n\n constructor({event, request}: ServerlessResponseOptions) {\n this.event = event;\n this.request = request;\n }\n\n /**\n * Get the current statusCode\n */\n get statusCode() {\n return this.#status;\n }\n\n set statusCode(status) {\n this.#status = status;\n }\n\n /**\n * An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any). Otherwise, this property is identical to app.locals.\n *\n * This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on.\n */\n get locals() {\n return this.#locals;\n }\n\n get isHeadersSent() {\n return this.#isHeadersSent;\n }\n\n getStatus() {\n return this.#status;\n }\n\n /**\n * Sets the HTTP status for the response.\n *\n * @param status\n */\n status(status: number) {\n this.#status = status;\n return this;\n }\n\n set(name: string, value: any) {\n return this.setHeader(name, value);\n }\n\n /**\n * Returns the HTTP response header specified by field. The match is case-insensitive.\n *\n * ```typescript\n * response.get('Content-Type') // => \"text/plain\"\n * ```\n *\n * @param name\n */\n get(name: string): string | number | boolean | undefined {\n return getValue(this.#headers, name.toLowerCase());\n }\n\n getHeaders(): Record<string, HeaderValue> {\n return {\n ...this.#headers\n };\n }\n\n hasStatus() {\n return this.#status !== 200;\n }\n\n /**\n * Set header `field` to `val`, or pass\n * an object of header fields.\n *\n * Examples:\n * ```typescript\n * response.setHeaders({ Accept: 'text/plain', 'X-API-Key': 'tobi' });\n * ```\n *\n * Aliased as `res.header()`.\n */\n setHeaders(headers: Record<string, HeaderValue>) {\n // apply headers\n Object.entries(headers).forEach(([key, item]) => {\n this.setHeader(key, item);\n });\n\n return this;\n }\n\n setHeader(key: string, item: HeaderValue | null | undefined) {\n if (item !== null && item !== undefined) {\n key = key.toLowerCase();\n\n if (key === \"location\") {\n // \"back\" is an alias for the referrer\n if (item === \"back\") {\n item = this.request.get(\"Referrer\") || \"/\";\n }\n\n item = encodeUrl(String(item));\n }\n\n this.#headers = {\n ...this.#headers,\n [key.toLowerCase()]: item as any\n };\n }\n\n return this;\n }\n\n /**\n * Set `Content-Type` response header with `type` through `mime.lookup()`\n * when it does not contain \"/\", or set the Content-Type to `type` otherwise.\n *\n * Examples:\n *\n * res.type('.html');\n * res.type('html');\n * res.type('json');\n * res.type('application/json');\n * res.type('png');\n */\n contentType(contentType: string) {\n this.setHeader(\"Content-Type\", mime.getType(contentType));\n\n return this;\n }\n\n contentLength(length: number) {\n this.setHeader(\"Content-Length\", length);\n return this;\n }\n\n getContentLength() {\n if (this.get(\"Content-Length\")) {\n return parseInt(this.get(\"Content-Length\") as any, 10) || 0;\n }\n }\n\n getContentType() {\n return String(this.get(\"Content-Type\") || \"\").split(\";\")[0];\n }\n\n /**\n * Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an [HTTP status code](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).\n * If not specified, status defaults to `302 Found`.\n *\n * @param status\n * @param url\n */\n redirect(status: number, url: string) {\n // Set location header\n const address = this.location(url).get(\"Location\");\n const body = `${getStatusMessage(status)}. Redirecting to ${address}`;\n\n this.status(status).set(\"Content-Length\", Buffer.byteLength(body)).body(body);\n\n return this;\n }\n\n /**\n * Sets the response Location HTTP header to the specified path parameter.\n *\n * @param url\n */\n location(url: string) {\n return this.set(\"Location\", url);\n }\n\n /**\n * Send any data to your consumer.\n *\n * This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data.\n * It choose the better way to send the data.\n *\n * @param body\n */\n body(body: any) {\n this.#body = body;\n\n return this;\n }\n\n getBody() {\n return this.#body;\n }\n\n isDone(): boolean {\n return this.#isHeadersSent;\n }\n\n onEnd(cb: Function) {\n this.#endListeners.push(cb);\n\n return this;\n }\n\n destroy() {\n this.#endListeners.forEach((cb: Function) => cb());\n this.#isHeadersSent = true;\n }\n}\n"]}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "@tsed/di";
|
|
2
|
+
export { Get, Post, Put, Delete, Head, Patch, Options, Route as Lambda } from "@tsed/schema";
|
|
3
|
+
export * from "@tsed/platform-params";
|
|
4
|
+
export * from "./builder/PlatformServerlessHandler";
|
|
5
|
+
export * from "./builder/PlatformServerless";
|
|
6
|
+
export * from "./services/PlatformServerlessTest";
|
|
7
|
+
// decorators
|
|
8
|
+
export * from "./domain/ServerlessContext";
|
|
9
|
+
export * from "./domain/ServerlessResponse";
|
|
10
|
+
export * from "./domain/ServerlessResponse";
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,OAAO,EAAC,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,IAAI,MAAM,EAAC,MAAM,cAAc,CAAC;AAC3F,cAAc,uBAAuB,CAAC;AAEtC,cAAc,qCAAqC,CAAC;AACpD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,mCAAmC,CAAC;AAElD,aAAa;AACb,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,6BAA6B,CAAC","sourcesContent":["export * from \"@tsed/di\";\nexport {Get, Post, Put, Delete, Head, Patch, Options, Route as Lambda} from \"@tsed/schema\";\nexport * from \"@tsed/platform-params\";\n\nexport * from \"./builder/PlatformServerlessHandler\";\nexport * from \"./builder/PlatformServerless\";\nexport * from \"./services/PlatformServerlessTest\";\n\n// decorators\nexport * from \"./domain/ServerlessContext\";\nexport * from \"./domain/ServerlessResponse\";\nexport * from \"./domain/ServerlessResponse\";\n"]}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { DITest } from "@tsed/di";
|
|
2
|
+
import { PlatformServerless } from "../builder/PlatformServerless";
|
|
3
|
+
import { ServerlessContext } from "../domain/ServerlessContext";
|
|
4
|
+
export class LambdaClientRequest extends Promise {
|
|
5
|
+
constructor() {
|
|
6
|
+
super(...arguments);
|
|
7
|
+
this.event = LambdaClientRequest.createFakeEvent();
|
|
8
|
+
this.context = LambdaClientRequest.createFakeContext();
|
|
9
|
+
}
|
|
10
|
+
static call(lambdaName) {
|
|
11
|
+
const resolvers = {};
|
|
12
|
+
const promise = new LambdaClientRequest((resolve, reject) => {
|
|
13
|
+
resolvers.resolve = resolve;
|
|
14
|
+
resolvers.reject = reject;
|
|
15
|
+
});
|
|
16
|
+
promise.init(lambdaName, resolvers.resolve, resolvers.reject);
|
|
17
|
+
return promise;
|
|
18
|
+
}
|
|
19
|
+
static get(path, options = {}) {
|
|
20
|
+
return this.call("handler").get(path, options);
|
|
21
|
+
}
|
|
22
|
+
static post(path, body, options = {}) {
|
|
23
|
+
return this.call("handler").post(path, body, options);
|
|
24
|
+
}
|
|
25
|
+
static put(path, body, options = {}) {
|
|
26
|
+
return this.call("handler").put(path, body, options);
|
|
27
|
+
}
|
|
28
|
+
static patch(path, body, options = {}) {
|
|
29
|
+
return this.call("handler").patch(path, body, options);
|
|
30
|
+
}
|
|
31
|
+
static delete(path, body, options = {}) {
|
|
32
|
+
return this.call("handler").delete(path, body, options);
|
|
33
|
+
}
|
|
34
|
+
static createFakeEvent(event = {}) {
|
|
35
|
+
return {
|
|
36
|
+
body: "",
|
|
37
|
+
headers: {},
|
|
38
|
+
httpMethod: "",
|
|
39
|
+
isBase64Encoded: false,
|
|
40
|
+
multiValueHeaders: {},
|
|
41
|
+
multiValueQueryStringParameters: {},
|
|
42
|
+
path: event.path || "/",
|
|
43
|
+
pathParameters: {},
|
|
44
|
+
queryStringParameters: {},
|
|
45
|
+
resource: "",
|
|
46
|
+
stageVariables: {},
|
|
47
|
+
...event,
|
|
48
|
+
requestContext: {
|
|
49
|
+
...event === null || event === void 0 ? void 0 : event.requestContext,
|
|
50
|
+
accountId: "",
|
|
51
|
+
apiId: "",
|
|
52
|
+
protocol: "https",
|
|
53
|
+
httpMethod: "",
|
|
54
|
+
identity: {},
|
|
55
|
+
path: event.path || "/",
|
|
56
|
+
stage: "",
|
|
57
|
+
requestId: "requestId",
|
|
58
|
+
requestTimeEpoch: 0,
|
|
59
|
+
resourceId: 1,
|
|
60
|
+
resourcePath: event.path || "/"
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
static createFakeContext(context) {
|
|
65
|
+
return {
|
|
66
|
+
awsRequestId: "awsRequestId",
|
|
67
|
+
callbackWaitsForEmptyEventLoop: false,
|
|
68
|
+
functionName: "",
|
|
69
|
+
functionVersion: "",
|
|
70
|
+
invokedFunctionArn: "",
|
|
71
|
+
logGroupName: "",
|
|
72
|
+
logStreamName: "",
|
|
73
|
+
memoryLimitInMB: "",
|
|
74
|
+
...(context || {})
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
get(path, options = {}) {
|
|
78
|
+
Object.assign(this.event, options);
|
|
79
|
+
this.event.path = path;
|
|
80
|
+
this.event.httpMethod = "GET";
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
post(path, body = {}, options = {}) {
|
|
84
|
+
Object.assign(this.event, options);
|
|
85
|
+
this.event.path = path;
|
|
86
|
+
this.event.httpMethod = "POST";
|
|
87
|
+
this.body(body);
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
patch(path, body = {}, options = {}) {
|
|
91
|
+
Object.assign(this.event, options);
|
|
92
|
+
this.event.path = path;
|
|
93
|
+
this.event.httpMethod = "PATCH";
|
|
94
|
+
this.body(body);
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
put(path, body = {}, options = {}) {
|
|
98
|
+
Object.assign(this.event, options);
|
|
99
|
+
this.event.path = path;
|
|
100
|
+
this.event.httpMethod = "PUT";
|
|
101
|
+
this.body(body);
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
delete(path, body = {}, options = {}) {
|
|
105
|
+
Object.assign(this.event, options);
|
|
106
|
+
this.event.path = path;
|
|
107
|
+
this.event.httpMethod = "DELETE";
|
|
108
|
+
this.body(body);
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
query(query) {
|
|
112
|
+
this.event.queryStringParameters = {
|
|
113
|
+
...this.event.queryStringParameters,
|
|
114
|
+
...JSON.parse(JSON.stringify(query))
|
|
115
|
+
};
|
|
116
|
+
return this;
|
|
117
|
+
}
|
|
118
|
+
params(pathParameters) {
|
|
119
|
+
this.event.pathParameters = {
|
|
120
|
+
...this.event.pathParameters,
|
|
121
|
+
...JSON.parse(JSON.stringify(pathParameters))
|
|
122
|
+
};
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
headers(headers) {
|
|
126
|
+
this.event.headers = {
|
|
127
|
+
...this.event.headers,
|
|
128
|
+
...JSON.parse(JSON.stringify(headers))
|
|
129
|
+
};
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
body(body) {
|
|
133
|
+
if (body !== undefined) {
|
|
134
|
+
this.event.headers["content-type"] = "application/json";
|
|
135
|
+
this.event.body = JSON.stringify(body);
|
|
136
|
+
}
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
init(lambda, resolve, reject) {
|
|
140
|
+
setTimeout(async () => {
|
|
141
|
+
try {
|
|
142
|
+
const result = await PlatformServerlessTest.callbacks[lambda](this.event, this.context, resolve);
|
|
143
|
+
resolve(result);
|
|
144
|
+
}
|
|
145
|
+
catch (er) {
|
|
146
|
+
reject(er);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
export class PlatformServerlessTest extends DITest {
|
|
152
|
+
static bootstrap(settings = {}) {
|
|
153
|
+
return async function before() {
|
|
154
|
+
// @ts-ignore
|
|
155
|
+
const instance = PlatformServerless.bootstrap(DITest.configure(settings));
|
|
156
|
+
PlatformServerlessTest.instance = instance;
|
|
157
|
+
PlatformServerlessTest.callbacks = instance.callbacks();
|
|
158
|
+
PlatformServerlessTest.callbacks.handler = instance.handler();
|
|
159
|
+
// used by inject method
|
|
160
|
+
DITest.injector = instance.injector;
|
|
161
|
+
return instance.promise;
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.
|
|
166
|
+
*/
|
|
167
|
+
static async reset() {
|
|
168
|
+
if (PlatformServerlessTest.instance) {
|
|
169
|
+
await PlatformServerlessTest.instance.stop();
|
|
170
|
+
}
|
|
171
|
+
if (DITest.hasInjector()) {
|
|
172
|
+
await DITest.injector.destroy();
|
|
173
|
+
DITest._injector = null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
static createServerlessContext({ endpoint }) {
|
|
177
|
+
const context = LambdaClientRequest.createFakeContext();
|
|
178
|
+
const event = LambdaClientRequest.createFakeEvent();
|
|
179
|
+
return new ServerlessContext({
|
|
180
|
+
event,
|
|
181
|
+
context,
|
|
182
|
+
id: context.awsRequestId,
|
|
183
|
+
logger: PlatformServerlessTest.injector.logger,
|
|
184
|
+
injector: PlatformServerlessTest.injector,
|
|
185
|
+
endpoint
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
PlatformServerlessTest.callbacks = {};
|
|
190
|
+
PlatformServerlessTest.request = LambdaClientRequest;
|
|
191
|
+
//# sourceMappingURL=PlatformServerlessTest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformServerlessTest.js","sourceRoot":"","sources":["../../../src/services/PlatformServerlessTest.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,MAAM,EAAC,MAAM,UAAU,CAAC;AAKhC,OAAO,EAAC,kBAAkB,EAAC,MAAM,+BAA+B,CAAC;AACjE,OAAO,EAAC,iBAAiB,EAAC,MAAM,6BAA6B,CAAC;AAI9D,MAAM,OAAO,mBAAoB,SAAQ,OAA8B;IAAvE;;QACE,UAAK,GAAG,mBAAmB,CAAC,eAAe,EAAE,CAAC;QAC9C,YAAO,GAAG,mBAAmB,CAAC,iBAAiB,EAAE,CAAC;IAmLpD,CAAC;IAjLC,MAAM,CAAC,IAAI,CAAC,UAAkB;QAC5B,MAAM,SAAS,GAAQ,EAAE,CAAC;QAC1B,MAAM,OAAO,GAAG,IAAI,mBAAmB,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC1D,SAAS,CAAC,OAAO,GAAG,OAAO,CAAC;YAC5B,SAAS,CAAC,MAAM,GAAG,MAAM,CAAC;QAC5B,CAAC,CAAC,CAAC;QAEH,OAAO,CAAC,IAAI,CAAC,UAAU,EAAE,SAAS,CAAC,OAAO,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;QAE9D,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,MAAM,CAAC,GAAG,CAAC,IAAY,EAAE,UAAsF,EAAE;QAC/G,OAAO,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACjD,CAAC;IAED,MAAM,CAAC,IAAI,CAAC,IAAY,EAAE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sonEntityStore} from \"@tsed/schema\";\nimport {APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEventBase, APIGatewayProxyHandler, Context} from \"aws-lambda\";\nimport {APIGatewayProxyResult} from \"aws-lambda/trigger/api-gateway-proxy\";\nimport {PlatformServerless} from \"../builder/PlatformServerless\";\nimport {ServerlessContext} from \"../domain/ServerlessContext\";\n\nexport interface LambdaPromiseResult extends Promise<APIGatewayProxyResult> {}\n\nexport class LambdaClientRequest extends Promise<APIGatewayProxyResult> {\n event = LambdaClientRequest.createFakeEvent();\n context = LambdaClientRequest.createFakeContext();\n\n static call(lambdaName: string) {\n const resolvers: any = {};\n const promise = new LambdaClientRequest((resolve, reject) => {\n resolvers.resolve = resolve;\n resolvers.reject = reject;\n });\n\n promise.init(lambdaName, resolvers.resolve, resolvers.reject);\n\n return promise;\n }\n\n static get(path: string, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {\n return this.call(\"handler\").get(path, options);\n }\n\n static post(path: string, body?: any, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {\n return this.call(\"handler\").post(path, body, options);\n }\n\n static put(path: string, body?: any, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {\n return this.call(\"handler\").put(path, body, options);\n }\n\n static patch(path: string, body?: any, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {\n return this.call(\"handler\").patch(path, body, options);\n }\n\n static delete(path: string, body?: any, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {\n return this.call(\"handler\").delete(path, body, options);\n }\n\n static createFakeEvent(event: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {\n return {\n body: \"\",\n headers: {},\n httpMethod: \"\",\n isBase64Encoded: false,\n multiValueHeaders: {},\n multiValueQueryStringParameters: {},\n path: event.path || \"/\",\n pathParameters: {},\n queryStringParameters: {},\n resource: \"\",\n stageVariables: {},\n ...event,\n requestContext: {\n ...event?.requestContext,\n accountId: \"\",\n apiId: \"\",\n protocol: \"https\",\n httpMethod: \"\",\n identity: {} as any,\n path: event.path || \"/\",\n stage: \"\",\n requestId: \"requestId\",\n requestTimeEpoch: 0,\n resourceId: 1,\n resourcePath: event.path || \"/\"\n } as any\n } as APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;\n }\n\n static createFakeContext(context?: Context): Context {\n return {\n awsRequestId: \"awsRequestId\",\n callbackWaitsForEmptyEventLoop: false,\n functionName: \"\",\n functionVersion: \"\",\n invokedFunctionArn: \"\",\n logGroupName: \"\",\n logStreamName: \"\",\n memoryLimitInMB: \"\",\n ...(context || {})\n } as Context;\n }\n\n get(path: string, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {\n Object.assign(this.event, options);\n\n this.event.path = path;\n this.event.httpMethod = \"GET\";\n\n return this;\n }\n\n post(path: string, body: any = {}, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {\n Object.assign(this.event, options);\n\n this.event.path = path;\n this.event.httpMethod = \"POST\";\n\n this.body(body);\n\n return this;\n }\n\n patch(path: string, body: any = {}, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {\n Object.assign(this.event, options);\n\n this.event.path = path;\n this.event.httpMethod = \"PATCH\";\n\n this.body(body);\n\n return this;\n }\n\n put(path: string, body: any = {}, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {\n Object.assign(this.event, options);\n\n this.event.path = path;\n this.event.httpMethod = \"PUT\";\n\n this.body(body);\n\n return this;\n }\n\n delete(path: string, body: any = {}, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {\n Object.assign(this.event, options);\n\n this.event.path = path;\n this.event.httpMethod = \"DELETE\";\n\n this.body(body);\n\n return this;\n }\n\n query(query: any) {\n this.event.queryStringParameters = {\n ...this.event.queryStringParameters,\n ...JSON.parse(JSON.stringify(query))\n };\n\n return this;\n }\n\n params(pathParameters: any) {\n this.event.pathParameters = {\n ...this.event.pathParameters,\n ...JSON.parse(JSON.stringify(pathParameters))\n };\n\n return this;\n }\n\n headers(headers: Record<string, any>) {\n this.event.headers = {\n ...this.event.headers,\n ...JSON.parse(JSON.stringify(headers))\n };\n\n return this;\n }\n\n body(body: any) {\n if (body !== undefined) {\n this.event.headers[\"content-type\"] = \"application/json\";\n\n this.event.body = JSON.stringify(body);\n }\n\n return this;\n }\n\n protected init(lambda: string, resolve: Function, reject: Function) {\n setTimeout(async () => {\n try {\n const result = await PlatformServerlessTest.callbacks[lambda](this.event, this.context, resolve as any);\n resolve(result as any);\n } catch (er) {\n reject(er);\n }\n });\n }\n}\n\nexport class PlatformServerlessTest extends DITest {\n static callbacks: Record<string, APIGatewayProxyHandler> = {};\n static instance: PlatformServerless;\n static request = LambdaClientRequest;\n\n static bootstrap(settings: Partial<TsED.Configuration & {lambda: Type[]}> = {}) {\n return async function before(): Promise<void> {\n // @ts-ignore\n const instance = PlatformServerless.bootstrap(DITest.configure(settings));\n PlatformServerlessTest.instance = instance;\n PlatformServerlessTest.callbacks = instance.callbacks();\n PlatformServerlessTest.callbacks.handler = instance.handler();\n // used by inject method\n DITest.injector = instance.injector;\n\n return instance.promise;\n };\n }\n\n /**\n * Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.\n */\n static async reset() {\n if (PlatformServerlessTest.instance) {\n await PlatformServerlessTest.instance.stop();\n }\n if (DITest.hasInjector()) {\n await DITest.injector.destroy();\n DITest._injector = null;\n }\n }\n\n static createServerlessContext({endpoint}: {endpoint: JsonEntityStore}) {\n const context: any = LambdaClientRequest.createFakeContext();\n const event: any = LambdaClientRequest.createFakeEvent();\n\n return new ServerlessContext({\n event,\n context,\n id: context.awsRequestId,\n logger: PlatformServerlessTest.injector.logger as Logger,\n injector: PlatformServerlessTest.injector,\n endpoint\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { v4 } from "uuid";
|
|
2
|
+
export function getRequestId(event, context) {
|
|
3
|
+
var _a;
|
|
4
|
+
if ((event === null || event === void 0 ? void 0 : event.headers) && event.headers["x-request-id"]) {
|
|
5
|
+
return event.headers["x-request-id"];
|
|
6
|
+
}
|
|
7
|
+
return ((_a = event === null || event === void 0 ? void 0 : event.requestContext) === null || _a === void 0 ? void 0 : _a.requestId) || (context === null || context === void 0 ? void 0 : context.awsRequestId) || v4().replace(/-/gi, "");
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=getRequestId.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getRequestId.js","sourceRoot":"","sources":["../../../src/utils/getRequestId.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,EAAE,EAAC,MAAM,MAAM,CAAC;AAExB,MAAM,UAAU,YAAY,CAAC,KAAwE,EAAE,OAAgB;;IACrH,IAAI,CAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,OAAO,KAAI,KAAK,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE;QACnD,OAAO,KAAK,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;KACtC;IAED,OAAO,CAAA,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,cAAc,0CAAE,SAAS,MAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY,CAAA,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;AAC9F,CAAC","sourcesContent":["import {APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEventBase, Context} from \"aws-lambda\";\nimport {v4} from \"uuid\";\n\nexport function getRequestId(event: APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>, context: Context) {\n if (event?.headers && event.headers[\"x-request-id\"]) {\n return event.headers[\"x-request-id\"];\n }\n\n return event?.requestContext?.requestId || context?.awsRequestId || v4().replace(/-/gi, \"\");\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function mergeHeaders(specHeaders, headers) {
|
|
2
|
+
return Object.entries(specHeaders).reduce((headers, [key, item]) => {
|
|
3
|
+
key = key.toLowerCase();
|
|
4
|
+
return {
|
|
5
|
+
...headers,
|
|
6
|
+
[key]: headers[key] === undefined ? String(item.example) : headers[key]
|
|
7
|
+
};
|
|
8
|
+
}, headers);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* @ignore
|
|
12
|
+
*/
|
|
13
|
+
export function setResponseHeaders(ctx) {
|
|
14
|
+
const { response, endpoint } = ctx;
|
|
15
|
+
const operation = endpoint.operation;
|
|
16
|
+
if (response.isDone()) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (!response.hasStatus()) {
|
|
20
|
+
// apply status only if the isn't already modified
|
|
21
|
+
response.status(operation.getStatus());
|
|
22
|
+
}
|
|
23
|
+
const statusCode = response.statusCode;
|
|
24
|
+
const headers = operation.getHeadersOf(response.statusCode);
|
|
25
|
+
const mergedHeaders = mergeHeaders(headers, response.getHeaders());
|
|
26
|
+
response.setHeaders(mergedHeaders);
|
|
27
|
+
if (operation.isRedirection(statusCode)) {
|
|
28
|
+
response.redirect(statusCode, String(mergedHeaders["location"]));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=setResponseHeaders.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setResponseHeaders.js","sourceRoot":"","sources":["../../../src/utils/setResponseHeaders.ts"],"names":[],"mappings":"AAIA,SAAS,YAAY,CAAC,WAA2D,EAAE,OAAoC;IACrH,OAAO,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,EAAE;QACjE,GAAG,GAAG,GAAG,CAAC,WAAW,EAAE,CAAC;QACxB,OAAO;YACL,GAAG,OAAO;YACV,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,GAAG,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC;SACxE,CAAC;IACJ,CAAC,EAAE,OAAO,CAAC,CAAC;AACd,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAAsB;IACvD,MAAM,EAAC,QAAQ,EAAE,QAAQ,EAAC,GAAG,GAAG,CAAC;IACjC,MAAM,SAAS,GAAG,QAAQ,CAAC,SAAU,CAAC;IAEtC,IAAI,QAAQ,CAAC,MAAM,EAAE,EAAE;QACrB,OAAO;KACR;IAED,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,EAAE;QACzB,kDAAkD;QAClD,QAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,EAAE,CAAC,CAAC;KACxC;IAED,MAAM,UAAU,GAAG,QAAQ,CAAC,UAAU,CAAC;IACvC,MAAM,OAAO,GAAG,SAAS,CAAC,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;IAC5D,MAAM,aAAa,GAAG,YAAY,CAAC,OAAO,EAAE,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC;IAEnE,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;IAEnC,IAAI,SAAS,CAAC,aAAa,CAAC,UAAU,CAAC,EAAE;QACvC,QAAQ,CAAC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;KAClE;AACH,CAAC","sourcesContent":["import {JsonHeader} from \"@tsed/schema\";\nimport {ServerlessContext} from \"../domain/ServerlessContext\";\nimport {HeaderValue} from \"../domain/ServerlessResponse\";\n\nfunction mergeHeaders(specHeaders: Record<string, JsonHeader & {example: string}>, headers: Record<string, HeaderValue>) {\n return Object.entries(specHeaders).reduce((headers, [key, item]) => {\n key = key.toLowerCase();\n return {\n ...headers,\n [key]: headers[key] === undefined ? String(item.example) : headers[key]\n };\n }, headers);\n}\n\n/**\n * @ignore\n */\nexport function setResponseHeaders(ctx: ServerlessContext) {\n const {response, endpoint} = ctx;\n const operation = endpoint.operation!;\n\n if (response.isDone()) {\n return;\n }\n\n if (!response.hasStatus()) {\n // apply status only if the isn't already modified\n response.status(operation.getStatus());\n }\n\n const statusCode = response.statusCode;\n const headers = operation.getHeadersOf(response.statusCode);\n const mergedHeaders = mergeHeaders(headers, response.getHeaders());\n\n response.setHeaders(mergedHeaders);\n\n if (operation.isRedirection(statusCode)) {\n response.redirect(statusCode, String(mergedHeaders[\"location\"]));\n }\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Type } from "@tsed/core";
|
|
2
|
+
import { InjectorService } from "@tsed/di";
|
|
3
|
+
import type { Instance } from "find-my-way";
|
|
4
|
+
import type { Handler } from "aws-lambda";
|
|
5
|
+
export interface PlatformServerlessSettings extends Partial<TsED.Configuration> {
|
|
6
|
+
lambda?: Type[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* @platform
|
|
10
|
+
*/
|
|
11
|
+
export declare class PlatformServerless {
|
|
12
|
+
readonly name: string;
|
|
13
|
+
private _injector;
|
|
14
|
+
private _router;
|
|
15
|
+
private _promise;
|
|
16
|
+
get injector(): InjectorService;
|
|
17
|
+
get settings(): TsED.Configuration & import("@tsed/di").DIConfiguration;
|
|
18
|
+
get promise(): Promise<any>;
|
|
19
|
+
static bootstrap(settings?: Partial<TsED.Configuration> & {
|
|
20
|
+
lambda?: Type[];
|
|
21
|
+
}): PlatformServerless;
|
|
22
|
+
handler(): Handler;
|
|
23
|
+
callbacks(tokens?: Type | Type[], callbacks?: any): Record<string, Handler>;
|
|
24
|
+
ready(): Promise<void>;
|
|
25
|
+
stop(): Promise<void>;
|
|
26
|
+
init(): Promise<any>;
|
|
27
|
+
protected callback(token: Type<any>, propertyKey: string): Handler;
|
|
28
|
+
protected initRouter(): Promise<Instance<any>>;
|
|
29
|
+
protected createInjector(settings: any): this;
|
|
30
|
+
protected loadInjector(): Promise<void>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { InjectorService, TokenProvider } from "@tsed/di";
|
|
2
|
+
import { PlatformParams } from "@tsed/platform-params";
|
|
3
|
+
import type { PlatformExceptions } from "@tsed/platform-exceptions";
|
|
4
|
+
import { ServerlessContext } from "../domain/ServerlessContext";
|
|
5
|
+
export declare class PlatformServerlessHandler {
|
|
6
|
+
protected injector: InjectorService;
|
|
7
|
+
protected params: PlatformParams;
|
|
8
|
+
protected exceptionsManager: Promise<PlatformExceptions>;
|
|
9
|
+
createHandler(token: TokenProvider, propertyKey: string | symbol): ($ctx: ServerlessContext) => Promise<{
|
|
10
|
+
statusCode: number;
|
|
11
|
+
body: any;
|
|
12
|
+
headers: {
|
|
13
|
+
"x-request-id": string;
|
|
14
|
+
};
|
|
15
|
+
isBase64Encoded: boolean;
|
|
16
|
+
}>;
|
|
17
|
+
private flush;
|
|
18
|
+
private processResult;
|
|
19
|
+
}
|