Skip to content

Commit aad4668

Browse files
authored
feat: 添加跨越请求的支持 (#106)
* feat: 添加跨越请求的支持 * Update types.ts
1 parent f15c661 commit aad4668

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

packages/vite-plugin-mock/src/createMockServer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export async function createMockServer(
2424
watchFiles: true,
2525
configPath: 'vite.mock.config.ts',
2626
logger: true,
27+
cors: true,
2728
...opt,
2829
}
2930

@@ -87,6 +88,10 @@ export async function requestMiddleware(opt: ViteMockOptions) {
8788
} else {
8889
const body = await parseJson(req)
8990
res.setHeader('Content-Type', 'application/json')
91+
if (opt) {
92+
res.setHeader('Access-Control-Allow-Credentials', true)
93+
res.setHeader('Access-Control-Allow-Origin', req.headers.origin || '*')
94+
}
9095
res.statusCode = statusCode || 200
9196
const mockResponse = isFunction(response)
9297
? response.bind(self)({ url: req.url as any, body, query, headers: req.headers })

packages/vite-plugin-mock/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface ViteMockOptions {
77
watchFiles?: boolean
88
enable?: boolean
99
logger?: boolean
10+
cors?: boolean
1011
}
1112

1213
export interface RespThisType {

0 commit comments

Comments
 (0)