simple-http-adapter

Normal function, promise can be used like http function.

Usage no npm install needed!

<script type="module">
  import simpleHttpAdapter from 'https://cdn.skypack.dev/simple-http-adapter';
</script>

README

Simple Http Adapter

Normal function, promise can be used like http function.

Install

$ npm i simple-http-adapter

Usage

const httpAdapter = require('simple-http-adapter');

function test({ a, b, c, d }) {
  if (a === 0) throw new httpAdapter.Error({ statusCode: 500 });

  return { b, c, d };
}

const httpEndpoint = httpAdapter.convert(httpAdapter.GET(200), test);

let response = await httpEndpoint(new httpAdapter.Request({
  method: httpAdapter.method.GET,
  body: {
    a: 1,
    b: 2,
    c: 3,
    d: 4,
  }
}));

console.log(response);

response = await httpEndpoint(new httpAdapter.Request({
  method: httpAdapter.method.GET,
  body: {
    a: 0,
    b: 2,
    c: 3,
    d: 4,
  }
}));

console.log(response);

Response { statusCode: 200, headers: {}, body: { b: 2, c: 3, d: 4 } }
Response { statusCode: 500, headers: {}, body: { message: '' } }

Spec

Convert

httpAdapter.convert({ expectedMethod, successStatusCode }, func, handlers = handler);

Handlers

const handlers = { errorHandler, requestParamsExtract, responseMapping };

Error Handel

function errorHandler(error) {}

Request Params Extract

function requestParamsExtract(request) {}

Response Mapping

function responseMapping(response, successStatusCode) {}