0.8.10 • Published 4 years ago

@pricewaiter/integration-api-client v0.8.10

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

PriceWaiter Integration API Client

npm.io

This package provides a function for making HTTP requests to endpoints that comply with the PriceWaiter Integration API Spec. It provides the following benefits:

  1. Signs requests using a shared secret
  2. Verifies response signatures using the same shared secret
  3. Parses { error: { code: 'whatever' } }-style errors out of responses and throws them

Usage

// 1. Require the library.
const createApiClient = require('@pricewaiter/integration-api-client');

// 2. Generate a configured client function.
const makeApiRequest = createApiClient({
    type: 'ping',
    version: '2016-03-01',
    url: 'https://example.org/ping',
    apiKey: 'APIKEY',
    sharedSecret: 'SEEEEEECRETS',
});

// 3. Make requests, providing the request body and an (optional) request UUID.
makeApiRequest({ foo: 'bar' }, 'bac658f0-0a58-4eb8-b710-de62126e4032')
    .then(({request, response}) => {
        // request.headers is the request headers
        // request.id is the UUID assigned to the request (2nd arg above)
        // request.url is the URL the request was made to.
        // request.body is the parsed JSON request body (if available)
        // request.rawBody is the stringified JSON request body
        //
        // response.statusCode is the HTTP status copde
        // response.headers is the response headers
        // response.body is the parsed JSON response body (if available)
        // response.rawBody is the unparsed response body
    })
    .catch(err => {
        // err.code is i.e. 'invalid_response_body'
        // err.statusCode is the response's HTTP status code (if available)
        // err.request is the same as r.request above (if available)
        // err.response is the same as r.response above (if available)
    })

Errors

API calls made using this library can return a lot of potential errors. All errors include an unvarying code property that can be used to quickly identify them.

Client Configuration Errors

Calling createApiClient() with invalid options will throw errors right away (rather than returning a rejected Promise). You should not retry client creation without modifying the relevant option.

CodeDescription
invalid_api_keyThe API client was configured with an invalid or missing store API key.
invalid_endpoint_typeThe type option was invalid.
invalid_shared_secretThe sharedSecret option was invalid or missing.
invalid_urlThe url was invalid or missing. Urls must use the http: or https: scheme.
invalid_versionThe version option was invalid or missing.

Once you have a configured API client function, calling it can return a rejected Promise with a number of other errors.

Network Errors

These errors indicate (potentially transient) network problems. You should be able to retry your requests without modifying them.

CodeDescription
connection_errorThe HTTP request could not be completed, perhaps due to the endpoint being unreachable, DNS resolution failing, or requests timing out.
http_errorThe HTTP request returned a status code other than 200 (success) or 400 (error). This usually indicates a misconfigured endpoint.

Request Content Errors

If there is a problem with your request's content, you should not resubmit your request without modifying it.

CodeDescription
invalid_request_bodyThe request body was invalid in some way.

Response Content Errors

These errors indicate that a response was received from the endpoint, but its format was invalid in some way. Whether you retry these errors will depend on the endpoint.

CodeDescription
invalid_response_bodyThe content in the response body was invalid in some way, such as containing invalid JSON.
invalid_response_content_typeThe response included a body but did not include a Content-type: application/json header.
invalid_response_signatureThe response was either missing its X-PriceWaiter-Signature header or the header was invalid.

Endpoint-Specific Errors

Each endpoint can return its own error codes under certain conditions (e.g. deal_not_found). See Endpoint Documentation for additional error codes.


Publishing New Versions

./publish.sh [--patch|--minor] [--dry-run] will cut a new version, push to git and publish to NPM.

0.8.10

4 years ago

0.8.9

4 years ago

0.8.8

5 years ago

0.8.7

7 years ago

0.8.4

7 years ago

0.8.3

7 years ago

0.8.2

7 years ago

0.9.0

7 years ago

0.8.1

8 years ago

0.8.0

8 years ago

0.7.1

8 years ago

0.7.0

8 years ago

0.6.1

8 years ago

0.6.0

8 years ago

0.5.0

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago