@cumulusds/aws-apig-bypass 0.0.1
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/.babelrc.js +15 -0
- package/.eslintrc.js +15 -0
- package/.flowconfig +14 -0
- package/.github/dependabot.yml +15 -0
- package/.github/pull_request_template.md +18 -0
- package/.github/workflows/ci-cd.yml +86 -0
- package/LICENSE +9 -0
- package/README.md +68 -0
- package/doc/CreateAccessKey.png +0 -0
- package/doc/development.md +244 -0
- package/doc/prettier-watchers-pc.xml +222 -0
- package/doc/prettier-watchers-unix.xml +222 -0
- package/doc/webstorm-file-watcher-prettier.png +0 -0
- package/doc/webstorm-flow.png +0 -0
- package/flow-typed/npm/jest_v26.x.x.js +1218 -0
- package/lib/create-api-gateway-event.js +50 -0
- package/lib/create-api-gateway-event.js.flow +57 -0
- package/lib/create-client.js +25 -0
- package/lib/create-client.js.flow +30 -0
- package/lib/create-lambda-client.js +33 -0
- package/lib/create-lambda-client.js.flow +35 -0
- package/lib/index.js +23 -0
- package/lib/index.js.flow +4 -0
- package/lib/json-stringify.js +23 -0
- package/lib/json-stringify.js.flow +16 -0
- package/lib/lambda-invoke.js +1 -0
- package/lib/lambda-invoke.js.flow +10 -0
- package/lib/parse-payload.js +31 -0
- package/lib/parse-payload.js.flow +25 -0
- package/lib/response.js +1 -0
- package/lib/response.js.flow +15 -0
- package/package.json +86 -0
- package/prettier.config.js +3 -0
- package/src/create-api-gateway-event.js +57 -0
- package/src/create-client.js +30 -0
- package/src/create-lambda-client.js +35 -0
- package/src/index.js +4 -0
- package/src/json-stringify.js +16 -0
- package/src/lambda-invoke.js +10 -0
- package/src/parse-payload.js +25 -0
- package/src/response.js +15 -0
- package/test/unit/index.test.js +142 -0
package/src/response.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Decoded handler response
|
|
5
|
+
*/
|
|
6
|
+
export type Response<T> = {|
|
|
7
|
+
// the response provided by the handler
|
|
8
|
+
data: T,
|
|
9
|
+
|
|
10
|
+
// the HTTP status code
|
|
11
|
+
status: number,
|
|
12
|
+
|
|
13
|
+
// response headers
|
|
14
|
+
headers: { [header: string]: boolean | number | string }
|
|
15
|
+
|};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
import type { Lambda$InvocationResponse } from "@cumulusds/flow-aws-sdk/clients/lambda";
|
|
4
|
+
import type { AWSError } from "@cumulusds/flow-aws-sdk/lib/error";
|
|
5
|
+
import type { Request } from "@cumulusds/flow-aws-sdk/lib/request";
|
|
6
|
+
import type { APIGatewayHandlerClient } from "../../src/create-client";
|
|
7
|
+
import { createAPIGatewayEvent, createClient } from "../../src";
|
|
8
|
+
|
|
9
|
+
type ClientPayload = {};
|
|
10
|
+
|
|
11
|
+
describe("createClient", () => {
|
|
12
|
+
const FunctionName = "service-stage-function";
|
|
13
|
+
const data = { hello: "world" };
|
|
14
|
+
const body = JSON.stringify(data);
|
|
15
|
+
|
|
16
|
+
function createLambdaClient({ StatusCode, Payload = JSON.stringify({ statusCode: 200, body }) }) {
|
|
17
|
+
const resolvedValue: Lambda$InvocationResponse = { StatusCode, Payload };
|
|
18
|
+
|
|
19
|
+
// $FlowFixMe[incompatible-type] complete Response class is not required.
|
|
20
|
+
const request: Request<Lambda$InvocationResponse, AWSError> = {
|
|
21
|
+
promise: jest.fn().mockResolvedValue(resolvedValue)
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const invoke = jest.fn(() => request);
|
|
25
|
+
const Lambda = {
|
|
26
|
+
invoke
|
|
27
|
+
};
|
|
28
|
+
return { invoke, Lambda };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe("when function exists", () => {
|
|
32
|
+
describe("with invocation StatusCode 200", () => {
|
|
33
|
+
const { invoke, Lambda } = createLambdaClient({ StatusCode: 200 });
|
|
34
|
+
|
|
35
|
+
it("invokes the Lambda function", async () => {
|
|
36
|
+
const client: APIGatewayHandlerClient<ClientPayload> = createClient<ClientPayload>({
|
|
37
|
+
Lambda,
|
|
38
|
+
FunctionName
|
|
39
|
+
});
|
|
40
|
+
const request = createAPIGatewayEvent();
|
|
41
|
+
expect(await client(request)).toEqual({ data, headers: {}, status: 200 });
|
|
42
|
+
expect(invoke).toHaveBeenCalledWith({ FunctionName, Payload: JSON.stringify(request) });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("is unsuccessful given no request", () => {
|
|
46
|
+
const client = createClient<ClientPayload>({
|
|
47
|
+
Lambda,
|
|
48
|
+
FunctionName
|
|
49
|
+
});
|
|
50
|
+
// $FlowFixMe[incompatible-call] this test calls the client with an invalid (undefined) argument to test a runtime error-handling branch that is not accessible with a valid argument
|
|
51
|
+
return expect(client()).rejects.toEqual(new Error("Cannot stringify value"));
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("is successful with payload statusCode 500", () => {
|
|
56
|
+
const { Lambda } = createLambdaClient({
|
|
57
|
+
StatusCode: 200,
|
|
58
|
+
Payload: JSON.stringify({ statusCode: 500, body })
|
|
59
|
+
});
|
|
60
|
+
const client = createClient<ClientPayload>({
|
|
61
|
+
Lambda,
|
|
62
|
+
FunctionName
|
|
63
|
+
});
|
|
64
|
+
const request = createAPIGatewayEvent({
|
|
65
|
+
pathParameters: {
|
|
66
|
+
site: "sts-playground-test",
|
|
67
|
+
jointIdentifierId: "1234"
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
return expect(client(request)).resolves.toEqual({ data, headers: {}, status: 500 });
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("is unsuccessful with base64-encoded response", () => {
|
|
74
|
+
const { Lambda } = createLambdaClient({
|
|
75
|
+
StatusCode: 200,
|
|
76
|
+
Payload: JSON.stringify({ statusCode: 500, isBase64Encoded: true, body: Buffer.from(body).toString("base64") })
|
|
77
|
+
});
|
|
78
|
+
const client = createClient<ClientPayload>({
|
|
79
|
+
Lambda,
|
|
80
|
+
FunctionName
|
|
81
|
+
});
|
|
82
|
+
const request = createAPIGatewayEvent({
|
|
83
|
+
pathParameters: {
|
|
84
|
+
site: "sts-playground-test",
|
|
85
|
+
jointIdentifierId: "1234"
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return expect(client(request)).rejects.toEqual(
|
|
89
|
+
new Error("A base64 encoded response from API Gateway handler was received, but is not supported.")
|
|
90
|
+
);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("is unsuccessful with AWS Lambda API StatusCode 500", () => {
|
|
94
|
+
const { Lambda } = createLambdaClient({ StatusCode: 500 });
|
|
95
|
+
const client = createClient<ClientPayload>({
|
|
96
|
+
Lambda,
|
|
97
|
+
FunctionName
|
|
98
|
+
});
|
|
99
|
+
const request = createAPIGatewayEvent({
|
|
100
|
+
pathParameters: {
|
|
101
|
+
site: "sts-playground-test",
|
|
102
|
+
jointIdentifierId: "1234"
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return expect(client(request)).rejects.toEqual(new Error("AWS Lambda invocation API failed with status 500"));
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("is unsuccessful with undefined StatusCode", () => {
|
|
109
|
+
const { Lambda } = createLambdaClient({});
|
|
110
|
+
const client = createClient<ClientPayload>({
|
|
111
|
+
Lambda,
|
|
112
|
+
FunctionName
|
|
113
|
+
});
|
|
114
|
+
const request = createAPIGatewayEvent({
|
|
115
|
+
pathParameters: {
|
|
116
|
+
site: "sts-playground-test",
|
|
117
|
+
jointIdentifierId: "1234"
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
return expect(client(request)).rejects.toEqual(
|
|
121
|
+
new Error("AWS Lambda invocation API failed with status (no status code)")
|
|
122
|
+
);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("is unsuccessful with no request payload", () => {
|
|
126
|
+
const { Lambda } = createLambdaClient({ StatusCode: 200, Payload: null });
|
|
127
|
+
const client = createClient<ClientPayload>({
|
|
128
|
+
Lambda,
|
|
129
|
+
FunctionName
|
|
130
|
+
});
|
|
131
|
+
const request = createAPIGatewayEvent({
|
|
132
|
+
pathParameters: {
|
|
133
|
+
site: "sts-playground-test",
|
|
134
|
+
jointIdentifierId: "1234"
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
return expect(client(request)).rejects.toEqual(
|
|
138
|
+
new Error("AWS Lambda invocation API returned the wrong payload type 'object'; expected 'string'")
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
});
|