@paychex/adapter-mock

2.1.20 • Public • Published

@paychex/adapter-mock

A data adapter that returns mock Responses based on conditions matching Requests. This package is intended for use with @paychex/core data pipelines.

Installation

npm install @paychex/adapter-mock

Importing

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' } ]
}

Package Sidebar

Install

npm i @paychex/adapter-mock

Weekly Downloads

2

Version

2.1.20

License

Apache-2.0

Unpacked Size

1.41 MB

Total Files

42

Last publish

Collaborators

  • paychex-ssmithrand
  • dbarnes1
  • best442
  • dhergert-payx