@scalar/mock-server 0.2.73 → 0.2.74

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +107 -10
  3. package/package.json +5 -5
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @scalar/mock-server
2
2
 
3
+ ## 0.2.74
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [c3e76d9]
8
+ - Updated dependencies [757fade]
9
+ - Updated dependencies [a607115]
10
+ - @scalar/oas-utils@0.2.70
11
+ - @scalar/openapi-parser@0.8.9
12
+ - @scalar/openapi-types@0.1.5
13
+
3
14
  ## 0.2.73
4
15
 
5
16
  ### Patch Changes
package/README.md CHANGED
@@ -5,21 +5,23 @@
5
5
  [![License](https://img.shields.io/npm/l/%40scalar%2Fmock-server)](https://www.npmjs.com/package/@scalar/mock-server)
6
6
  [![Discord](https://img.shields.io/discord/1135330207960678410?style=flat&color=5865F2)](https://discord.gg/scalar)
7
7
 
8
- A powerful Node.js server that generates and returns realistic mock data based on OpenAPI specifications. Ideal for API development, testing, and prototyping.
8
+ A powerful Node.js mock server that automatically generates realistic API responses from your OpenAPI/Swagger documents. It creates fully-functional endpoints with mock data, handles authentication, and respects content types - making it perfect for frontend development, API prototyping, and integration testing.
9
9
 
10
10
  ![](https://raw.githubusercontent.com/scalar/scalar/main/packages/cli/screenshots/mock.png)
11
11
 
12
12
  ## Features
13
13
 
14
- - Automatically creates endpoints from your OpenAPI/Swagger document
15
- - Generates realistic sample data matching your schema definitions
16
- - Supports Swagger 2.0, OpenAPI 3.0 and 3.1 specifications
17
- - Customizable response handling and data generation
18
- - Perfect for frontend development without an actual backend
14
+ - Perfect for frontend development and testing
15
+ - Creates endpoints automatically from OpenAPI documents
16
+ - Generates realistic mock data based on your schemas
17
+ - Handles authentication and responds with defined HTTP headers
18
+ - Supports Swagger 2.0 and OpenAPI 3.x documents
19
+ - Customizable response handling
19
20
 
20
21
  ## Quickstart
21
22
 
22
- It’s part of [our Scalar CLI](https://github.com/scalar/scalar/tree/main/packages/cli), you can boot it literllay in seconds:
23
+ The easiest way to get started is through [our Scalar CLI](https://github.com/scalar/scalar/tree/main/packages/cli).
24
+ You can have a mock server up and running in seconds:
23
25
 
24
26
  ```bash
25
27
  npx @scalar/cli mock openapi.json --watch
@@ -27,7 +29,7 @@ npx @scalar/cli mock openapi.json --watch
27
29
 
28
30
  ## Installation
29
31
 
30
- For more advanced use cases or finer control over the mock server, you can use the package directly in your Node.js application:
32
+ For advanced use cases, you can integrate the mock server directly into your Node.js application for full control:
31
33
 
32
34
  ```bash
33
35
  npm install @scalar/mock-server
@@ -39,9 +41,9 @@ npm install @scalar/mock-server
39
41
  import { serve } from '@hono/node-server'
40
42
  import { createMockServer } from '@scalar/mock-server'
41
43
 
42
- // Your OpenAPI specification
44
+ // Your OpenAPI document
43
45
  const specification = {
44
- openapi: '3.1.0',
46
+ openapi: '3.1.1',
45
47
  info: {
46
48
  title: 'Hello World',
47
49
  version: '1.0.0',
@@ -69,6 +71,7 @@ const specification = {
69
71
  // Create the mocked routes
70
72
  const app = await createMockServer({
71
73
  specification,
74
+ // Custom logging
72
75
  onRequest({ context, operation }) {
73
76
  console.log(context.req.method, context.req.path)
74
77
  },
@@ -86,6 +89,100 @@ serve(
86
89
  )
87
90
  ```
88
91
 
92
+ ### Authentication
93
+
94
+ You can define security schemes in your OpenAPI document and the mock server will validate the authentication:
95
+
96
+ ```ts
97
+ import { serve } from '@hono/node-server'
98
+ import { createMockServer } from '@scalar/mock-server'
99
+
100
+ // Your OpenAPI document
101
+ const specification = {
102
+ openapi: '3.1.1',
103
+ info: {
104
+ title: 'Hello World',
105
+ version: '1.0.0',
106
+ },
107
+ paths: {
108
+ '/secret': {
109
+ get: {
110
+ security: [
111
+ {
112
+ bearerAuth: [],
113
+ },
114
+ {
115
+ apiKey: [],
116
+ },
117
+ ],
118
+ responses: {
119
+ '200': {
120
+ description: 'OK',
121
+ content: {
122
+ 'application/json': {
123
+ example: {
124
+ foo: 'bar',
125
+ },
126
+ },
127
+ },
128
+ },
129
+ '401': {
130
+ description: 'Unauthorized',
131
+ content: {
132
+ 'application/json': {
133
+ example: {
134
+ error: 'Unauthorized',
135
+ },
136
+ },
137
+ },
138
+ },
139
+ },
140
+ },
141
+ },
142
+ },
143
+ components: {
144
+ securitySchemes: {
145
+ bearerAuth: {
146
+ type: 'http',
147
+ scheme: 'bearer',
148
+ bearerFormat: 'JWT',
149
+ },
150
+ apiKey: {
151
+ type: 'apiKey',
152
+ in: 'header',
153
+ name: 'X-API-Key',
154
+ },
155
+ },
156
+ },
157
+ }
158
+
159
+ // Create the mocked routes
160
+ const app = await createMockServer({
161
+ specification,
162
+ // Custom logging
163
+ onRequest({ context, operation }) {
164
+ console.log(context.req.method, context.req.path)
165
+ },
166
+ })
167
+
168
+ // Start the server
169
+ serve(
170
+ {
171
+ fetch: app.fetch,
172
+ port: 3000,
173
+ },
174
+ (info) => {
175
+ console.log(`Listening on http://localhost:${info.port}`)
176
+ },
177
+ )
178
+ ```
179
+
180
+ ### OpenAPI endpoints
181
+
182
+ The given OpenAPI document is automatically exposed:
183
+
184
+ - `/openapi.json` and `/openapi.yaml`
185
+
89
186
  ## Community
90
187
 
91
188
  We are API nerds. You too? Let’s chat on Discord: <https://discord.gg/scalar>
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "swagger",
17
17
  "cli"
18
18
  ],
19
- "version": "0.2.73",
19
+ "version": "0.2.74",
20
20
  "engines": {
21
21
  "node": ">=18"
22
22
  },
@@ -38,15 +38,15 @@
38
38
  "dependencies": {
39
39
  "hono": "^4.6.5",
40
40
  "object-to-xml": "^2.0.0",
41
- "@scalar/openapi-parser": "0.8.8",
42
- "@scalar/openapi-types": "0.1.4",
43
- "@scalar/oas-utils": "0.2.69"
41
+ "@scalar/oas-utils": "0.2.70",
42
+ "@scalar/openapi-types": "0.1.5",
43
+ "@scalar/openapi-parser": "0.8.9"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@hono/node-server": "^1.11.0",
47
47
  "@types/node": "^20.14.10",
48
48
  "@scalar/build-tooling": "0.1.11",
49
- "@scalar/hono-api-reference": "0.5.158"
49
+ "@scalar/hono-api-reference": "0.5.159"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "scalar-build-rollup",