Options
All
  • Public
  • Public/Protected
  • All
Menu

@paychex/adapter-mock

esm

import * as Mock from '@paychex/adapter-mock';
import { delay, success, failure, mock } from '@paychex/adapter-mock';

cjs

const Mock = require('@paychex/adapter-mock');
const { delay, success, failure, mock } = require('@paychex/adapter-mock');

amd

define(['@paychex/adapter-mock'], function(Mock) { ... });
define(['@paychex/adapter-mock'], function({ delay, success, failure, mock }) { ... });
require(['@paychex/adapter-mock'], function(Mock) { ... });
require(['@paychex/adapter-mock'], function({ delay, success, failure, mock }) { ... });

iife

const Mock = window['@paychex/adapter-mock'];
const { delay, success, failure, mock } = window['@paychex/adapter-mock'];

Usage

import {
proxy,
fetch,
setAdapter,
createRequest,
} from '~/path/to/datalayer';

// --- the code below can be removed when real endpoints exist --- //

import {
delay,
success,
failure,
mock,
} from '@paychex/adapter-mock';

const otherwise = () => true;
const item = { id: 123, key: 'value' };
const items = [item];

setAdapter('mock-endpoint', mock([
[ { path: '/items' }, delay(20, success(items)) ],
[ { path: '/items/123' }, delay(50, success(item)) ],
[ otherwise, failure(404) ];
]));

proxy.use({
adapter: 'mock-endpoint',
match: {
base: 'my-endpoint'
}
});

// --- the code below stays the same --- //

proxy.use({
protocol: 'https',
host: process.env.MY_ENDPOINT_HOST,
match: {
base: 'my-endpoint',
}
});

const operation = {
method: 'GET',
path: '/items',
base: 'my-endpoint',
};

export async function loadItems() {
const request = createRequest(operation);
const response = await fetch(request);
return response.data; // [ { id: 123, key: 'value' } ]
}

Index

Type aliases

ConditionResult: Response | Promise<Response> | ResponseFactory
MockRule: [any, any]

Functions

  • delay(ms?: number, response?: Response<any>): () => Promise<unknown>
  • Waits the specified number of milliseconds before returning the mock Response.

    example
    const adapter = mock([
    [{ method: 'GET', path: '/' }, delay(200, success({ ... }))],
    [{ method: 'GET', path: '/timeout' }, delay(60000, failure(0))],
    [{ method: 'GET', path: '/not-found' }, delay(100, failure(404))],
    ]);

    Parameters

    • ms: number = 0

      The number of milliseconds to wait before resolving.

    • response: Response<any> = ...

      The mock Response to resolve with.

    Returns () => Promise<unknown>

      • (): Promise<unknown>
      • Returns Promise<unknown>

  • failure(status: number, data?: any): Response
  • Creates a failure Response instance with the specified failure HTTP status code and optional data payload.

    example

    const otherwise = () => true; const payload = { id: 123, key: 'value' }; const adapter = mock([ [{ path: '/items/123' }, success(payload)], [otherwise, failure(404)], ]);

    Parameters

    • status: number

      The HTTP status code to use.

    • data: any = null

      Optional value to return in the Response data.

    Returns Response

    A Response object that indicates a failed data call.

  • A data adapter that returns mock Responses based on conditions matching Requests.

    example
    const otherwise = () => true;
    const item = { id: 123, key: 'value' };
    const items = [item];

    dataLayer.setAdapter('mock-endpoint', mock([
    [ { path: '/items' }, delay(20, success(items)) ],
    [ ['path', '/items/123'], delay(50, success(item)) ],
    [ otherwise, failure(404) ];
    ]));

    proxy.use({
    adapter: 'mock-endpoint',
    match: {
    base: 'my-endpoint'
    }
    });

    Parameters

    • rules: MockRule[]

      The rules specifying which mock Responses should be returned based on which conditions match incoming Requests.

    Returns Adapter

    The adapter to register with a data pipeline.

  • success(data?: any): Response
  • Returns a successful Response (status 200) with the optional specified payload.

    example
    const payload = [];
    const adapter = mock([
    [{ path: '/items' }, success(payload)],
    [{ path: '/not-found' }, failure(404)],
    ]);

    Parameters

    • data: any = null

      Optional value to return in the Response data.

    Returns Response

    A Response object that indicates a successful data call.