|
| 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