@shiftapi/vite-plugin 0.0.8 → 0.0.10
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/index.js +32 -19
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
mkdirSync,
|
|
7
7
|
existsSync
|
|
8
8
|
} from "fs";
|
|
9
|
+
import { createRequire } from "module";
|
|
9
10
|
import { spawn } from "child_process";
|
|
10
11
|
import { createServer as createTcpServer } from "net";
|
|
11
12
|
|
|
@@ -61,13 +62,14 @@ async function generateTypes(spec) {
|
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
// src/virtualModule.ts
|
|
64
|
-
function buildVirtualModuleSource(baseUrl) {
|
|
65
|
+
function buildVirtualModuleSource(baseUrl, devApiPrefix) {
|
|
66
|
+
const baseUrlExpr = devApiPrefix ? `import.meta.env.VITE_SHIFTAPI_BASE_URL || (import.meta.env.DEV ? ${JSON.stringify(devApiPrefix)} : ${JSON.stringify(baseUrl)})` : `import.meta.env.VITE_SHIFTAPI_BASE_URL || ${JSON.stringify(baseUrl)}`;
|
|
65
67
|
return `// Auto-generated by @shiftapi/vite-plugin
|
|
66
68
|
import createClient from "openapi-fetch";
|
|
67
69
|
|
|
68
70
|
/** Pre-configured, fully-typed API client. */
|
|
69
71
|
export const client = createClient({
|
|
70
|
-
baseUrl:
|
|
72
|
+
baseUrl: ${baseUrlExpr},
|
|
71
73
|
});
|
|
72
74
|
|
|
73
75
|
export { createClient };
|
|
@@ -75,8 +77,10 @@ export { createClient };
|
|
|
75
77
|
}
|
|
76
78
|
|
|
77
79
|
// src/index.ts
|
|
80
|
+
var pluginRequire = createRequire(import.meta.url);
|
|
78
81
|
var MODULE_ID = "@shiftapi/client";
|
|
79
82
|
var RESOLVED_MODULE_ID = "\0" + MODULE_ID;
|
|
83
|
+
var DEV_API_PREFIX = "/__shiftapi";
|
|
80
84
|
function isPortFree(port) {
|
|
81
85
|
return new Promise((resolve2) => {
|
|
82
86
|
const server = createTcpServer();
|
|
@@ -113,6 +117,7 @@ function shiftapiPlugin(options) {
|
|
|
113
117
|
let goProcess = null;
|
|
114
118
|
let debounceTimer = null;
|
|
115
119
|
let projectRoot = process.cwd();
|
|
120
|
+
let isDev = false;
|
|
116
121
|
function getSpec() {
|
|
117
122
|
if (!cachedSpec) {
|
|
118
123
|
cachedSpec = extractSpec(
|
|
@@ -130,7 +135,10 @@ function shiftapiPlugin(options) {
|
|
|
130
135
|
return false;
|
|
131
136
|
}
|
|
132
137
|
generatedDts = types;
|
|
133
|
-
virtualModuleSource = buildVirtualModuleSource(
|
|
138
|
+
virtualModuleSource = buildVirtualModuleSource(
|
|
139
|
+
baseUrl,
|
|
140
|
+
isDev ? DEV_API_PREFIX : void 0
|
|
141
|
+
);
|
|
134
142
|
return true;
|
|
135
143
|
}
|
|
136
144
|
function writeDtsFile() {
|
|
@@ -245,32 +253,34 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
|
|
|
245
253
|
},
|
|
246
254
|
async config(_, env) {
|
|
247
255
|
if (env.command === "serve") {
|
|
256
|
+
isDev = true;
|
|
248
257
|
goPort = await findFreePort(basePort);
|
|
249
258
|
if (goPort !== basePort) {
|
|
250
259
|
console.log(
|
|
251
260
|
`[shiftapi] Port ${basePort} is in use, using ${goPort}`
|
|
252
261
|
);
|
|
253
262
|
}
|
|
263
|
+
const targetUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}:${goPort}`;
|
|
264
|
+
return {
|
|
265
|
+
server: {
|
|
266
|
+
proxy: {
|
|
267
|
+
[DEV_API_PREFIX]: {
|
|
268
|
+
target: targetUrl,
|
|
269
|
+
rewrite: (path) => path.replace(new RegExp(`^${DEV_API_PREFIX}`), "") || "/"
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
};
|
|
254
274
|
}
|
|
255
|
-
const spec = getSpec();
|
|
256
|
-
const paths = spec.paths;
|
|
257
|
-
if (!paths) {
|
|
258
|
-
console.warn("[shiftapi] No paths found in OpenAPI spec. Proxy will not be configured.");
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
const targetUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}:${goPort}`;
|
|
262
|
-
const proxy = {};
|
|
263
|
-
for (const path of Object.keys(paths)) {
|
|
264
|
-
proxy[path] = targetUrl;
|
|
265
|
-
}
|
|
266
|
-
return {
|
|
267
|
-
server: { proxy }
|
|
268
|
-
};
|
|
269
275
|
},
|
|
270
276
|
configureServer(server) {
|
|
271
277
|
devServer = server;
|
|
272
278
|
server.watcher.add(resolve(goRoot));
|
|
273
|
-
startGoServer().
|
|
279
|
+
startGoServer().then(() => {
|
|
280
|
+
console.log(
|
|
281
|
+
`[shiftapi] API docs available at http://localhost:${goPort}/docs`
|
|
282
|
+
);
|
|
283
|
+
}).catch((err) => {
|
|
274
284
|
console.error("[shiftapi] Go server failed to start:", err);
|
|
275
285
|
});
|
|
276
286
|
function clearDebounce() {
|
|
@@ -309,10 +319,13 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
|
|
|
309
319
|
await regenerate();
|
|
310
320
|
writeDtsFile();
|
|
311
321
|
},
|
|
312
|
-
resolveId(id) {
|
|
322
|
+
resolveId(id, importer) {
|
|
313
323
|
if (id === MODULE_ID) {
|
|
314
324
|
return RESOLVED_MODULE_ID;
|
|
315
325
|
}
|
|
326
|
+
if (id === "openapi-fetch" && importer === RESOLVED_MODULE_ID) {
|
|
327
|
+
return pluginRequire.resolve("openapi-fetch");
|
|
328
|
+
}
|
|
316
329
|
},
|
|
317
330
|
load(id) {
|
|
318
331
|
if (id === RESOLVED_MODULE_ID) {
|
package/package.json
CHANGED