Giter VIP home page Giter VIP logo

http-middleware's Introduction

@mswjs/http-middleware

Spawn an Express server from your Mock Service Worker request handlers or apply them to an existing server using a middleware.

When to use this?

You should always prefer Mock Service Worker for API mocking because it can meet most of your requirements without having to spawn and maintain an actual HTTP server. Please refer to the Getting started tutorial to integrate next-generation API mocking into your application.

There are, however, use cases when this extension can be applicable:

  • If you wish to curl your mock definitions locally;
  • When prototyping a Node.js backend implementation;
  • When integrating API mocking in a complex application architecture (i.e. for dockerized applications).

Getting started

Install

$ npm install @mswjs/http-middleware

Declare request handlers

// src/mocks/handlers.js
import { http, graphql, HttpResponse } from 'msw'

export const handlers = [
  http.post('/user', () => {
    return HttpResponse.json({ firstName: 'John' })
  }),
  graphql.query('GetUser', () => {
    return HttpResponse.json({
      data: {
        user: {
          firstName: 'John',
        },
      },
    })
  }),
]

Learn more about writing request handlers.

Integration

Option 1: Standalone server

import { createServer } from '@mswjs/http-middleware'
import { handlers } from './handlers'

const httpServer = createServer(...handlers)

httpServer.listen(9090)

Option 2: Middleware

import { createMiddleware } from '@mswjs/http-middleware'
import app from './app'
import { handlers } from './handlers'

app.use(createMiddleware(...handlers))

API

createServer(...handlers: RequestHandler[])

Establishes a standalone Express server that uses the given request handlers to process all incoming requests.

import { http, HttpResponse } from 'msw'
import { createServer } from '@mswjs/http-middleware'

const httpServer = createServer(
  http.get('/user', () => {
    return HttpResponse.json({ firstName: 'John' })
  }),
)

httpServer.listen(9090)

Making a GET http://localhost:9090/user request returns the following response:

200 OK
Content-Type: application/json

{
  "firstName": "John"
}

createMiddleware(...handlers: RequestHandler[])

Creates an Express middleware function that uses the given request handlers to process all incoming requests.

import { http, HttpResponse } from 'msw'
import { createMiddleware } from '@mswjs/http-middleware'

const app = express()

app.use(
  createMiddleware(
    http.get('/user', () => {
      return HttpResponse.json({ firstName: 'John' })
    }),
  ),
)

app.use(9090)

Mentions

http-middleware's People

Contributors

kettanaito avatar yshrsmz avatar idolize avatar ruisaraiva19 avatar robbtraister avatar devniel avatar stijnvanhulle avatar pacop avatar

Forkers

liip-lausanne

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.