Package detail

mappersmith

tulios1.6mMIT2.45.0

It is a lightweight rest client for node.js and the browser

rest, client, rest-client, isomorphic

readme

npm version Node.js CI Windows Tests

Mappersmith

Mappersmith is a lightweight rest client for node.js and the browser. It creates a client for your API, gathering all configurations into a single place, freeing your code from HTTP configurations.

Table of Contents

Installation

npm install mappersmith --save

or

yarn add mappersmith

Build from the source

Install the dependencies

yarn

Build

yarn build
yarn release # for minified version

Usage

To create a client for your API you will need to provide a simple manifest. If your API reside in the same domain as your app you can skip the host configuration. Each resource has a name and a list of methods with its definitions, like:

import forge, { configs } from "mappersmith"
import { Fetch } from "mappersmith/gateway/fetch"

configs.gateway = Fetch;

const github = forge({
  clientId: "github",
  host: "https://www.githubstatus.com",
  resources: {
    Status: {
      current: { path: "/api/v2/status.json" },
      summary: { path: "/api/v2/summary.json" },
      components: { path: "/api/v2/components.json" },
    },
  },
});

github.Status.current().then((response) => {
  console.log(`summary`, response.data());
});

Commonjs

If you are using commonjs, your require should look like:

const forge = require("mappersmith").default;
const { configs } = require("mappersmith");
const FetchGateway = require("mappersmith/gateway/fetch").default;

Configuring my resources

Each resource has a name and a list of methods with its definitions. A method definition can have host, path, method, headers, params, bodyAttr, headersAttr and authAttr. Example:

const client = forge({
  resources: {
    User: {
      all: { path: '/users' },

      // {id} is a dynamic segment and will be replaced by the parameter "id"
      // when called
      byId: { path: '/users/{id}' },

      // {group} is also a dynamic segment but it has default value "general"
      byGroup: { path: '/users/groups/{group}', params: { group: 'general' } },

      // {market?} is an optional dynamic segment. If called without a value
      // for the "market" parameter, {market?} will be removed from the path
      // including any prefixing "/".
      // This example: '/{market?}/users' => '/users'
      count: { path: '/{market?}/users' } }
    },
    Blog: {
      // The HTTP method can be configured through the `method` key, and a default
      // header "X-Special-Header" has been configured for this resource
      create: { method: 'post', path: '/blogs', headers: { 'X-Special-Header': 'value' } },

      // There are no restrictions for dynamic segments and HTTP methods
      addComment: { method: 'put', path: '/blogs/{id}/comment' },

      // `queryParamAlias` will map parameter names to their alias when
      // constructing the query string
      bySubject: { path: '/blogs', queryParamAlias: { subjectId: 'subject_id' } },

      // `path` is a function to map passed params to a custom path
      byDate: { path: ({date}) => `${date.getYear()}/${date.getMonth()}/${date.getDate()}` }
    }
  }
})

Parameters

If your method doesn't require any parameter, you can just call it without them:

client.User
  .all() // https://my.api.com/users
  .then((response) => console.log(response.data()))
  .catch((response) => console.error(response.data()))

Every parameter that doesn't match a pattern {parameter-name} in path will be sent as part of the query string:

client.User.all({ active: true }) // https://my.api.com/users?active=true

When a method requires a parameters and the method is called without it, Mappersmith will raise an error:

client.User.byId(/* missing id */)
// throw '[Mappersmith] required parameter missing (id), "/users/{id}" cannot be resolved'

You can optionally set parameterEncoder: yourEncodingFunction to change the default encoding function for parameters. This is useful when you are calling an endpoint which for example requires not encoded characters like : that are otherwise encoded by the default behaviour of the encodeURIComponent function (external documentation).

const client = forge({
  host: 'https://custom-host.com',
  parameterEncoder: yourEncodingFunction,
  resources: { ... }
})

Default Parameters

It is possible to configure default parameters for your resources, just use the key params in the definition. It will replace params in the URL or include query strings.

If we call client.User.byGroup without any params it will default group to "general"

client.User.byGroup() // https://my.api.com/users/groups/general

And, of course, we can override the defaults:

client.User.byGroup({ group: 'cool' }) // https://my.api.com/users/groups/cool

Renaming query parameters

Sometimes the expected format of your query parameters doesn't match that of your codebase. For example, maybe you're using camelCase in your code but the API you are calling expects snake_case. In that case, set queryParamAlias in the definition to an object that describes a mapping between your input parameter and the desired output format.

This mapping will not be applied to params in the URL.

client.Blog.all({ subjectId: 10 }) // https://my.api.com/blogs?subject_id=10

Body

To send values in the request body (usually for POST, PUT or PATCH methods) you will use the special parameter body:

client.Blog.create({
  body: {
    title: 'Title',
    tags: ['party', 'launch']
  }
})

By default, it will create a urlencoded version of the object (title=Title&tags[]=party&tags[]=launch). If the body used is not an object it will use the original value. If body is not possible as a special parameter for your API you can configure it through the param bodyAttr:

// ...
{
  create: { method: 'post', path: '/blogs', bodyAttr: 'payload' }
}
// ...

client.Blog.create({
  payload: {
    title: 'Title',
    tags: ['party', 'launch']
  }
})

NOTE: It's possible to post body as JSON, check the EncodeJsonMiddleware below for more information NOTE: The bodyAttr param can be set at manifest level.

Headers

To define headers in the method call use the parameter headers:

client.User.all({ headers: { Authorization: 'token 1d1435k' } })

If headers is not possible as a special parameter for your API you can configure it through the param headersAttr:

// ...
{
  all: { path: '/users', headersAttr: 'h' }
}
// ...

client.User.all({ h: { Authorization: 'token 1d1435k' } })

NOTE: The headersAttr param can be set at manifest level.

Basic auth

To define credentials for basic auth use the parameter auth:

client.User.all({ auth: { username: 'bob', password: 'bob' } })

The available attributes are: username and password. This will set an Authorization header. This can still be overridden by custom headers.

If auth is not possible as a special parameter for your API you can configure it through the param authAttr:

// ...
{
  all: { path: '/users', authAttr: 'secret' }
}
// ...

client.User.all({ secret: { username: 'bob', password: 'bob' } })

