@cumulusds/aws-apig-bypass 0.2.0 → 0.2.2

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.
@@ -18,7 +18,7 @@ jobs:
18
18
  - name: Checkout
19
19
  uses: actions/checkout@v2
20
20
  - name: Install Node ${{ matrix.node }}
21
- uses: actions/setup-node@v2.1.4
21
+ uses: actions/setup-node@v2
22
22
  with:
23
23
  node-version: ${{ matrix.node }}
24
24
  - name: Install NPM credentials
@@ -28,7 +28,8 @@ jobs:
28
28
  - name: Get yarn cache
29
29
  id: yarn-cache
30
30
  run: echo "::set-output name=dir::$(yarn cache dir)"
31
- - uses: actions/cache@v2.1.3
31
+ - name: Restore yarn cache
32
+ uses: actions/cache@v2
32
33
  with:
33
34
  path: ${{ steps.yarn-cache.outputs.dir }}
34
35
  key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
@@ -49,7 +50,7 @@ jobs:
49
50
  SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
50
51
  if: always()
51
52
  - name: Upload artifacts
52
- uses: actions/upload-artifact@v2.2.2
53
+ uses: actions/upload-artifact@v2
53
54
  with:
54
55
  name: var
55
56
  path: var
@@ -62,7 +63,7 @@ jobs:
62
63
  - name: Checkout
63
64
  uses: actions/checkout@v2
64
65
  - name: Install Node 14
65
- uses: actions/setup-node@v2.1.4
66
+ uses: actions/setup-node@v2
66
67
  with:
67
68
  node-version: 14
68
69
  - name: Install NPM credentials
@@ -72,7 +73,8 @@ jobs:
72
73
  - name: Get yarn cache
73
74
  id: yarn-cache
74
75
  run: echo "::set-output name=dir::$(yarn cache dir)"
75
- - uses: actions/cache@v2.1.3
76
+ - name: Restore yarn cache
77
+ uses: actions/cache@v2
76
78
  with:
77
79
  path: ${{ steps.yarn-cache.outputs.dir }}
78
80
  key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
@@ -4,10 +4,24 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = createLambdaClient;
7
+ exports.LambdaInvokeError = void 0;
7
8
 
9
+ class LambdaInvokeError extends Error {
10
+ constructor(response) {
11
+ var _response$FunctionErr;
12
+
13
+ super(["Invocation Failed", response.ExecutedVersion, response.StatusCode, response.FunctionError, response.Payload, response.LogResult != null ? Buffer.from(response.LogResult, "base64").toString("utf-8") : null].filter(x => x != null).join(": "));
14
+ this.code = (_response$FunctionErr = response.FunctionError) !== null && _response$FunctionErr !== void 0 ? _response$FunctionErr : "";
15
+ }
16
+
17
+ }
8
18
  /**
9
19
  * AWS Lambda SDK adapter for createClient.
10
20
  */
