@springworks/static-api-server
TypeScript icon, indicating that this package has built-in type declarations

3005.293.0 • Public • Published

@springworks/static-api-server

A static version of an API, responding with fixtures. Useful to mimic behavior of an actual API server, but with static fixture responses instead.

yarn add @springworks/static-api-server

Usage

Send requests to API and get matching fixtures back:

import static_api_server from '@springworks/static-api-server';
import { createFixtureLoader } from '@springworks/fixture-loader';

const created_server = await static_api_server.createServer({
  swagger_spec: swagger_spec,
  fixture_loader: createFixtureLoader('./path-to-fixtures'),
  host: 'localhost',
  port: 3001,
  output_validation: false,
});

await created_server.start();

// Send requests to server, which will respond with successful status code and fixture

// Call `stop()` when done
await created_server.stop();

API

createServer({ swagger_spec, fixture_loader, host, port, output_validation })

output_validation is optional and defaults to off. When enabled, all responses are validated against the API specification using hapi-sov.

Creates and configures a Hapi server. Routes defined in swagger_spec will be matched with fixtures from fixture_loader.

Returns a Hapi server instance.

server.replaceFixtures(verb, route, fixtures)

Gives you the ability to adjust the mocks for certain calls. fixtures is an array of fixtures, where each element in the array corresponds to the call count of the invocation. Meaning, request number 1 will receive fixture at index 0, and so on. This is an instance method on the created server object. NOTE! Make sure to enable output_validation in the server options if using shadow fixtures! This ensures that your mocked fixtures validate against the swagger spec.

server.shadowFixtures(verb, route, fixtures)

Gives you the ability to adjust the mocks for certain calls. fixtures is an array of fixtures, where each element in the array corresponds to the call count of the invocation. This method uses lodash.merge for each fixture call. If you want to replace this fixture, please use replaceFixtures. Meaning, request number 1 will receive fixture at index 0, and so on. This is an instance method on the created server object. NOTE! Make sure to enable output_validation in the server options if using shadow fixtures! This ensures that your mocked fixtures validate against the swagger spec.

server.purgeShadowFixtures()

Purge any existing shadow fixtures. Useful as an afterEach() during testing.

Readme

Keywords

none

Package Sidebar

Install

npm i @springworks/static-api-server

Weekly Downloads

313

Version

3005.293.0

License

none

Unpacked Size

61.9 kB

Total Files

31

Last publish

Collaborators

  • springworksdev
  • springworksprime