NOTE: A default basic auth can be configured with the use of the BasicAuthMiddleware, check the middleware section below for more information. NOTE: The authAttr param can be set at manifest level.

Timeout

To define the number of milliseconds before the request times out use the parameter timeout:

client.User.all({ timeout: 1000 })

If timeout is not possible as a special parameter for your API you can configure it through the param timeoutAttr:

// ...
{
  all: { path: '/users', timeoutAttr: 'maxWait' }
}
// ...

client.User.all({ maxWait: 500 })

NOTE: A default timeout can be configured with the use of the TimeoutMiddleware, check the middleware section below for more information. NOTE: The timeoutAttr param can be set at manifest level.

Abort Signal

The AbortSignal interface represents a signal object that allows you to communicate with an asynchronous operation (such as a fetch request) and abort it if required via an AbortController object. All gateway APIs (Fetch, HTTP and XHR) support this interface via the signal parameter:

const abortController = new AbortController()
client.User.all({ signal: abortController.signal })
// abort!
abortController.abort()

If signal is not possible as a special parameter for your API you can configure it through the param signalAttr:

// ...
{
  all: { path: '/users', signalAttr: 'abortSignal' }
}
// ...

const abortController = new AbortController()
client.User.all({ abortSignal: abortController.signal })
// abort!
abortController.abort()

NOTE: The signalAttr param can be set at manifest level.

Alternative host

There are some cases where a resource method resides in another host, in those cases you can use the host key to configure a new host:

// ...
{
  all: { path: '/users', host: 'http://old-api.com' }
}
// ...

client.User.all() // http://old-api.com/users

In case you need to overwrite the host for a specific call, you can do so through the param host:

// ...
{
  all: { path: '/users', host: 'http://old-api.com' }
}
// ...

client.User.all({ host: 'http://very-old-api.com' }) // http://very-old-api.com/users

If host is not possible as a special parameter for your API, you can configure it through the param hostAttr:

// ...
{
  all: { path: '/users', hostAttr: 'baseUrl' }
}
// ...

client.User.all({ baseUrl: 'http://very-old-api.com' }) // http://very-old-api.com/users

NOTE: Since version 2.34.0 you need to also use allowResourceHostOverride: true, example:

