@tsed/platform-serverless-http 6.91.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/jest.config.js +14 -0
- package/lib/cjs/builder/PlatformServerlessHttp.d.ts +8 -0
- package/lib/cjs/builder/PlatformServerlessHttp.js +30 -0
- package/lib/cjs/builder/PlatformServerlessHttp.js.map +1 -0
- package/lib/cjs/constants/index.d.ts +1 -0
- package/lib/cjs/constants/index.js +5 -0
- package/lib/cjs/constants/index.js.map +1 -0
- package/lib/cjs/decorators/serverlessContext.d.ts +7 -0
- package/lib/cjs/decorators/serverlessContext.js +13 -0
- package/lib/cjs/decorators/serverlessContext.js.map +1 -0
- package/lib/cjs/decorators/serverlessEvent.d.ts +5 -0
- package/lib/cjs/decorators/serverlessEvent.js +13 -0
- package/lib/cjs/decorators/serverlessEvent.js.map +1 -0
- package/lib/cjs/index.d.ts +3 -0
- package/lib/cjs/index.js +7 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/esm/builder/PlatformServerlessHttp.js +25 -0
- package/lib/esm/builder/PlatformServerlessHttp.js.map +1 -0
- package/lib/esm/constants/index.js +2 -0
- package/lib/esm/constants/index.js.map +1 -0
- package/lib/esm/decorators/serverlessContext.js +9 -0
- package/lib/esm/decorators/serverlessContext.js.map +1 -0
- package/lib/esm/decorators/serverlessEvent.js +9 -0
- package/lib/esm/decorators/serverlessEvent.js.map +1 -0
- package/lib/esm/index.js +4 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/types/builder/PlatformServerlessHttp.d.ts +8 -0
- package/lib/types/constants/index.d.ts +1 -0
- package/lib/types/decorators/serverlessContext.d.ts +7 -0
- package/lib/types/decorators/serverlessEvent.d.ts +5 -0
- package/lib/types/index.d.ts +3 -0
- package/package.json +75 -0
- package/readme.md +331 -0
- package/tsconfig.compile.esm.json +23 -0
package/jest.config.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// For a detailed explanation regarding each configuration property, visit:
|
|
2
|
+
// https://jestjs.io/docs/en/configuration.html
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
...require("@tsed/jest-config")(__dirname, "platform-serverless-http"),
|
|
6
|
+
coverageThreshold: {
|
|
7
|
+
global: {
|
|
8
|
+
branches: 100,
|
|
9
|
+
functions: 100,
|
|
10
|
+
lines: 100,
|
|
11
|
+
statements: 100
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { PlatformBuilder, PlatformBuilderSettings } from "@tsed/common";
|
|
2
|
+
import { Type } from "@tsed/core";
|
|
3
|
+
import type { Handler } from "aws-lambda";
|
|
4
|
+
export declare class PlatformServerlessHttp {
|
|
5
|
+
static bootstrap(module: Type<any>, settings: PlatformBuilderSettings): PlatformBuilder & {
|
|
6
|
+
handler(): Handler;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PlatformServerlessHttp = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@tsed/common");
|
|
6
|
+
const serverless_http_1 = (0, tslib_1.__importDefault)(require("serverless-http"));
|
|
7
|
+
class PlatformServerlessHttp {
|
|
8
|
+
static bootstrap(module, settings) {
|
|
9
|
+
const platform = common_1.PlatformBuilder.create(module, settings);
|
|
10
|
+
const promise = platform.listen();
|
|
11
|
+
platform.handler = () => {
|
|
12
|
+
let handler;
|
|
13
|
+
return async (event, context) => {
|
|
14
|
+
// istanbul ignore else
|
|
15
|
+
if (!handler) {
|
|
16
|
+
await promise;
|
|
17
|
+
handler = (0, serverless_http_1.default)(platform.callback(), {
|
|
18
|
+
request(request) {
|
|
19
|
+
request.serverless = { event, context };
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return handler(event, context);
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
return platform;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.PlatformServerlessHttp = PlatformServerlessHttp;
|
|
30
|
+
//# sourceMappingURL=PlatformServerlessHttp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformServerlessHttp.js","sourceRoot":"","sources":["../../../src/builder/PlatformServerlessHttp.ts"],"names":[],"mappings":";;;;AAAA,yCAAsE;AAGtE,mFAAyC;AAEzC,MAAa,sBAAsB;IACjC,MAAM,CAAC,SAAS,CAAC,MAAiB,EAAE,QAAiC;QACnE,MAAM,QAAQ,GAAG,wBAAe,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QAC1D,MAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC;QAEjC,QAAgB,CAAC,OAAO,GAAG,GAAY,EAAE;YACxC,IAAI,OAAY,CAAC;YAEjB,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;gBAC9B,uBAAuB;gBACvB,IAAI,CAAC,OAAO,EAAE;oBACZ,MAAM,OAAO,CAAC;oBACd,OAAO,GAAG,IAAA,yBAAU,EAAC,QAAQ,CAAC,QAAQ,EAAE,EAAE;wBACxC,OAAO,CAAC,OAAY;4BAClB,OAAO,CAAC,UAAU,GAAG,EAAC,KAAK,EAAE,OAAO,EAAC,CAAC;wBACxC,CAAC;qBACF,CAAC,CAAC;iBACJ;gBAED,OAAO,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YACjC,CAAC,CAAC;QACJ,CAAC,CAAC;QAEF,OAAO,QAAe,CAAC;IACzB,CAAC;CACF;AAzBD,wDAyBC","sourcesContent":["import {PlatformBuilder, PlatformBuilderSettings} from \"@tsed/common\";\nimport {Type} from \"@tsed/core\";\nimport type {Handler} from \"aws-lambda\";\nimport serverless from \"serverless-http\";\n\nexport class PlatformServerlessHttp {\n static bootstrap(module: Type<any>, settings: PlatformBuilderSettings): PlatformBuilder & {handler(): Handler} {\n const platform = PlatformBuilder.create(module, settings);\n const promise = platform.listen();\n\n (platform as any).handler = (): Handler => {\n let handler: any;\n\n return async (event, context) => {\n // istanbul ignore else\n if (!handler) {\n await promise;\n handler = serverless(platform.callback(), {\n request(request: any) {\n request.serverless = {event, context};\n }\n });\n }\n\n return handler(event, context);\n };\n };\n\n return platform as any;\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const AWS_API_GATEWAY = "AWS_API_GATEWAY";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/constants/index.ts"],"names":[],"mappings":";;;AAAa,QAAA,eAAe,GAAG,iBAAiB,CAAC","sourcesContent":["export const AWS_API_GATEWAY = \"AWS_API_GATEWAY\";\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServerlessContext = void 0;
|
|
4
|
+
const common_1 = require("@tsed/common");
|
|
5
|
+
/**
|
|
6
|
+
* Return the Serverless context
|
|
7
|
+
* @decorator
|
|
8
|
+
*/
|
|
9
|
+
function ServerlessContext() {
|
|
10
|
+
return (0, common_1.Req)("serverless.context");
|
|
11
|
+
}
|
|
12
|
+
exports.ServerlessContext = ServerlessContext;
|
|
13
|
+
//# sourceMappingURL=serverlessContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serverlessContext.js","sourceRoot":"","sources":["../../../src/decorators/serverlessContext.ts"],"names":[],"mappings":";;;AAAA,yCAAiC;AAGjC;;;GAGG;AACH,SAAgB,iBAAiB;IAC/B,OAAO,IAAA,YAAG,EAAC,oBAAoB,CAAC,CAAC;AACnC,CAAC;AAFD,8CAEC","sourcesContent":["import {Req} from \"@tsed/common\";\nimport {Context} from \"aws-lambda\";\n\n/**\n * Return the Serverless context\n * @decorator\n */\nexport function ServerlessContext(): ParameterDecorator {\n return Req(\"serverless.context\");\n}\n\nexport type ServerlessContext = Context;\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServerlessEvent = void 0;
|
|
4
|
+
const common_1 = require("@tsed/common");
|
|
5
|
+
/**
|
|
6
|
+
* Return the serverless.event
|
|
7
|
+
* @decorator
|
|
8
|
+
*/
|
|
9
|
+
function ServerlessEvent() {
|
|
10
|
+
return (0, common_1.Req)("serverless.event");
|
|
11
|
+
}
|
|
12
|
+
exports.ServerlessEvent = ServerlessEvent;
|
|
13
|
+
//# sourceMappingURL=serverlessEvent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serverlessEvent.js","sourceRoot":"","sources":["../../../src/decorators/serverlessEvent.ts"],"names":[],"mappings":";;;AAAA,yCAAiC;AAEjC;;;GAGG;AACH,SAAgB,eAAe;IAC7B,OAAO,IAAA,YAAG,EAAC,kBAAkB,CAAC,CAAC;AACjC,CAAC;AAFD,0CAEC","sourcesContent":["import {Req} from \"@tsed/common\";\n\n/**\n * Return the serverless.event\n * @decorator\n */\nexport function ServerlessEvent(): ParameterDecorator {\n return Req(\"serverless.event\");\n}\n"]}
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
(0, tslib_1.__exportStar)(require("./builder/PlatformServerlessHttp"), exports);
|
|
5
|
+
(0, tslib_1.__exportStar)(require("./decorators/serverlessContext"), exports);
|
|
6
|
+
(0, tslib_1.__exportStar)(require("./decorators/serverlessEvent"), exports);
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,gFAAiD;AACjD,8EAA+C;AAC/C,4EAA6C","sourcesContent":["export * from \"./builder/PlatformServerlessHttp\";\nexport * from \"./decorators/serverlessContext\";\nexport * from \"./decorators/serverlessEvent\";\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { PlatformBuilder } from "@tsed/common";
|
|
2
|
+
import serverless from "serverless-http";
|
|
3
|
+
export class PlatformServerlessHttp {
|
|
4
|
+
static bootstrap(module, settings) {
|
|
5
|
+
const platform = PlatformBuilder.create(module, settings);
|
|
6
|
+
const promise = platform.listen();
|
|
7
|
+
platform.handler = () => {
|
|
8
|
+
let handler;
|
|
9
|
+
return async (event, context) => {
|
|
10
|
+
// istanbul ignore else
|
|
11
|
+
if (!handler) {
|
|
12
|
+
await promise;
|
|
13
|
+
handler = serverless(platform.callback(), {
|
|
14
|
+
request(request) {
|
|
15
|
+
request.serverless = { event, context };
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return handler(event, context);
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
return platform;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=PlatformServerlessHttp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformServerlessHttp.js","sourceRoot":"","sources":["../../../src/builder/PlatformServerlessHttp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAA0B,MAAM,cAAc,CAAC;AAGtE,OAAO,UAAU,MAAM,iBAAiB,CAAC;AAEzC,MAAM,OAAO,sBAAsB;IACjC,MAAM,CAAC,SAAS,CAAC,MAAiB,EAAE,QAAiC;QACnE,MAAM,QAAQ,GAAG,eAAe,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QAC1D,MAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC;QAEjC,QAAgB,CAAC,OAAO,GAAG,GAAY,EAAE;YACxC,IAAI,OAAY,CAAC;YAEjB,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;gBAC9B,uBAAuB;gBACvB,IAAI,CAAC,OAAO,EAAE;oBACZ,MAAM,OAAO,CAAC;oBACd,OAAO,GAAG,UAAU,CAAC,QAAQ,CAAC,QAAQ,EAAE,EAAE;wBACxC,OAAO,CAAC,OAAY;4BAClB,OAAO,CAAC,UAAU,GAAG,EAAC,KAAK,EAAE,OAAO,EAAC,CAAC;wBACxC,CAAC;qBACF,CAAC,CAAC;iBACJ;gBAED,OAAO,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YACjC,CAAC,CAAC;QACJ,CAAC,CAAC;QAEF,OAAO,QAAe,CAAC;IACzB,CAAC;CACF","sourcesContent":["import {PlatformBuilder, PlatformBuilderSettings} from \"@tsed/common\";\nimport {Type} from \"@tsed/core\";\nimport type {Handler} from \"aws-lambda\";\nimport serverless from \"serverless-http\";\n\nexport class PlatformServerlessHttp {\n static bootstrap(module: Type<any>, settings: PlatformBuilderSettings): PlatformBuilder & {handler(): Handler} {\n const platform = PlatformBuilder.create(module, settings);\n const promise = platform.listen();\n\n (platform as any).handler = (): Handler => {\n let handler: any;\n\n return async (event, context) => {\n // istanbul ignore else\n if (!handler) {\n await promise;\n handler = serverless(platform.callback(), {\n request(request: any) {\n request.serverless = {event, context};\n }\n });\n }\n\n return handler(event, context);\n };\n };\n\n return platform as any;\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/constants/index.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,eAAe,GAAG,iBAAiB,CAAC","sourcesContent":["export const AWS_API_GATEWAY = \"AWS_API_GATEWAY\";\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serverlessContext.js","sourceRoot":"","sources":["../../../src/decorators/serverlessContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAC,MAAM,cAAc,CAAC;AAGjC;;;GAGG;AACH,MAAM,UAAU,iBAAiB;IAC/B,OAAO,GAAG,CAAC,oBAAoB,CAAC,CAAC;AACnC,CAAC","sourcesContent":["import {Req} from \"@tsed/common\";\nimport {Context} from \"aws-lambda\";\n\n/**\n * Return the Serverless context\n * @decorator\n */\nexport function ServerlessContext(): ParameterDecorator {\n return Req(\"serverless.context\");\n}\n\nexport type ServerlessContext = Context;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serverlessEvent.js","sourceRoot":"","sources":["../../../src/decorators/serverlessEvent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAC,MAAM,cAAc,CAAC;AAEjC;;;GAGG;AACH,MAAM,UAAU,eAAe;IAC7B,OAAO,GAAG,CAAC,kBAAkB,CAAC,CAAC;AACjC,CAAC","sourcesContent":["import {Req} from \"@tsed/common\";\n\n/**\n * Return the serverless.event\n * @decorator\n */\nexport function ServerlessEvent(): ParameterDecorator {\n return Req(\"serverless.event\");\n}\n"]}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kCAAkC,CAAC;AACjD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,8BAA8B,CAAC","sourcesContent":["export * from \"./builder/PlatformServerlessHttp\";\nexport * from \"./decorators/serverlessContext\";\nexport * from \"./decorators/serverlessEvent\";\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { PlatformBuilder, PlatformBuilderSettings } from "@tsed/common";
|
|
2
|
+
import { Type } from "@tsed/core";
|
|
3
|
+
import type { Handler } from "aws-lambda";
|
|
4
|
+
export declare class PlatformServerlessHttp {
|
|
5
|
+
static bootstrap(module: Type<any>, settings: PlatformBuilderSettings): PlatformBuilder & {
|
|
6
|
+
handler(): Handler;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const AWS_API_GATEWAY = "AWS_API_GATEWAY";
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsed/platform-serverless-http",
|
|
3
|
+
"version": "6.91.0",
|
|
4
|
+
"description": "Module to support Serverless HTTP function with Ts.ED",
|
|
5
|
+
"source": "./src/index.ts",
|
|
6
|
+
"main": "./lib/cjs/index.js",
|
|
7
|
+
"typings": "./lib/types/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
"types": "./lib/types/index.d.ts",
|
|
10
|
+
"import": "./lib/esm/index.js",
|
|
11
|
+
"require": "./lib/cjs/index.js",
|
|
12
|
+
"default": "./lib/esm/index.js"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "yarn run build:esm && yarn run build:cjs",
|
|
16
|
+
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
17
|
+
"build:esm": "tsc --build tsconfig.compile.esm.json",
|
|
18
|
+
"test": "cross-env NODE_ENV=test jest"
|
|
19
|
+
},
|
|
20
|
+
"private": false,
|
|
21
|
+
"keywords": [
|
|
22
|
+
"aws",
|
|
23
|
+
"lambda",
|
|
24
|
+
"functions",
|
|
25
|
+
"TypeScript",
|
|
26
|
+
"typescript",
|
|
27
|
+
"Decorator",
|
|
28
|
+
"decorators",
|
|
29
|
+
"decorator",
|
|
30
|
+
"express",
|
|
31
|
+
"koa",
|
|
32
|
+
"Controller",
|
|
33
|
+
"Inject",
|
|
34
|
+
"ioc",
|
|
35
|
+
"di",
|
|
36
|
+
"mvc",
|
|
37
|
+
"swagger",
|
|
38
|
+
"swagger ui",
|
|
39
|
+
"ES2015",
|
|
40
|
+
"ES6",
|
|
41
|
+
"server",
|
|
42
|
+
"rest",
|
|
43
|
+
"api",
|
|
44
|
+
"validation"
|
|
45
|
+
],
|
|
46
|
+
"author": "Romain Lenzotti",
|
|
47
|
+
"license": "MIT",
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/tsedio/tsed/issues"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/tsedio/tsed/tree/production/packages/platform/platform-serverless-http",
|
|
52
|
+
"repository": "https://github.com/tsedio/tsed",
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"tslib": "2.2.0"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@tsed/common": "6.91.0",
|
|
58
|
+
"@tsed/core": "6.91.0",
|
|
59
|
+
"@tsed/di": "6.91.0",
|
|
60
|
+
"@tsed/platform-serverless-testing": "6.91.0",
|
|
61
|
+
"aws-lambda": "^1.0.7",
|
|
62
|
+
"serverless-http": "^2.7.0"
|
|
63
|
+
},
|
|
64
|
+
"peerDependencies": {
|
|
65
|
+
"@tsed/common": "^6.91.0",
|
|
66
|
+
"@tsed/core": "^6.91.0",
|
|
67
|
+
"@tsed/di": "^6.91.0",
|
|
68
|
+
"@tsed/json-mapper": "^6.91.0",
|
|
69
|
+
"@tsed/logger": ">=6.0.0",
|
|
70
|
+
"@tsed/openspec": "^6.91.0",
|
|
71
|
+
"@tsed/schema": "^6.91.0",
|
|
72
|
+
"aws-lambda": ">=1.0.7",
|
|
73
|
+
"serverless-http": ">=2.0.0"
|
|
74
|
+
}
|
|
75
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
<p style="text-align: center" align="center">
|
|
2
|
+
<a href="https://tsed.io" target="_blank"><img src="https://tsed.io/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
<h1>Platform Serverless Http</h1>
|
|
7
|
+
|
|
8
|
+
[](https://github.com/tsedio/tsed/actions?query=workflow%3A%22Build+%26+Release%22)
|
|
9
|
+
[](https://github.com/tsedio/tsed/blob/master/CONTRIBUTING.md)
|
|
10
|
+
[](https://coveralls.io/github/tsedio/tsed?branch=production)
|
|
11
|
+
[](https://badge.fury.io/js/%40tsed%2Fcommon)
|
|
12
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
13
|
+
[](https://github.com/prettier/prettier)
|
|
14
|
+
[](https://opencollective.com/tsed)
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
<div align="center">
|
|
19
|
+
<a href="https://tsed.io/">Website</a>
|
|
20
|
+
<span> • </span>
|
|
21
|
+
<a href="https://tsed.io/getting-started/">Getting started</a>
|
|
22
|
+
<span> • </span>
|
|
23
|
+
<a href="https://api.tsed.io/rest/slack/tsedio/tsed">Slack</a>
|
|
24
|
+
<span> • </span>
|
|
25
|
+
<a href="https://twitter.com/TsED_io">Twitter</a>
|
|
26
|
+
</div>
|
|
27
|
+
|
|
28
|
+
<hr />
|
|
29
|
+
|
|
30
|
+
A package of Ts.ED framework. See website: https://tsed.io/tutorials/aws.html
|
|
31
|
+
|
|
32
|
+
## Features
|
|
33
|
+
|
|
34
|
+
This package allows the creation of a Lambda AWS with an Express.js/Koa.js server.
|
|
35
|
+
|
|
36
|
+
It supports:
|
|
37
|
+
|
|
38
|
+
- Mounting Express.js/Koa.js app with `serverless-http` module,
|
|
39
|
+
- DI injection with `@tsed/di`,
|
|
40
|
+
- Models mapping using `@tsed/schema` and `@tsed/json-mapper`,
|
|
41
|
+
- Params decorators can be used from `@tsed/platform-params` to get Query, Body, etc...
|
|
42
|
+
- Operation descriptions like `@Returns`,
|
|
43
|
+
- `@tsed/async-hook-context` to inject Context anywhere in your class!
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
npm install --save @tsed/platform-serverless-http serverless-http serverless-offline
|
|
49
|
+
npm install --save-dev @types/aws-lambda
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Configuration
|
|
53
|
+
|
|
54
|
+
In the `src/lambda` create a new Lambda class:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import {Controller, Inject} from "@tsed/di";
|
|
58
|
+
import {Get, Returns, Summary} from "@tsed/schema";
|
|
59
|
+
import {QueryParams} from "@tsed/platform-params";
|
|
60
|
+
import {TimeslotsService} from "../services/TimeslotsService";
|
|
61
|
+
import {TimeslotModel} from "../models/TimeslotModel";
|
|
62
|
+
|
|
63
|
+
@Controller("/timeslots")
|
|
64
|
+
export class TimeslotsController {
|
|
65
|
+
@Inject()
|
|
66
|
+
protected timeslotsService: TimeslotsService;
|
|
67
|
+
|
|
68
|
+
@Get("/")
|
|
69
|
+
@Summary("Return a list of timeslots")
|
|
70
|
+
@Returns(200, Array).Of(TimeslotModel)
|
|
71
|
+
get(@QueryParams("date_start") dateStart: Date, @QueryParams("date_end") dateEnd: Date) {
|
|
72
|
+
return this.timeslotsService.find({
|
|
73
|
+
dateStart,
|
|
74
|
+
dateEnd
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Create new `Server.ts` to configure your Ts.ED application:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import {Configuration, Inject} from "@tsed/di";
|
|
84
|
+
import {PlatformApplication} from "@tsed/common";
|
|
85
|
+
import bodyParser from "body-parser";
|
|
86
|
+
import compress from "compression";
|
|
87
|
+
import cookieParser from "cookie-parser";
|
|
88
|
+
import methodOverride from "method-override";
|
|
89
|
+
import cors from "cors";
|
|
90
|
+
import "@tsed/ajv";
|
|
91
|
+
import "@tsed/swagger";
|
|
92
|
+
import {TimeslotsController} from "./controllers/TimeslotsController";
|
|
93
|
+
|
|
94
|
+
@Configuration({
|
|
95
|
+
acceptMimes: ["application/json"],
|
|
96
|
+
mount: {
|
|
97
|
+
"/": [
|
|
98
|
+
TimeslotsController
|
|
99
|
+
]
|
|
100
|
+
},
|
|
101
|
+
swagger: [
|
|
102
|
+
{
|
|
103
|
+
path: "/v3/docs",
|
|
104
|
+
specVersion: "3.0.1"
|
|
105
|
+
}
|
|
106
|
+
],
|
|
107
|
+
views: {
|
|
108
|
+
root: `${rootDir}/views`,
|
|
109
|
+
extensions: {
|
|
110
|
+
ejs: "ejs"
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
exclude: [
|
|
114
|
+
"**/*.spec.ts"
|
|
115
|
+
]
|
|
116
|
+
})
|
|
117
|
+
export class Server {
|
|
118
|
+
$beforeRoutesInit(): void {
|
|
119
|
+
this.app
|
|
120
|
+
.use(cors())
|
|
121
|
+
.use(cookieParser())
|
|
122
|
+
.use(compress({}))
|
|
123
|
+
.use(methodOverride())
|
|
124
|
+
.use(bodyParser.json())
|
|
125
|
+
.use(bodyParser.urlencoded({
|
|
126
|
+
extended: true
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Create new `handler.ts` to expose your lambda:
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import {PlatformServerless} from "@tsed/platform-serverless-http";
|
|
136
|
+
import {PlatformExpress} from "@tsed/platform-express";
|
|
137
|
+
import {Server} from "./Server";
|
|
138
|
+
|
|
139
|
+
const platform = PlatformServerless.bootstrap(Server, {
|
|
140
|
+
adapter: PlatformExpress
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
export const handler = platform.handler();
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Create also `index.ts` to expose run Ts.ED on you local machine:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import {PlatformExpress} from "@tsed/platform-express";
|
|
150
|
+
import {Server} from "./Server";
|
|
151
|
+
|
|
152
|
+
async function bootstrap() {
|
|
153
|
+
const platform = await PlatformExpress.bootstrap(Server, {
|
|
154
|
+
httpsPort: false,
|
|
155
|
+
httpPort: process.env.PORT || 3000,
|
|
156
|
+
disableComponentsScan: true
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
await platform.listen();
|
|
160
|
+
|
|
161
|
+
return platform;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
bootstrap();
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Finally, create the `serverless.yml`:
|
|
168
|
+
|
|
169
|
+
```yml
|
|
170
|
+
service: timeslots
|
|
171
|
+
|
|
172
|
+
frameworkVersion: '2'
|
|
173
|
+
|
|
174
|
+
provider:
|
|
175
|
+
name: aws
|
|
176
|
+
runtime: nodejs14.x
|
|
177
|
+
lambdaHashingVersion: '20201221'
|
|
178
|
+
|
|
179
|
+
plugins:
|
|
180
|
+
- serverless-offline
|
|
181
|
+
|
|
182
|
+
functions:
|
|
183
|
+
any:
|
|
184
|
+
handler: dist/handler.handler
|
|
185
|
+
events:
|
|
186
|
+
- http:
|
|
187
|
+
method: ANY
|
|
188
|
+
path: /
|
|
189
|
+
- http:
|
|
190
|
+
method: ANY
|
|
191
|
+
path: '{proxy+}'
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Invoke a lambda with serverless
|
|
195
|
+
|
|
196
|
+
Serverless provide a plugin named `serverless-offline`. This Serverless plugin emulates AWS λ and API Gateway on your
|
|
197
|
+
local machine to speed up your development cycles. To do so, it starts an HTTP server that handles the request's
|
|
198
|
+
lifecycle like API does and invokes your handlers.
|
|
199
|
+
|
|
200
|
+
So, by using the `serverless offline` command, we'll be able to invoke our function. For that, we need also to build our
|
|
201
|
+
code before invoke the lambda.
|
|
202
|
+
|
|
203
|
+
To simplify our workflow, we can add the following npm script command in our `package.json`:
|
|
204
|
+
|
|
205
|
+
```json
|
|
206
|
+
{
|
|
207
|
+
"scripts": {
|
|
208
|
+
"invoke": "yarn serverless invoke local -f any --data '{\"path\":\"/timeslots\", \"httpMethod\": \"GET\"}'"
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Now, we can run the following command to invoke our lambda:
|
|
214
|
+
|
|
215
|
+
```
|
|
216
|
+
yarn invoke
|
|
217
|
+
// OR
|
|
218
|
+
npm run invoke
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
You should see in the terminal the following result:
|
|
222
|
+
|
|
223
|
+
```json
|
|
224
|
+
{
|
|
225
|
+
"statusCode": 200,
|
|
226
|
+
"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\"}]",
|
|
227
|
+
"headers": {
|
|
228
|
+
"content-type": "application/json",
|
|
229
|
+
"x-request-id": "ebb52d5e-113b-40da-b34e-c14811df596b"
|
|
230
|
+
},
|
|
231
|
+
"isBase64Encoded": false
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## Get Aws Context and Aws Event
|
|
236
|
+
|
|
237
|
+
This package includes decorators to easily get the event object Lambda received from API Gateway:
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
import {Controller, Get} from "@tsed/common";
|
|
241
|
+
import {ServerlessEvent, ServerlessContext} from "@tsed/platform-serverless-http";
|
|
242
|
+
|
|
243
|
+
@Controller("/")
|
|
244
|
+
class MyCtrl {
|
|
245
|
+
@Get("/")
|
|
246
|
+
get(@ServerlessEvent() event: any, @ServerlessContext() context: ServerlessContext) {
|
|
247
|
+
console.log("Event", event);
|
|
248
|
+
console.log("Context", context);
|
|
249
|
+
|
|
250
|
+
return { event, context };
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
## Testing
|
|
257
|
+
|
|
258
|
+
Ts.ED provide a way to test you lambda with mocked Aws event and context by using the @@PlatformServerlessTest@@ util.
|
|
259
|
+
|
|
260
|
+
Here an example to test a Lambda controller:
|
|
261
|
+
|
|
262
|
+
```typescript
|
|
263
|
+
import {PlatformServerless} from "@tsed/platform-serverless-http";
|
|
264
|
+
import {PlatformServerlessTest} from "@tsed/platform-serverless-testing";
|
|
265
|
+
import {PlatformExpress} from "@tsed/platform-express";
|
|
266
|
+
import {Server} from "./Server";
|
|
267
|
+
|
|
268
|
+
@Controller("/timeslots")
|
|
269
|
+
class TimeslotsController {
|
|
270
|
+
@Get("/")
|
|
271
|
+
getAll() {
|
|
272
|
+
return [];
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
describe("TimeslotsController", () => {
|
|
277
|
+
beforeEach(
|
|
278
|
+
PlatformServerlessTest.bootstrap(PlatformServerlessHttp, {
|
|
279
|
+
server: Server,
|
|
280
|
+
mount: {
|
|
281
|
+
"/": [TimeslotsLambdaController]
|
|
282
|
+
}
|
|
283
|
+
})
|
|
284
|
+
);
|
|
285
|
+
afterEach(() => PlatformServerlessTest.reset());
|
|
286
|
+
|
|
287
|
+
it("should call getAll Lambda", async () => {
|
|
288
|
+
const response = await PlatformServerlessTest.request.get("/timeslots");
|
|
289
|
+
|
|
290
|
+
expect(response.statusCode).toEqual(200);
|
|
291
|
+
expect(JSON.parse(response.body)).toEqual([]);
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
## Contributors
|
|
298
|
+
|
|
299
|
+
Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html)
|
|
300
|
+
|
|
301
|
+
<a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
|
|
302
|
+
|
|
303
|
+
## Backers
|
|
304
|
+
|
|
305
|
+
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
|
|
306
|
+
|
|
307
|
+
<a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/backers.svg?width=890"></a>
|
|
308
|
+
|
|
309
|
+
## Sponsors
|
|
310
|
+
|
|
311
|
+
Support this project by becoming a sponsor. Your logo will show up here with a link to your
|
|
312
|
+
website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
|
|
313
|
+
|
|
314
|
+
## License
|
|
315
|
+
|
|
316
|
+
The MIT License (MIT)
|
|
317
|
+
|
|
318
|
+
Copyright (c) 2016 - 2018 Romain Lenzotti
|
|
319
|
+
|
|
320
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
321
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
322
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
323
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
324
|
+
|
|
325
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
326
|
+
Software.
|
|
327
|
+
|
|
328
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
329
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
330
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
331
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -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
|
+
}
|