@smartlyio/oats-mirage-adapter
TypeScript icon, indicating that this package has built-in type declarations

7.4.7 • Public • Published

Oats Mirage Adapter

Typesafe Typescript OpenaApi3 support for Mirage.js mock http servers using Oats.

What is Oats?

Oats is a library that parses OpenAPI specifications and generates client and server definitions in TypeScript.

Installation

Use npm or yarn to install oats-mirage-adapter.

npm install oats-mirage-adapter

Usage

Oats Mirage Adapter exports a single bind function that can be used to bind the routes defined in an openapi spec to the mirage routes.

import * as runtime from "@smartlyio/oats-runtime";
import * as mirageAdapter from "@smartlyio/oats-mirage-adapter"
import * as api from "./server.generated"
import * as mirage from "miragejs";

// the implementation for the endpoints from example.yml
const spec: api.Endpoints = {
  '/example/{id}': {
    get: async (ctx) => {

      // @ts-expect-error
      void ctx.params.nonExisting // <- ctx is a typesafe object containing the request

      return runtime.json(200, { message: 'get '  + ctx.params.id + ' ' + ctx.query.foo });
    },
    post: async (ctx) => {
      return runtime.json(200, { message: 'post ' + ctx.params.id + ' ' + ctx.body.value.message});
    }
  }
}

export function fake() {
  return mirage.createServer({
    routes() {
      // non openapi route
      this.get("/non-openapi-route", () => ({ ok: true}));

      // bind example.yml endpoints under namespace "api"
      this.namespace = "api";
      mirageAdapter.bind({
        server: this,
        handler: runtime.server.createHandlerFactory<api.Endpoints>(
          api.endpointHandlers
        ),
        spec})
    }
  });
}

For a working example see test-app which contains a standard create-react-app using the generated mirage mock.

Package Sidebar

Install

npm i @smartlyio/oats-mirage-adapter

Weekly Downloads

1

Version

7.4.7

License

none

Unpacked Size

18.2 kB

Total Files

13

Last publish

Collaborators

  • smartlyio