1.3.0 • Published 12 months ago

@nicolalopatriello/msw-openapi v1.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
12 months ago

msw-openapi

msw-openapi use msw library, OPENAPI spec and faker.js to generate all handlers.

Why?

To easily integrate msw handlers from openapi specifications.


Installation

To use msw-openapi, install it and other packages via npm:

npm i --save-dev @nicolalopatriello/msw-openapi
npm i --save-dev msw
npm i --save-dev @faker-js/faker
npm i --save-dev ollama

Using AI Features with Ollama

To enable AI-based handler generation, you need to set up an Ollama instance. The easiest way to do this is by running it in a Docker container.

docker run -d \
  -v ollama:/root/.ollama \
  -p 11434:11434 \
  -e OLLAMA_ORIGINS=* \
  --name ollama \
  ollama/ollama 
docker exec -it ollama ollama run gemma2:2b 

Usage

For help run msw-openapi --help

msw-openapi --swagger-url "http://localhost:3002" --mock-base-url "http://msw-server.local" --output-dir "./libs/core/api-client/msw" --with-ai --ollama-host "http://locahost:11434"

  • Edit your main.ts file as follow:

ALL_HANDLERS file will be available in output-dir folder set in msw-openapi command

import {ALL_HANDLERS} from '@gfm/core';
...

async function prepareApp() {
  if (isDevMode()) {
    return setupWorker(...ALL_HANDLERS).start({
      onUnhandledRequest: "bypass"
    })
  }
  return Promise.resolve()
}

prepareApp().then(() => {
  bootstrapApplication(AppComponent, appConfig).catch((err) =>
    console.error(err)
  );
})
  • Add mockServiceWorker.js file to project.json->assets section

ALL_HANDLERS file will be available in output-dir folder set in msw-openapi command

...
       "assets": [
         {
            "glob": "mockServiceWorker.js",
            "input": "<output-dir>",
            "output": "/"
          },
...         

Release

Run following commands to release a new version

npm run ci
git add .
git commit -m <msg>
npx changeset
npm run local-release
git push origin <branch>