Skip to content

Commit

Permalink
feat: 添加跨越请求的支持 (#106)
Browse files Browse the repository at this point in the history
* feat: 添加跨越请求的支持

* Update types.ts
  • Loading branch information
Estelle00 authored Jan 17, 2024
1 parent f15c661 commit aad4668
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/vite-plugin-mock/src/createMockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export async function createMockServer(
watchFiles: true,
configPath: 'vite.mock.config.ts',
logger: true,
cors: true,
...opt,
}

Expand Down Expand Up @@ -87,6 +88,10 @@ export async function requestMiddleware(opt: ViteMockOptions) {
} else {
const body = await parseJson(req)
res.setHeader('Content-Type', 'application/json')
if (opt) {
res.setHeader('Access-Control-Allow-Credentials', true)
res.setHeader('Access-Control-Allow-Origin', req.headers.origin || '*')
}
res.statusCode = statusCode || 200
const mockResponse = isFunction(response)
? response.bind(self)({ url: req.url as any, body, query, headers: req.headers })
Expand Down
1 change: 1 addition & 0 deletions packages/vite-plugin-mock/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface ViteMockOptions {
watchFiles?: boolean
enable?: boolean
logger?: boolean
cors?: boolean
}

export interface RespThisType {
Expand Down

0 comments on commit aad4668

Please sign in to comment.