@shiftapi/vite-plugin 0.0.8 → 0.0.9

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.
Files changed (2) hide show
  1. package/dist/index.js +26 -18
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -61,13 +61,14 @@ async function generateTypes(spec) {
61
61
  }
62
62
 
63
63
  // src/virtualModule.ts
64
- function buildVirtualModuleSource(baseUrl) {
64
+ function buildVirtualModuleSource(baseUrl, devApiPrefix) {
65
+ 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
66
  return `// Auto-generated by @shiftapi/vite-plugin
66
67
  import createClient from "openapi-fetch";
67
68
 
68
69
  /** Pre-configured, fully-typed API client. */
69
70
  export const client = createClient({
70
- baseUrl: import.meta.env.VITE_SHIFTAPI_BASE_URL || ${JSON.stringify(baseUrl)},
71
+ baseUrl: ${baseUrlExpr},
71
72
  });
72
73
 
73
74
  export { createClient };
@@ -77,6 +78,7 @@ export { createClient };
77
78
  // src/index.ts
78
79
  var MODULE_ID = "@shiftapi/client";
79
80
  var RESOLVED_MODULE_ID = "\0" + MODULE_ID;
81
+ var DEV_API_PREFIX = "/__shiftapi";
80
82
  function isPortFree(port) {
81
83
  return new Promise((resolve2) => {
82
84
  const server = createTcpServer();
@@ -113,6 +115,7 @@ function shiftapiPlugin(options) {
113
115
  let goProcess = null;
114
116
  let debounceTimer = null;
115
117
  let projectRoot = process.cwd();
118
+ let isDev = false;
116
119
  function getSpec() {
117
120
  if (!cachedSpec) {
118
121
  cachedSpec = extractSpec(
@@ -130,7 +133,10 @@ function shiftapiPlugin(options) {
130
133
  return false;
131
134
  }
132
135
  generatedDts = types;
133
- virtualModuleSource = buildVirtualModuleSource(baseUrl);
136
+ virtualModuleSource = buildVirtualModuleSource(
137
+ baseUrl,
138
+ isDev ? DEV_API_PREFIX : void 0
139
+ );
134
140
  return true;
135
141
  }
136
142
  function writeDtsFile() {
@@ -245,32 +251,34 @@ ${generatedDts.split("\n").map((line) => line ? " " + line : line).join("\n")}
245
251
  },
246
252
  async config(_, env) {
247
253
  if (env.command === "serve") {
254
+ isDev = true;
248
255
  goPort = await findFreePort(basePort);
249
256
  if (goPort !== basePort) {
250
257
  console.log(
251
258
  `[shiftapi] Port ${basePort} is in use, using ${goPort}`
252
259
  );
253
260
  }
261
+ const targetUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}:${goPort}`;
262
+ return {
263
+ server: {
264
+ proxy: {
265
+ [DEV_API_PREFIX]: {
266
+ target: targetUrl,
267
+ rewrite: (path) => path.replace(new RegExp(`^${DEV_API_PREFIX}`), "") || "/"
268
+ }
269
+ }
270
+ }
271
+ };
254
272
  }
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
273
  },
270
274
  configureServer(server) {
271
275
  devServer = server;
272
276
  server.watcher.add(resolve(goRoot));
273
- startGoServer().catch((err) => {
277
+ startGoServer().then(() => {
278
+ console.log(
279
+ `[shiftapi] API docs available at http://localhost:${goPort}/docs`
280
+ );
281
+ }).catch((err) => {
274
282
  console.error("[shiftapi] Go server failed to start:", err);
275
283
  });
276
284
  function clearDebounce() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shiftapi/vite-plugin",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "Vite plugin for fully-typed TypeScript clients from shiftapi Go servers",
5
5
  "author": "Frank Chiarulli Jr. <frank@frankchiarulli.com>",
6
6
  "license": "MIT",