NPM Module. A very, very tiny object creator for lambda-proxy responses. Primarily for 200 and 400 responses with an optional message. Also supports 301 and 302 responses with redirect URI.
npm install --save create-response-object
let params = {
code: '200',
message: 'Hello world!'
};
createResponseObject(params);
-
code: (optional but recommended) The HTTP status code to return. Defaults to '200'.
{string} '200', '301', '302', '400', etc - uri: (required only for 301|302 codes) The redirect uri.
-
message: (optional) The response body's message.
{string} 'Hello World!' -
cors: (optional) Adds CORS headers to the response.
-
allowHeaders (optional) List of allowed headers separated by comma.
{string} Defaults to 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token' -
allowOrigin (optional) Origin to include in the CORS response.
{string} Defaults to '*' -
allowMethods (optional) List of allowed headers seperated by comma.
{string} Defaults to '*'
-
allowHeaders (optional) List of allowed headers separated by comma.
import createResponseObject from 'create-response-object'; // ES Modulees import
const createResponseObject = require('create-response-object'); // CommonJS import
exports.handler = async (event, context) => {
return await createResponseObject({
code: '400',
message: 'Something went wrong!'
});
};
Will output:
{
"statusCode": "400",
"body": "Something went wrong!",
"headers": {
"Content-Type": "application/json"
}
}
exports.handler = async (event, context) => {
return await createResponseObject({
code: '200',
message: 'Hello World!'
cors: {
allowHeaders: 'Content-Type',
allowOrigin: 'http://localhost:3000',
allowMethods: 'OPTIONS,GET'
}
});
};
Will output:
{
"statusCode": "200",
"body": "Hello world!",
"headers": {
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Methods": "OPTIONS,GET",
"Access-Control-Allow-Origin": "http://localhost:3000",
"Content-Type": "application/json"
}
}
exports.handler = async (event, context) => {
return await createResponseObject({
code: '301',
uri: 'https://www.npmjs.com/package/create-response-object'
});
};
Will output:
{
"statusCode": "301",
"body": "Hello World!",
"headers": {
"Location": "https://www.npmjs.com/package/create-response-object"
}
}
This function, as small as it is, draws inspiration from jroberson.