every-plugin 2.5.5 → 2.5.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build/rspack/dev-server-middleware.cjs +18 -2
- package/dist/build/rspack/dev-server-middleware.cjs.map +1 -1
- package/dist/build/rspack/dev-server-middleware.mjs +18 -2
- package/dist/build/rspack/dev-server-middleware.mjs.map +1 -1
- package/dist/runtime/mf-config.cjs +1 -1
- package/dist/runtime/mf-config.mjs +1 -1
- package/package.json +1 -1
- package/src/build/rspack/dev-server-middleware.ts +20 -2
|
@@ -109,6 +109,22 @@ function setupPluginMiddleware(devServer, pluginInfo, devConfig, port) {
|
|
|
109
109
|
applyCorsHeaders(res);
|
|
110
110
|
res.status(200).send("OK");
|
|
111
111
|
});
|
|
112
|
+
const buildDevContext = (_req, webRequest) => {
|
|
113
|
+
const rawClone = webRequest.method === "GET" || webRequest.method === "HEAD" ? null : webRequest.clone();
|
|
114
|
+
let cachedRawBody = null;
|
|
115
|
+
return {
|
|
116
|
+
reqHeaders: webRequest.headers,
|
|
117
|
+
getRawBody: async () => {
|
|
118
|
+
if (cachedRawBody !== null) return cachedRawBody;
|
|
119
|
+
if (!rawClone) {
|
|
120
|
+
cachedRawBody = "";
|
|
121
|
+
return cachedRawBody;
|
|
122
|
+
}
|
|
123
|
+
cachedRawBody = await rawClone.text();
|
|
124
|
+
return cachedRawBody;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
};
|
|
112
128
|
const handleApiRequest = async (req, res) => {
|
|
113
129
|
applyCorsHeaders(res);
|
|
114
130
|
const apiHandler = devServer.app.locals.handlers?.api;
|
|
@@ -123,7 +139,7 @@ function setupPluginMiddleware(devServer, pluginInfo, devConfig, port) {
|
|
|
123
139
|
});
|
|
124
140
|
const result = await apiHandler.handle(webRequest, {
|
|
125
141
|
prefix: "/api",
|
|
126
|
-
context:
|
|
142
|
+
context: buildDevContext(req, webRequest)
|
|
127
143
|
});
|
|
128
144
|
if (result.response) {
|
|
129
145
|
res.status(result.response.status);
|
|
@@ -152,7 +168,7 @@ function setupPluginMiddleware(devServer, pluginInfo, devConfig, port) {
|
|
|
152
168
|
});
|
|
153
169
|
const result = await rpcHandler.handle(webRequest, {
|
|
154
170
|
prefix: `/api/rpc${rpcPrefix}`,
|
|
155
|
-
context:
|
|
171
|
+
context: buildDevContext(req, webRequest)
|
|
156
172
|
});
|
|
157
173
|
if (result.response) {
|
|
158
174
|
res.status(result.response.status);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dev-server-middleware.cjs","names":[],"sources":["../../../src/build/rspack/dev-server-middleware.ts"],"sourcesContent":["import type { PluginInfo } from \"./utils\";\n\nconst corsHeaders = {\n \"Access-Control-Allow-Origin\": \"*\",\n \"Access-Control-Allow-Methods\": \"GET, POST, PUT, DELETE, PATCH, OPTIONS\",\n \"Access-Control-Allow-Headers\": \"X-Requested-With, content-type, Authorization\",\n};\n\nconst applyCorsHeaders = (res: any) => {\n Object.entries(corsHeaders).forEach(([key, value]) => {\n res.setHeader(key, value);\n });\n};\n\nconst normalizePrefix = (prefix?: string): string => {\n if (!prefix) return \"\";\n const cleaned = prefix.replace(/^\\/+|\\/+$/g, \"\");\n return cleaned ? `/${cleaned}` : \"\";\n};\n\nexport function setupPluginMiddleware(\n devServer: any,\n pluginInfo: PluginInfo,\n devConfig: any,\n port: number,\n) {\n const rpcPrefix = normalizePrefix(devConfig?.prefix);\n const handlers: { rpc: any; api: any } = { rpc: null, api: null };\n let cleanup: (() => Promise<void>) | null = null;\n\n const performCleanup = async () => {\n if (cleanup) {\n await cleanup();\n cleanup = null;\n }\n };\n\n (async () => {\n await performCleanup();\n\n try {\n const { createPluginRuntime } = await import(\"every-plugin\");\n const { RPCHandler } = await import(\"@orpc/server/fetch\");\n const { OpenAPIHandler } = await import(\"@orpc/openapi/fetch\");\n const { OpenAPIReferencePlugin } = await import(\"@orpc/openapi/plugins\");\n const { ZodToJsonSchemaConverter } = await import(\"@orpc/zod/zod4\");\n const { onError } = await import(\"every-plugin/orpc\");\n const { formatORPCError } = await import(\"every-plugin/errors\");\n\n const pluginId = devConfig?.pluginId || pluginInfo.normalizedName;\n\n const runtime = createPluginRuntime({\n registry: {\n [pluginId]: {\n remote: `http://localhost:${port}/remoteEntry.js`,\n },\n },\n });\n\n const defaultConfig = { variables: {}, secrets: {} };\n\n // @ts-expect-error we don't know the plugin id\n const loaded = await runtime.usePlugin(pluginId, (devConfig?.config ?? defaultConfig) as any);\n\n cleanup = async () => {\n if (runtime) await runtime.shutdown();\n handlers.rpc = null;\n handlers.api = null;\n if (devServer.app.locals.handlers) {\n devServer.app.locals.handlers = null;\n }\n };\n\n handlers.rpc = new RPCHandler(loaded.router, {\n interceptors: [\n onError((error: any) => {\n formatORPCError(error);\n }),\n ],\n });\n\n handlers.api = new OpenAPIHandler(loaded.router, {\n plugins: [\n new OpenAPIReferencePlugin({\n schemaConverters: [new ZodToJsonSchemaConverter()],\n }),\n ],\n interceptors: [\n onError((error: any) => {\n formatORPCError(error);\n }),\n ],\n });\n\n console.log(`╭─────────────────────────────────────────────`);\n console.log(`│ ✅ Plugin dev server ready: `);\n console.log(`├─────────────────────────────────────────────`);\n console.log(`│ 📡 RPC: http://localhost:${port}/api/rpc${rpcPrefix}`);\n console.log(`│ 📖 Docs: http://localhost:${port}/api`);\n console.log(`│ 💚 Health: http://localhost:${port}/`);\n console.log(`╰─────────────────────────────────────────────`);\n\n devServer.app.locals.handlers = handlers;\n\n if (devServer.server) {\n devServer.server.once(\"close\", async () => {\n await performCleanup();\n });\n }\n } catch (error) {\n console.error(\"❌ Failed to load plugin:\", error);\n await performCleanup();\n }\n })().catch((err) => {\n console.error(\"❌ Plugin dev server fatal error:\", err);\n });\n\n process.once(\"SIGINT\", async () => {\n const timeout = setTimeout(() => process.exit(0), 3000);\n await performCleanup();\n clearTimeout(timeout);\n });\n process.once(\"SIGTERM\", async () => {\n const timeout = setTimeout(() => process.exit(0), 3000);\n await performCleanup();\n clearTimeout(timeout);\n });\n\n devServer.app.options(\"*\", (_req: any, res: any) => {\n applyCorsHeaders(res);\n res.status(200).end();\n });\n\n devServer.app.get(\"/\", (_req: any, res: any) => {\n applyCorsHeaders(res);\n res.json({\n ok: true,\n plugin: pluginInfo.normalizedName,\n version: pluginInfo.version,\n status: devServer.app.locals.handlers?.rpc ? \"ready\" : \"loading\",\n endpoints: {\n health: \"/\",\n docs: \"/api\",\n rpc: `/api/rpc${rpcPrefix}`,\n },\n });\n });\n\n devServer.app.get(\"/health\", (_req: any, res: any) => {\n applyCorsHeaders(res);\n res.status(200).send(\"OK\");\n });\n\n const handleApiRequest = async (req: any, res: any) => {\n applyCorsHeaders(res);\n const apiHandler = devServer.app.locals.handlers?.api;\n if (!apiHandler) {\n return res.status(503).json({ error: \"Plugin still loading...\" });\n }\n\n try {\n const url = `http://${req.headers.host}${req.url}`;\n const webRequest = new Request(url, {\n method: req.method,\n headers: req.headers,\n body: req.method !== \"GET\" && req.method !== \"HEAD\" ? req : undefined,\n duplex: req.method !== \"GET\" && req.method !== \"HEAD\" ? \"half\" : undefined,\n } as RequestInit);\n\n const result = await apiHandler.handle(webRequest, {\n prefix: \"/api\",\n context: {},\n });\n\n if (result.response) {\n res.status(result.response.status);\n result.response.headers.forEach((value: string, key: string) => {\n res.setHeader(key, value);\n });\n const text = await result.response.text();\n res.send(text);\n } else {\n res.status(404).send(\"Not Found\");\n }\n } catch (error) {\n console.error(\"OpenAPI error:\", error);\n res.status(500).json({ error: (error as Error).message });\n }\n };\n\n devServer.app.all(`/api/rpc${rpcPrefix}/*`, async (req: any, res: any) => {\n applyCorsHeaders(res);\n const rpcHandler = devServer.app.locals.handlers?.rpc;\n if (!rpcHandler) {\n return res.status(503).json({ error: \"Plugin still loading...\" });\n }\n\n try {\n const url = `http://${req.headers.host}${req.url}`;\n const webRequest = new Request(url, {\n method: req.method,\n headers: req.headers,\n body: req.method !== \"GET\" && req.method !== \"HEAD\" ? req : undefined,\n duplex: req.method !== \"GET\" && req.method !== \"HEAD\" ? \"half\" : undefined,\n } as RequestInit);\n\n const result = await rpcHandler.handle(webRequest, {\n prefix: `/api/rpc${rpcPrefix}`,\n context: {},\n });\n\n if (result.response) {\n res.status(result.response.status);\n result.response.headers.forEach((value: string, key: string) => {\n res.setHeader(key, value);\n });\n const text = await result.response.text();\n res.send(text);\n } else {\n res.status(404).send(\"Not Found\");\n }\n } catch (error) {\n console.error(\"RPC error:\", error);\n res.status(500).json({ error: (error as Error).message });\n }\n });\n\n devServer.app.all(\"/api\", handleApiRequest);\n devServer.app.all(\"/api/*\", handleApiRequest);\n}\n"],"mappings":";;AAEA,MAAM,cAAc;CAClB,+BAA+B;CAC/B,gCAAgC;CAChC,gCAAgC;CACjC;AAED,MAAM,oBAAoB,QAAa;AACrC,QAAO,QAAQ,YAAY,CAAC,SAAS,CAAC,KAAK,WAAW;AACpD,MAAI,UAAU,KAAK,MAAM;GACzB;;AAGJ,MAAM,mBAAmB,WAA4B;AACnD,KAAI,CAAC,OAAQ,QAAO;CACpB,MAAM,UAAU,OAAO,QAAQ,cAAc,GAAG;AAChD,QAAO,UAAU,IAAI,YAAY;;AAGnC,SAAgB,sBACd,WACA,YACA,WACA,MACA;CACA,MAAM,YAAY,gBAAgB,WAAW,OAAO;CACpD,MAAM,WAAmC;EAAE,KAAK;EAAM,KAAK;EAAM;CACjE,IAAI,UAAwC;CAE5C,MAAM,iBAAiB,YAAY;AACjC,MAAI,SAAS;AACX,SAAM,SAAS;AACf,aAAU;;;AAId,EAAC,YAAY;AACX,QAAM,gBAAgB;AAEtB,MAAI;GACF,MAAM,EAAE,wBAAwB,2CAAM;GACtC,MAAM,EAAE,eAAe,MAAM,OAAO;GACpC,MAAM,EAAE,mBAAmB,MAAM,OAAO;GACxC,MAAM,EAAE,2BAA2B,MAAM,OAAO;GAChD,MAAM,EAAE,6BAA6B,MAAM,OAAO;GAClD,MAAM,EAAE,YAAY,2CAAM;GAC1B,MAAM,EAAE,oBAA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MAAM;AAClC,OAAI,OAAO,IAAI,CAAC,KAAK,EAAE,OAAQ,MAAgB,SAAS,CAAC;;GAE3D;AAEF,WAAU,IAAI,IAAI,QAAQ,iBAAiB;AAC3C,WAAU,IAAI,IAAI,UAAU,iBAAiB"}
|
|
1
|
+
{"version":3,"file":"dev-server-middleware.cjs","names":[],"sources":["../../../src/build/rspack/dev-server-middleware.ts"],"sourcesContent":["import type { PluginInfo } from \"./utils\";\n\nconst corsHeaders = {\n \"Access-Control-Allow-Origin\": \"*\",\n \"Access-Control-Allow-Methods\": \"GET, POST, PUT, DELETE, PATCH, OPTIONS\",\n \"Access-Control-Allow-Headers\": \"X-Requested-With, content-type, Authorization\",\n};\n\nconst applyCorsHeaders = (res: any) => {\n Object.entries(corsHeaders).forEach(([key, value]) => {\n res.setHeader(key, value);\n });\n};\n\nconst normalizePrefix = (prefix?: string): string => {\n if (!prefix) return \"\";\n const cleaned = prefix.replace(/^\\/+|\\/+$/g, \"\");\n return cleaned ? `/${cleaned}` : \"\";\n};\n\nexport function setupPluginMiddleware(\n devServer: any,\n pluginInfo: PluginInfo,\n devConfig: any,\n port: number,\n) {\n const rpcPrefix = normalizePrefix(devConfig?.prefix);\n const handlers: { rpc: any; api: any } = { rpc: null, api: null };\n let cleanup: (() => Promise<void>) | null = null;\n\n const performCleanup = async () => {\n if (cleanup) {\n await cleanup();\n cleanup = null;\n }\n };\n\n (async () => {\n await performCleanup();\n\n try {\n const { createPluginRuntime } = await import(\"every-plugin\");\n const { RPCHandler } = await import(\"@orpc/server/fetch\");\n const { OpenAPIHandler } = await import(\"@orpc/openapi/fetch\");\n const { OpenAPIReferencePlugin } = await import(\"@orpc/openapi/plugins\");\n const { ZodToJsonSchemaConverter } = await import(\"@orpc/zod/zod4\");\n const { onError } = await import(\"every-plugin/orpc\");\n const { formatORPCError } = await import(\"every-plugin/errors\");\n\n const pluginId = devConfig?.pluginId || pluginInfo.normalizedName;\n\n const runtime = createPluginRuntime({\n registry: {\n [pluginId]: {\n remote: `http://localhost:${port}/remoteEntry.js`,\n },\n },\n });\n\n const defaultConfig = { variables: {}, secrets: {} };\n\n // @ts-expect-error we don't know the plugin id\n const loaded = await runtime.usePlugin(pluginId, (devConfig?.config ?? defaultConfig) as any);\n\n cleanup = async () => {\n if (runtime) await runtime.shutdown();\n handlers.rpc = null;\n handlers.api = null;\n if (devServer.app.locals.handlers) {\n devServer.app.locals.handlers = null;\n }\n };\n\n handlers.rpc = new RPCHandler(loaded.router, {\n interceptors: [\n onError((error: any) => {\n formatORPCError(error);\n }),\n ],\n });\n\n handlers.api = new OpenAPIHandler(loaded.router, {\n plugins: [\n new OpenAPIReferencePlugin({\n schemaConverters: [new ZodToJsonSchemaConverter()],\n }),\n ],\n interceptors: [\n onError((error: any) => {\n formatORPCError(error);\n }),\n ],\n });\n\n console.log(`╭─────────────────────────────────────────────`);\n console.log(`│ ✅ Plugin dev server ready: `);\n console.log(`├─────────────────────────────────────────────`);\n console.log(`│ 📡 RPC: http://localhost:${port}/api/rpc${rpcPrefix}`);\n console.log(`│ 📖 Docs: http://localhost:${port}/api`);\n console.log(`│ 💚 Health: http://localhost:${port}/`);\n console.log(`╰─────────────────────────────────────────────`);\n\n devServer.app.locals.handlers = handlers;\n\n if (devServer.server) {\n devServer.server.once(\"close\", async () => {\n await performCleanup();\n });\n }\n } catch (error) {\n console.error(\"❌ Failed to load plugin:\", error);\n await performCleanup();\n }\n })().catch((err) => {\n console.error(\"❌ Plugin dev server fatal error:\", err);\n });\n\n process.once(\"SIGINT\", async () => {\n const timeout = setTimeout(() => process.exit(0), 3000);\n await performCleanup();\n clearTimeout(timeout);\n });\n process.once(\"SIGTERM\", async () => {\n const timeout = setTimeout(() => process.exit(0), 3000);\n await performCleanup();\n clearTimeout(timeout);\n });\n\n devServer.app.options(\"*\", (_req: any, res: any) => {\n applyCorsHeaders(res);\n res.status(200).end();\n });\n\n devServer.app.get(\"/\", (_req: any, res: any) => {\n applyCorsHeaders(res);\n res.json({\n ok: true,\n plugin: pluginInfo.normalizedName,\n version: pluginInfo.version,\n status: devServer.app.locals.handlers?.rpc ? \"ready\" : \"loading\",\n endpoints: {\n health: \"/\",\n docs: \"/api\",\n rpc: `/api/rpc${rpcPrefix}`,\n },\n });\n });\n\n devServer.app.get(\"/health\", (_req: any, res: any) => {\n applyCorsHeaders(res);\n res.status(200).send(\"OK\");\n });\n\n const buildDevContext = (_req: any, webRequest: Request) => {\n const rawClone =\n webRequest.method === \"GET\" || webRequest.method === \"HEAD\" ? null : webRequest.clone();\n let cachedRawBody: string | null = null;\n return {\n reqHeaders: webRequest.headers,\n getRawBody: async (): Promise<string> => {\n if (cachedRawBody !== null) return cachedRawBody;\n if (!rawClone) {\n cachedRawBody = \"\";\n return cachedRawBody;\n }\n cachedRawBody = await rawClone.text();\n return cachedRawBody;\n },\n };\n };\n\n const handleApiRequest = async (req: any, res: any) => {\n applyCorsHeaders(res);\n const apiHandler = devServer.app.locals.handlers?.api;\n if (!apiHandler) {\n return res.status(503).json({ error: \"Plugin still loading...\" });\n }\n\n try {\n const url = `http://${req.headers.host}${req.url}`;\n const webRequest = new Request(url, {\n method: req.method,\n headers: req.headers,\n body: req.method !== \"GET\" && req.method !== \"HEAD\" ? req : undefined,\n duplex: req.method !== \"GET\" && req.method !== \"HEAD\" ? \"half\" : undefined,\n } as RequestInit);\n\n const result = await apiHandler.handle(webRequest, {\n prefix: \"/api\",\n context: buildDevContext(req, webRequest),\n });\n\n if (result.response) {\n res.status(result.response.status);\n result.response.headers.forEach((value: string, key: string) => {\n res.setHeader(key, value);\n });\n const text = await result.response.text();\n res.send(text);\n } else {\n res.status(404).send(\"Not Found\");\n }\n } catch (error) {\n console.error(\"OpenAPI error:\", error);\n res.status(500).json({ error: (error as Error).message });\n }\n };\n\n devServer.app.all(`/api/rpc${rpcPrefix}/*`, async (req: any, res: any) => {\n applyCorsHeaders(res);\n const rpcHandler = devServer.app.locals.handlers?.rpc;\n if (!rpcHandler) {\n return res.status(503).json({ error: \"Plugin still loading...\" });\n }\n\n try {\n const url = `http://${req.headers.host}${req.url}`;\n const webRequest = new Request(url, {\n method: req.method,\n headers: req.headers,\n body: req.method !== \"GET\" && req.method !== \"HEAD\" ? req : undefined,\n duplex: req.method !== \"GET\" && req.method !== \"HEAD\" ? \"half\" : undefined,\n } as RequestInit);\n\n const result = await rpcHandler.handle(webRequest, {\n prefix: `/api/rpc${rpcPrefix}`,\n context: buildDevContext(req, webRequest),\n });\n\n if (result.response) {\n res.status(result.response.status);\n result.response.headers.forEach((value: string, key: string) => {\n res.setHeader(key, value);\n });\n const text = await result.response.text();\n res.send(text);\n } else {\n res.status(404).send(\"Not Found\");\n }\n } catch (error) {\n console.error(\"RPC error:\", error);\n res.status(500).json({ error: (error as Error).message });\n }\n });\n\n devServer.app.all(\"/api\", handleApiRequest);\n devServer.app.all(\"/api/*\", handleApiRequest);\n}\n"],"mappings":";;AAEA,MAAM,cAAc;CAClB,+BAA+B;CAC/B,gCAAgC;CAChC,gCAAgC;CACjC;AAED,MAAM,oBAAoB,QAAa;AACrC,QAAO,QAAQ,YAAY,CAAC,SAAS,CAAC,KAAK,WAAW;AACpD,MAAI,UAAU,KAAK,MAAM;GACzB;;AAGJ,MAAM,mBAAmB,WAA4B;AACnD,KAAI,CAAC,OAAQ,QAAO;CACpB,MAAM,UAAU,OAAO,QAAQ,cAAc,GAAG;AAChD,QAAO,UAAU,IAAI,YAAY;;AAGnC,SAAgB,sBACd,WACA,YACA,WACA,MACA;CACA,MAAM,YAAY,gBAAgB,WAAW,OAAO;CACpD,MAAM,WAAmC;EAAE,KAAK;EAAM,KAAK;EAAM;CACjE,IAAI,UAAwC;CAE5C,MAAM,iBAAiB,YAAY;AACjC,MAAI,SAAS;AACX,SAAM,SAAS;AACf,aAAU;;;AAId,EAAC,YAAY;AACX,QAAM,gBAAgB;AAEtB,MAAI;GACF,MAAM,EAAE,wBAAwB,2CAAM;GACtC,MAAM,EAAE,eAAe,MAAM,OAAO;GACpC,MAAM,EAAE,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SAAS,IAAI,WAAW,SAAS,MAAM;IAC5D,QAAQ,IAAI,WAAW,SAAS,IAAI,WAAW,SAAS,SAAS;IAClE,CAAgB;GAEjB,MAAM,SAAS,MAAM,WAAW,OAAO,YAAY;IACjD,QAAQ,WAAW;IACnB,SAAS,gBAAgB,KAAK,WAAW;IAC1C,CAAC;AAEF,OAAI,OAAO,UAAU;AACnB,QAAI,OAAO,OAAO,SAAS,OAAO;AAClC,WAAO,SAAS,QAAQ,SAAS,OAAe,QAAgB;AAC9D,SAAI,UAAU,KAAK,MAAM;MACzB;IACF,MAAM,OAAO,MAAM,OAAO,SAAS,MAAM;AACzC,QAAI,KAAK,KAAK;SAEd,KAAI,OAAO,IAAI,CAAC,KAAK,YAAY;WAE5B,OAAO;AACd,WAAQ,MAAM,cAAc,MAAM;AAClC,OAAI,OAAO,IAAI,CAAC,KAAK,EAAE,OAAQ,MAAgB,SAAS,CAAC;;GAE3D;AAEF,WAAU,IAAI,IAAI,QAAQ,iBAAiB;AAC3C,WAAU,IAAI,IAAI,UAAU,iBAAiB"}
|
|
@@ -108,6 +108,22 @@ function setupPluginMiddleware(devServer, pluginInfo, devConfig, port) {
|
|
|
108
108
|
applyCorsHeaders(res);
|
|
109
109
|
res.status(200).send("OK");
|
|
110
110
|
});
|
|
111
|
+
const buildDevContext = (_req, webRequest) => {
|
|
112
|
+
const rawClone = webRequest.method === "GET" || webRequest.method === "HEAD" ? null : webRequest.clone();
|
|
113
|
+
let cachedRawBody = null;
|
|
114
|
+
return {
|
|
115
|
+
reqHeaders: webRequest.headers,
|
|
116
|
+
getRawBody: async () => {
|
|
117
|
+
if (cachedRawBody !== null) return cachedRawBody;
|
|
118
|
+
if (!rawClone) {
|
|
119
|
+
cachedRawBody = "";
|
|
120
|
+
return cachedRawBody;
|
|
121
|
+
}
|
|
122
|
+
cachedRawBody = await rawClone.text();
|
|
123
|
+
return cachedRawBody;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
};
|
|
111
127
|
const handleApiRequest = async (req, res) => {
|
|
112
128
|
applyCorsHeaders(res);
|
|
113
129
|
const apiHandler = devServer.app.locals.handlers?.api;
|
|
@@ -122,7 +138,7 @@ function setupPluginMiddleware(devServer, pluginInfo, devConfig, port) {
|
|
|
122
138
|
});
|
|
123
139
|
const result = await apiHandler.handle(webRequest, {
|
|
124
140
|
prefix: "/api",
|
|
125
|
-
context:
|
|
141
|
+
context: buildDevContext(req, webRequest)
|
|
126
142
|
});
|
|
127
143
|
if (result.response) {
|
|
128
144
|
res.status(result.response.status);
|
|
@@ -151,7 +167,7 @@ function setupPluginMiddleware(devServer, pluginInfo, devConfig, port) {
|
|
|
151
167
|
});
|
|
152
168
|
const result = await rpcHandler.handle(webRequest, {
|
|
153
169
|
prefix: `/api/rpc${rpcPrefix}`,
|
|
154
|
-
context:
|
|
170
|
+
context: buildDevContext(req, webRequest)
|
|
155
171
|
});
|
|
156
172
|
if (result.response) {
|
|
157
173
|
res.status(result.response.status);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dev-server-middleware.mjs","names":[],"sources":["../../../src/build/rspack/dev-server-middleware.ts"],"sourcesContent":["import type { PluginInfo } from \"./utils\";\n\nconst corsHeaders = {\n \"Access-Control-Allow-Origin\": \"*\",\n \"Access-Control-Allow-Methods\": \"GET, POST, PUT, DELETE, PATCH, OPTIONS\",\n \"Access-Control-Allow-Headers\": \"X-Requested-With, content-type, Authorization\",\n};\n\nconst applyCorsHeaders = (res: any) => {\n Object.entries(corsHeaders).forEach(([key, value]) => {\n res.setHeader(key, value);\n });\n};\n\nconst normalizePrefix = (prefix?: string): string => {\n if (!prefix) return \"\";\n const cleaned = prefix.replace(/^\\/+|\\/+$/g, \"\");\n return cleaned ? `/${cleaned}` : \"\";\n};\n\nexport function setupPluginMiddleware(\n devServer: any,\n pluginInfo: PluginInfo,\n devConfig: any,\n port: number,\n) {\n const rpcPrefix = normalizePrefix(devConfig?.prefix);\n const handlers: { rpc: any; api: any } = { rpc: null, api: null };\n let cleanup: (() => Promise<void>) | null = null;\n\n const performCleanup = async () => {\n if (cleanup) {\n await cleanup();\n cleanup = null;\n }\n };\n\n (async () => {\n await performCleanup();\n\n try {\n const { createPluginRuntime } = await import(\"every-plugin\");\n const { RPCHandler } = await import(\"@orpc/server/fetch\");\n const { OpenAPIHandler } = await import(\"@orpc/openapi/fetch\");\n const { OpenAPIReferencePlugin } = await import(\"@orpc/openapi/plugins\");\n const { ZodToJsonSchemaConverter } = await import(\"@orpc/zod/zod4\");\n const { onError } = await import(\"every-plugin/orpc\");\n const { formatORPCError } = await import(\"every-plugin/errors\");\n\n const pluginId = devConfig?.pluginId || pluginInfo.normalizedName;\n\n const runtime = createPluginRuntime({\n registry: {\n [pluginId]: {\n remote: `http://localhost:${port}/remoteEntry.js`,\n },\n },\n });\n\n const defaultConfig = { variables: {}, secrets: {} };\n\n // @ts-expect-error we don't know the plugin id\n const loaded = await runtime.usePlugin(pluginId, (devConfig?.config ?? defaultConfig) as any);\n\n cleanup = async () => {\n if (runtime) await runtime.shutdown();\n handlers.rpc = null;\n handlers.api = null;\n if (devServer.app.locals.handlers) {\n devServer.app.locals.handlers = null;\n }\n };\n\n handlers.rpc = new RPCHandler(loaded.router, {\n interceptors: [\n onError((error: any) => {\n formatORPCError(error);\n }),\n ],\n });\n\n handlers.api = new OpenAPIHandler(loaded.router, {\n plugins: [\n new OpenAPIReferencePlugin({\n schemaConverters: [new ZodToJsonSchemaConverter()],\n }),\n ],\n interceptors: [\n onError((error: any) => {\n formatORPCError(error);\n }),\n ],\n });\n\n console.log(`╭─────────────────────────────────────────────`);\n console.log(`│ ✅ Plugin dev server ready: `);\n console.log(`├─────────────────────────────────────────────`);\n console.log(`│ 📡 RPC: http://localhost:${port}/api/rpc${rpcPrefix}`);\n console.log(`│ 📖 Docs: http://localhost:${port}/api`);\n console.log(`│ 💚 Health: http://localhost:${port}/`);\n console.log(`╰─────────────────────────────────────────────`);\n\n devServer.app.locals.handlers = handlers;\n\n if (devServer.server) {\n devServer.server.once(\"close\", async () => {\n await performCleanup();\n });\n }\n } catch (error) {\n console.error(\"❌ Failed to load plugin:\", error);\n await performCleanup();\n }\n })().catch((err) => {\n console.error(\"❌ Plugin dev server fatal error:\", err);\n });\n\n process.once(\"SIGINT\", async () => {\n const timeout = setTimeout(() => process.exit(0), 3000);\n await performCleanup();\n clearTimeout(timeout);\n });\n process.once(\"SIGTERM\", async () => {\n const timeout = setTimeout(() => process.exit(0), 3000);\n await performCleanup();\n clearTimeout(timeout);\n });\n\n devServer.app.options(\"*\", (_req: any, res: any) => {\n applyCorsHeaders(res);\n res.status(200).end();\n });\n\n devServer.app.get(\"/\", (_req: any, res: any) => {\n applyCorsHeaders(res);\n res.json({\n ok: true,\n plugin: pluginInfo.normalizedName,\n version: pluginInfo.version,\n status: devServer.app.locals.handlers?.rpc ? \"ready\" : \"loading\",\n endpoints: {\n health: \"/\",\n docs: \"/api\",\n rpc: `/api/rpc${rpcPrefix}`,\n },\n });\n });\n\n devServer.app.get(\"/health\", (_req: any, res: any) => {\n applyCorsHeaders(res);\n res.status(200).send(\"OK\");\n });\n\n const handleApiRequest = async (req: any, res: any) => {\n applyCorsHeaders(res);\n const apiHandler = devServer.app.locals.handlers?.api;\n if (!apiHandler) {\n return res.status(503).json({ error: \"Plugin still loading...\" });\n }\n\n try {\n const url = `http://${req.headers.host}${req.url}`;\n const webRequest = new Request(url, {\n method: req.method,\n headers: req.headers,\n body: req.method !== \"GET\" && req.method !== \"HEAD\" ? req : undefined,\n duplex: req.method !== \"GET\" && req.method !== \"HEAD\" ? \"half\" : undefined,\n } as RequestInit);\n\n const result = await apiHandler.handle(webRequest, {\n prefix: \"/api\",\n context: {},\n });\n\n if (result.response) {\n res.status(result.response.status);\n result.response.headers.forEach((value: string, key: string) => {\n res.setHeader(key, value);\n });\n const text = await result.response.text();\n res.send(text);\n } else {\n res.status(404).send(\"Not Found\");\n }\n } catch (error) {\n console.error(\"OpenAPI error:\", error);\n res.status(500).json({ error: (error as Error).message });\n }\n };\n\n devServer.app.all(`/api/rpc${rpcPrefix}/*`, async (req: any, res: any) => {\n applyCorsHeaders(res);\n const rpcHandler = devServer.app.locals.handlers?.rpc;\n if (!rpcHandler) {\n return res.status(503).json({ error: \"Plugin still loading...\" });\n }\n\n try {\n const url = `http://${req.headers.host}${req.url}`;\n const webRequest = new Request(url, {\n method: req.method,\n headers: req.headers,\n body: req.method !== \"GET\" && req.method !== \"HEAD\" ? req : undefined,\n duplex: req.method !== \"GET\" && req.method !== \"HEAD\" ? \"half\" : undefined,\n } as RequestInit);\n\n const result = await rpcHandler.handle(webRequest, {\n prefix: `/api/rpc${rpcPrefix}`,\n context: {},\n });\n\n if (result.response) {\n res.status(result.response.status);\n result.response.headers.forEach((value: string, key: string) => {\n res.setHeader(key, value);\n });\n const text = await result.response.text();\n res.send(text);\n } else {\n res.status(404).send(\"Not Found\");\n }\n } catch (error) {\n console.error(\"RPC error:\", error);\n res.status(500).json({ error: (error as Error).message });\n }\n });\n\n devServer.app.all(\"/api\", handleApiRequest);\n devServer.app.all(\"/api/*\", handleApiRequest);\n}\n"],"mappings":";AAEA,MAAM,cAAc;CAClB,+BAA+B;CAC/B,gCAAgC;CAChC,gCAAgC;CACjC;AAED,MAAM,oBAAoB,QAAa;AACrC,QAAO,QAAQ,YAAY,CAAC,SAAS,CAAC,KAAK,WAAW;AACpD,MAAI,UAAU,KAAK,MAAM;GACzB;;AAGJ,MAAM,mBAAmB,WAA4B;AACnD,KAAI,CAAC,OAAQ,QAAO;CACpB,MAAM,UAAU,OAAO,QAAQ,cAAc,GAAG;AAChD,QAAO,UAAU,IAAI,YAAY;;AAGnC,SAAgB,sBACd,WACA,YACA,WACA,MACA;CACA,MAAM,YAAY,gBAAgB,WAAW,OAAO;CACpD,MAAM,WAAmC;EAAE,KAAK;EAAM,KAAK;EAAM;CACjE,IAAI,UAAwC;CAE5C,MAAM,iBAAiB,YAAY;AACjC,MAAI,SAAS;AACX,SAAM,SAAS;AACf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nB,SAAS,EAAE;IACZ,CAAC;AAEF,OAAI,OAAO,UAAU;AACnB,QAAI,OAAO,OAAO,SAAS,OAAO;AAClC,WAAO,SAAS,QAAQ,SAAS,OAAe,QAAgB;AAC9D,SAAI,UAAU,KAAK,MAAM;MACzB;IACF,MAAM,OAAO,MAAM,OAAO,SAAS,MAAM;AACzC,QAAI,KAAK,KAAK;SAEd,KAAI,OAAO,IAAI,CAAC,KAAK,YAAY;WAE5B,OAAO;AACd,WAAQ,MAAM,cAAc,MAAM;AAClC,OAAI,OAAO,IAAI,CAAC,KAAK,EAAE,OAAQ,MAAgB,SAAS,CAAC;;GAE3D;AAEF,WAAU,IAAI,IAAI,QAAQ,iBAAiB;AAC3C,WAAU,IAAI,IAAI,UAAU,iBAAiB"}
|
|
1
|
+
{"version":3,"file":"dev-server-middleware.mjs","names":[],"sources":["../../../src/build/rspack/dev-server-middleware.ts"],"sourcesContent":["import type { PluginInfo } from \"./utils\";\n\nconst corsHeaders = {\n \"Access-Control-Allow-Origin\": \"*\",\n \"Access-Control-Allow-Methods\": \"GET, POST, PUT, DELETE, PATCH, OPTIONS\",\n \"Access-Control-Allow-Headers\": \"X-Requested-With, content-type, Authorization\",\n};\n\nconst applyCorsHeaders = (res: any) => {\n Object.entries(corsHeaders).forEach(([key, value]) => {\n res.setHeader(key, value);\n });\n};\n\nconst normalizePrefix = (prefix?: string): string => {\n if (!prefix) return \"\";\n const cleaned = prefix.replace(/^\\/+|\\/+$/g, \"\");\n return cleaned ? `/${cleaned}` : \"\";\n};\n\nexport function setupPluginMiddleware(\n devServer: any,\n pluginInfo: PluginInfo,\n devConfig: any,\n port: number,\n) {\n const rpcPrefix = normalizePrefix(devConfig?.prefix);\n const handlers: { rpc: any; api: any } = { rpc: null, api: null };\n let cleanup: (() => Promise<void>) | null = null;\n\n const performCleanup = async () => {\n if (cleanup) {\n await cleanup();\n cleanup = null;\n }\n };\n\n (async () => {\n await performCleanup();\n\n try {\n const { createPluginRuntime } = await import(\"every-plugin\");\n const { RPCHandler } = await import(\"@orpc/server/fetch\");\n const { OpenAPIHandler } = await import(\"@orpc/openapi/fetch\");\n const { OpenAPIReferencePlugin } = await import(\"@orpc/openapi/plugins\");\n const { ZodToJsonSchemaConverter } = await import(\"@orpc/zod/zod4\");\n const { onError } = await import(\"every-plugin/orpc\");\n const { formatORPCError } = await import(\"every-plugin/errors\");\n\n const pluginId = devConfig?.pluginId || pluginInfo.normalizedName;\n\n const runtime = createPluginRuntime({\n registry: {\n [pluginId]: {\n remote: `http://localhost:${port}/remoteEntry.js`,\n },\n },\n });\n\n const defaultConfig = { variables: {}, secrets: {} };\n\n // @ts-expect-error we don't know the plugin id\n const loaded = await runtime.usePlugin(pluginId, (devConfig?.config ?? defaultConfig) as any);\n\n cleanup = async () => {\n if (runtime) await runtime.shutdown();\n handlers.rpc = null;\n handlers.api = null;\n if (devServer.app.locals.handlers) {\n devServer.app.locals.handlers = null;\n }\n };\n\n handlers.rpc = new RPCHandler(loaded.router, {\n interceptors: [\n onError((error: any) => {\n formatORPCError(error);\n }),\n ],\n });\n\n handlers.api = new OpenAPIHandler(loaded.router, {\n plugins: [\n new OpenAPIReferencePlugin({\n schemaConverters: [new ZodToJsonSchemaConverter()],\n }),\n ],\n interceptors: [\n onError((error: any) => {\n formatORPCError(error);\n }),\n ],\n });\n\n console.log(`╭─────────────────────────────────────────────`);\n console.log(`│ ✅ Plugin dev server ready: `);\n console.log(`├─────────────────────────────────────────────`);\n console.log(`│ 📡 RPC: http://localhost:${port}/api/rpc${rpcPrefix}`);\n console.log(`│ 📖 Docs: http://localhost:${port}/api`);\n console.log(`│ 💚 Health: http://localhost:${port}/`);\n console.log(`╰─────────────────────────────────────────────`);\n\n devServer.app.locals.handlers = handlers;\n\n if (devServer.server) {\n devServer.server.once(\"close\", async () => {\n await performCleanup();\n });\n }\n } catch (error) {\n console.error(\"❌ Failed to load plugin:\", error);\n await performCleanup();\n }\n })().catch((err) => {\n console.error(\"❌ Plugin dev server fatal error:\", err);\n });\n\n process.once(\"SIGINT\", async () => {\n const timeout = setTimeout(() => process.exit(0), 3000);\n await performCleanup();\n clearTimeout(timeout);\n });\n process.once(\"SIGTERM\", async () => {\n const timeout = setTimeout(() => process.exit(0), 3000);\n await performCleanup();\n clearTimeout(timeout);\n });\n\n devServer.app.options(\"*\", (_req: any, res: any) => {\n applyCorsHeaders(res);\n res.status(200).end();\n });\n\n devServer.app.get(\"/\", (_req: any, res: any) => {\n applyCorsHeaders(res);\n res.json({\n ok: true,\n plugin: pluginInfo.normalizedName,\n version: pluginInfo.version,\n status: devServer.app.locals.handlers?.rpc ? \"ready\" : \"loading\",\n endpoints: {\n health: \"/\",\n docs: \"/api\",\n rpc: `/api/rpc${rpcPrefix}`,\n },\n });\n });\n\n devServer.app.get(\"/health\", (_req: any, res: any) => {\n applyCorsHeaders(res);\n res.status(200).send(\"OK\");\n });\n\n const buildDevContext = (_req: any, webRequest: Request) => {\n const rawClone =\n webRequest.method === \"GET\" || webRequest.method === \"HEAD\" ? null : webRequest.clone();\n let cachedRawBody: string | null = null;\n return {\n reqHeaders: webRequest.headers,\n getRawBody: async (): Promise<string> => {\n if (cachedRawBody !== null) return cachedRawBody;\n if (!rawClone) {\n cachedRawBody = \"\";\n return cachedRawBody;\n }\n cachedRawBody = await rawClone.text();\n return cachedRawBody;\n },\n };\n };\n\n const handleApiRequest = async (req: any, res: any) => {\n applyCorsHeaders(res);\n const apiHandler = devServer.app.locals.handlers?.api;\n if (!apiHandler) {\n return res.status(503).json({ error: \"Plugin still loading...\" });\n }\n\n try {\n const url = `http://${req.headers.host}${req.url}`;\n const webRequest = new Request(url, {\n method: req.method,\n headers: req.headers,\n body: req.method !== \"GET\" && req.method !== \"HEAD\" ? req : undefined,\n duplex: req.method !== \"GET\" && req.method !== \"HEAD\" ? \"half\" : undefined,\n } as RequestInit);\n\n const result = await apiHandler.handle(webRequest, {\n prefix: \"/api\",\n context: buildDevContext(req, webRequest),\n });\n\n if (result.response) {\n res.status(result.response.status);\n result.response.headers.forEach((value: string, key: string) => {\n res.setHeader(key, value);\n });\n const text = await result.response.text();\n res.send(text);\n } else {\n res.status(404).send(\"Not Found\");\n }\n } catch (error) {\n console.error(\"OpenAPI error:\", error);\n res.status(500).json({ error: (error as Error).message });\n }\n };\n\n devServer.app.all(`/api/rpc${rpcPrefix}/*`, async (req: any, res: any) => {\n applyCorsHeaders(res);\n const rpcHandler = devServer.app.locals.handlers?.rpc;\n if (!rpcHandler) {\n return res.status(503).json({ error: \"Plugin still loading...\" });\n }\n\n try {\n const url = `http://${req.headers.host}${req.url}`;\n const webRequest = new Request(url, {\n method: req.method,\n headers: req.headers,\n body: req.method !== \"GET\" && req.method !== \"HEAD\" ? req : undefined,\n duplex: req.method !== \"GET\" && req.method !== \"HEAD\" ? \"half\" : undefined,\n } as RequestInit);\n\n const result = await rpcHandler.handle(webRequest, {\n prefix: `/api/rpc${rpcPrefix}`,\n context: buildDevContext(req, webRequest),\n });\n\n if (result.response) {\n res.status(result.response.status);\n result.response.headers.forEach((value: string, key: string) => {\n res.setHeader(key, value);\n });\n const text = await result.response.text();\n res.send(text);\n } else {\n res.status(404).send(\"Not Found\");\n }\n } catch (error) {\n console.error(\"RPC error:\", error);\n res.status(500).json({ error: (error as Error).message });\n }\n });\n\n devServer.app.all(\"/api\", handleApiRequest);\n devServer.app.all(\"/api/*\", handleApiRequest);\n}\n"],"mappings":";AAEA,MAAM,cAAc;CAClB,+BAA+B;CAC/B,gCAAgC;CAChC,gCAAgC;CACjC;AAED,MAAM,oBAAoB,QAAa;AACrC,QAAO,QAAQ,YAAY,CAAC,SAAS,CAAC,KAAK,WAAW;AACpD,MAAI,UAAU,KAAK,MAAM;GACzB;;AAGJ,MAAM,mBAAmB,WAA4B;AACnD,KAAI,CAAC,OAAQ,QAAO;CACpB,MAAM,UAAU,OAAO,QAAQ,cAAc,GAAG;AAChD,QAAO,UAAU,IAAI,YAAY;;AAGnC,SAAgB,sBACd,WACA,YACA,WACA,MACA;CACA,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K,OAAO,KAAU,QAAa;AACxE,mBAAiB,IAAI;EACrB,MAAM,aAAa,UAAU,IAAI,OAAO,UAAU;AAClD,MAAI,CAAC,WACH,QAAO,IAAI,OAAO,IAAI,CAAC,KAAK,EAAE,OAAO,2BAA2B,CAAC;AAGnE,MAAI;GACF,MAAM,MAAM,UAAU,IAAI,QAAQ,OAAO,IAAI;GAC7C,MAAM,aAAa,IAAI,QAAQ,KAAK;IAClC,QAAQ,IAAI;IACZ,SAAS,IAAI;IACb,MAAM,IAAI,WAAW,SAAS,IAAI,WAAW,SAAS,MAAM;IAC5D,QAAQ,IAAI,WAAW,SAAS,IAAI,WAAW,SAAS,SAAS;IAClE,CAAgB;GAEjB,MAAM,SAAS,MAAM,WAAW,OAAO,YAAY;IACjD,QAAQ,WAAW;IACnB,SAAS,gBAAgB,KAAK,WAAW;IAC1C,CAAC;AAEF,OAAI,OAAO,UAAU;AACnB,QAAI,OAAO,OAAO,SAAS,OAAO;AAClC,WAAO,SAAS,QAAQ,SAAS,OAAe,QAAgB;AAC9D,SAAI,UAAU,KAAK,MAAM;MACzB;IACF,MAAM,OAAO,MAAM,OAAO,SAAS,MAAM;AACzC,QAAI,KAAK,KAAK;SAEd,KAAI,OAAO,IAAI,CAAC,KAAK,YAAY;WAE5B,OAAO;AACd,WAAQ,MAAM,cAAc,MAAM;AAClC,OAAI,OAAO,IAAI,CAAC,KAAK,EAAE,OAAQ,MAAgB,SAAS,CAAC;;GAE3D;AAEF,WAAU,IAAI,IAAI,QAAQ,iBAAiB;AAC3C,WAAU,IAAI,IAAI,UAAU,iBAAiB"}
|
|
@@ -12,7 +12,7 @@ function getInstalledPackageVersion(packageName, fallbackRange) {
|
|
|
12
12
|
return match ? match[0] : fallbackRange.replace(/^[\^~>=<\s]+/, "");
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
-
const PLUGIN_VERSION = "2.5.
|
|
15
|
+
const PLUGIN_VERSION = "2.5.7";
|
|
16
16
|
const SHARE_CONFIG = {
|
|
17
17
|
singleton: true,
|
|
18
18
|
requiredVersion: false,
|
|
@@ -10,7 +10,7 @@ function getInstalledPackageVersion(packageName, fallbackRange) {
|
|
|
10
10
|
return match ? match[0] : fallbackRange.replace(/^[\^~>=<\s]+/, "");
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
-
const PLUGIN_VERSION = "2.5.
|
|
13
|
+
const PLUGIN_VERSION = "2.5.7";
|
|
14
14
|
const SHARE_CONFIG = {
|
|
15
15
|
singleton: true,
|
|
16
16
|
requiredVersion: false,
|
package/package.json
CHANGED
|
@@ -151,6 +151,24 @@ export function setupPluginMiddleware(
|
|
|
151
151
|
res.status(200).send("OK");
|
|
152
152
|
});
|
|
153
153
|
|
|
154
|
+
const buildDevContext = (_req: any, webRequest: Request) => {
|
|
155
|
+
const rawClone =
|
|
156
|
+
webRequest.method === "GET" || webRequest.method === "HEAD" ? null : webRequest.clone();
|
|
157
|
+
let cachedRawBody: string | null = null;
|
|
158
|
+
return {
|
|
159
|
+
reqHeaders: webRequest.headers,
|
|
160
|
+
getRawBody: async (): Promise<string> => {
|
|
161
|
+
if (cachedRawBody !== null) return cachedRawBody;
|
|
162
|
+
if (!rawClone) {
|
|
163
|
+
cachedRawBody = "";
|
|
164
|
+
return cachedRawBody;
|
|
165
|
+
}
|
|
166
|
+
cachedRawBody = await rawClone.text();
|
|
167
|
+
return cachedRawBody;
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
|
|
154
172
|
const handleApiRequest = async (req: any, res: any) => {
|
|
155
173
|
applyCorsHeaders(res);
|
|
156
174
|
const apiHandler = devServer.app.locals.handlers?.api;
|
|
@@ -169,7 +187,7 @@ export function setupPluginMiddleware(
|
|
|
169
187
|
|
|
170
188
|
const result = await apiHandler.handle(webRequest, {
|
|
171
189
|
prefix: "/api",
|
|
172
|
-
context:
|
|
190
|
+
context: buildDevContext(req, webRequest),
|
|
173
191
|
});
|
|
174
192
|
|
|
175
193
|
if (result.response) {
|
|
@@ -206,7 +224,7 @@ export function setupPluginMiddleware(
|
|
|
206
224
|
|
|
207
225
|
const result = await rpcHandler.handle(webRequest, {
|
|
208
226
|
prefix: `/api/rpc${rpcPrefix}`,
|
|
209
|
-
context:
|
|
227
|
+
context: buildDevContext(req, webRequest),
|
|
210
228
|
});
|
|
211
229
|
|
|
212
230
|
if (result.response) {
|