@sfd-br/response-util

Http Utility to make the SFD Standard Response

Usage no npm install needed!

<script type="module">
  import sfdBrResponseUtil from 'https://cdn.skypack.dev/@sfd-br/response-util';
</script>

README

Http Utility to make the SFD Standard Response

Install

$ npm i -S @sfd-br/response-util

Usage

  const responseUtil = require('@sfd-br/response-util');
  // Send a 200 response with a JSON as result 
  responseUtil.makeResponseSuccess(response, result);
  // Send a 200 response with a Plain result number/string 
  responseUtil.makeResponseSuccessPlain(response, result);
  // Send a 200 response with a JSON as result
  responseUtil.makeResponseCreated(response, result);
  // Send SFD ResponseError with any error statusCode, treated or not
  responseUtil.makeResponseError(response, error);

Example

  const responseUtil = require('@sfd-br/response-util');
/*
 * Returns all available assets.
 */
function listAssets(req, res) {
  var asset = req.swagger.params.name.value;
  var blockchain = req.swagger.params.blockchain.value;
  service(blockchain).listAssetsWithIssuer(asset).then(
    function(result) {
      responseUtil.makeResponseSuccess(res, result);
    }).catch(function(error) {
    responseUtil.makeResponseError(res, error);
  });

}

License

SFD