1.0.0 • Published 6 years ago

ibm-cloud-functions-response-builder v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

npm-packages-ibm-cloud-functions-response-builder

Provides a builder class for http responses for the ibmcloud functions response type http in a serverless framework handler.

How it works

You can change response parameters like status code, headers or body by calling methods on the response object. Because these methods return the respone object after alteration, method calls can be chained and used in a return statement.

const res = ...;
return res.status(404).json({error: {status: 404, message: 'No item with id 3 exists'}});

With that, the call of an API endpoint using this function will return something like this:

response_example

A handler function for a ibmcloud action with the serverless framework would normally look like this:

module.exports.handler = params => {
    ...
    return {
        statusCode: 404,
        body: {
            error: {
                status: 404,
                message: 'No item with id 3 exists'
            }
        }
    }
}

The builder can be integrated into this format like this:

const builder = require('ibm-cloud-functions-response-builder');

module.exports = params => builder(params, (params, res) => {
    ...
    return res.status(404).json({error: {status: 404, message: 'No item with id 3 exists'}})
})

Or even shorter like this:

const builder = require('ibm-cloud-functions-response-builder');

module.exports = params => builder(params, (params, res) => {
    ...
    return res.toError(404, 'No item with id 3 exists')
})

I prefer code like this. Of course you can disagree, so just use this if you like it.

Default Values

The default status code is 200 (OK) while body and headers are empty by default.