@8ms/helpers 1.1.82 → 1.1.83

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,12 +1,13 @@
1
1
  declare type Invoke = {
2
+ awaitResponse?: boolean;
2
3
  functionName: string;
4
+ isJson?: boolean;
3
5
  payload?: object;
4
- response?: boolean;
5
6
  };
6
7
  /**
7
8
  * Invoke a AWS Lambda function by passing in the Function name and optional Payload data.
8
9
  * Runs asynchronously, doesn't wait for function to fully end.
9
10
  * Library: @aws-sdk/client-lambda
10
11
  */
11
- declare const invoke: ({ functionName, payload, response }: Invoke) => Promise<any>;
12
+ declare const invoke: ({ awaitResponse, functionName, isJson, payload }: Invoke) => Promise<any>;
12
13
  export default invoke;
@@ -9,10 +9,11 @@ const isResponse200_1 = __importDefault(require("../isResponse200"));
9
9
  * Runs asynchronously, doesn't wait for function to fully end.
10
10
  * Library: @aws-sdk/client-lambda
11
11
  */
12
- const invoke = async ({ functionName, payload, response }) => {
12
+ const invoke = async ({ awaitResponse, functionName, isJson, payload }) => {
13
+ let response = false;
13
14
  const params = {
14
15
  FunctionName: functionName,
15
- InvocationType: true === response ? 'RequestResponse' : 'Event',
16
+ InvocationType: true === awaitResponse ? 'RequestResponse' : 'Event',
16
17
  };
17
18
  // Payload is defined add to parameters
18
19
  if (undefined !== payload) {
@@ -26,17 +27,23 @@ const invoke = async ({ functionName, payload, response }) => {
26
27
  // Success
27
28
  if ((0, isResponse200_1.default)({ apiResponse })) {
28
29
  // Return the data
29
- if (response && undefined !== apiResponse['Payload']) {
30
+ if (awaitResponse && undefined !== apiResponse.Payload) {
30
31
  const asciiDecoder = new TextDecoder('ascii');
31
- const decoded = asciiDecoder.decode(apiResponse['Payload']);
32
- const decodedJson = JSON.parse(decoded);
33
- if (undefined !== decodedJson['body']) {
34
- return decodedJson['body'];
32
+ response = asciiDecoder.decode(apiResponse.Payload);
33
+ // Decode string as JSON object
34
+ if (true === isJson) {
35
+ response = JSON.parse(response);
36
+ // If it's still a string, double decode it
37
+ if ('string' === typeof response) {
38
+ response = JSON.parse(response);
39
+ }
35
40
  }
36
- return decodedJson;
37
41
  }
38
- return true;
42
+ // Success but not waiting for response
43
+ else {
44
+ response = true;
45
+ }
39
46
  }
40
- return false;
47
+ return response;
41
48
  };
42
49
  exports.default = invoke;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "1.1.82",
4
+ "version": "1.1.83",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"