@tsed/platform-serverless-testing 7.73.2 → 7.73.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,198 +0,0 @@
1
- import {DITest} from "@tsed/di";
2
- import {APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEventBase, APIGatewayProxyHandler} from "aws-lambda";
3
- import {APIGatewayProxyResult} from "aws-lambda/trigger/api-gateway-proxy";
4
- import {createFakeEvent} from "./createFakeEvent.js";
5
- import {createFakeContext} from "./createFakeContext.js";
6
- import {nameOf, Type} from "@tsed/core";
7
- import {PlatformBuilder, PlatformBuilderSettings} from "@tsed/common";
8
-
9
- export interface LambdaPromiseResult extends Promise<APIGatewayProxyResult> {}
10
-
11
- export class LambdaClientRequest extends Promise<APIGatewayProxyResult> {
12
- event = createFakeEvent();
13
- context = createFakeContext();
14
-
15
- static call(lambdaName: string) {
16
- const resolvers: any = {};
17
- const promise = new LambdaClientRequest((resolve, reject) => {
18
- resolvers.resolve = resolve;
19
- resolvers.reject = reject;
20
- });
21
-
22
- promise.init(lambdaName, resolvers.resolve, resolvers.reject);
23
-
24
- return promise;
25
- }
26
-
27
- static get(path: string, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {
28
- return this.call("handler").get(path, options);
29
- }
30
-
31
- static post(path: string, body?: any, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {
32
- return this.call("handler").post(path, body, options);
33
- }
34
-
35
- static put(path: string, body?: any, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {
36
- return this.call("handler").put(path, body, options);
37
- }
38
-
39
- static patch(path: string, body?: any, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {
40
- return this.call("handler").patch(path, body, options);
41
- }
42
-
43
- static delete(path: string, body?: any, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {
44
- return this.call("handler").delete(path, body, options);
45
- }
46
-
47
- get(path: string, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {
48
- Object.assign(this.event, options);
49
-
50
- this.event.path = path;
51
- this.event.httpMethod = "GET";
52
-
53
- return this;
54
- }
55
-
56
- post(path: string, body: any = {}, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {
57
- Object.assign(this.event, options);
58
-
59
- this.event.path = path;
60
- this.event.httpMethod = "POST";
61
-
62
- this.body(body);
63
-
64
- return this;
65
- }
66
-
67
- patch(path: string, body: any = {}, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {
68
- Object.assign(this.event, options);
69
-
70
- this.event.path = path;
71
- this.event.httpMethod = "PATCH";
72
-
73
- this.body(body);
74
-
75
- return this;
76
- }
77
-
78
- put(path: string, body: any = {}, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {
79
- Object.assign(this.event, options);
80
-
81
- this.event.path = path;
82
- this.event.httpMethod = "PUT";
83
-
84
- this.body(body);
85
-
86
- return this;
87
- }
88
-
89
- delete(path: string, body: any = {}, options: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {
90
- Object.assign(this.event, options);
91
-
92
- this.event.path = path;
93
- this.event.httpMethod = "DELETE";
94
-
95
- this.body(body);
96
-
97
- return this;
98
- }
99
-
100
- query(query: any) {
101
- this.event.queryStringParameters = {
102
- ...this.event.queryStringParameters,
103
- ...JSON.parse(JSON.stringify(query))
104
- };
105
-
106
- return this;
107
- }
108
-
109
- params(pathParameters: any) {
110
- this.event.pathParameters = {
111
- ...this.event.pathParameters,
112
- ...JSON.parse(JSON.stringify(pathParameters))
113
- };
114
-
115
- return this;
116
- }
117
-
118
- headers(headers: Record<string, any>) {
119
- this.event.headers = {
120
- ...this.event.headers,
121
- ...JSON.parse(JSON.stringify(headers))
122
- };
123
-
124
- return this;
125
- }
126
-
127
- body(body: any) {
128
- if (body !== undefined) {
129
- this.event.headers["content-type"] = "application/json";
130
-
131
- this.event.body = JSON.stringify(body);
132
- }
133
-
134
- return this;
135
- }
136
-
137
- protected init(lambda: string, resolve: Function, reject: Function) {
138
- setTimeout(async () => {
139
- try {
140
- const result = await PlatformServerlessTest.callbacks[lambda](this.event, this.context, resolve as any);
141
- resolve(result as any);
142
- } catch (er) {
143
- reject(er);
144
- }
145
- });
146
- }
147
- }
148
-
149
- export class PlatformServerlessTest extends DITest {
150
- static callbacks: Record<string, APIGatewayProxyHandler> = {};
151
- static instance: any;
152
- static request = LambdaClientRequest;
153
-
154
- static bootstrap(
155
- serverless: {bootstrap: (server: Type<any>, settings: PlatformBuilderSettings<any>) => PlatformBuilder},
156
- {server, ...settings}: PlatformBuilderSettings<any> & {server: Type<any>}
157
- ): () => Promise<any>;
158
- static bootstrap(
159
- serverless: {bootstrap: (settings: Partial<TsED.Configuration> & {lambda?: Type[]}) => any},
160
- {server, ...settings}: PlatformBuilderSettings<any>
161
- ): () => Promise<any>;
162
- static bootstrap(serverless: any, {server, ...settings}: PlatformBuilderSettings<any>) {
163
- return function before(): Promise<void> {
164
- settings = DITest.configure(settings);
165
-
166
- const isServerlessHttp = nameOf(serverless).includes("Http");
167
-
168
- // @ts-ignore
169
- const instance = isServerlessHttp ? serverless.bootstrap(server, settings) : serverless.bootstrap(settings);
170
-
171
- PlatformServerlessTest.instance = instance;
172
- PlatformServerlessTest.callbacks = {};
173
-
174
- if (!isServerlessHttp) {
175
- PlatformServerlessTest.callbacks = instance.callbacks();
176
- }
177
-
178
- PlatformServerlessTest.callbacks.handler = instance.handler();
179
- // used by inject method
180
- DITest.injector = instance.injector;
181
-
182
- return instance.promise;
183
- };
184
- }
185
-
186
- /**
187
- * Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.
188
- */
189
- static async reset() {
190
- if (PlatformServerlessTest.instance) {
191
- await PlatformServerlessTest.instance.stop();
192
- }
193
- if (DITest.hasInjector()) {
194
- await DITest.injector.destroy();
195
- DITest._injector = null;
196
- }
197
- }
198
- }
@@ -1,15 +0,0 @@
1
- import {Context} from "aws-lambda";
2
-
3
- export function createFakeContext(context?: Context): Context {
4
- return {
5
- awsRequestId: "awsRequestId",
6
- callbackWaitsForEmptyEventLoop: false,
7
- functionName: "",
8
- functionVersion: "",
9
- invokedFunctionArn: "",
10
- logGroupName: "",
11
- logStreamName: "",
12
- memoryLimitInMB: "",
13
- ...(context || {})
14
- } as Context;
15
- }
@@ -1,32 +0,0 @@
1
- import {APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEventBase} from "aws-lambda";
2
-
3
- export function createFakeEvent(event: Partial<APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>> = {}) {
4
- return {
5
- body: "",
6
- headers: {},
7
- httpMethod: "",
8
- isBase64Encoded: false,
9
- multiValueHeaders: {},
10
- multiValueQueryStringParameters: {},
11
- path: event.path || "/",
12
- pathParameters: {},
13
- queryStringParameters: {},
14
- resource: "",
15
- stageVariables: {},
16
- ...event,
17
- requestContext: {
18
- ...event?.requestContext,
19
- accountId: "",
20
- apiId: "",
21
- protocol: "https",
22
- httpMethod: "",
23
- identity: {} as any,
24
- path: event.path || "/",
25
- stage: "",
26
- requestId: "requestId",
27
- requestTimeEpoch: 0,
28
- resourceId: 1,
29
- resourcePath: event.path || "/"
30
- } as any
31
- } as APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;
32
- }
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from "./createFakeEvent.js";
2
- export * from "./createFakeContext.js";
3
- export * from "./PlatformServerlessTest.js";
package/tsconfig.json DELETED
@@ -1,48 +0,0 @@
1
- {
2
- "extends": "@tsed/typescript/tsconfig.node.json",
3
- "compilerOptions": {
4
- "baseUrl": ".",
5
- "module": "commonjs",
6
- "rootDir": "src",
7
- "outDir": "./lib/cjs",
8
- "declaration": true,
9
- "declarationDir": "./lib/types",
10
- "composite": true,
11
- "noEmit": false
12
- },
13
- "include": ["src", "src/**/*.json"],
14
- "exclude": [
15
- "node_modules",
16
- "test",
17
- "lib",
18
- "benchmark",
19
- "coverage",
20
- "spec",
21
- "**/*.benchmark.ts",
22
- "**/*.spec.ts",
23
- "keys",
24
- "jest.config.js",
25
- "**/__mock__/**",
26
- "webpack.config.js"
27
- ],
28
- "references": [
29
- {
30
- "path": "../common"
31
- },
32
- {
33
- "path": "../../core"
34
- },
35
- {
36
- "path": "../../di"
37
- },
38
- {
39
- "path": "../../specs/json-mapper"
40
- },
41
- {
42
- "path": "../../specs/openspec"
43
- },
44
- {
45
- "path": "../../specs/schema"
46
- }
47
- ]
48
- }