Skip to content

Commit

Permalink
Refactor response() handling to enable returning arbitrary data
Browse files Browse the repository at this point in the history
Fix tests borken by previous changes
Update readme
  • Loading branch information
ryanblock committed Oct 1, 2023
1 parent 89285e2 commit 0d7fc77
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ jobs:
- name: Install
run: npm install

- name: Link (npm < 7)
if: matrix.node-version == '14.x'
run: |
cd plugins/s3
npm link
cd ../../
npm link @aws-lite/s3
- name: Test
run: npm test
env:
Expand Down
10 changes: 7 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ The following parameters may be passed with individual client requests; only `se
- By default, all headers are included in [authentication via AWS signature v4](https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html)
- **`payload` (object, buffer, readable stream, string)**
- Aliases: `body`, `data`, `json`
- As a convenience, any passed objects are automatically JSON-encoded (with the appropriate `content-type` header set, if not already present); buffers and strings simply pass through as is
- As a convenience, any passed objects are automatically JSON-encoded (with the appropriate `content-type` header set, if not already present); buffers and strings simply pass through as-is
- Passing a Node.js readable stream is currently experimental; this initiates an HTTP data stream to the API endpoint instead of writing a normal HTTP body payload
- **`query` (object)**
- Serialize the passed object and append it to your `endpoint` as a query string in your request
Expand Down Expand Up @@ -343,7 +343,7 @@ The `response()` lifecycle hook is an async function that enables mutation of se

`response()` is executed with two positional arguments:

- **`params` (object)**
- **`response` (object)**
- An object containing three properties from the API response:
- **`statusCode` (number)**
- HTTP response status code
Expand All @@ -354,7 +354,11 @@ The `response()` lifecycle hook is an async function that enables mutation of se
- **`utils` (object)**
- [Plugin helper utilities](#plugin-utils)

The `response()` method may return nothing, or it may return an object containing the following optional properties: `statusCode` (number), `headers` (object), `payload` (object or string), and `awsjson` (that behaves the same as in [client requests](#client-requests)). An example:
The `response()` method may return: nothing (which will pass through the `response` object as-is), a mutated version of the `response` object, or any other data type (most commonly an object or string).

Should you return an object, you may also include an `awsjson` property (that behaves the same as in [client requests](#client-requests)). The `awsjson` property will be stripped from any returned response.

An example:

```js
// Automatically deserialize AWS-flavored JSON
Expand Down
18 changes: 9 additions & 9 deletions src/client-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,16 @@ module.exports = async function clientFactory (config, creds, region) {
catch (methodError) {
errorHandler({ error: methodError, metadata })
}
if (pluginRes) {
let { statusCode, headers, payload } = pluginRes
if (pluginRes.awsjson) {
payload = awsjson.unmarshall(payload, pluginRes.awsjson)
}
response = {
statusCode: statusCode || response.statusCode,
headers: headers || response.headers,
payload: payload || response.payload,
if (pluginRes !== undefined) {
let unmarshalling = pluginRes.awsjson
if (unmarshalling) {
delete pluginRes.awsjson
// If a payload property isn't included, it _is_ the payload
let payload = pluginRes.payload || pluginRes
let unmarshalled = awsjson.unmarshall(payload, unmarshalling)
response = { ...pluginRes, ...unmarshalled }
}
else response = pluginRes
}
}
return response
Expand Down
2 changes: 2 additions & 0 deletions test/live/_iam.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default {
}
}
},
response: async ({ payload }) => payload,
},
CreateRole: {
request: async function ({ name, policyDoc, desc, path }) {
Expand All @@ -26,6 +27,7 @@ export default {
}
}
},
response: async ({ payload }) => payload,
}
}
}
2 changes: 2 additions & 0 deletions test/live/_lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
endpoint: `/2015-03-31/functions/${name}/configuration`
}
},
response: async ({ payload }) => payload,
},

// https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html
Expand All @@ -35,6 +36,7 @@ module.exports = {
endpoint: `/2015-03-31/functions/${name}/invocations`
}
},
response: async ({ payload }) => payload,
},
}
}

0 comments on commit 0d7fc77

Please sign in to comment.