diff --git a/packages/vite-plugin/dist/index.js b/packages/vite-plugin/dist/index.js index 0d64be2..d9cd930 100644 --- a/packages/vite-plugin/dist/index.js +++ b/packages/vite-plugin/dist/index.js @@ -7,6 +7,7 @@ const config = { type: "admin", reqUrl: "", + demo: false, nameTag: true, eps: { enable: true, @@ -432,7 +433,7 @@ _permission: { ${permission.map((e) => `${e}: boolean;`).join("\n")} }; `; t += ` - request: Service['request'] + request: Request `; } t += "}\n\n"; @@ -454,20 +455,25 @@ ${controller} + interface RequestOptions { + url: string; + method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS"; + data?: any; + params?: any; + header?: any; + timeout?: number; + withCredentials?: boolean; + firstIpv4?: boolean; + enableChunked?: boolean; + } + + type Request = (options: RequestOptions) => Promise; + interface Service { /** * 基础请求 */ - request(options?: { - url: string; - method?: "POST" | "GET" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS"; - data?: any; - params?: any; - headers?: any, - timeout?: number; - proxy?: boolean; - [key: string]: any; - }): Promise; + request: Request; ${chain} } @@ -483,10 +489,10 @@ // 文件名 let name = "eps.d.ts"; if (config.type == "uniapp-x") { - name = "eps.uts"; + name = "eps.ts"; text = text .replaceAll("interface ", "export interface ") - .replaceAll("type Dict", "export type Dict") + .replaceAll("type ", "export type ") .replaceAll("[key: string]: any;", ""); } else { @@ -670,8 +676,13 @@ if (match) { const value = match[1]; try { - const { target, rewrite } = proxy[`/${value}/`]; - return target + rewrite(`/${value}`); + if (config.type == "uniapp-x") { + return proxy[value].target; + } + else { + const { target, rewrite } = proxy[`/${value}/`]; + return target + rewrite(`/${value}`); + } } catch (err) { error(`[cool-proxy] Error:${value} → ` + getPath()); @@ -1551,7 +1562,7 @@ if (typeof window !== 'undefined') { name: "vite-cool-uniappx-code-pre", enforce: "pre", async transform(code, id) { - if (id.includes("/cool/virtual.ts")) { + if (id.includes("/cool/ctx.ts")) { const ctx = await createCtx(); ctx["SAFE_CHAR_MAP"] = []; for (const i in SAFE_CHAR_MAP) { diff --git a/packages/vite-plugin/src/eps/index.ts b/packages/vite-plugin/src/eps/index.ts index bcd990f..03a1b7c 100644 --- a/packages/vite-plugin/src/eps/index.ts +++ b/packages/vite-plugin/src/eps/index.ts @@ -363,7 +363,7 @@ async function createDescribe({ list, service }: { list: Eps.Entity[]; service: `; t += ` - request: Service['request'] + request: Request `; } @@ -388,20 +388,25 @@ async function createDescribe({ list, service }: { list: Eps.Entity[]; service: ${controller} + interface RequestOptions { + url: string; + method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS"; + data?: any; + params?: any; + header?: any; + timeout?: number; + withCredentials?: boolean; + firstIpv4?: boolean; + enableChunked?: boolean; + } + + type Request = (options: RequestOptions) => Promise; + interface Service { /** * 基础请求 */ - request(options?: { - url: string; - method?: "POST" | "GET" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS"; - data?: any; - params?: any; - headers?: any, - timeout?: number; - proxy?: boolean; - [key: string]: any; - }): Promise; + request: Request; ${chain} } @@ -420,10 +425,11 @@ async function createDescribe({ list, service }: { list: Eps.Entity[]; service: let name = "eps.d.ts"; if (config.type == "uniapp-x") { - name = "eps.uts"; + name = "eps.ts"; + text = text .replaceAll("interface ", "export interface ") - .replaceAll("type Dict", "export type Dict") + .replaceAll("type ", "export type ") .replaceAll("[key: string]: any;", ""); } else { text = ` diff --git a/packages/vite-plugin/src/proxy/index.ts b/packages/vite-plugin/src/proxy/index.ts index 9d7e4e9..6dc966c 100644 --- a/packages/vite-plugin/src/proxy/index.ts +++ b/packages/vite-plugin/src/proxy/index.ts @@ -26,8 +26,12 @@ export function getProxyTarget(proxy: any) { const value = match[1]; try { - const { target, rewrite } = proxy[`/${value}/`]; - return target + rewrite(`/${value}`); + if (config.type == "uniapp-x") { + return proxy[value].target; + } else { + const { target, rewrite } = proxy[`/${value}/`]; + return target + rewrite(`/${value}`); + } } catch (err) { error(`[cool-proxy] Error:${value} → ` + getPath()); return ""; diff --git a/packages/vite-plugin/src/uniapp-x/code.ts b/packages/vite-plugin/src/uniapp-x/code.ts index 69d4652..89cc3fe 100644 --- a/packages/vite-plugin/src/uniapp-x/code.ts +++ b/packages/vite-plugin/src/uniapp-x/code.ts @@ -9,7 +9,7 @@ export function codePlugin(): Plugin[] { name: "vite-cool-uniappx-code-pre", enforce: "pre", async transform(code, id) { - if (id.includes("/cool/virtual.ts")) { + if (id.includes("/cool/ctx.ts")) { const ctx = await createCtx(); ctx["SAFE_CHAR_MAP"] = [];