@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,19 @@
|
|
|
1
|
+
import { DIContext, DIContextOptions } from "@tsed/di";
|
|
2
|
+
import { JsonEntityStore } from "@tsed/schema";
|
|
3
|
+
import { APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEventBase, Context } from "aws-lambda";
|
|
4
|
+
import { ServerlessRequest } from "./ServerlessRequest";
|
|
5
|
+
import { ServerlessResponse } from "./ServerlessResponse";
|
|
6
|
+
export interface ServerlessContextOptions extends DIContextOptions {
|
|
7
|
+
event: APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;
|
|
8
|
+
context: Context;
|
|
9
|
+
endpoint: JsonEntityStore;
|
|
10
|
+
}
|
|
11
|
+
export declare class ServerlessContext extends DIContext {
|
|
12
|
+
readonly response: ServerlessResponse;
|
|
13
|
+
readonly request: ServerlessRequest;
|
|
14
|
+
readonly context: Context;
|
|
15
|
+
readonly event: APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;
|
|
16
|
+
readonly endpoint: JsonEntityStore;
|
|
17
|
+
constructor({ event, context, endpoint, ...options }: ServerlessContextOptions);
|
|
18
|
+
destroy(): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEventBase, Context } from "aws-lambda";
|
|
2
|
+
export interface ServerlessRequestOptions {
|
|
3
|
+
event: APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;
|
|
4
|
+
context: Context;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* @platform
|
|
8
|
+
*/
|
|
9
|
+
export declare class ServerlessRequest {
|
|
10
|
+
readonly raw: APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;
|
|
11
|
+
constructor({ event }: ServerlessRequestOptions);
|
|
12
|
+
get secure(): boolean;
|
|
13
|
+
get host(): string;
|
|
14
|
+
get protocol(): string;
|
|
15
|
+
/**
|
|
16
|
+
* Get the url of the request.
|
|
17
|
+
*
|
|
18
|
+
* Is equivalent of `express.response.originalUrl || express.response.url`.
|
|
19
|
+
*/
|
|
20
|
+
get url(): string;
|
|
21
|
+
get headers(): import("aws-lambda").APIGatewayProxyEventHeaders;
|
|
22
|
+
get method(): string;
|
|
23
|
+
/**
|
|
24
|
+
* Contains key-value pairs of data submitted in the request body. By default, it is `undefined`, and is populated when you use
|
|
25
|
+
* `body-parsing` middleware such as `express.json()` or `express.urlencoded()`.
|
|
26
|
+
*/
|
|
27
|
+
get body(): any;
|
|
28
|
+
get rawBody(): any;
|
|
29
|
+
/**
|
|
30
|
+
* This property is an object containing properties mapped to the named route `parameters`.
|
|
31
|
+
* For example, if you have the route `/user/:name`, then the `name` property is available as `req.params.name`.
|
|
32
|
+
* This object defaults to `{}`.
|
|
33
|
+
*/
|
|
34
|
+
get params(): {
|
|
35
|
+
[key: string]: any;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* This property is an object containing a property for each query string parameter in the route.
|
|
39
|
+
* When query parser is set to disabled, it is an empty object `{}`, otherwise it is the result of the configured query parser.
|
|
40
|
+
*/
|
|
41
|
+
get query(): {
|
|
42
|
+
[key: string]: any;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Returns the HTTP request header specified by field. The match is case-insensitive.
|
|
46
|
+
*
|
|
47
|
+
* ```typescript
|
|
48
|
+
* request.get('Content-Type') // => "text/plain"
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @param name
|
|
52
|
+
*/
|
|
53
|
+
get(name: string): any;
|
|
54
|
+
destroy(): void;
|
|
55
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEventBase, Context } from "aws-lambda";
|
|
2
|
+
import type { ServerlessRequest } from "./ServerlessRequest";
|
|
3
|
+
export interface ServerlessResponseOptions {
|
|
4
|
+
event: APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;
|
|
5
|
+
context: Context;
|
|
6
|
+
request: ServerlessRequest;
|
|
7
|
+
}
|
|
8
|
+
export declare type HeaderValue = boolean | number | string;
|
|
9
|
+
/**
|
|
10
|
+
* @platform
|
|
11
|
+
*/
|
|
12
|
+
export declare class ServerlessResponse {
|
|
13
|
+
#private;
|
|
14
|
+
readonly event: APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;
|
|
15
|
+
readonly request: ServerlessRequest;
|
|
16
|
+
constructor({ event, request }: ServerlessResponseOptions);
|
|
17
|
+
/**
|
|
18
|
+
* Get the current statusCode
|
|
19
|
+
*/
|
|
20
|
+
get statusCode(): number;
|
|
21
|
+
set statusCode(status: number);
|
|
22
|
+
/**
|
|
23
|
+
* 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.
|
|
24
|
+
*
|
|
25
|
+
* This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on.
|
|
26
|
+
*/
|
|
27
|
+
get locals(): Record<string, any>;
|
|
28
|
+
get isHeadersSent(): boolean;
|
|
29
|
+
getStatus(): number;
|
|
30
|
+
/**
|
|
31
|
+
* Sets the HTTP status for the response.
|
|
32
|
+
*
|
|
33
|
+
* @param status
|
|
34
|
+
*/
|
|
35
|
+
status(status: number): this;
|
|
36
|
+
set(name: string, value: any): this;
|
|
37
|
+
/**
|
|
38
|
+
* Returns the HTTP response header specified by field. The match is case-insensitive.
|
|
39
|
+
*
|
|
40
|
+
* ```typescript
|
|
41
|
+
* response.get('Content-Type') // => "text/plain"
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @param name
|
|
45
|
+
*/
|
|
46
|
+
get(name: string): string | number | boolean | undefined;
|
|
47
|
+
getHeaders(): Record<string, HeaderValue>;
|
|
48
|
+
hasStatus(): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Set header `field` to `val`, or pass
|
|
51
|
+
* an object of header fields.
|
|
52
|
+
*
|
|
53
|
+
* Examples:
|
|
54
|
+
* ```typescript
|
|
55
|
+
* response.setHeaders({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* Aliased as `res.header()`.
|
|
59
|
+
*/
|
|
60
|
+
setHeaders(headers: Record<string, HeaderValue>): this;
|
|
61
|
+
setHeader(key: string, item: HeaderValue | null | undefined): this;
|
|
62
|
+
/**
|
|
63
|
+
* Set `Content-Type` response header with `type` through `mime.lookup()`
|
|
64
|
+
* when it does not contain "/", or set the Content-Type to `type` otherwise.
|
|
65
|
+
*
|
|
66
|
+
* Examples:
|
|
67
|
+
*
|
|
68
|
+
* res.type('.html');
|
|
69
|
+
* res.type('html');
|
|
70
|
+
* res.type('json');
|
|
71
|
+
* res.type('application/json');
|
|
72
|
+
* res.type('png');
|
|
73
|
+
*/
|
|
74
|
+
contentType(contentType: string): this;
|
|
75
|
+
contentLength(length: number): this;
|
|
76
|
+
getContentLength(): number | undefined;
|
|
77
|
+
getContentType(): string;
|
|
78
|
+
/**
|
|
79
|
+
* 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).
|
|
80
|
+
* If not specified, status defaults to `302 Found`.
|
|
81
|
+
*
|
|
82
|
+
* @param status
|
|
83
|
+
* @param url
|
|
84
|
+
*/
|
|
85
|
+
redirect(status: number, url: string): this;
|
|
86
|
+
/**
|
|
87
|
+
* Sets the response Location HTTP header to the specified path parameter.
|
|
88
|
+
*
|
|
89
|
+
* @param url
|
|
90
|
+
*/
|
|
91
|
+
location(url: string): this;
|
|
92
|
+
/**
|
|
93
|
+
* Send any data to your consumer.
|
|
94
|
+
*
|
|
95
|
+
* This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data.
|
|
96
|
+
* It choose the better way to send the data.
|
|
97
|
+
*
|
|
98
|
+
* @param body
|
|
99
|
+
*/
|
|
100
|
+
body(body: any): this;
|
|
101
|
+
getBody(): any;
|
|
102
|
+
isDone(): boolean;
|
|
103
|
+
onEnd(cb: Function): this;
|
|
104
|
+
destroy(): void;
|
|
105
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
export * from "./domain/ServerlessContext";
|
|
8
|
+
export * from "./domain/ServerlessResponse";
|
|
9
|
+
export * from "./domain/ServerlessResponse";
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Type } from "@tsed/core";
|
|
2
|
+
import { DITest } from "@tsed/di";
|
|
3
|
+
import { JsonEntityStore } from "@tsed/schema";
|
|
4
|
+
import { APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEventBase, APIGatewayProxyHandler, Context } from "aws-lambda";
|
|
5
|
+
import { APIGatewayProxyResult } from "aws-lambda/trigger/api-gateway-proxy";
|
|
6
|
+
import { PlatformServerless } from "../builder/PlatformServerless";
|
|
7
|
+
import { ServerlessContext } from "../domain/ServerlessContext";
|
|
8
|
+
export interface LambdaPromiseResult extends Promise<APIGatewayProxyResult> {
|
|
9
|
+
}
|
|
10
|
+
export declare class LambdaClientRequest extends Promise<APIGatewayProxyResult> {
|
|
11
|
+
event: APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;
|
|
12
|
+
context: Context;
|
|
13
|
+
static call(lambdaName: string): LambdaClientRequest;
|
|
14
|
+
static get(path: string, options?: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>>): LambdaClientRequest;
|
|
15
|
+
static post(path: string, body?: any, options?: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>>): LambdaClientRequest;
|
|
16
|
+
static put(path: string, body?: any, options?: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>>): LambdaClientRequest;
|
|
17
|
+
static patch(path: string, body?: any, options?: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>>): LambdaClientRequest;
|
|
18
|
+
static delete(path: string, body?: any, options?: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>>): LambdaClientRequest;
|
|
19
|
+
static createFakeEvent(event?: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>>): APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;
|
|
20
|
+
static createFakeContext(context?: Context): Context;
|
|
21
|
+
get(path: string, options?: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>>): this;
|
|
22
|
+
post(path: string, body?: any, options?: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>>): this;
|
|
23
|
+
patch(path: string, body?: any, options?: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>>): this;
|
|
24
|
+
put(path: string, body?: any, options?: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>>): this;
|
|
25
|
+
delete(path: string, body?: any, options?: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>>): this;
|
|
26
|
+
query(query: any): this;
|
|
27
|
+
params(pathParameters: any): this;
|
|
28
|
+
headers(headers: Record<string, any>): this;
|
|
29
|
+
body(body: any): this;
|
|
30
|
+
protected init(lambda: string, resolve: Function, reject: Function): void;
|
|
31
|
+
}
|
|
32
|
+
export declare class PlatformServerlessTest extends DITest {
|
|
33
|
+
static callbacks: Record<string, APIGatewayProxyHandler>;
|
|
34
|
+
static instance: PlatformServerless;
|
|
35
|
+
static request: typeof LambdaClientRequest;
|
|
36
|
+
static bootstrap(settings?: Partial<TsED.Configuration & {
|
|
37
|
+
lambda: Type[];
|
|
38
|
+
}>): () => Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.
|
|
41
|
+
*/
|
|
42
|
+
static reset(): Promise<void>;
|
|
43
|
+
static createServerlessContext({ endpoint }: {
|
|
44
|
+
endpoint: JsonEntityStore;
|
|
45
|
+
}): ServerlessContext;
|
|
46
|
+
}
|
package/package.json
CHANGED
|
@@ -1,28 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/platform-serverless",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.90.0",
|
|
4
4
|
"description": "Platform serverless module for Ts.ED Framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
|
-
"main": "./lib/index.js",
|
|
8
|
-
"typings": "./lib/index.d.ts",
|
|
7
|
+
"main": "./lib/cjs/index.js",
|
|
8
|
+
"typings": "./lib/types/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
|
-
"
|
|
11
|
-
"
|
|
10
|
+
"types": "./lib/types/index.d.ts",
|
|
11
|
+
"import": "./lib/esm/index.js",
|
|
12
|
+
"require": "./lib/cjs/index.js",
|
|
13
|
+
"default": "./lib/esm/index.js"
|
|
12
14
|
},
|
|
13
15
|
"scripts": {
|
|
14
|
-
"build": "
|
|
16
|
+
"build": "yarn run build:esm && yarn run build:cjs",
|
|
17
|
+
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
18
|
+
"build:esm": "tsc --build tsconfig.compile.esm.json",
|
|
15
19
|
"test": "cross-env NODE_ENV=test jest"
|
|
16
20
|
},
|
|
17
21
|
"dependencies": {
|
|
18
|
-
"@tsed/core": "6.
|
|
19
|
-
"@tsed/di": "6.
|
|
20
|
-
"@tsed/exceptions": "6.
|
|
21
|
-
"@tsed/json-mapper": "6.
|
|
22
|
+
"@tsed/core": "6.90.0",
|
|
23
|
+
"@tsed/di": "6.90.0",
|
|
24
|
+
"@tsed/exceptions": "6.90.0",
|
|
25
|
+
"@tsed/json-mapper": "6.90.0",
|
|
22
26
|
"@tsed/logger": "^6.0.0",
|
|
23
|
-
"@tsed/platform-exceptions": "6.
|
|
24
|
-
"@tsed/platform-params": "6.
|
|
25
|
-
"@tsed/schema": "6.
|
|
27
|
+
"@tsed/platform-exceptions": "6.90.0",
|
|
28
|
+
"@tsed/platform-params": "6.90.0",
|
|
29
|
+
"@tsed/schema": "6.90.0",
|
|
26
30
|
"encodeurl": "^1.0.2",
|
|
27
31
|
"mime": "^2.5.2",
|
|
28
32
|
"tslib": "2.2.0",
|
package/readme.md
CHANGED
|
@@ -117,6 +117,46 @@ functions:
|
|
|
117
117
|
method: get
|
|
118
118
|
```
|
|
119
119
|
|
|
120
|
+
## Invoke a lambda with serverless
|
|
121
|
+
|
|
122
|
+
Serverless provide a plugin named `serverless-offline`. This Serverless plugin emulates AWS λ and API Gateway on your local machine to speed up your development cycles.
|
|
123
|
+
To do so, it starts an HTTP server that handles the request's lifecycle like APIG does and invokes your handlers.
|
|
124
|
+
|
|
125
|
+
So, by using the `serverless offline` command, we'll be able to invoke our function. For that, we need also to build our code before invoke the lambda.
|
|
126
|
+
|
|
127
|
+
To simplify our workflow, we can add the following npm script command in our `package.json`:
|
|
128
|
+
|
|
129
|
+
```json
|
|
130
|
+
{
|
|
131
|
+
"scripts": {
|
|
132
|
+
"invoke:timeslots": "yarn build && serverless invoke local -f timeslots",
|
|
133
|
+
"invoke:any": "yarn serverless invoke local -f any --data '{\"path\":\"/timeslots\", \"httpMethod\": \"GET\"}'"
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Now, we can run the following command to invoke our lambda:
|
|
139
|
+
|
|
140
|
+
```
|
|
141
|
+
yarn invoke:timeslots
|
|
142
|
+
// OR
|
|
143
|
+
npm run invoke:timeslots
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
You should see in the terminal the following result:
|
|
147
|
+
|
|
148
|
+
```json
|
|
149
|
+
{
|
|
150
|
+
"statusCode": 200,
|
|
151
|
+
"body": "[{\"id\":\"b6de4fc7-faaa-4cd7-a144-42f6af0dec6b\",\"title\":\"title\",\"description\":\"description\",\"start_date\":\"2021-10-29T10:40:57.019Z\",\"end_date\":\"2021-10-29T10:40:57.019Z\",\"created_at\":\"2021-10-29T10:40:57.019Z\",\"update_at\":\"2021-10-29T10:40:57.019Z\"}]",
|
|
152
|
+
"headers": {
|
|
153
|
+
"content-type": "application/json",
|
|
154
|
+
"x-request-id": "ebb52d5e-113b-40da-b34e-c14811df596b"
|
|
155
|
+
},
|
|
156
|
+
"isBase64Encoded": false
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
120
160
|
## Manage routes from code
|
|
121
161
|
|
|
122
162
|
Declaring all routes in the `serverless.yml` file can be a source of error. `@tsed/platform-serverless` can
|
|
@@ -149,47 +189,31 @@ functions:
|
|
|
149
189
|
path: '{proxy+}'
|
|
150
190
|
```
|
|
151
191
|
|
|
152
|
-
|
|
192
|
+
Then, edit the `handler.ts` and change the exported functions:
|
|
153
193
|
|
|
154
|
-
|
|
194
|
+
```typescript
|
|
195
|
+
import {PlatformServerless} from "@tsed/platform-serverless";
|
|
196
|
+
import {TimeslotsLambda} from "./TimeslotsLambda";
|
|
155
197
|
|
|
156
|
-
|
|
157
|
-
|
|
198
|
+
const platform = PlatformServerless.bootstrap({
|
|
199
|
+
lambda: [TimeslotsLambda]
|
|
200
|
+
});
|
|
158
201
|
|
|
159
|
-
|
|
202
|
+
export const handler = platform.handler();
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Now, Ts.ED will handle request and call the expected lambda in your controllers.
|
|
160
206
|
|
|
161
207
|
To simplify our workflow, we can add the following npm script command in our `package.json`:
|
|
162
208
|
|
|
163
209
|
```json
|
|
164
210
|
{
|
|
165
211
|
"scripts": {
|
|
166
|
-
"invoke:
|
|
212
|
+
"invoke:any": "yarn serverless invoke local -f any --data '{\"path\":\"/timeslots\", \"httpMethod\": \"GET\"}'"
|
|
167
213
|
}
|
|
168
214
|
}
|
|
169
215
|
```
|
|
170
216
|
|
|
171
|
-
Now, we can run the following command to invoke our lambda:
|
|
172
|
-
|
|
173
|
-
```
|
|
174
|
-
yarn invoke:timeslots
|
|
175
|
-
// OR
|
|
176
|
-
npm run invoke:timeslots
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
You should see in the terminal the following result:
|
|
180
|
-
|
|
181
|
-
```json
|
|
182
|
-
{
|
|
183
|
-
"statusCode": 200,
|
|
184
|
-
"body": "[{\"id\":\"b6de4fc7-faaa-4cd7-a144-42f6af0dec6b\",\"title\":\"title\",\"description\":\"description\",\"start_date\":\"2021-10-29T10:40:57.019Z\",\"end_date\":\"2021-10-29T10:40:57.019Z\",\"created_at\":\"2021-10-29T10:40:57.019Z\",\"update_at\":\"2021-10-29T10:40:57.019Z\"}]",
|
|
185
|
-
"headers": {
|
|
186
|
-
"content-type": "application/json",
|
|
187
|
-
"x-request-id": "ebb52d5e-113b-40da-b34e-c14811df596b"
|
|
188
|
-
},
|
|
189
|
-
"isBase64Encoded": false
|
|
190
|
-
}
|
|
191
|
-
```
|
|
192
|
-
|
|
193
217
|
## Get AwsContext and AwsEvent
|
|
194
218
|
|
|
195
219
|
```typescript
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.compile.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": ".",
|
|
5
|
+
"module": "ES2020",
|
|
6
|
+
"rootDir": "src",
|
|
7
|
+
"outDir": "./lib/esm",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"declarationDir": "./lib/types"
|
|
10
|
+
},
|
|
11
|
+
"exclude": [
|
|
12
|
+
"node_modules",
|
|
13
|
+
"test",
|
|
14
|
+
"lib",
|
|
15
|
+
"benchmark",
|
|
16
|
+
"coverage",
|
|
17
|
+
"spec",
|
|
18
|
+
"**/*.benchmark.ts",
|
|
19
|
+
"**/*.spec.ts",
|
|
20
|
+
"keys",
|
|
21
|
+
"jest.config.js"
|
|
22
|
+
]
|
|
23
|
+
}
|