nuxt-msw
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

nuxt-msw

Mock api with MSW in your Nuxt app

Features

  • ⛰  Zero config integration with MSW
  • 🚠  Works both in the browser and on the server during SSR

Quick Setup

  1. Add nuxt-msw and msw dependencies to your project
# Using pnpm
pnpm add -D nuxt-msw msw

# Using yarn
yarn add --dev nuxt-msw msw

# Using npm
npm install --save-dev nuxt-msw msw
  1. Add nuxt-msw to the modules section of nuxt.config.ts and enable it in options:
export default defineNuxtConfig({
  modules: [
    'nuxt-msw'
  ],
  msw: {
    enabled: true
  }
})
  1. Write some request handlers in msw/handlers.ts.
import { http, HttpResponse } from 'msw'

export default [
  http.get('https://example.com/api/user/1', () => HttpResponse.json({
    id: 12,
    name: 'Albert Einstein'
  })),
  http.post('https://example.com/api/user', () => HttpResponse.text('OK')),
]

That's it! Now the specified requests are intercepted by MSW both in the browser and on the server during SSR ✨

const { data } = useFetch('https://example.com/api/user/1')
watch(data, (value) => {
  console.log(value) // { id: 12, name: 'Albert Einstein' }
})

Development

# Install dependencies
yarn install

# Generate type stubs
yarn run dev:prepare

# Develop with the playground
yarn run dev

# Build the playground
yarn run dev:build

# Run ESLint
yarn run lint

# Run Vitest
yarn run test
yarn run test:watch

# Release new version
yarn run release

Readme

Keywords

Package Sidebar

Install

npm i nuxt-msw

Weekly Downloads

1

Version

0.0.2

License

MIT

Unpacked Size

16.5 kB

Total Files

15

Last publish

Collaborators

  • cybersanya