1.3.0 • Published 4 years ago

simple-http-adapter v1.3.0

Weekly downloads
16
License
MIT
Repository
-
Last release
4 years ago

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) {}
1.2.6

4 years ago

1.3.0

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.0

4 years ago