@tsed/platform-serverless 6.86.2 → 6.88.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/package.json +8 -8
- package/readme.md +52 -28
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/platform-serverless",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.88.0",
|
|
4
4
|
"description": "Platform serverless module for Ts.ED Framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -15,14 +15,14 @@
|
|
|
15
15
|
"test": "cross-env NODE_ENV=test jest"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@tsed/core": "6.
|
|
19
|
-
"@tsed/di": "6.
|
|
20
|
-
"@tsed/exceptions": "6.
|
|
21
|
-
"@tsed/json-mapper": "6.
|
|
18
|
+
"@tsed/core": "6.88.0",
|
|
19
|
+
"@tsed/di": "6.88.0",
|
|
20
|
+
"@tsed/exceptions": "6.88.0",
|
|
21
|
+
"@tsed/json-mapper": "6.88.0",
|
|
22
22
|
"@tsed/logger": "^6.0.0",
|
|
23
|
-
"@tsed/platform-exceptions": "6.
|
|
24
|
-
"@tsed/platform-params": "6.
|
|
25
|
-
"@tsed/schema": "6.
|
|
23
|
+
"@tsed/platform-exceptions": "6.88.0",
|
|
24
|
+
"@tsed/platform-params": "6.88.0",
|
|
25
|
+
"@tsed/schema": "6.88.0",
|
|
26
26
|
"encodeurl": "^1.0.2",
|
|
27
27
|
"mime": "^2.5.2",
|
|
28
28
|
"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
|