const client = forge({
  host: 'https://new-host.com',
  allowResourceHostOverride: true,
  resources: {
    User: {
      all: { path: '/users', host: 'https://old-host.com }
    }
  }
})

Whenever using host overrides, be diligent about how you pass parameters to your resource methods. If you spread unverified attributes, you might open your server to SSR attacks.

Alternative path

In case you need to overwrite the path for a specific call, you can do so through the param path:

// ...
{
  all: { path: '/users' }
}
// ...

client.User.all({ path: '/people' })

If path is not possible as a special parameter for your API, you can configure it through the param pathAttr:

// ...
{
  all: { path: '/users', pathAttr: '__path' }
}
// ...

client.User.all({ __path: '/people' })

Binary data

If the data being fetched is in binary form, such as a PDF, you may add the binary key, and set it to true. The response data will then be a Buffer in NodeJS, and a Blob in the browser.


// ...
{
  report: { path: '/report.pdf', binary: true }
}
// ...

Promises

Mappersmith does not apply any polyfills, it depends on a native Promise implementation to be supported. If your environment doesn't support Promises, please apply the polyfill first. One option can be then/promises

In some cases it is not possible to use/assign the global Promise constant, for those cases you can define the promise implementation used by Mappersmith.

For example, using the project rsvp.js (a tiny implementation of Promises/A+):

import RSVP from 'rsvp'
import { configs } from 'mappersmith'

configs.Promise = RSVP.Promise

All Promise references in Mappersmith use configs.Promise. The default value is the global Promise.

Response object

Mappersmith will provide an instance of its own Response object to the promises. This object has the methods:

  • request() - Returns the original Request
  • status() - Returns the status number
  • success() - Returns true for status greater than 200 and lower than 400
  • headers() - Returns an object with all headers, keys in lower case
  • header(name) - Returns the value of the header
  • data() - Returns the response data, if Content-Type is application/json it parses the response and returns an object
  • error() - Returns the last error instance that caused the request to fail or null

Middleware

The behavior between your client and the API can be customized with middleware. A middleware is a function which returns an object with two methods: request and response.

Creating middleware

The prepareRequest method receives a function which returns a Promise resolving the Request. This function must return a Promise resolving the request. The method enhance can be used to generate a new request based on the previous one.

const MyMiddleware = () => ({
  prepareRequest(next) {
    return next().then(request => request.enhance({
      headers: { 'x-special-request': '->' }
    }))
  }
})

If you have multiple middleware it is possible to pass information from an earlier ran middleware to a later one via the request context:

const MyMiddlewareOne = () => ({
  async prepareRequest(next) {
    const request = await next().then(request => request.enhance({}, { message: 'hello from mw1' }))
  }
})

const MyMiddlewareTwo = () => ({
  async prepareRequest(next) {
    const request = await next()
    const { message } = request.getContext()
    // Logs: "hello from mw1"
    console.log(message)
    return request
  }
})

The above example assumes you synthesized your middleware in this order when calling forge: middleware: [MyMiddlewareOne, MyMiddlewareTwo]

The response method receives a function which returns a Promise resolving the Response. This function must return a Promise resolving the Response. The method enhance can be used to generate a new response based on the previous one.

const MyMiddleware = () => ({
  response(next) {
    return next().then((response) => response.enhance({
      headers: { 'x-special-response': '<-' }
    }))
  }
})

Context (deprecated)

⚠️ setContext is not safe for concurrent use, and shouldn't be used!

Why is it not safe? Basically, the setContext function mutates a global state (see here), hence it is the last call to setContext that decides its global value. Which leads to a race condition when handling concurrent requests.

Optional arguments

It can, optionally, receive resourceName, resourceMethod, #context, clientId and mockRequest. Example:

const MyMiddleware = ({ resourceName, resourceMethod, context, clientId, mockRequest }) => ({
  /* ... */
})

client.User.all()
// resourceName: 'User'
// resourceMethod: 'all'
// clientId: 'myClient'
// context: {}
// mockRequest: false
mockRequest

Before mocked clients can assert whether or not their mock definition matches a request they have to execute their middleware on that request. This means that middleware might be executed multiple times for the same request. More specifically, the middleware will be executed once per mocked client that utilises the middleware until a mocked client with a matching definition is found. If you want to avoid middleware from being called multiple times you can use the optional "mockRequest" boolean flag. The value of this flag will be truthy whenever the middleware is being executed during the mock definition matching phase. Otherwise its value will be falsy. Example:

const MyMiddleware = ({ mockRequest }) => {
  prepareRequest(next) {
    if (mockRequest) {
      ... // executed once for each mocked client that utilises the middleware
    }
    if (!mockRequest) {
      ... // executed once for the matching mock definition
    }
    return next().then(request => request)
  }
}
Abort

The prepareRequest phase can optionally receive a function called "abort". This function can be used to abort the middleware execution early-on and throw a custom error to the user. Example:

const MyMiddleware = () => {
  prepareRequest(next, abort) {
    return next().then(request =>
      request.header('x-special')
        ? response
        : abort(new Error('"x-special" must be set!'))
    )
  }
}
Renew

The response phase can optionally receive a function called "renew". This function can be used to rerun the middleware stack. This feature is useful in some scenarios, for example, automatically refreshing an expired access token. Example:

const AccessTokenMiddleware = () => {
  // maybe this is stored elsewhere, here for simplicity
  let accessToken = null

  return () => ({
    request(request) {
      return Promise
        .resolve(accessToken)
        .then((token) => token || fetchAccessToken())
        .then((token) => {
          accessToken = token
          return request.enhance({
            headers: { 'Authorization': `Token ${token}` }
          })
        })
    },
    response(next, renew) {
      return next().catch(response => {
        if (response.status() === 401) { // token expired
          accessToken = null
          return renew()
        }

        return next()
      })
    }
  })
}

Then:

const AccessToken = AccessTokenMiddleware()
const client = forge({
  // ...
  middleware: [ AccessToken ],
  // ...
})

"renew" can only be invoked sometimes before it's considered an infinite loop, make sure your middleware can distinguish an error from a "renew". By default, mappersmith will allow 2 calls to "renew". This can be configured with configs.maxMiddlewareStackExecutionAllowed. It's advised to keep this number low. Example:

import { configs } from 'mappersmith'
configs.maxMiddlewareStackExecutionAllowed = 3

If an infinite loop is detected, mappersmith will throw an error.

request

The response phase can optionally receive an argument called "request". This argument is the final request (after the whole middleware chain has prepared and all prepareRequest been executed). This is useful in some scenarios, for example when you want to get access to the request without invoking next:

const CircuitBreakerMiddleware = () => {
  return () => ({
    response(next, renew, request) {
      // Creating the breaker required some information available only on `request`:
      const breaker = createBreaker({ ..., timeout: request.timeout })
      // Note: `next` is still wrapped:
      return breaker.invoke(createExecutor(next))
    }
  })
}

Configuring middleware

Middleware scope can be Global, Client or on Resource level. The order will be applied in this order: Resource level applies first, then Client level, and finally Global level. The subsections below describes the differences and how to use them correctly.

Resource level middleware

Resource middleware are configured using the key middleware in the resource level of manifest, example:

const client = forge({
  clientId: 'myClient',
  resources: {
    User: {
      all: {
        // only the `all` resource will include MyMiddleware:
        middleware: [ MyMiddleware ],
        path: '/users'
      }
    }
  }
})

Client level middleware

Client middleware are configured using the key middleware in the root level of manifest, example:

const client = forge({
  clientId: 'myClient',
  // all resources in this client will include MyMiddleware:
  middleware: [ MyMiddleware ],
  resources: {
    User: {
      all: { path: '/users' }
    }
  }
})

Global middleware

Global middleware are configured on a config level, and all new clients will automatically include the defined middleware, example:

import { forge, configs } from 'mappersmith'

configs.middleware = [MyMiddleware]
// all clients defined from now on will include MyMiddleware
  • Global middleware can be disabled for specific clients with the option ignoreGlobalMiddleware, e.g:
forge({
  ignoreGlobalMiddleware: true,
  // + the usual configurations
})

Built-in middleware

BasicAuth

Automatically configure your requests with basic auth

import { BasicAuthMiddleware } from 'mappersmith/middleware'
const BasicAuth = BasicAuthMiddleware({ username: 'bob', password: 'bob' })

const client = forge({
  middleware: [ BasicAuth ],
  /* ... */
})

client.User.all()
// => header: "Authorization: Basic Ym9iOmJvYg=="

** The default auth can be overridden with the explicit use of the auth parameter, example:

client.User.all({ auth: { username: 'bill', password: 'bill' } })
// auth will be { username: 'bill', password: 'bill' } instead of { username: 'bob', password: 'bob' }

CSRF

Automatically configure your requests by adding a header with the value of a cookie - If it exists. The name of the cookie (defaults to "csrfToken") and the header (defaults to "x-csrf-token") can be set as following;

import { CsrfMiddleware } from 'mappersmith/middleware'

const client = forge({
  middleware: [ CsrfMiddleware('csrfToken', 'x-csrf-token') ],
  /* ... */
})

client.User.all()

Duration

Automatically adds X-Started-At, X-Ended-At and X-Duration headers to the response.

import { DurationMiddleware } from 'mappersmith/middleware'

const client = forge({
  middleware: [ DurationMiddleware ],
  /* ... */
})

client.User.all({ body: { name: 'bob' } })
// => headers: "X-Started-At=1492529128453;X-Ended-At=1492529128473;X-Duration=20"

EncodeJson

Automatically encode your objects into JSON

import { EncodeJsonMiddleware } from 'mappersmith/middleware'

const client = forge({
  middleware: [ EncodeJsonMiddleware ],
  /* ... */
})

client.User.all({ body: { name: 'bob' } })
// => body: {"name":"bob"}
// => header: "Content-Type=application/json;charset=utf-8"

GlobalErrorHandler

Provides a catch-all function for all requests. If the catch-all function returns true it prevents the original promise to continue.

import { GlobalErrorHandlerMiddleware, setErrorHandler } from 'mappersmith/middleware'

setErrorHandler((response) => {
  console.log('global error handler')
  return response.status() === 500
})

const client = forge({
  middleware: [ GlobalErrorHandlerMiddleware ],
  /* ... */
})

client.User
  .all()
  .catch((response) => console.error('my error'))

// If status != 500
// output:
//   -> global error handler
//   -> my error

// IF status == 500
// output:
//   -> global error handler

Log

Log all requests and responses. Might be useful in development mode.

import { LogMiddleware } from 'mappersmith/middleware'

const client = forge({
  middleware: [ LogMiddleware ],
  /* ... */
})

Retry

This middleware will automatically retry GET requests up to the configured amount of retries using a randomization function that grows exponentially. The retry count and the time used will be included as a header in the response. By default on requests with response statuses >= 500 will be retried.

It's possible to configure the header names and parameters used in the calculation by providing a configuration object when creating the middleware.

If no configuration is passed when creating the middleware then the defaults will be used.

import { RetryMiddleware } from 'mappersmith/middleware'

const retryConfigs = {
  headerRetryCount: 'X-Mappersmith-Retry-Count',
  headerRetryTime: 'X-Mappersmith-Retry-Time',
  maxRetryTimeInSecs: 5,
  initialRetryTimeInSecs: 0.1,
  factor: 0.2, // randomization factor
  multiplier: 2, // exponential factor
  retries: 5, // max retries
  validateRetry: (response) => response.responseStatus >= 500 // a function that returns true if the request should be retried
}

const client = forge({
  middleware: [ Retry(retryConfigs) ],
  /* ... */
})

Timeout

Automatically configure your requests with a default timeout

import { TimeoutMiddleware } from 'mappersmith/middleware'
const Timeout = TimeoutMiddleware(500)

const client = forge({
  middleware: [ Timeout ],
  /* ... */
})

client.User.all()

** The default timeout can be overridden with the explicit use of the timeout parameter, example:

client.User.all({ timeout: 100 })
// timeout will be 100 instead of 500

Middleware legacy notes

This section is only relevant for mappersmith versions older than but not including 2.27.0, when the method prepareRequest did not exist. This section describes how to create a middleware using older versions.

Since version 2.27.0 a new method was introduced: prepareRequest. This method aims to replace the request method in future versions of mappersmith, it has a similar signature as the response method and it is always async. All previous middleware are backward compatible, the default implementation of prepareRequest will call the request method if it exists.

The request method receives an instance of the Request object and it must return a Request. The method enhance can be used to generate a new request based on the previous one.

Example:

const MyMiddleware = () => ({
  request(request) {
    return request.enhance({
      headers: { 'x-special-request': '->' }
    })
  },

  response(next) {
    return next().then((response) => response.enhance({
      headers: { 'x-special-response': '<-' }
    }))
  }
})

The request phase can be asynchronous, just return a promise resolving a request. Example:

const MyMiddleware = () => ({
  request(request) {
    return Promise.resolve(
      request.enhance({
        headers: { 'x-special-token': 'abc123' }
      })
    )
  }
})

Testing Mappersmith

Mappersmith plays nice with all test frameworks, the generated client is a plain javascript object and all the methods can be mocked without any problem. However, this experience can be greatly improved with the test library.

The test library has 4 utilities: install, uninstall, mockClient and mockRequest

install and uninstall

They are used to setup the test library, example using jasmine:

import { install, uninstall } from 'mappersmith/test'

describe('Feature', () => {
  beforeEach(() => install())
  afterEach(() => uninstall())
})

mockClient

mockClient offers a high level abstraction, it works directly on your client mocking the resources and their methods.

It accepts the methods:

  • resource(resourceName), ex: resource('Users')
  • method(resourceMethodName), ex: method('byId')
  • with(resourceMethodArguments), ex: with({ id: 1 })
  • status(statusNumber | statusHandler), ex: status(204) or status((request, mock) => 200)
  • headers(responseHeaders), ex: headers({ 'x-header': 'value' })
  • response(responseData | responseHandler), ex: response({ user: { id: 1 } }) or response((request, mock) => ({ user: { id: request.body().id } }))
  • assertObject()
  • assertObjectAsync()

Example using jasmine:

import { forge } from 'mappersmith'
import { install, uninstall, mockClient } from 'mappersmith/test'

describe('Feature', () => {
  beforeEach(() => install())
  afterEach(() => uninstall())

  it('works', (done) => {
    const myManifest = {} // Let's assume I have my manifest here
    const client = forge(myManifest)

    mockClient(client)
      .resource('User')
      .method('all')
      .response({ allUsers: [{id: 1}] })

    // now if I call my resource method, it should return my mock response
    client.User
      .all()
      .then((response) => expect(response.data()).toEqual({ allUsers: [{id: 1}] }))
      .then(done)
  })
})

To mock a failure just use the correct HTTP status, example:

// ...
mockClient(client)
  .resource('User')
  .method('byId')
  .with({ id: 'ABC' })
  .status(422)
  .response({ error: 'invalid ID' })
// ...

The method with accepts the body and headers attributes, example:

// ...
mockClient(client)
  .with({
    id: 'abc',
    headers: { 'x-special': 'value'},
    body: { payload: 1 }
  })
  // ...

It's possible to use a match function to assert params and body, example:

import { m } from 'mappersmith/test'

mockClient(client)
  .with({
    id: 'abc',
    name: m.stringContaining('john'),
    headers: { 'x-special': 'value'},
    body: m.stringMatching(/token=[^&]+&other=true$/)
  })

The assert object can be used to retrieve the requests that went through the created mock, example:

const mock = mockClient(client)
  .resource('User')
  .method('all')
  .response({ allUsers: [{id: 1}] })
  .assertObject()

console.log(mock.mostRecentCall())
console.log(mock.callsCount())
console.log(mock.calls())

The mock object is an instance of MockAssert and exposes three methods:

  • calls(): returns a Request array;
  • mostRecentCall(): returns the last Request made. Returns null if array is empty.
  • callsCount(): returns the number of requests that were made through the mocked client;

Note: The assert object will also be returned in the mockRequest function call.

If you have a middleware with an async request phase use assertObjectAsync to await for the middleware execution, example:

const mock = await mockClient(client)
  .resource('User')
  .method('all')
  .response({ allUsers: [{id: 1}] })
  .assertObjectAsync()

console.log(mock.mostRecentCall())
console.log(mock.callsCount())
console.log(mock.calls())

response and status can accept functions to generate response body or status. This can be useful when you want to return different responses for the same request being made several times.

const generateResponse = () => {
  return (request, mock) => mock.callsCount() === 0
    ? {}
    : { user: { id: 1 } }
}

const mock = mockClient(client)
  .resource('User')
  .method('create')
  .response(generateResponse())

mockRequest

mockRequest offers a low level abstraction, very useful for automations.

It accepts the params: method, url, body and response

It returns an assert object

Example using jasmine:

import { forge } from 'mappersmith'
import { install, uninstall, mockRequest } from 'mappersmith/test'

describe('Feature', () => {
  beforeEach(() => install())
  afterEach(() => uninstall())

  it('works', (done) => {
    mockRequest({
      method: 'get',
      url: 'https://my.api.com/users?someParam=true',
      response: {
        body: { allUsers: [{id: 1}] }
      }
    })

    const myManifest = {} // Let's assume I have my manifest here
    const client = forge(myManifest)

    client.User
      .all()
      .then((response) => expect(response.data()).toEqual({ allUsers: [{id: 1}] }))
      .then(done)
  })
})

A more complete example:

// ...
mockRequest({
  method: 'post',
  url: 'http://example.org/blogs',
  body: 'param1=A&param2=B', // request body
  response: {
    status: 503,
    body: { error: true },
    headers: { 'x-header': 'nope' }
  }
})
// ...

It's possible to use a match function to assert the body and the URL, example:

import { m } from 'mappersmith/test'

mockRequest({
  method: 'post',
  url: m.stringMatching(/example\.org/),
  body: m.anything(),
  response: {
    body: { allUsers: [{id: 1}] }
  }
})

Using the assert object:

const mock = mockRequest({
  method: 'get',
  url: 'https://my.api.com/users?someParam=true',
  response: {
    body: { allUsers: [{id: 1}] }
  }
})

console.log(mock.mostRecentCall())
console.log(mock.callsCount())
console.log(mock.calls())

Match functions

mockClient and mockRequest accept match functions, the available built-in match functions are:

import { m } from 'mappersmith/test'

m.stringMatching(/something/) // accepts a regexp
m.stringContaining('some-string') // accepts a string
m.anything()
m.uuid4()

A match function is a function which returns a boolean, example:

mockClient(client)
  .with({
    id: 'abc',
    headers: { 'x-special': 'value'},
    body: (body) => body === 'something'
  })

Note: mockClient only accepts match functions for body and params mockRequest only accepts match functions for body and url

unusedMocks

unusedMocks can be used to check if there are any unused mocks after each test. It will return count of unused mocks. It can be either unused mockRequest or mockClient.

import { install, uninstall, unusedMocks } from 'mappersmith/test'

describe('Feature', () => {
  beforeEach(() => install())
  afterEach(() => {
    const unusedMocksCount = unusedMocks()
    uninstall()
    if (unusedMocksCount > 0) {
      throw new Error(`There are ${unusedMocksCount} unused mocks`) // fail the test
    }
  })
})

Gateways

Mappersmith has a pluggable transport layer and it includes by default three gateways: xhr, http and fetch. Mappersmith will pick the correct gateway based on the environment you are running (nodejs, service worker or the browser).

You can write your own gateway, take a look at XHR for an example. To configure, import the configs object and assign the gateway option, like:

import { configs } from 'mappersmith'
configs.gateway = MyGateway

It's possible to globally configure your gateway through the option gatewayConfigs.

HTTP

When running with node.js you can configure the configure callback to further customize the http/https module, example:

import fs from 'fs'
import https from 'https'
import { configs } from 'mappersmith'

const key = fs.readFileSync('/path/to/my-key.pem')
const cert =  fs.readFileSync('/path/to/my-cert.pem')

configs.gatewayConfigs.HTTP = {
  configure() {
    return {
      agent: new https.Agent({ key, cert })
    }
  }
}

The new configurations will be merged. configure also receives the requestParams as the first argument. Take a look here for more options.

The HTTP gatewayConfigs also provides several callback functions that will be called when various events are emitted on the request, socket, and response EventEmitters. These callbacks can be used as a hook into the event cycle to execute any custom code. For example, you may want to time how long each stage of the request or response takes. These callback functions will receive the requestParams as the first argument.

The following callbacks are supported:

  • onRequestWillStart - This callback is not based on a event emitted by Node but is called just before the request method is called.
  • onRequestSocketAssigned - Called when the 'socket' event is emitted on the request
  • onSocketLookup - Called when the lookup event is emitted on the socket
  • onSocketConnect - Called when the connect event is emitted on the socket
  • onSocketSecureConnect - Called when the secureConnect event is emitted on the socket
  • onResponseReadable - Called when the readable event is emitted on the response
  • onResponseEnd - Called when the end event is emitted on the response
let startTime

configs.gatewayConfigs.HTTP = {
  onRequestWillStart() {
    startTime = Date.now()
  }
  onResponseReadable() {
    console.log('Time to first byte', Date.now() - startTime)
  }
}

XHR

When running in the browser you can configure withCredentials and configure to further customize the XMLHttpRequest object, example:

import { configs } from 'mappersmith'
configs.gatewayConfigs.XHR = {
  withCredentials: true,
  configure(xhr) {
    xhr.ontimeout = () => console.error('timeout!')
  }
}

Take a look here for more options.

Fetch

Mappersmith does not apply any polyfills, it depends on a native fetch implementation to be supported. It is possible to assign the fetch implementation used by Mappersmith:

import { configs } from 'mappersmith'
configs.fetch = fetchFunction

Fetch is not used by default, you can configure it through configs.gateway.

import { FetchGateway } from 'mappersmith/gateway'
import { configs } from 'mappersmith'

configs.gateway = FetchGateway

// Extra configurations, if needed
configs.gatewayConfigs.Fetch = {
  credentials: 'same-origin'
}

Take a look here for more options.

TypeScript

Mappersmith also supports TypeScript (>=3.5). In the following sections there are some common examples for using TypeScript with Mappersmith where it is not too obvious how typings are properly applied.

Create a middleware with TypeScript

To create a middleware using TypeScript you just have to add the Middleware interface to your middleware object:

import type { Middleware } from 'mappersmith'

const MyMiddleware: Middleware = () => ({
  prepareRequest(next) {
    return next().then(request => request.enhance({
      headers: { 'x-special-request': '->' }
    }))
  },

  response(next) {
    return next().then(response => response.enhance({
      headers: { 'x-special-response': '<-' }
    }))
  }
})

Use mockClient with TypeScript

To use the mockClient with proper types you need to pass a typeof your client as generic to the mockClient function:

import { forge } from 'mappersmith'
import { mockClient } from 'mappersmith/test'

const github = forge({
  clientId: 'github',
  host: 'https://status.github.com',
  resources: {
    Status: {
      current: { path: '/api/status.json' },
      messages: { path: '/api/messages.json' },
      lastMessage: { path: '/api/last-message.json' },
    },
  },
})

const mock = mockClient<typeof github>(github)
  .resource('Status')
  .method('current')
  .with({ id: 'abc' })
  .response({ allUsers: [] })
  .assertObject()

console.log(mock.mostRecentCall())
console.log(mock.callsCount())
console.log(mock.calls())

Use mockRequest with Typescript

const mock = mockRequest({
  method: 'get',
  url: 'https://status.github.com/api/status.json',
  response: {
    status: 503,
    body: { error: true },
  }
})

console.log(mock.mostRecentCall())
console.log(mock.callsCount())
console.log(mock.calls())

Development

Node version

This project uses ASDF to manage the node version used via .tool-versions.

Running unit tests:

yarn test:browser
yarn test:node

Running integration tests:

yarn integration-server &
yarn test:browser:integration
yarn test:node:integration

Running all tests

yarn test

Package and release

Package project only

Useful for testing a branch against local projects. Run the build step, and yarn link to the dist/ folder:

yarn publish:prepare

In remote project:

yarn link ../mappersmith/dist

Release

  1. Create a release branch, e.g. git checkout -b release/2.43.0
  2. Update package version and generate an updated CHANGELOG.md:
yarn changeset version
yarn copy:version:src
  1. Merge the PR.
  2. From master: pull the latest changes, and build the dist/ folder which will be published to npm:
yarn publish:prepare
  1. Verify the release works. If you are using npm pack to create a local tarball, delete this file after the verification has been done.
  2. Finally, publish the contents of dist/ folder to npm:
cd dist/
rm *.tgz # do not accidentally publish any tarball
npm publish
  1. Tag the release and push the tags.
git tag 2.43.0
git push --tags

Linting

This project uses prettier and eslint, it is recommended to install extensions in your editor to format on save.

Contributors

Check it out!

https://github.com/tulios/mappersmith/graphs/contributors

License

See LICENSE for more details.

changelog

Changelog

2.45.0

Minor Changes

  • 624c15d: Switch to using default imports for libraries imported in gateway

2.44.0

Minor Changes

  • 3a3c092: # Add support for abort signals

    The AbortSignal interface represents a signal object that allows you to communicate with an asynchronous operation (such as a fetch request) and abort it if required via an AbortController object. All gateway APIs (Fetch, HTTP and XHR) support this interface via the signal parameter:

    const abortController = new AbortController()
    // Start a long running task...
    client.Bitcoin.mine({ signal: abortController.signal })
    // This takes too long, abort!
    abortController.abort()

    Minor type fixes

    The return value of some functions on Request have been updated to highlight that they might return undefined:

    • Request#body()
    • Request#auth()
    • Request#timeout()

    The reasoning behind this change is that if you didn't pass them (and no middleware set them) they might simply be undefined. So the types were simply wrong before. If you experience a "breaking change" due to this change, then it means you have a potential bug that you didn't properly handle before.

2.43.4

Patch Changes

  • 7356e30: Expose deep imports for backwards compatibility

2.43.3

Patch Changes

  • 404f7ba: Exporting client-builder

2.43.2

Patch Changes

  • 0b832ab: Fix ESM build to output correct code as per ESM spec

2.43.1

Patch Changes

  • 0177911: Fixes middlewares import path

2.43.0

Minor Changes

  • 6e94f97: Bundle with ESM exports.

    • The recommended way to use mappersmith in ESM projects is to do import { forge } from 'mappersmith' rather than the old import forge from 'mappersmith'. The reason is because test runners like jest and vitest might get confused when mixing named/default exports together with ESM, even though tsc and node has no problems with it.
    • A similar recommendation change goes for importing middleware: do import { EncodeJsonMiddleware } from 'mappersmith/middleware' (note the mappersmith/middleware folder) rather than deep import import EncodeJsonMiddleware from 'mappersmith/middleware/encode-json'. We still support the old import, but it will be deprecated in the future.
    • The same recommendation goes for importing gateway: do import { FetchGateway } from 'mappersmith/gateway' (note the mappersmith/gateway folder) rather than deep import import FetchGateway from 'mappersmith/gateway/fetch'. We still support the old import, but it will be deprecated in the future.
  • 9da82f6: Fixes memory leak when using http(s) agent with keep-alive

Patch Changes

  • e01114f: Fixed missing type declaration for unusedMocks

2.42.0

Minor Changes

  • mappersmith: EncodeJSON middleware now properly serializes +json family of content-types #362

2.41.0

Fixed:

  • mappersmith: Allow path to be empty string in manifest #327
  • mappersmith: Response.errors should only be allowed to contain Error or string #325

Added:

  • mappersmith: Accept path as a resource method param #328
  • mappersmith: A forged client now optionally accepts request context as its second argument #330
  • mappersmith: Add support for accessing the final request object in middleware response phase #321

2.40.0

Fixed:

  • mappersmith/test: Properly merge headers when one of the sides is a mock matcher #316

Added:

  • mappersmith: Add support for passing transient contexts between middleware #320

2.39.1

Fixed:

  • mappersmith: Typo in basic auth import no longer causes build errors #313

2.39.0

Added:

  • mappersmith: Added service workers support with default gateway fetch #311

Refactored:

  • mappersmith: Migrated middleware to typescript #306

2.38.1

Fixed:

  • mappersmith: Preserve rawData as empty string instead of converting it to null #297
  • mappersmith/test: Allow Buffer (and similar) as valid response data for mockRequest #299
  • mappersmith: Ensure references to regeneratorRuntime is not part of compiled bundle #303

2.38.0

Added:

  • mappersmith/test: mockClient responses are now clones of the fixture instead of references to them #158
  • mappersmith/*: Move typings into src folder next to each file it describes #291
  • mappersmith: Convert Gateway to typescript #287
  • mappersmith: Add the possibility to update the default encoding function for query params #296

2.37.1

Fixed:

  • (internal) Fixed bad release folder of 2.37.0

2.37.0

Fixed:

  • mappersmith/test: Fix bug in mockRequest/mockClient where body params did not match independent of order #268

Added:

  • mappersmith: Make Response accept a generic type that specifies the form of the data returned #265
  • mappersmith/test: Add responseFactory and requestFactory helpers to mappersmith/test #265
  • mappersmith/test: If body is not provided to a mock, it will match on any body. #229 #152

2.36.5

Fixed:

  • (internal) Fixed bad release folder of 2.36.4

2.36.4

  • mappersmith: Fix unintended new query string behaviour introduced in 2.36 #281

2.36.3

Fixed:

  • Fix missing retry-v2 typings in index.d.ts #261

Refactored:

  • Migrated ClientBuilder to typescript #259

2.36.1

Fixed:

  • Fix broken typings in index.d.ts #257

2.36.0

Added:

  • Add option parameterEncoder which can optionally be used to override the encoding function for request params. Default is encodeURIComponent #251

Fixed:

  • Fix x-started-at header getting set to new Date.now() during testing and failing header match #248
  • Fix Request.pathTemplate to return result of the function instead of the function itself #249

Refactored:

  • Migrated Manifest to typescript #254
  • Migrated MethodDescriptor to typescript #245
  • Migrated Request to typescript #245
  • Migrated Response to typescript #249

2.35.0

Fixed:

  • Respect allowResourceHostOverride configuration in middlewares #240
  • A successful middleware should no longer overwriting a previous middleware's error #230

Added:

  • mappersmith: Request.pathTemplate - Returns the template path, without params, before interpolation #194
  • mappersmith/test: unusedMocks - get count of unused mocks #227
  • The +json family of MIME types are parsed as json #223
  • Add headers to mock matching strategy #168

Deprecated:

  • mappersmith: setContext - this is not safe for concurrent use #239

2.34.0

  • Add json-encode middleware export CONTENT_TYPE_JSON to type definition #203
  • Only accept host overrides if allowResourceHostOverride=true #204

2.33.3

  • Fix GatewayConfiguration typings

2.33.2

  • Add enableHTTP408OnTimeouts to HTTPGatewayConfiguration typings

2.33.1

  • Use latest request in response mock #191

2.33.0

  • Add support for (Typescript) boolean as parameters #185
  • Update typings for HTTPGateway configurations #182
  • Accept a function as MethodDescriptor.path #186

2.32.1

  • Bugfix: Only use param matchers on the attributes assigned to them #181

2.32.0

  • New option on the HTTP gateway (useSocketConnectionTimeout) to include DNS and Socket connection on the timeout #179

2.31.2

  • Bugfix: Preserve timeElapsed for response.enhance #178

2.31.1

  • Ignore null or undefined values for dynamic segments #175

2.31.0

  • Bugfix: Fetch gateway not using the right configs #161
  • Bugfix: Regexp injection vulnerability #171
  • Lazy-match body to not trigger body-matching callback unnecessarily #163
  • Allow not sending a config in the Retry middleware #164
  • Fix mockRequest.url type #166
  • Fix MockClient types #170
  • Add support for optional path parameters #171
  • Replace multiple instances of same path parameter #174

2.30.1

  • Remove type GlobalFetch from FetchGateway #155

2.30.0

  • Add ignoreGlobalMiddleware to typescript type definitions #148
  • Update EncodeJson middleware to not override pre-existing content-type #149
  • Accept host as a resource method param #151

2.29.3

  • Bugfix: Encode dynamic section params #141

2.29.2

  • Add clear and m TypeScript definitions

2.29.1

  • Add mockRequest TypeScript definitions

2.29.0

  • Ability to define middleware on resource level #134
  • Add TypeScript definitions #136

2.28.0

  • Add socket and response callbacks to the HTTP Gateway to allow for timing request stages #127
  • Allow use of mockRequest and mockClient with params/body that are independent of order #121

2.27.2

  • Bugfix: Fix "ReferenceError: regeneratorRuntime is not defined" when importing "mappersmith/test" #131

2.27.1

  • Bugfix: mockRequest would attempt to run the old request phase without considering async definitions #130

2.27.0

  • Add prepareRequest phase to middleware #129

2.26.1

  • Bugfix: Extra query string when path already contains query string #128

2.26.0

  • Add async middleware requests support to mocked clients #122

2.25.1

  • Allow setting of some resource configs at manifest level #114
  • Bugfix: Reject the promise when using the retry middleware and another middleware on the stack throws an error #118

2.25.0

  • Returns HTTP 408 (instead of 400) when request times out #88

2.24.1

  • Bugfix: Plain object response data is not stringified on subsequent mock requests #107

2.24.0

  • Add response callback support to mockClient #91
  • Add status callback support to mockClient #105

2.23.0

  • Add a way to rename queryparams (queryParamAlias) #102

2.22.2

  • Cache some RegExp to improve overall performance

2.22.1

  • Bugfix: Abort requests on timeout (http gateway)

2.22.0

  • Improve "network error" handling #99
  • Send error instance to gateway#dispatchClientError #99

2.21.0

  • Throw errors when the middleware request phase fails
    • If the request phase throws an error (e.g.: [Mappersmith] middleware "MyMiddleware" failed in the request phase: <Original error message>)
    • If the request phase returns something different than a mappersmith Request object (e.g.: [Mappersmith] middleware "MyMiddleware" should return "Request" but returned "boolean")

2.20.0

  • Allow retries on successful calls
  • Add the ability to re-run the middleware stack from the response phase (renew)
  • Add Request#header to get a single header value by name

2.19.0

  • Add support for HEAD HTTP method #90
  • Bugfix: Calculate Ended-At after evaluating next in the duration middleware

2.18.0

  • Add support to binary payloads when using fetch gateway #89

2.17.0

  • Add support to async middleware request phase #86
  • Ensures all middleware expose name #83

2.16.1

  • Bugfix: Use uppercase HTTP methods with XHR. CORS preflight requests will fail if the method name doesn't match #80

2.16.0

  • Add Retry middleware v2 which doesn't use a global retry configuration #79

2.15.1

  • Retry middleware: by default don't retry requests with statuses < 500 #78

2.15.0

  • Add option to ignore global middleware
  • Bugfix: Ignore Node.js files when bundling the client

2.14.3

  • Bugfix: fix issue when mocking clients with middleware that use context #77

2.14.2

  • Bugfix: fix IE binary request by configuring responseType after open #76
  • Enable Windows integration tests via Appveyor #75

2.14.1

  • Add current version to main module

2.14.0

  • Add clientId to help identify different clients #73
  • Add global context to help with request life cycle #73

2.13.0

  • Add a retry validation callback to the Retry Middleware #72

2.12.0

  • Renames middlewares folder to middleware, but keep importable files
  • Add globally defined middleware

2.11.2

  • Bugfix: fix CSRF middleware cookie parser #69
  • Add uuid4 matcher to the test library

2.11.1

This version was removed from NPM because the test library wasn't included to the final bundle

2.11.0

This version was removed from NPM because the test library wasn't included to the final bundle

2.10.0

  • Adds CSRF middleware #68

2.9.2

  • bugfix: Fix param matchers for mockClient

2.9.1

  • bugfix: Auth mask was mutating the auth config #65

2.9.0

  • Add support to binary payloads #64
  • Switch tests to chrome-headless #63

2.8.0

  • Mask auth password in the Response object #60

2.7.0

  • Scope gatewayConfigs to allow different instances of mappersmith clients to use different configurations

2.6.1

  • bugfix: HTTP Gateway was calculating content-length wrongly #59

2.6.0

  • Remove performance.now polyfill to allow the use with web workers #55

2.5.1

2.5.0

  • Accept a matcher function as an URL in mockRequest
  • Add a configure callback to the http gateway

2.4.0

  • Adds Duration middleware #50

2.3.1

  • bugfix: eval('process') causes some problems in strict mode on PhantomJS #51

2.3.0

  • Allow mockClient and mockRequest to use match functions to check body #49
  • Add match functions to the test module #49

2.2.1

  • bugfix: body, auth and timeout were always being replaced by request#enhance

2.2.0

  • Add support to basic auth for all gateways #46
  • Add BasicAuthMiddleware to configure a default basic auth #46
  • Add support to timeout for all gateways #47
  • Add TimeoutMiddleware to configure a default timeout #47

2.1.0

  • Add a retry middleware with exponential retry time #38
  • Add a new gateway backed by fetch #42
  • Add Response#header to get a single header value by name

2.0.1

  • bugfix: Send resourceName and resourceMethod when running the test lib
  • Add flag (mockRequest) when executing the middlewares from the test lib

2.0.0

  • Expose resourceName and resourceMethod to middlewares
  • Features from 2.0.0-rc1 to 2.0.0-rc7

2.0.0-rc7

  • bugfix: EncodeJSON middleware was only returning requests if the original request had a body or caused an error
  • Prevent method clear from the test lib to expose the internal store
  • Normalize responseData to be always null when not defined
  • Make mockClient independent of the response call

2.0.0-rc6

  • bugfix: MockClient should use the same middlewares configured in the client #37

2.0.0-rc5

  • bugfix: ClientBuilder isn't using the new configured gateway when config.gateway changes #36

2.0.0-rc4

  • Fix regression introduced in rc3, disable gateway http when transpiling for browser

2.0.0-rc3

  • Publish only lib to NPM

2.0.0-rc2

  • Add content-length only for gateway http #35
  • Eval process to avoid webpack polyfills

2.0.0-rc1

  • New API
  • New test library
  • Middlewares

0.13.4

  • Add yarn.lock
  • bugfix: duplicated content-type on vanilla gateway #32

0.13.3

  • Included beforeSend callback, this should be configured through Global configurations and URL matching. It will follow the same behavior as the processor callback
  • bugfix: some body parsers are super strict and will fail if charset has a “;” termination. PR #26 introduced this bug trying to solve issue #27

0.13.2

  • bugfix: prioritizes user-defined content-type header even for post/put/patch/delete methods. application/x-www-form-urlencoded is not forced if Content-Type header is defined.

0.13.1

  • bugfix: wrong content-type on vanilla gateway, a semicolon was missing on charset=UTF-8

0.13.0

  • Included status code for success calls
  • withCredentials option for VanillaGateway and JQueryGateway
  • Method to configure a global success handler per client
  • bugfix: rules matcher now uses full URL instead of descriptor path

0.12.1

  • Optional callback parameter when passing options to gateway. Check the section "Specifics of each gateway" for more info

0.12.0

  • Included request and response headers into stats object
  • Included a way to assign headers directly from method calls

0.11.0

  • Method to configure a global error handler per client
  • Included status code in the error request object

0.10.0

  • Bult-in fixture support
  • Holding error stack for better debugging

0.9.0

  • Allows Promise implementation to be configured, defaults to global Promise
  • Headers option for all gateways

0.8.1

  • bugfix: support for https in NodeVanillaGateway

0.8.0

  • Support for Promises
  • bugfix: made headers from options higher priority than built-in headers
  • Fail callback now receives the requested resource (url, host, path and params). This change breaks the API for fail callback, the original error objects will be available from the second argument and beyond

0.7.0

  • Included host and path into stats object

0.6.0

  • Improved package size
  • Included url and params into stats object
  • Support for alternative hosts for each resource method
  • Ability to disable host resolution

0.5.0

  • Measure of the time elapsed between the request and the callback invocation
  • Stats object for success callbacks

0.4.1

  • bugfix: VanillaGateway doesn't parse JSON responses

0.4.0

  • Support for Node vanilla gateway

0.3.0

  • Ability to configure default parameters for resources

0.2.1

  • bugfix: fixed typo

0.2.0

  • Support for emulateHTTP
  • Support for global configurations and per url match configurations
  • Support for POST, PUT, PATCH and DELETE methods
  • Exposed body value to gateway
  • Option to change the bodyAttr name
  • Processor functions for resources
  • Exposed request params to the gateway to allow o POST, PUT, etc
  • Compact syntax (syntatic sugar for GET methods)

0.1.1

  • bugfix: fixed reference of Mapper and VanillaGateway in index.js

0.1.0

  • Basic structure with Jquery and Vanilla gateways
  • Support for GET method