Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

feat: new @scalar/mock-server package #32

Merged
merged 4 commits into from
Feb 14, 2024
Merged

Conversation

hanspagel
Copy link
Member

This PR separates the mock server from the CLI. I’m not sure whether this will be something we’ll really provide to the users, but definitely something we’ll need in other places. That’s why I didn’t bother to make the API too simple.

Here is a small example with the current approach:

import { serve } from '@hono/node-server'
import { createMockServer } from '@scalar/mock-server'

// Your OpenAPI specification
const openapi = {
  openapi: '3.1.0',
  info: {
    title: 'Hello World',
    version: '1.0.0',
  },
  paths: {
    '/foobar': {
      get: {
        responses: {
          '200': {
            description: 'OK',
            content: {
              'application/json': {
                example: {
                  foo: 'bar',
                },
              },
            },
          },
        },
      },
    },
  },
}

// Create the mocked routes
const app = await createMockServer({
  openapi,
  onRequest({ context, operation }) {
    console.log(context.req.method, context.req.path)
  },
})

// Start the server
serve({
  fetch: app.fetch,
  port: 3000,
}, (info) => {
  console.log(`Listening on http://localhost:${info.port}`)
})

Copy link
Member

@marclave marclave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@marclave marclave merged commit 96b85ef into main Feb 14, 2024
6 checks passed
@marclave marclave deleted the feat/mock-server-package branch February 14, 2024 20:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants