aleksandryackovlev / openapi-mock-express-middleware

Generates express mock-servers from OpenAPI specs
MIT License
27 stars 11 forks source link

Feature: synchronously generate a mock object according to a spec without a server or middleware #34

Open daniellizik opened 3 years ago

daniellizik commented 3 years ago

Hello, thanks for this library! I use it at work to run faked e2e tests, this lib is a big help.

Is your feature request related to a problem? Please describe.

While this lib works great with an express server, it would be nice to be able synchronously generate mocked objects without a need for a server.

I use this lib for testing, but it would also be great for e2e documentation in Storybook. I am envisioning a setup where we have our component stories pre-populated with mocked fixtures, this lets us document the full user flow from frontend to server.

Describe the solution you'd like

Currently, I would need to deploy an express server along with the build Storybook bundle (it just gets built as a static js bundle), so having the ability to do something like this would be awesome

const openApiMock = new OpenApiMock({ spec: 'path/to/doc' })

const Template: Story<React.ComponentProps<typeof Review>> = () => <Review />;

export default {
  title: 'something',
  component: SomeComponent
} as Meta;

export const Default = Template.bind({});
Default.parameters = {
  // synchronously generates mock api response
  apiResult: openApiMock.produce({
    // provide http request params that would match a request described by the openapi doc
    route: '/blah',
    query: { locale: 'en' },
    body: { ...bodyParams }
  })
};
aleksandryackovlev commented 2 years ago

Seems like a usefull feature. I've added it to the roadmap for the next major version.