Skip to content

Commit 6d1a953

Browse files
committed
Merge remote-tracking branch 'origin/main' into prototype/sse
2 parents 68cd5b7 + 98f3540 commit 6d1a953

44 files changed

Lines changed: 1366 additions & 169 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.husky/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pnpm run -r lint-staged
1+
pnpm -r --workspace-concurrency=1 run lint-staged

packages/docs/content/docs/cli.mdx

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: CLI
3+
description: Command Line Interface for mock-config-server
4+
---
5+
6+
## Run mock-config-server
7+
8+
`mock-config-server`
9+
10+
Starts a mock server in the current directory using a configuration file `mock-server.config.(js|ts)`.
11+
12+
### Usage
13+
14+
```bash
15+
npx mock-config-server [options]
16+
```
17+
18+
> If the package is already installed, you can use the short command `mcs`
19+
20+
### Options
21+
22+
| Option | Short | Description | Default value |
23+
| -------------- | ----- | -------------------------------------------------------------------- | ----------------------------- |
24+
| `--baseUrl` | `-b` | Specify the base URL path from config. Must start with `/`. | `/` |
25+
| `--port` | `-p` | Specify the server port from config. | `31299` |
26+
| `--staticPath` | `-s` | Specify the static assets path from the config. Must start with `/`. | |
27+
| `--config` | `-c` | Set path to the config file. | `mock-server.config.(js\|ts)` |
28+
| `--watch` | `-w` | Enables server restart when config file changes. | `false` |
29+
| `--help` | `-h` | Display help for the command. | |
30+
| `--version` | `-v` | Display the mock-config-server version. | |
31+
32+
## Create a mock-config-server template
33+
34+
`mock-config-server init`
35+
36+
Creates a mock configuration. It generates a `mock-server.config.(js|ts)` file with a config template and sample request examples in the current directory. An interactive flow lets you choose between TypeScript and JavaScript, select an API type (REST, GraphQL or both), and set basic server options like base URL, port and static path.
37+
38+
### Usage
39+
40+
```bash
41+
npx mock-config-server init
42+
```
43+
44+
> If the package is already installed, you can use the short command `mcs`
45+
46+
### Options
47+
48+
| Option | Short | Description | Default value |
49+
| -------------- | ----- | ---------------------------------------------------- | ------------- |
50+
| `--baseUrl` | `-b` | Specify the base URL. Must start with `/`. | `/` |
51+
| `--port` | `-p` | Specify the server port. | `31299` |
52+
| `--staticPath` | `-s` | Specify the static assets path. Must start with `/`. | |
53+
54+
### Interactive flow
55+
56+
The command asks a few short questions:
57+
58+
```bash
59+
? Would you like to use TypeScript? › No / Yes
60+
? Choose API type (Use arrow keys)
61+
❯ REST
62+
GraphQL
63+
Both
64+
? Base URL (must start with a forward slash): › /
65+
? Port: › (31299)
66+
? Static path (must start with a forward slash): › /
67+
```
68+
69+
- `Would you like to use TypeScript?` (toggle): choose whether to generate a TypeScript or JavaScript configuration file.
70+
- `Choose API type` (select): select the type of API to generate mock examples for. Options: `REST`, `GraphQL`, or `Both`.
71+
- `Base URL` (string): set the base URL path for the mock server. Must start with `/`. Default: `/`.
72+
- `Port` (number): set the port number for the mock server. Default: `31299`.
73+
- `Static path` (string): set the static assets path for the mock server. Must start with `/`. Default: `/`.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
title: CORS
3+
description: Cross-Origin Resource Sharing settings for mock server
4+
---
5+
6+
Using `cors` field you can emulate CORS behavior (headers, credentials) to mirror how a real backend would respond.
7+
For more details about CORS, [see](https://developer.mozilla.org/ru/docs/Web/HTTP/Guides/CORS).
8+
9+
## Quick Example
10+
11+
```typescript
12+
import type { MockServerConfig } from 'mock-config-server';
13+
14+
export const mockServerConfig: MockServerConfig = [
15+
{
16+
cors: {
17+
origin: ['http://localhost:3000', /\.example\.com$/],
18+
methods: ['GET'],
19+
allowedHeaders: ['accept'],
20+
exposedHeaders: ['accept'],
21+
maxAge: 3600,
22+
credentials: true
23+
}
24+
},
25+
...
26+
];
27+
```
28+
29+
## Parameters
30+
31+
### origin
32+
33+
Type: <code>((request: Request) => <a href='./references/Cors#CorsOrigin'>CorsOrigin</a> | Promise{'<'}<a href='./references/Cors#CorsOrigin'>CorsOrigin</a>{'>'}) | <a href='./references/Cors#CorsOrigin'>CorsOrigin</a></code>
34+
Defines which origins are allowed to access the server. Use a string for a single origin, a RegExp for pattern matching, an array for multiple origins, or a function for dynamic rules.
35+
36+
```typescript
37+
import type { MockServerConfig } from 'mock-config-server';
38+
39+
export const mockServerConfig: MockServerConfig = [
40+
{
41+
cors: {
42+
origin: ['https://example.com', /\.example\.com$/]
43+
}
44+
},
45+
...
46+
];
47+
```
48+
49+
### methods
50+
51+
Type: `Array<GET | POST | DELETE | PUT | PATCH | OPTIONS>`
52+
Allowed HTTP methods. Defaults to `GET,OPTIONS,PUT,PATCH,POST,DELETE`. Include `OPTIONS` if you expect preflight requests.
53+
54+
```typescript
55+
import type { MockServerConfig } from 'mock-config-server';
56+
57+
export const mockServerConfig: MockServerConfig = [
58+
{
59+
cors: {
60+
methods: ['GET', 'POST', 'OPTIONS']
61+
}
62+
},
63+
...
64+
];
65+
```
66+
67+
### allowedHeaders
68+
69+
Type: `string[]`
70+
Allowed request headers. Default is `*`. List the headers your client actually sends.
71+
72+
```typescript
73+
import type { MockServerConfig } from 'mock-config-server';
74+
75+
export const mockServerConfig: MockServerConfig = [
76+
{
77+
cors: {
78+
allowedHeaders: ['Content-Type', 'Authorization']
79+
}
80+
},
81+
...
82+
];
83+
```
84+
85+
### exposedHeaders
86+
87+
Type: `string[]`
88+
Response headers exposed to browser JavaScript. Default is `*`. Use this when the frontend needs to read custom response headers.
89+
90+
```typescript
91+
import type { MockServerConfig } from 'mock-config-server';
92+
93+
export const mockServerConfig: MockServerConfig = [
94+
{
95+
cors: {
96+
exposedHeaders: ['X-Request-Id']
97+
}
98+
},
99+
...
100+
];
101+
```
102+
103+
### credentials
104+
105+
Type: `boolean`
106+
Whether to expose the response to frontend JavaScript when using credentials. Default is `true`. Enable when you use `credentials: "include"` on the client (cookies, auth).
107+
108+
```typescript
109+
import type { MockServerConfig } from 'mock-config-server';
110+
111+
export const mockServerConfig: MockServerConfig = [
112+
{
113+
cors: {
114+
origin: 'https://app.example.com',
115+
credentials: true
116+
}
117+
},
118+
...
119+
];
120+
```
121+
122+
### maxAge
123+
124+
Type: `number`
125+
How long the preflight response can be cached (in seconds). Default is `3600`. Increasing `maxAge` reduces preflight requests.
126+
127+
```typescript
128+
import type { MockServerConfig } from 'mock-config-server';
129+
130+
export const mockServerConfig: MockServerConfig = [
131+
{
132+
cors: {
133+
maxAge: 600
134+
}
135+
},
136+
...
137+
];
138+
```

0 commit comments

Comments
 (0)