@cloudflare/util-mock-api

In-memory API scaffolding for mocking backends that don't exist yet.

Usage no npm install needed!

<script type="module">
  import cloudflareUtilMockApi from 'https://cdn.skypack.dev/@cloudflare/util-mock-api';
</script>

README

util-mock-api

Express style, in-memory API scaffolding for mocking backends that don't exist yet.

Prototype faster, now, today.

Installation

$ npm install @cloudflare/util-mock-api

Usage

In your feature, make a mockApi.ts and implement your own api responses and store.

import mockApi from '@cloudflare/util-mock-api'

// persist your store in localStorage
const _storedWorkers = window.localStorage.getItem('storedWorkers')


// fetchGetWorkerDetails({ routeParams, queryParams }) => ...
const fetchGetWorkerDetails = ({ routeParams }) => {
  const found = _storedWorkers.find(
    worker => worker.script_id === routeParams.workerId,
  )

  return {
    body: {
      result: found,
    }
  }
}

export default mockApi.server({
  endpoints: [
    {
      route: '/api/v3/workers/scripts/:workerId/details',
      response: fetchGetWorkerDetails,
    }
  ]
})

When mockApi.server() appears in your code, Mock API controls appear in the top corner of your UI where you can toggle the mock server on and off as well as clear the current mock data. Clearing mock data will generate fresh mocks.