@lukoweb/apitogo 0.2.50 → 0.2.52
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/cli/cli.js +128 -72
- package/dist/declarations/lib/plugins/openapi/util/createHttpSnippet.d.ts +2 -0
- package/package.json +1 -1
- package/src/config/v2/full-server.ts +1 -1
- package/src/config/v2/gateway-reference.ts +15 -2
- package/src/config/v2/init.ts +1 -1
- package/src/config/v2/starter-landing.ts +13 -6
- package/src/config/v2/wrapper.ts +22 -2
- package/src/lib/plugins/openapi/util/createHttpSnippet.ts +147 -135
- package/src/vite/api/SchemaManager.ts +376 -371
- package/src/vite/plugin-api.ts +328 -328
- package/src/vite/plugin-config.ts +192 -131
|
@@ -1,371 +1,376 @@
|
|
|
1
|
-
import fs from "node:fs/promises";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import {
|
|
4
|
-
$RefParser,
|
|
5
|
-
type JSONSchema,
|
|
6
|
-
} from "@apidevtools/json-schema-ref-parser";
|
|
7
|
-
import { upgrade, validate } from "@scalar/openapi-parser";
|
|
8
|
-
import { deepEqual } from "fast-equals";
|
|
9
|
-
import type { LoadedConfig } from "../../config/config.js";
|
|
10
|
-
import type { Processor } from "../../config/validators/BuildSchema.js";
|
|
11
|
-
import type { VersionConfig } from "../../config/validators/ZudokuConfig.js";
|
|
12
|
-
import type { OpenAPIDocument } from "../../lib/oas/parser/index.js";
|
|
13
|
-
import { ensureArray } from "../../lib/util/ensureArray.js";
|
|
14
|
-
import { flattenAllOfProcessor } from "../../lib/util/flattenAllOfProcessor.js";
|
|
15
|
-
import { joinUrl } from "../../lib/util/joinUrl.js";
|
|
16
|
-
import { slugify } from "../../lib/util/slugify.js";
|
|
17
|
-
import { generateCode } from "./schema-codegen.js";
|
|
18
|
-
|
|
19
|
-
type ProcessedSchema = {
|
|
20
|
-
schema: OpenAPIDocument;
|
|
21
|
-
version: string;
|
|
22
|
-
path: string;
|
|
23
|
-
label?: string;
|
|
24
|
-
inputPath: string;
|
|
25
|
-
params: Record<string, string>;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
params:
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
private
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
this.referencedBy.
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
public
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
$RefParser,
|
|
5
|
+
type JSONSchema,
|
|
6
|
+
} from "@apidevtools/json-schema-ref-parser";
|
|
7
|
+
import { upgrade, validate } from "@scalar/openapi-parser";
|
|
8
|
+
import { deepEqual } from "fast-equals";
|
|
9
|
+
import type { LoadedConfig } from "../../config/config.js";
|
|
10
|
+
import type { Processor } from "../../config/validators/BuildSchema.js";
|
|
11
|
+
import type { VersionConfig } from "../../config/validators/ZudokuConfig.js";
|
|
12
|
+
import type { OpenAPIDocument } from "../../lib/oas/parser/index.js";
|
|
13
|
+
import { ensureArray } from "../../lib/util/ensureArray.js";
|
|
14
|
+
import { flattenAllOfProcessor } from "../../lib/util/flattenAllOfProcessor.js";
|
|
15
|
+
import { joinUrl } from "../../lib/util/joinUrl.js";
|
|
16
|
+
import { slugify } from "../../lib/util/slugify.js";
|
|
17
|
+
import { generateCode } from "./schema-codegen.js";
|
|
18
|
+
|
|
19
|
+
type ProcessedSchema = {
|
|
20
|
+
schema: OpenAPIDocument;
|
|
21
|
+
version: string;
|
|
22
|
+
path: string;
|
|
23
|
+
label?: string;
|
|
24
|
+
inputPath: string;
|
|
25
|
+
params: Record<string, string>;
|
|
26
|
+
/** The lookup key the browser uses for this schema: a file name, never a path of the build machine (API-127). */
|
|
27
|
+
importKey: string;
|
|
28
|
+
/** Absolute path of the processed module; only the build imports it. */
|
|
29
|
+
processedFilePath: string;
|
|
30
|
+
downloadUrl: string;
|
|
31
|
+
processedJsonPath: string;
|
|
32
|
+
processedTime: number;
|
|
33
|
+
};
|
|
34
|
+
const FALLBACK_VERSION = "default";
|
|
35
|
+
|
|
36
|
+
type InputConfig = Partial<VersionConfig> & {
|
|
37
|
+
input: string;
|
|
38
|
+
params: Record<string, string>;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const parseInputString = (input: string) => {
|
|
42
|
+
const idx = input.indexOf("?");
|
|
43
|
+
if (idx === -1)
|
|
44
|
+
return { filePath: input, params: {} as Record<string, string> };
|
|
45
|
+
return {
|
|
46
|
+
filePath: input.slice(0, idx),
|
|
47
|
+
params: Object.fromEntries(new URLSearchParams(input.slice(idx))),
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const canonicalParams = (params: Record<string, string>) =>
|
|
52
|
+
Object.entries(params)
|
|
53
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
54
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
55
|
+
.join("&");
|
|
56
|
+
|
|
57
|
+
const paramsSuffix = (params: Record<string, string>) => {
|
|
58
|
+
const canonical = canonicalParams(params);
|
|
59
|
+
return canonical ? `-${slugify(canonical)}` : "";
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const paramsPath = (params: Record<string, string>) => {
|
|
63
|
+
const canonical = canonicalParams(params);
|
|
64
|
+
return canonical ? slugify(canonical) : "";
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const normalizeInputs = (
|
|
68
|
+
inputs:
|
|
69
|
+
| string
|
|
70
|
+
| InputConfig
|
|
71
|
+
| Array<
|
|
72
|
+
| string
|
|
73
|
+
| (Omit<InputConfig, "params"> & { params?: Record<string, string> })
|
|
74
|
+
>,
|
|
75
|
+
): InputConfig[] =>
|
|
76
|
+
ensureArray(inputs).map((i) => {
|
|
77
|
+
const raw = typeof i === "string" ? { input: i } : i;
|
|
78
|
+
const { filePath, params } = parseInputString(raw.input);
|
|
79
|
+
return { ...raw, input: filePath, params: { ...params, ...raw.params } };
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
export class SchemaManager {
|
|
83
|
+
private storeDir: string;
|
|
84
|
+
private processors: Processor[];
|
|
85
|
+
private processedSchemas: Record<string, ProcessedSchema[]> = {};
|
|
86
|
+
private referencedBy = new Map<string, Set<string>>();
|
|
87
|
+
public config: LoadedConfig;
|
|
88
|
+
|
|
89
|
+
constructor({
|
|
90
|
+
storeDir,
|
|
91
|
+
config,
|
|
92
|
+
processors,
|
|
93
|
+
}: {
|
|
94
|
+
storeDir: string;
|
|
95
|
+
config: LoadedConfig;
|
|
96
|
+
processors: Processor[];
|
|
97
|
+
}) {
|
|
98
|
+
this.storeDir = storeDir;
|
|
99
|
+
this.config = config;
|
|
100
|
+
this.processors = [
|
|
101
|
+
({ schema }) => upgrade(schema).specification as OpenAPIDocument,
|
|
102
|
+
flattenAllOfProcessor,
|
|
103
|
+
...processors,
|
|
104
|
+
];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private getPathForFile = (input: string, params: Record<string, string>) => {
|
|
108
|
+
const filePath = path.resolve(this.config.__meta.rootDir, input);
|
|
109
|
+
|
|
110
|
+
const apis = ensureArray(this.config.apis ?? []);
|
|
111
|
+
for (const apiConfig of apis) {
|
|
112
|
+
if (!apiConfig || apiConfig.type !== "file" || !apiConfig.path) continue;
|
|
113
|
+
|
|
114
|
+
const match = normalizeInputs(apiConfig.input).some(
|
|
115
|
+
(i) =>
|
|
116
|
+
path.resolve(this.config.__meta.rootDir, i.input) === filePath &&
|
|
117
|
+
deepEqual(i.params, params),
|
|
118
|
+
);
|
|
119
|
+
if (match) return apiConfig.path;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
public processSchema = async (input: InputConfig) => {
|
|
124
|
+
const filePath = path.resolve(this.config.__meta.rootDir, input.input);
|
|
125
|
+
const params = input.params;
|
|
126
|
+
const configuredPath = this.getPathForFile(input.input, params);
|
|
127
|
+
if (!configuredPath) {
|
|
128
|
+
// biome-ignore lint/suspicious/noConsole: Logging allowed here
|
|
129
|
+
console.warn(`No path found for file ${input.input}`);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const parser = new $RefParser();
|
|
134
|
+
const schema = await parser.bundle(filePath, {
|
|
135
|
+
dereference: { preservedProperties: ["description", "summary"] },
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
this.referencedBy.set(filePath, new Set());
|
|
139
|
+
|
|
140
|
+
parser.$refs
|
|
141
|
+
.paths()
|
|
142
|
+
.filter((file) => file !== filePath)
|
|
143
|
+
.forEach((file) => {
|
|
144
|
+
if (!this.referencedBy.has(file)) {
|
|
145
|
+
this.referencedBy.set(file, new Set());
|
|
146
|
+
}
|
|
147
|
+
this.referencedBy.get(file)?.add(filePath);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const validatedSchema = await this.validateSchema(schema, filePath);
|
|
151
|
+
const processedSchema = await this.processors.reduce(
|
|
152
|
+
async (schema, processor) =>
|
|
153
|
+
processor({
|
|
154
|
+
schema: await schema,
|
|
155
|
+
file: filePath,
|
|
156
|
+
params,
|
|
157
|
+
dereference: (schema) =>
|
|
158
|
+
new $RefParser<OpenAPIDocument>().dereference(schema, {
|
|
159
|
+
dereference: {
|
|
160
|
+
preservedProperties: ["description", "summary"],
|
|
161
|
+
circular: "ignore",
|
|
162
|
+
},
|
|
163
|
+
}),
|
|
164
|
+
}),
|
|
165
|
+
Promise.resolve(validatedSchema),
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const processedTime = Date.now();
|
|
169
|
+
const code = generateCode(processedSchema, filePath);
|
|
170
|
+
|
|
171
|
+
const prefixPath = slugify(configuredPath);
|
|
172
|
+
const processedFilePath = path.posix.join(
|
|
173
|
+
this.storeDir,
|
|
174
|
+
`${prefixPath}-${path.basename(filePath)}${paramsSuffix(params)}.js`,
|
|
175
|
+
);
|
|
176
|
+
const importKey = path.posix.basename(processedFilePath);
|
|
177
|
+
|
|
178
|
+
await fs.writeFile(processedFilePath, code);
|
|
179
|
+
|
|
180
|
+
const processedJsonPath = paramsSuffix(params)
|
|
181
|
+
? processedFilePath.replace(/\.js$/, ".json")
|
|
182
|
+
: "";
|
|
183
|
+
if (processedJsonPath) {
|
|
184
|
+
await fs.writeFile(
|
|
185
|
+
processedJsonPath,
|
|
186
|
+
JSON.stringify(processedSchema, null, 2),
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const schemas = this.processedSchemas[configuredPath];
|
|
191
|
+
|
|
192
|
+
if (!schemas) {
|
|
193
|
+
throw new Error(`No schemas found for navigation ID ${configuredPath}.`);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const index = schemas.findIndex(
|
|
197
|
+
(s) => s.inputPath === filePath && deepEqual(s.params, params),
|
|
198
|
+
);
|
|
199
|
+
const existingSchema = schemas[index];
|
|
200
|
+
|
|
201
|
+
const schemaVersion = processedSchema.info.version ?? FALLBACK_VERSION;
|
|
202
|
+
const versionPath =
|
|
203
|
+
existingSchema?.path && existingSchema.path.length > 0
|
|
204
|
+
? existingSchema.path
|
|
205
|
+
: paramsPath(params) || schemaVersion;
|
|
206
|
+
|
|
207
|
+
const processed = {
|
|
208
|
+
schema: processedSchema,
|
|
209
|
+
version: schemaVersion,
|
|
210
|
+
path: versionPath,
|
|
211
|
+
label:
|
|
212
|
+
existingSchema?.label ??
|
|
213
|
+
(Object.keys(params).length > 0
|
|
214
|
+
? Object.values(params).join(", ")
|
|
215
|
+
: undefined),
|
|
216
|
+
inputPath: filePath,
|
|
217
|
+
params,
|
|
218
|
+
importKey,
|
|
219
|
+
processedFilePath,
|
|
220
|
+
downloadUrl: this.createSchemaPath(
|
|
221
|
+
filePath,
|
|
222
|
+
versionPath,
|
|
223
|
+
configuredPath,
|
|
224
|
+
params,
|
|
225
|
+
),
|
|
226
|
+
processedJsonPath,
|
|
227
|
+
processedTime,
|
|
228
|
+
} satisfies ProcessedSchema;
|
|
229
|
+
|
|
230
|
+
if (index > -1) {
|
|
231
|
+
schemas[index] = processed;
|
|
232
|
+
} else {
|
|
233
|
+
throw new Error(
|
|
234
|
+
`Schema with input path ${filePath} was not pre-initialized for ${configuredPath}.`,
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
return processed;
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
public getAllTrackedFiles = () => Array.from(this.referencedBy.keys());
|
|
241
|
+
|
|
242
|
+
public getFilesToReprocess = (changedFile: string): InputConfig[] => {
|
|
243
|
+
const resolvedPath = path.resolve(this.config.__meta.rootDir, changedFile);
|
|
244
|
+
const referencedBy = this.referencedBy.get(resolvedPath);
|
|
245
|
+
|
|
246
|
+
if (!referencedBy) return [];
|
|
247
|
+
|
|
248
|
+
const filesToProcess =
|
|
249
|
+
referencedBy.size === 0 ? [resolvedPath] : Array.from(referencedBy);
|
|
250
|
+
|
|
251
|
+
return filesToProcess.flatMap((fp) => {
|
|
252
|
+
const configs: InputConfig[] = [];
|
|
253
|
+
for (const schemas of Object.values(this.processedSchemas)) {
|
|
254
|
+
for (const s of schemas) {
|
|
255
|
+
if (s.inputPath === fp) {
|
|
256
|
+
configs.push({ input: fp, params: s.params });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return configs.length > 0 ? configs : [{ input: fp, params: {} }];
|
|
261
|
+
});
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
public processAllSchemas = async () => {
|
|
265
|
+
this.referencedBy.clear();
|
|
266
|
+
this.processedSchemas = {};
|
|
267
|
+
|
|
268
|
+
const apis = ensureArray(this.config.apis ?? []);
|
|
269
|
+
for (const apiConfig of apis) {
|
|
270
|
+
if (apiConfig.type !== "file" || !apiConfig.path) continue;
|
|
271
|
+
|
|
272
|
+
const inputs = normalizeInputs(apiConfig.input);
|
|
273
|
+
if (inputs.length === 0) throw new Error("No schema found");
|
|
274
|
+
|
|
275
|
+
this.processedSchemas[apiConfig.path] = inputs.map((input) => ({
|
|
276
|
+
schema: {} as OpenAPIDocument,
|
|
277
|
+
version: "",
|
|
278
|
+
path: input.path ?? "",
|
|
279
|
+
label: input.label,
|
|
280
|
+
inputPath: path.resolve(this.config.__meta.rootDir, input.input),
|
|
281
|
+
params: input.params,
|
|
282
|
+
importKey: "",
|
|
283
|
+
processedFilePath: "",
|
|
284
|
+
downloadUrl: "",
|
|
285
|
+
processedJsonPath: "",
|
|
286
|
+
processedTime: 0,
|
|
287
|
+
}));
|
|
288
|
+
|
|
289
|
+
const results = await Promise.allSettled(
|
|
290
|
+
inputs.map((input) => this.processSchema(input)),
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
const errors = results.flatMap((r) =>
|
|
294
|
+
r.status === "rejected" ? r.reason : [],
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
if (errors.length > 0) {
|
|
298
|
+
throw new Error(
|
|
299
|
+
`Failed to process schemas for ${apiConfig.path}: ${errors.join(", ")}`,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
public getLatestSchema = (path: string) => this.processedSchemas[path]?.at(0);
|
|
306
|
+
|
|
307
|
+
public getSchemasForPath = (path: string) => this.processedSchemas[path];
|
|
308
|
+
|
|
309
|
+
public getSchemaImports = () =>
|
|
310
|
+
Object.values(this.processedSchemas)
|
|
311
|
+
.flat()
|
|
312
|
+
.filter((s) => s.importKey);
|
|
313
|
+
|
|
314
|
+
public getUrlToFilePathMap = () => {
|
|
315
|
+
const map = new Map<string, string>();
|
|
316
|
+
|
|
317
|
+
const apis = ensureArray(this.config.apis ?? []);
|
|
318
|
+
for (const apiConfig of apis) {
|
|
319
|
+
if (apiConfig.type !== "file" || !apiConfig.path) continue;
|
|
320
|
+
|
|
321
|
+
const downloadEnabled =
|
|
322
|
+
apiConfig.options?.schemaDownload?.enabled ??
|
|
323
|
+
this.config.defaults?.apis?.schemaDownload?.enabled ??
|
|
324
|
+
false;
|
|
325
|
+
|
|
326
|
+
if (!downloadEnabled) continue;
|
|
327
|
+
|
|
328
|
+
const schemas = this.processedSchemas[apiConfig.path];
|
|
329
|
+
|
|
330
|
+
if (!schemas || schemas.length === 0) continue;
|
|
331
|
+
|
|
332
|
+
for (const schema of schemas) {
|
|
333
|
+
map.set(
|
|
334
|
+
schema.downloadUrl,
|
|
335
|
+
schema.processedJsonPath || schema.inputPath,
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return map;
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
private createSchemaPath = (
|
|
344
|
+
inputPath: string,
|
|
345
|
+
versionPath: string,
|
|
346
|
+
apiPath: string,
|
|
347
|
+
params: Record<string, string>,
|
|
348
|
+
) => {
|
|
349
|
+
const suffix = paramsSuffix(params);
|
|
350
|
+
const extension = suffix ? ".json" : path.extname(inputPath);
|
|
351
|
+
|
|
352
|
+
return joinUrl(
|
|
353
|
+
this.config.basePath,
|
|
354
|
+
apiPath,
|
|
355
|
+
versionPath,
|
|
356
|
+
`schema${suffix}${extension}`,
|
|
357
|
+
);
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
private validateSchema = async (
|
|
361
|
+
schema: JSONSchema,
|
|
362
|
+
filePath: string,
|
|
363
|
+
): Promise<OpenAPIDocument> => {
|
|
364
|
+
const validated = await validate(schema);
|
|
365
|
+
if (validated.errors?.length) {
|
|
366
|
+
// biome-ignore lint/suspicious/noConsole: Logging allowed here
|
|
367
|
+
console.warn(`Schema warnings in ${filePath}:`);
|
|
368
|
+
for (const error of validated.errors) {
|
|
369
|
+
// biome-ignore lint/suspicious/noConsole: Logging allowed here
|
|
370
|
+
console.warn(error);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return schema as OpenAPIDocument;
|
|
375
|
+
};
|
|
376
|
+
}
|