2.1.0 • Published 7 years ago

wreckage v2.1.0

Weekly downloads
12
License
MIT
Repository
github
Last release
7 years ago

Wreckage

npm version

Build Status

Coverage Status

A convenient, modern request library built around Wreck.

Overview

A simple wrapper around Wreck providing consistent error responses, status code validations, and hashing options.

(Wreck was recently updated to support async/await natively, eliminating my original need for this module, but the sugar is still handy)

Installation

NPM

$ npm install wreckage --save

Yarn

$ yarn add wreckage

Usage

import wreckage from 'wreckage';

const request = wreckage.create({
  request: {
    baseUrl: 'https://jsonplaceholder.typicode.com'
  }
});

const getSomething = async function () {
  const {payload} = await request.get('/posts/1');
  // payload.userId === 1
}

Methods

.get(uri, [options])

.post(uri, payload, [options])

.put(uri, payload, [options])

.patch(uri, payload, [options])

.delete(uri, payload, [options])

.create(configuration)

.request(method, uri, [options])

.defaults

GET

.get(uri, [options])

Performs a GET request

POST

.post(uri, payload, [options])

Performs a POST request

PUT

.put(uri, payload, [options])

Performs a PUT request

PATCH

.patch(uri, payload, [options])

Performs a PUT request

DELETE

.delete(uri, [options])

Performs a DELETE request

Request

.request(method, uri, [options])

Access the Promise request wrapper for Wreck

Create

.create([options])

Create a new instance of Wreckage with your options

Defaults

.defaults

Access the defaults for the instance

Config options

request and read are passed directly to Wreck, so, you get the same options.

Additionally, you'll find:

  • errorHandling which allows you to define return, to return, rather than throw your errors
  • validateStatus allows you to validate the statusCode of your response, to determine what will actually trigger an error
  • read.hash you can optionally have a has generated with your payload. Uses crypto.createHash so all hash types supported there are available. Default is sha1

This is an example, and these are the defaults.

{
  request: {
    headers: {},
    redirects: 3
  },
  read: {
    json: true
  },
  errorHandling: 'throw',
  validateStatus(statusCode) {
    return statusCode >= 200 && statusCode < 300;
  }
}

Response object

Something like this:

{
  statusCode: 200,
  statusMessage: 'ok',
  payload: {
    userId: 1
  },
  config: {...}, // whatever config was used in the request
  headers: {} // response headers
}

Or, if you choose to return your error

{
  error: {...} // A Boom wrapped error object
}

Errors

As mentioned above, can be returned or thrown. They get wrapped by Boom, and a fair amount of information is passed through the data object.

TODO

  • Test coverage
2.1.0

7 years ago

2.0.0

7 years ago

1.1.1

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

0.1.0

7 years ago