From aad4668037e08ffbd59a70c9a9cd4e6d958f53bc Mon Sep 17 00:00:00 2001 From: Estelle00 <530908773@qq.com> Date: Wed, 17 Jan 2024 14:58:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=B7=A8=E8=B6=8A?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E7=9A=84=E6=94=AF=E6=8C=81=20(#106)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 添加跨越请求的支持 * Update types.ts --- packages/vite-plugin-mock/src/createMockServer.ts | 5 +++++ packages/vite-plugin-mock/src/types.ts | 1 + 2 files changed, 6 insertions(+) diff --git a/packages/vite-plugin-mock/src/createMockServer.ts b/packages/vite-plugin-mock/src/createMockServer.ts index 8579c5d..b760355 100644 --- a/packages/vite-plugin-mock/src/createMockServer.ts +++ b/packages/vite-plugin-mock/src/createMockServer.ts @@ -24,6 +24,7 @@ export async function createMockServer( watchFiles: true, configPath: 'vite.mock.config.ts', logger: true, + cors: true, ...opt, } @@ -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 }) diff --git a/packages/vite-plugin-mock/src/types.ts b/packages/vite-plugin-mock/src/types.ts index 105142d..7378772 100644 --- a/packages/vite-plugin-mock/src/types.ts +++ b/packages/vite-plugin-mock/src/types.ts @@ -7,6 +7,7 @@ export interface ViteMockOptions { watchFiles?: boolean enable?: boolean logger?: boolean + cors?: boolean } export interface RespThisType {