@nicolalopatriello/msw-openapi v1.3.0
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 ollamaUsing 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.tsfile as follow:
ALL_HANDLERSfile will be available inoutput-dirfolder set inmsw-openapicommand
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.jsfile toproject.json->assetssection
ALL_HANDLERSfile will be available inoutput-dirfolder set inmsw-openapicommand
...
"assets": [
{
"glob": "mockServiceWorker.js",
"input": "<output-dir>",
"output": "/"
},
... - Trust host
Be sure to add host to trusted host in Chrome: http://msw-server.local
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>