0.1.1 • Published 6 years ago

@aerofer/sdk-interceptors v0.1.1

Weekly downloads
2
License
-
Repository
-
Last release
6 years ago

Logo

SDK Interceptors

This package includes multiple request & response interceptors that add utility by mutating the original request before it is sent and the response before it is returned.

Multiple request & response interceptors can be applied and called in sequence, on a per-request basis, by using applyInterceptors([...]), demonstrated in the example below.

Note interceptors are not loaded as standard and must be included from the @aerofer/sdk-interceptors package.

const interceptors = require('@aerofer/sdk-interceptors');

const spec = require('./spec.json');

const api = require('@aerofer/sdk-core')(spec, { interceptors });

const requestInterceptor = api.interceptors.applyInterceptors(
  // any function can be used!
  console.log,
  // AWS SigV4 Interceptor is described below
  api.interceptors.sigV4RequestInterceptor(creds, 'eu-west-1')
);

const opts = { requestInterceptor };

const response = await sdk.tag.operationId({}, opts);

Reducing bundle size

It's best to only include the request & response interceptors your application needs. You can do this in the following way:

const sigV4RequestInterceptor = require('@aerofer/sdk-interceptors/request/sigv4');

const applyInterceptors = require('@aerofer/sdk-interceptors/apply');

const interceptors = { applyInterceptors, sigV4RequestInterceptor };

const api = require('@aerofer/sdk-core')(spec, { interceptors });

AWS SigV4

A request interceptor, producing SigV4 signatures for authenticating against AWS Services and API Gateway. Provided any valid Credentials object along with a specific region and service (defaults to execute-api), this interceptor will sign the outbound request that it is applied to.

const AWS = require('aws-sdk');

const sdk = require('@myco/sdk');

const credentials = () => new Promise((resolve, reject) => {
  new AWS.CredentialProviderChain().resolve((chainErr, creds) => {
    if (chainErr) {
      reject(chainErr);
    } else {
      creds.refresh((refreshErr) => {
        if (refreshErr) {
          reject(refreshErr);
        } else {
          resolve(creds);
        }
      });
    }
  });
});

const execute = async () => {
  const creds = await credentials();

  const requestInterceptor = sdk.interceptors.applyInterceptors(
    sdk.interceptors.sigV4RequestInterceptor(creds, 'eu-west-1')
  );

  const data = await sdk.tags.someOperation({}, { requestInterceptor });
}