shiftapi 0.0.14 → 0.0.15
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/LICENSE +21 -0
- package/package.json +7 -7
- package/dist/chunk-32WL5S5C.js +0 -231
- package/dist/chunk-4ZZ6RIUO.js +0 -231
- package/dist/chunk-AYZYNLT6.js +0 -61
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Frank Chiarulli Jr.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "shiftapi",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.15",
|
|
4
4
|
"description": "CLI and codegen for shiftapi – fully-typed TypeScript clients from Go servers",
|
|
5
5
|
"author": "Frank Chiarulli Jr. <frank@frankchiarulli.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,11 +32,6 @@
|
|
|
32
32
|
"files": [
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
|
-
"scripts": {
|
|
36
|
-
"build": "tsup src/index.ts src/internal.ts src/prepare.ts --format esm --dts",
|
|
37
|
-
"dev": "tsup src/index.ts src/internal.ts src/prepare.ts --format esm --dts --watch",
|
|
38
|
-
"test": "vitest run"
|
|
39
|
-
},
|
|
40
35
|
"dependencies": {
|
|
41
36
|
"comment-json": "^4.5.1",
|
|
42
37
|
"jiti": "^2.6.1",
|
|
@@ -47,5 +42,10 @@
|
|
|
47
42
|
"tsup": "^8.0.0",
|
|
48
43
|
"typescript": "^5.5.0",
|
|
49
44
|
"vitest": "^2.0.0"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsup src/index.ts src/internal.ts src/prepare.ts --format esm --dts",
|
|
48
|
+
"dev": "tsup src/index.ts src/internal.ts src/prepare.ts --format esm --dts --watch",
|
|
49
|
+
"test": "vitest run"
|
|
50
50
|
}
|
|
51
|
-
}
|
|
51
|
+
}
|
package/dist/chunk-32WL5S5C.js
DELETED
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
// src/config.ts
|
|
2
|
-
import { existsSync } from "fs";
|
|
3
|
-
import { resolve, dirname } from "path";
|
|
4
|
-
import { createRequire } from "module";
|
|
5
|
-
function defineConfig(config) {
|
|
6
|
-
return config;
|
|
7
|
-
}
|
|
8
|
-
var CONFIG_FILENAMES = [
|
|
9
|
-
"shiftapi.config.ts",
|
|
10
|
-
"shiftapi.config.js",
|
|
11
|
-
"shiftapi.config.mjs"
|
|
12
|
-
];
|
|
13
|
-
async function loadConfig(startDir, configPath) {
|
|
14
|
-
let resolvedPath;
|
|
15
|
-
if (configPath) {
|
|
16
|
-
resolvedPath = resolve(startDir, configPath);
|
|
17
|
-
if (!existsSync(resolvedPath)) {
|
|
18
|
-
throw new Error(`[shiftapi] Config file not found: ${resolvedPath}`);
|
|
19
|
-
}
|
|
20
|
-
} else {
|
|
21
|
-
resolvedPath = findConfigFile(startDir);
|
|
22
|
-
if (!resolvedPath) {
|
|
23
|
-
throw new Error(
|
|
24
|
-
`[shiftapi] Could not find shiftapi.config.ts (searched upward from ${startDir}). Create one with: import { defineConfig } from "shiftapi"`
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
const require2 = createRequire(import.meta.url);
|
|
29
|
-
const { createJiti } = require2("jiti");
|
|
30
|
-
const jiti = createJiti(resolvedPath);
|
|
31
|
-
const mod = await jiti.import(resolvedPath);
|
|
32
|
-
const config = mod.default ?? mod;
|
|
33
|
-
if (!config.server) {
|
|
34
|
-
throw new Error(
|
|
35
|
-
`[shiftapi] Config at ${resolvedPath} must specify a "server" field (path to Go entry point)`
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
return { config, configDir: dirname(resolvedPath) };
|
|
39
|
-
}
|
|
40
|
-
function findConfigFile(startDir) {
|
|
41
|
-
let dir = resolve(startDir);
|
|
42
|
-
const root = resolve("/");
|
|
43
|
-
while (true) {
|
|
44
|
-
for (const name of CONFIG_FILENAMES) {
|
|
45
|
-
const candidate = resolve(dir, name);
|
|
46
|
-
if (existsSync(candidate)) {
|
|
47
|
-
return candidate;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
const parent = dirname(dir);
|
|
51
|
-
if (parent === dir || dir === root) {
|
|
52
|
-
return void 0;
|
|
53
|
-
}
|
|
54
|
-
dir = parent;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// src/extract.ts
|
|
59
|
-
import { execFileSync } from "child_process";
|
|
60
|
-
import { readFileSync, unlinkSync, rmSync, mkdtempSync } from "fs";
|
|
61
|
-
import { join } from "path";
|
|
62
|
-
import { tmpdir } from "os";
|
|
63
|
-
function extractSpec(serverEntry, goRoot) {
|
|
64
|
-
const tempDir = mkdtempSync(join(tmpdir(), "shiftapi-"));
|
|
65
|
-
const specPath = join(tempDir, "openapi.json");
|
|
66
|
-
try {
|
|
67
|
-
execFileSync("go", ["run", "-tags", "shiftapidev", serverEntry], {
|
|
68
|
-
cwd: goRoot,
|
|
69
|
-
env: {
|
|
70
|
-
...process.env,
|
|
71
|
-
SHIFTAPI_EXPORT_SPEC: specPath
|
|
72
|
-
},
|
|
73
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
74
|
-
timeout: 3e4
|
|
75
|
-
});
|
|
76
|
-
} catch (err) {
|
|
77
|
-
const stderr = err instanceof Error && "stderr" in err ? String(err.stderr) : "";
|
|
78
|
-
throw new Error(
|
|
79
|
-
`shiftapi: Failed to extract OpenAPI spec.
|
|
80
|
-
Command: go run ${serverEntry}
|
|
81
|
-
CWD: ${goRoot}
|
|
82
|
-
Error: ${stderr || String(err)}`
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
let raw;
|
|
86
|
-
try {
|
|
87
|
-
raw = readFileSync(specPath, "utf-8");
|
|
88
|
-
} catch {
|
|
89
|
-
throw new Error(
|
|
90
|
-
`shiftapi: Spec file was not created at ${specPath}.
|
|
91
|
-
Make sure your Go server calls shiftapi.ListenAndServe().`
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
try {
|
|
95
|
-
unlinkSync(specPath);
|
|
96
|
-
rmSync(tempDir, { recursive: true });
|
|
97
|
-
} catch {
|
|
98
|
-
}
|
|
99
|
-
return JSON.parse(raw);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// src/generate.ts
|
|
103
|
-
import openapiTS, { astToString } from "openapi-typescript";
|
|
104
|
-
async function generateTypes(spec) {
|
|
105
|
-
const ast = await openapiTS(spec);
|
|
106
|
-
return astToString(ast);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// src/constants.ts
|
|
110
|
-
var MODULE_ID = "@shiftapi/client";
|
|
111
|
-
var RESOLVED_MODULE_ID = "\0" + MODULE_ID;
|
|
112
|
-
var DEV_API_PREFIX = "/__shiftapi";
|
|
113
|
-
|
|
114
|
-
// src/templates.ts
|
|
115
|
-
function indent(text, spaces = 2) {
|
|
116
|
-
const prefix = " ".repeat(spaces);
|
|
117
|
-
return text.split("\n").map((line) => line ? prefix + line : line).join("\n");
|
|
118
|
-
}
|
|
119
|
-
function dtsTemplate(generatedTypes) {
|
|
120
|
-
return `// Auto-generated by shiftapi. Do not edit.
|
|
121
|
-
declare module "@shiftapi/client" {
|
|
122
|
-
${indent(generatedTypes)}
|
|
123
|
-
|
|
124
|
-
import type createClient from "openapi-fetch";
|
|
125
|
-
|
|
126
|
-
export const client: ReturnType<typeof createClient<paths>>;
|
|
127
|
-
export { createClient };
|
|
128
|
-
}
|
|
129
|
-
`;
|
|
130
|
-
}
|
|
131
|
-
function clientJsTemplate(baseUrl) {
|
|
132
|
-
return `// Auto-generated by shiftapi. Do not edit.
|
|
133
|
-
import createClient from "openapi-fetch";
|
|
134
|
-
|
|
135
|
-
/** Pre-configured, fully-typed API client. */
|
|
136
|
-
export const client = createClient({
|
|
137
|
-
baseUrl: ${JSON.stringify(baseUrl)},
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
export { createClient };
|
|
141
|
-
`;
|
|
142
|
-
}
|
|
143
|
-
function virtualModuleTemplate(baseUrl, devApiPrefix) {
|
|
144
|
-
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)}`;
|
|
145
|
-
return `// Auto-generated by @shiftapi/vite-plugin
|
|
146
|
-
import createClient from "openapi-fetch";
|
|
147
|
-
|
|
148
|
-
/** Pre-configured, fully-typed API client. */
|
|
149
|
-
export const client = createClient({
|
|
150
|
-
baseUrl: ${baseUrlExpr},
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
export { createClient };
|
|
154
|
-
`;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// src/codegen.ts
|
|
158
|
-
import { resolve as resolve2, relative } from "path";
|
|
159
|
-
import { writeFileSync, readFileSync as readFileSync2, mkdirSync, existsSync as existsSync2 } from "fs";
|
|
160
|
-
import { parse, stringify } from "comment-json";
|
|
161
|
-
async function regenerateTypes(serverEntry, goRoot, baseUrl, isDev, previousTypes) {
|
|
162
|
-
const spec = extractSpec(serverEntry, resolve2(goRoot));
|
|
163
|
-
const types = await generateTypes(spec);
|
|
164
|
-
const changed = types !== previousTypes;
|
|
165
|
-
const virtualModuleSource = virtualModuleTemplate(
|
|
166
|
-
baseUrl,
|
|
167
|
-
isDev ? DEV_API_PREFIX : void 0
|
|
168
|
-
);
|
|
169
|
-
return { types, virtualModuleSource, changed };
|
|
170
|
-
}
|
|
171
|
-
function writeGeneratedFiles(typesRoot, generatedDts, baseUrl) {
|
|
172
|
-
const outDir = resolve2(typesRoot, ".shiftapi");
|
|
173
|
-
if (!existsSync2(outDir)) {
|
|
174
|
-
mkdirSync(outDir, { recursive: true });
|
|
175
|
-
}
|
|
176
|
-
writeFileSync(resolve2(outDir, "client.d.ts"), dtsTemplate(generatedDts));
|
|
177
|
-
writeFileSync(resolve2(outDir, "client.js"), clientJsTemplate(baseUrl));
|
|
178
|
-
writeFileSync(
|
|
179
|
-
resolve2(outDir, "tsconfig.json"),
|
|
180
|
-
JSON.stringify(
|
|
181
|
-
{
|
|
182
|
-
compilerOptions: {
|
|
183
|
-
paths: {
|
|
184
|
-
[MODULE_ID]: ["./client.d.ts"]
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
},
|
|
188
|
-
null,
|
|
189
|
-
2
|
|
190
|
-
) + "\n"
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
function patchTsConfig(tsconfigDir, typesRoot) {
|
|
194
|
-
const tsconfigPath = resolve2(tsconfigDir, "tsconfig.json");
|
|
195
|
-
if (!existsSync2(tsconfigPath)) return;
|
|
196
|
-
const raw = readFileSync2(tsconfigPath, "utf-8");
|
|
197
|
-
let tsconfig;
|
|
198
|
-
try {
|
|
199
|
-
tsconfig = parse(raw);
|
|
200
|
-
} catch (err) {
|
|
201
|
-
console.warn(
|
|
202
|
-
`[shiftapi] Failed to parse tsconfig.json: ${err instanceof Error ? err.message : String(err)}`
|
|
203
|
-
);
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
const rel = relative(tsconfigDir, resolve2(typesRoot, ".shiftapi", "tsconfig.json"));
|
|
207
|
-
const extendsPath = rel.startsWith("..") ? rel : `./${rel}`;
|
|
208
|
-
if (tsconfig?.extends === extendsPath) return;
|
|
209
|
-
tsconfig.extends = extendsPath;
|
|
210
|
-
const detectedIndent = raw.match(/^[ \t]+/m)?.[0] ?? " ";
|
|
211
|
-
writeFileSync(tsconfigPath, stringify(tsconfig, null, detectedIndent) + "\n");
|
|
212
|
-
console.log(
|
|
213
|
-
"[shiftapi] Updated tsconfig.json to extend .shiftapi/tsconfig.json"
|
|
214
|
-
);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
export {
|
|
218
|
-
defineConfig,
|
|
219
|
-
loadConfig,
|
|
220
|
-
extractSpec,
|
|
221
|
-
generateTypes,
|
|
222
|
-
MODULE_ID,
|
|
223
|
-
RESOLVED_MODULE_ID,
|
|
224
|
-
DEV_API_PREFIX,
|
|
225
|
-
dtsTemplate,
|
|
226
|
-
clientJsTemplate,
|
|
227
|
-
virtualModuleTemplate,
|
|
228
|
-
regenerateTypes,
|
|
229
|
-
writeGeneratedFiles,
|
|
230
|
-
patchTsConfig
|
|
231
|
-
};
|
package/dist/chunk-4ZZ6RIUO.js
DELETED
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
// src/config.ts
|
|
2
|
-
import { existsSync } from "fs";
|
|
3
|
-
import { resolve, dirname } from "path";
|
|
4
|
-
import { createRequire } from "module";
|
|
5
|
-
function defineConfig(config) {
|
|
6
|
-
return config;
|
|
7
|
-
}
|
|
8
|
-
var CONFIG_FILENAMES = [
|
|
9
|
-
"shiftapi.config.ts",
|
|
10
|
-
"shiftapi.config.js",
|
|
11
|
-
"shiftapi.config.mjs"
|
|
12
|
-
];
|
|
13
|
-
async function loadConfig(startDir, configPath) {
|
|
14
|
-
let resolvedPath;
|
|
15
|
-
if (configPath) {
|
|
16
|
-
resolvedPath = resolve(startDir, configPath);
|
|
17
|
-
if (!existsSync(resolvedPath)) {
|
|
18
|
-
throw new Error(`[shiftapi] Config file not found: ${resolvedPath}`);
|
|
19
|
-
}
|
|
20
|
-
} else {
|
|
21
|
-
resolvedPath = findConfigFile(startDir);
|
|
22
|
-
if (!resolvedPath) {
|
|
23
|
-
throw new Error(
|
|
24
|
-
`[shiftapi] Could not find shiftapi.config.ts (searched upward from ${startDir}). Create one with: import { defineConfig } from "@shiftapi/cli"`
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
const require2 = createRequire(import.meta.url);
|
|
29
|
-
const { createJiti } = require2("jiti");
|
|
30
|
-
const jiti = createJiti(resolvedPath);
|
|
31
|
-
const mod = await jiti.import(resolvedPath);
|
|
32
|
-
const config = mod.default ?? mod;
|
|
33
|
-
if (!config.server) {
|
|
34
|
-
throw new Error(
|
|
35
|
-
`[shiftapi] Config at ${resolvedPath} must specify a "server" field (path to Go entry point)`
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
return { config, configDir: dirname(resolvedPath) };
|
|
39
|
-
}
|
|
40
|
-
function findConfigFile(startDir) {
|
|
41
|
-
let dir = resolve(startDir);
|
|
42
|
-
const root = resolve("/");
|
|
43
|
-
while (true) {
|
|
44
|
-
for (const name of CONFIG_FILENAMES) {
|
|
45
|
-
const candidate = resolve(dir, name);
|
|
46
|
-
if (existsSync(candidate)) {
|
|
47
|
-
return candidate;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
const parent = dirname(dir);
|
|
51
|
-
if (parent === dir || dir === root) {
|
|
52
|
-
return void 0;
|
|
53
|
-
}
|
|
54
|
-
dir = parent;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// src/extract.ts
|
|
59
|
-
import { execFileSync } from "child_process";
|
|
60
|
-
import { readFileSync, unlinkSync, rmSync, mkdtempSync } from "fs";
|
|
61
|
-
import { join } from "path";
|
|
62
|
-
import { tmpdir } from "os";
|
|
63
|
-
function extractSpec(serverEntry, goRoot) {
|
|
64
|
-
const tempDir = mkdtempSync(join(tmpdir(), "shiftapi-"));
|
|
65
|
-
const specPath = join(tempDir, "openapi.json");
|
|
66
|
-
try {
|
|
67
|
-
execFileSync("go", ["run", "-tags", "shiftapidev", serverEntry], {
|
|
68
|
-
cwd: goRoot,
|
|
69
|
-
env: {
|
|
70
|
-
...process.env,
|
|
71
|
-
SHIFTAPI_EXPORT_SPEC: specPath
|
|
72
|
-
},
|
|
73
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
74
|
-
timeout: 3e4
|
|
75
|
-
});
|
|
76
|
-
} catch (err) {
|
|
77
|
-
const stderr = err instanceof Error && "stderr" in err ? String(err.stderr) : "";
|
|
78
|
-
throw new Error(
|
|
79
|
-
`@shiftapi/cli: Failed to extract OpenAPI spec.
|
|
80
|
-
Command: go run ${serverEntry}
|
|
81
|
-
CWD: ${goRoot}
|
|
82
|
-
Error: ${stderr || String(err)}`
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
let raw;
|
|
86
|
-
try {
|
|
87
|
-
raw = readFileSync(specPath, "utf-8");
|
|
88
|
-
} catch {
|
|
89
|
-
throw new Error(
|
|
90
|
-
`@shiftapi/cli: Spec file was not created at ${specPath}.
|
|
91
|
-
Make sure your Go server calls shiftapi.ListenAndServe().`
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
try {
|
|
95
|
-
unlinkSync(specPath);
|
|
96
|
-
rmSync(tempDir, { recursive: true });
|
|
97
|
-
} catch {
|
|
98
|
-
}
|
|
99
|
-
return JSON.parse(raw);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// src/generate.ts
|
|
103
|
-
import openapiTS, { astToString } from "openapi-typescript";
|
|
104
|
-
async function generateTypes(spec) {
|
|
105
|
-
const ast = await openapiTS(spec);
|
|
106
|
-
return astToString(ast);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// src/constants.ts
|
|
110
|
-
var MODULE_ID = "@shiftapi/client";
|
|
111
|
-
var RESOLVED_MODULE_ID = "\0" + MODULE_ID;
|
|
112
|
-
var DEV_API_PREFIX = "/__shiftapi";
|
|
113
|
-
|
|
114
|
-
// src/templates.ts
|
|
115
|
-
function indent(text, spaces = 2) {
|
|
116
|
-
const prefix = " ".repeat(spaces);
|
|
117
|
-
return text.split("\n").map((line) => line ? prefix + line : line).join("\n");
|
|
118
|
-
}
|
|
119
|
-
function dtsTemplate(generatedTypes) {
|
|
120
|
-
return `// Auto-generated by @shiftapi/cli. Do not edit.
|
|
121
|
-
declare module "@shiftapi/client" {
|
|
122
|
-
${indent(generatedTypes)}
|
|
123
|
-
|
|
124
|
-
import type createClient from "openapi-fetch";
|
|
125
|
-
|
|
126
|
-
export const client: ReturnType<typeof createClient<paths>>;
|
|
127
|
-
export { createClient };
|
|
128
|
-
}
|
|
129
|
-
`;
|
|
130
|
-
}
|
|
131
|
-
function clientJsTemplate(baseUrl) {
|
|
132
|
-
return `// Auto-generated by @shiftapi/cli. Do not edit.
|
|
133
|
-
import createClient from "openapi-fetch";
|
|
134
|
-
|
|
135
|
-
/** Pre-configured, fully-typed API client. */
|
|
136
|
-
export const client = createClient({
|
|
137
|
-
baseUrl: ${JSON.stringify(baseUrl)},
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
export { createClient };
|
|
141
|
-
`;
|
|
142
|
-
}
|
|
143
|
-
function virtualModuleTemplate(baseUrl, devApiPrefix) {
|
|
144
|
-
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)}`;
|
|
145
|
-
return `// Auto-generated by @shiftapi/vite-plugin
|
|
146
|
-
import createClient from "openapi-fetch";
|
|
147
|
-
|
|
148
|
-
/** Pre-configured, fully-typed API client. */
|
|
149
|
-
export const client = createClient({
|
|
150
|
-
baseUrl: ${baseUrlExpr},
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
export { createClient };
|
|
154
|
-
`;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// src/codegen.ts
|
|
158
|
-
import { resolve as resolve2, relative } from "path";
|
|
159
|
-
import { writeFileSync, readFileSync as readFileSync2, mkdirSync, existsSync as existsSync2 } from "fs";
|
|
160
|
-
import { parse, stringify } from "comment-json";
|
|
161
|
-
async function regenerateTypes(serverEntry, goRoot, baseUrl, isDev, previousTypes) {
|
|
162
|
-
const spec = extractSpec(serverEntry, resolve2(goRoot));
|
|
163
|
-
const types = await generateTypes(spec);
|
|
164
|
-
const changed = types !== previousTypes;
|
|
165
|
-
const virtualModuleSource = virtualModuleTemplate(
|
|
166
|
-
baseUrl,
|
|
167
|
-
isDev ? DEV_API_PREFIX : void 0
|
|
168
|
-
);
|
|
169
|
-
return { types, virtualModuleSource, changed };
|
|
170
|
-
}
|
|
171
|
-
function writeGeneratedFiles(typesRoot, generatedDts, baseUrl) {
|
|
172
|
-
const outDir = resolve2(typesRoot, ".shiftapi");
|
|
173
|
-
if (!existsSync2(outDir)) {
|
|
174
|
-
mkdirSync(outDir, { recursive: true });
|
|
175
|
-
}
|
|
176
|
-
writeFileSync(resolve2(outDir, "client.d.ts"), dtsTemplate(generatedDts));
|
|
177
|
-
writeFileSync(resolve2(outDir, "client.js"), clientJsTemplate(baseUrl));
|
|
178
|
-
writeFileSync(
|
|
179
|
-
resolve2(outDir, "tsconfig.json"),
|
|
180
|
-
JSON.stringify(
|
|
181
|
-
{
|
|
182
|
-
compilerOptions: {
|
|
183
|
-
paths: {
|
|
184
|
-
[MODULE_ID]: ["./client.d.ts"]
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
},
|
|
188
|
-
null,
|
|
189
|
-
2
|
|
190
|
-
) + "\n"
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
function patchTsConfig(tsconfigDir, typesRoot) {
|
|
194
|
-
const tsconfigPath = resolve2(tsconfigDir, "tsconfig.json");
|
|
195
|
-
if (!existsSync2(tsconfigPath)) return;
|
|
196
|
-
const raw = readFileSync2(tsconfigPath, "utf-8");
|
|
197
|
-
let tsconfig;
|
|
198
|
-
try {
|
|
199
|
-
tsconfig = parse(raw);
|
|
200
|
-
} catch (err) {
|
|
201
|
-
console.warn(
|
|
202
|
-
`[shiftapi] Failed to parse tsconfig.json: ${err instanceof Error ? err.message : String(err)}`
|
|
203
|
-
);
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
const rel = relative(tsconfigDir, resolve2(typesRoot, ".shiftapi", "tsconfig.json"));
|
|
207
|
-
const extendsPath = rel.startsWith("..") ? rel : `./${rel}`;
|
|
208
|
-
if (tsconfig?.extends === extendsPath) return;
|
|
209
|
-
tsconfig.extends = extendsPath;
|
|
210
|
-
const detectedIndent = raw.match(/^[ \t]+/m)?.[0] ?? " ";
|
|
211
|
-
writeFileSync(tsconfigPath, stringify(tsconfig, null, detectedIndent) + "\n");
|
|
212
|
-
console.log(
|
|
213
|
-
"[shiftapi] Updated tsconfig.json to extend .shiftapi/tsconfig.json"
|
|
214
|
-
);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
export {
|
|
218
|
-
defineConfig,
|
|
219
|
-
loadConfig,
|
|
220
|
-
extractSpec,
|
|
221
|
-
generateTypes,
|
|
222
|
-
MODULE_ID,
|
|
223
|
-
RESOLVED_MODULE_ID,
|
|
224
|
-
DEV_API_PREFIX,
|
|
225
|
-
dtsTemplate,
|
|
226
|
-
clientJsTemplate,
|
|
227
|
-
virtualModuleTemplate,
|
|
228
|
-
regenerateTypes,
|
|
229
|
-
writeGeneratedFiles,
|
|
230
|
-
patchTsConfig
|
|
231
|
-
};
|
package/dist/chunk-AYZYNLT6.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
// src/config.ts
|
|
2
|
-
import { existsSync } from "fs";
|
|
3
|
-
import { resolve, dirname } from "path";
|
|
4
|
-
import { createRequire } from "module";
|
|
5
|
-
function defineConfig(config) {
|
|
6
|
-
return config;
|
|
7
|
-
}
|
|
8
|
-
var CONFIG_FILENAMES = [
|
|
9
|
-
"shiftapi.config.ts",
|
|
10
|
-
"shiftapi.config.js",
|
|
11
|
-
"shiftapi.config.mjs"
|
|
12
|
-
];
|
|
13
|
-
async function loadConfig(startDir, configPath) {
|
|
14
|
-
let resolvedPath;
|
|
15
|
-
if (configPath) {
|
|
16
|
-
resolvedPath = resolve(startDir, configPath);
|
|
17
|
-
if (!existsSync(resolvedPath)) {
|
|
18
|
-
throw new Error(`[shiftapi] Config file not found: ${resolvedPath}`);
|
|
19
|
-
}
|
|
20
|
-
} else {
|
|
21
|
-
resolvedPath = findConfigFile(startDir);
|
|
22
|
-
if (!resolvedPath) {
|
|
23
|
-
throw new Error(
|
|
24
|
-
`[shiftapi] Could not find shiftapi.config.ts (searched upward from ${startDir}). Create one with: import { defineConfig } from "@shiftapi/core"`
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
const require2 = createRequire(import.meta.url);
|
|
29
|
-
const { createJiti } = require2("jiti");
|
|
30
|
-
const jiti = createJiti(resolvedPath);
|
|
31
|
-
const mod = await jiti.import(resolvedPath);
|
|
32
|
-
const config = mod.default ?? mod;
|
|
33
|
-
if (!config.server) {
|
|
34
|
-
throw new Error(
|
|
35
|
-
`[shiftapi] Config at ${resolvedPath} must specify a "server" field (path to Go entry point)`
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
return { config, configDir: dirname(resolvedPath) };
|
|
39
|
-
}
|
|
40
|
-
function findConfigFile(startDir) {
|
|
41
|
-
let dir = resolve(startDir);
|
|
42
|
-
const root = resolve("/");
|
|
43
|
-
while (true) {
|
|
44
|
-
for (const name of CONFIG_FILENAMES) {
|
|
45
|
-
const candidate = resolve(dir, name);
|
|
46
|
-
if (existsSync(candidate)) {
|
|
47
|
-
return candidate;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
const parent = dirname(dir);
|
|
51
|
-
if (parent === dir || dir === root) {
|
|
52
|
-
return void 0;
|
|
53
|
-
}
|
|
54
|
-
dir = parent;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export {
|
|
59
|
-
defineConfig,
|
|
60
|
-
loadConfig
|
|
61
|
-
};
|