21
+
22
+
23
+ exports.LambdaInvokeError = LambdaInvokeError;
24
+
11
25
  function createLambdaClient({
12
26
  Lambda,
13
27
  FunctionName
@@ -18,10 +32,8 @@ function createLambdaClient({
18
32
  Payload
19
33
  }).promise();
20
34
 
21
- if (invocationResponse.StatusCode !== 200) {
22
- var _invocationResponse$S;
23
-
24
- throw new Error(`AWS Lambda invocation API failed with status ${(_invocationResponse$S = invocationResponse.StatusCode) !== null && _invocationResponse$S !== void 0 ? _invocationResponse$S : "(no status code)"}`);
35
+ if (invocationResponse.StatusCode !== 200 || invocationResponse.FunctionError != null) {
36
+ throw new LambdaInvokeError(invocationResponse);
25
37
  }
26
38
 
27
39
  if (typeof invocationResponse.Payload !== "string") {
@@ -11,6 +11,53 @@ export type CreateLambdaClientOptions = {
11
11
  FunctionName: string
12
12
  };
13
13
 
14
+ export type Lambda$InvocationResponse = {
15
+ /**
16
+ * The HTTP status code is in the 200 range for a successful request. For the RequestResponse invocation type, this status code is 200. For the Event invocation type, this status code is 202. For the DryRun invocation type, the status code is 204.
17
+ */
18
+ StatusCode?: number,
19
+
20
+ /**
21
+ * If present, indicates that an error occurred during function execution. Details about the error are included in the response payload.
22
+ */
23
+ FunctionError?: string,
24
+
25
+ /**
26
+ * The last 4 KB of the execution log, which is base64 encoded.
27
+ */
28
+ LogResult?: string,
29
+
30
+ /**
31
+ * The response from the function, or an error object.
32
+ */
33
+ Payload?: Buffer | Uint8Array | Blob | string,
34
+
35
+ /**
36
+ * The version of the function that executed. When you invoke a function with an alias, this indicates which version the alias resolved to.
37
+ */
38
+ ExecutedVersion?: string
39
+ };
40
+
41
+ export class LambdaInvokeError extends Error {
42
+ code: string;
43
+
44
+ constructor(response: Lambda$InvocationResponse) {
45
+ super(
46
+ [
47
+ "Invocation Failed",
48
+ response.ExecutedVersion,
49
+ response.StatusCode,
50
+ response.FunctionError,
51
+ response.Payload,
52
+ response.LogResult != null ? Buffer.from(response.LogResult, "base64").toString("utf-8") : null
53
+ ]
54
+ .filter(x => x != null)
55
+ .join(": ")
56
+ );
57
+ this.code = response.FunctionError ?? "";
58
+ }
59
+ }
60
+
14
61
  /**
15
62
  * AWS Lambda SDK adapter for createClient.
16
63
  */
@@ -20,10 +67,8 @@ export default function createLambdaClient({ Lambda, FunctionName }: CreateLambd
20
67
  FunctionName,
21
68
  Payload
22
69
  }).promise();
23
- if (invocationResponse.StatusCode !== 200) {
24
- throw new Error(
25
- `AWS Lambda invocation API failed with status ${invocationResponse.StatusCode ?? "(no status code)"}`
26
- );
70
+ if (invocationResponse.StatusCode !== 200 || invocationResponse.FunctionError != null) {
71
+ throw new LambdaInvokeError(invocationResponse);
27
72
  }
28
73
  if (typeof invocationResponse.Payload !== "string") {
29
74
  throw new Error(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cumulusds/aws-apig-bypass",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Client for API Gateway handlers, bypassing API Gateway by directly invoking the Lambda",
5
5
  "repository": "https://github.com/CumulusDS/aws-apig-bypass",
6
6
  "author": "Cumulus Digital Systems",
@@ -52,10 +52,7 @@
52
52
  }
53
53
  }
54
54
  },
55
- "dependencies": {
56
- "@cumulusds/flow-aws-sdk": "^2.818.0",
57
- "flow-aws-lambda": "CumulusDS/flow-aws-lambda#ef1bd9a0342e55a0e9e1b225bdbc2a3c2a4d5632"
58
- },
55
+ "dependencies": {},
59
56
  "devDependencies": {
60
57
  "@babel/cli": "^7.0.0",
61
58
  "@babel/core": "^7.7.4",
@@ -63,6 +60,7 @@
63
60
  "@babel/plugin-proposal-optional-chaining": "^7.12.7",
64
61
  "@babel/preset-env": "^7.0.0",
65
62
  "@babel/preset-flow": "^7.0.0",
63
+ "@cumulusds/flow-aws-sdk": "^2.818.0",
66
64
  "babel-eslint": "10.0.3",
67
65
  "babel-jest": "^24.9.0",
68
66
  "eslint": "^7.5.0",
@@ -72,6 +70,7 @@
72
70
  "eslint-plugin-import": "2.18.2",
73
71
  "eslint-plugin-jest": "^21.15.1",
74
72
  "eslint-plugin-prettier": "^2.6.0",
73
+ "flow-aws-lambda": "^1.0.6",
75
74
  "flow-bin": "^0.141.0",
76
75
  "flow-copy-source": "^2.0.9",
77
76
  "flow-coverage-report": "^0.8.0",
@@ -11,6 +11,53 @@ export type CreateLambdaClientOptions = {
11
11
  FunctionName: string
12
12
  };
13
13
 
14
+ export type Lambda$InvocationResponse = {
15
+ /**
16
+ * The HTTP status code is in the 200 range for a successful request. For the RequestResponse invocation type, this status code is 200. For the Event invocation type, this status code is 202. For the DryRun invocation type, the status code is 204.
17
+ */
18
+ StatusCode?: number,
19
+
20
+ /**
21
+ * If present, indicates that an error occurred during function execution. Details about the error are included in the response payload.
22
+ */
23
+ FunctionError?: string,
24
+
25
+ /**
26
+ * The last 4 KB of the execution log, which is base64 encoded.
27
+ */
28
+ LogResult?: string,
29
+
30
+ /**
31
+ * The response from the function, or an error object.
32
+ */
33
+ Payload?: Buffer | Uint8Array | Blob | string,
34
+
35
+ /**
36
+ * The version of the function that executed. When you invoke a function with an alias, this indicates which version the alias resolved to.
37
+ */
38
+ ExecutedVersion?: string
39
+ };
40
+
41
+ export class LambdaInvokeError extends Error {
42
+ code: string;
43
+
44
+ constructor(response: Lambda$InvocationResponse) {
45
+ super(
46
+ [
47
+ "Invocation Failed",
48
+ response.ExecutedVersion,
49
+ response.StatusCode,
50
+ response.FunctionError,
51
+ response.Payload,
52
+ response.LogResult != null ? Buffer.from(response.LogResult, "base64").toString("utf-8") : null
53
+ ]
54
+ .filter(x => x != null)
55
+ .join(": ")
56
+ );
57
+ this.code = response.FunctionError ?? "";
58
+ }
59
+ }
60
+
14
61
  /**
15
62
  * AWS Lambda SDK adapter for createClient.
16
63
  */
@@ -20,10 +67,8 @@ export default function createLambdaClient({ Lambda, FunctionName }: CreateLambd
20
67
  FunctionName,
21
68
  Payload
22
69
  }).promise();
23
- if (invocationResponse.StatusCode !== 200) {
24
- throw new Error(
25
- `AWS Lambda invocation API failed with status ${invocationResponse.StatusCode ?? "(no status code)"}`
26
- );
70
+ if (invocationResponse.StatusCode !== 200 || invocationResponse.FunctionError != null) {
71
+ throw new LambdaInvokeError(invocationResponse);
27
72
  }
28
73
  if (typeof invocationResponse.Payload !== "string") {
29
74
  throw new Error(
@@ -5,6 +5,7 @@ import type { AWSError } from "@cumulusds/flow-aws-sdk/lib/error";
5
5
  import type { Request } from "@cumulusds/flow-aws-sdk/lib/request";
6
6
  import type { APIGatewayHandlerClient } from "../../src/create-client";
7
7
  import { createAPIGatewayEvent, createClient } from "../../src";
8
+ import { LambdaInvokeError } from "../../src/create-lambda-client";
8
9
 
9
10
  type ClientPayload = {};
10
11
 
@@ -13,8 +14,9 @@ describe("createClient", () => {
13
14
  const data = { hello: "world" };
14
15
  const body = JSON.stringify(data);
15
16
 
16
- function createLambdaClient({ StatusCode, Payload = JSON.stringify({ statusCode: 200, body }) }) {
17
- const resolvedValue: Lambda$InvocationResponse = { StatusCode, Payload };
17
+ function createLambdaClient({ StatusCode, Payload = JSON.stringify({ statusCode: 200, body }), LogResult = null }) {
18
+ // $FlowFixMe
19
+ const resolvedValue: Lambda$InvocationResponse = { StatusCode, Payload, LogResult };
18
20
 
19
21
  // $FlowFixMe[incompatible-type] complete Response class is not required.
20
22
  const request: Request<Lambda$InvocationResponse, AWSError> = {
@@ -91,7 +93,7 @@ describe("createClient", () => {
91
93
  });
92
94
 
93
95
  it("is unsuccessful with AWS Lambda API StatusCode 500", () => {
94
- const { Lambda } = createLambdaClient({ StatusCode: 500 });
96
+ const { Lambda } = createLambdaClient({ StatusCode: 500, LogResult: "Ym9ya2Vu" });
95
97
  const client = createClient<ClientPayload>({
96
98
  Lambda,
97
99
  FunctionName
@@ -102,7 +104,13 @@ describe("createClient", () => {
102
104
  jointIdentifierId: "1234"
103
105
  }
104
106
  });
105
- return expect(client(request)).rejects.toEqual(new Error("AWS Lambda invocation API failed with status 500"));
107
+ return expect(client(request)).rejects.toEqual(
108
+ new LambdaInvokeError({
109
+ StatusCode: 500,
110
+ LogResult: "Ym9ya2Vu",
111
+ Payload: JSON.stringify({ statusCode: 200, body })
112
+ })
113
+ );
106
114
  });
107
115
 
108
116
  it("is unsuccessful with undefined StatusCode", () => {
@@ -118,7 +126,7 @@ describe("createClient", () => {
118
126
  }
119
127
  });
120
128
  return expect(client(request)).rejects.toEqual(
121
- new Error("AWS Lambda invocation API failed with status (no status code)")
129
+ new LambdaInvokeError({ Payload: JSON.stringify({ statusCode: 200, body }) })
122
130
  );
123
131
  });
124
132