@kizunajs/cli 2.0.0-beta.32 → 2.0.0-beta.34
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.cjs +2 -2
- package/dist/cli.mjs +2 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +14 -3
- package/dist/index.d.mts +14 -3
- package/dist/index.mjs +1 -1
- package/dist/{route-map-C0K0orUw.mjs → route-map-CnQV3Vwj.mjs} +52 -11
- package/dist/{route-map-BHe__KDP.cjs → route-map-D8l0hbLo.cjs} +51 -10
- package/package.json +3 -3
package/dist/cli.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const require_route_map = require("./route-map-
|
|
2
|
+
const require_route_map = require("./route-map-D8l0hbLo.cjs");
|
|
3
3
|
let kizunajs_generator = require("kizunajs/generator");
|
|
4
4
|
let node_path = require("node:path");
|
|
5
5
|
let node_fs = require("node:fs");
|
|
@@ -115,7 +115,7 @@ const runGenerate = async (values) => {
|
|
|
115
115
|
const typesPath = (0, node_path.resolve)((0, node_path.dirname)(configPath), typesOutput);
|
|
116
116
|
let types;
|
|
117
117
|
try {
|
|
118
|
-
types = require_route_map.generateConfigTypes((0, node_fs.readFileSync)(configPath, "utf8"), configPath, typesPath);
|
|
118
|
+
types = require_route_map.generateConfigTypes((0, node_fs.readFileSync)(configPath, "utf8"), configPath, typesPath, config.typesExtensions);
|
|
119
119
|
} catch (error) {
|
|
120
120
|
if (error instanceof require_route_map.ConfigSyntaxError) die(`${displayPath(configPath)}: ${error.message}`);
|
|
121
121
|
throw error;
|
package/dist/cli.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { _ as loadConfig, a as ConfigSyntaxError, c as formatStale, d as diffSnapshots, f as formatChange, h as snapshotTarget, i as readSnapshot, l as writeFiles, m as snapshotPathFor, n as routeMap, o as generateConfigTypes, p as hasBreakingChange, r as diffAgainst, s as checkFiles, t as formatRoutes } from "./route-map-
|
|
2
|
+
import { _ as loadConfig, a as ConfigSyntaxError, c as formatStale, d as diffSnapshots, f as formatChange, h as snapshotTarget, i as readSnapshot, l as writeFiles, m as snapshotPathFor, n as routeMap, o as generateConfigTypes, p as hasBreakingChange, r as diffAgainst, s as checkFiles, t as formatRoutes } from "./route-map-CnQV3Vwj.mjs";
|
|
3
3
|
import { walkApi } from "kizunajs/generator";
|
|
4
4
|
import { dirname, relative, resolve } from "node:path";
|
|
5
5
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
@@ -115,7 +115,7 @@ const runGenerate = async (values) => {
|
|
|
115
115
|
const typesPath = resolve(dirname(configPath), typesOutput);
|
|
116
116
|
let types;
|
|
117
117
|
try {
|
|
118
|
-
types = generateConfigTypes(readFileSync(configPath, "utf8"), configPath, typesPath);
|
|
118
|
+
types = generateConfigTypes(readFileSync(configPath, "utf8"), configPath, typesPath, config.typesExtensions);
|
|
119
119
|
} catch (error) {
|
|
120
120
|
if (error instanceof ConfigSyntaxError) die(`${displayPath(configPath)}: ${error.message}`);
|
|
121
121
|
throw error;
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_route_map = require("./route-map-
|
|
2
|
+
const require_route_map = require("./route-map-D8l0hbLo.cjs");
|
|
3
3
|
let kizunajs_generator = require("kizunajs/generator");
|
|
4
4
|
//#region src/api-notices.ts
|
|
5
5
|
const readSunset = (route) => {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ApiDefinition, DiffSettings, GeneratedFile } from "kizunajs";
|
|
1
|
+
import { ApiDefinition, ConfigExtension, DiffSettings, GeneratedFile } from "kizunajs";
|
|
2
2
|
//#region src/load-config.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* One api a config declares, with everything generated from it. A config that
|
|
@@ -16,6 +16,10 @@ interface LoadedApi {
|
|
|
16
16
|
* Where `kizuna generate` writes the `Config`, when the config says.
|
|
17
17
|
*/
|
|
18
18
|
typesOutput: string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* What `typescript.extend` adds to the `Config`.
|
|
21
|
+
*/
|
|
22
|
+
typesExtensions: readonly ConfigExtension[];
|
|
19
23
|
/**
|
|
20
24
|
* What else `kizuna diff` treats as breaking.
|
|
21
25
|
*/
|
|
@@ -97,6 +101,11 @@ type SchemaNode = {
|
|
|
97
101
|
nullable?: true;
|
|
98
102
|
values: string[];
|
|
99
103
|
refinements?: number;
|
|
104
|
+
} | {
|
|
105
|
+
kind: 'model';
|
|
106
|
+
nullable?: true;
|
|
107
|
+
model: string;
|
|
108
|
+
refinements?: number;
|
|
100
109
|
} | {
|
|
101
110
|
kind: string;
|
|
102
111
|
nullable?: true;
|
|
@@ -208,12 +217,14 @@ declare class ConfigSyntaxError extends Error {}
|
|
|
208
217
|
*
|
|
209
218
|
* Read from the config's syntax rather than by running it: the generated file
|
|
210
219
|
* imports the same names the config did, and a config that loads is not a
|
|
211
|
-
* prerequisite for typing the routes it serves.
|
|
220
|
+
* prerequisite for typing the routes it serves. What
|
|
221
|
+
* `typescript.extend` adds exists only once the config has run, so the caller passes
|
|
222
|
+
* it in.
|
|
212
223
|
*
|
|
213
224
|
* `outputPath` is what the imports are written relative to. Left out, they stay
|
|
214
225
|
* relative to the config.
|
|
215
226
|
*/
|
|
216
|
-
declare const generateConfigTypes: (configSource: string, fileName?: string, outputPath?: string) => string;
|
|
227
|
+
declare const generateConfigTypes: (configSource: string, fileName?: string, outputPath?: string, extensions?: readonly ConfigExtension[]) => string;
|
|
217
228
|
//#endregion
|
|
218
229
|
//#region src/diff-schemas.d.ts
|
|
219
230
|
/**
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ApiDefinition, DiffSettings, GeneratedFile } from "kizunajs";
|
|
1
|
+
import { ApiDefinition, ConfigExtension, DiffSettings, GeneratedFile } from "kizunajs";
|
|
2
2
|
//#region src/load-config.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* One api a config declares, with everything generated from it. A config that
|
|
@@ -16,6 +16,10 @@ interface LoadedApi {
|
|
|
16
16
|
* Where `kizuna generate` writes the `Config`, when the config says.
|
|
17
17
|
*/
|
|
18
18
|
typesOutput: string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* What `typescript.extend` adds to the `Config`.
|
|
21
|
+
*/
|
|
22
|
+
typesExtensions: readonly ConfigExtension[];
|
|
19
23
|
/**
|
|
20
24
|
* What else `kizuna diff` treats as breaking.
|
|
21
25
|
*/
|
|
@@ -97,6 +101,11 @@ type SchemaNode = {
|
|
|
97
101
|
nullable?: true;
|
|
98
102
|
values: string[];
|
|
99
103
|
refinements?: number;
|
|
104
|
+
} | {
|
|
105
|
+
kind: 'model';
|
|
106
|
+
nullable?: true;
|
|
107
|
+
model: string;
|
|
108
|
+
refinements?: number;
|
|
100
109
|
} | {
|
|
101
110
|
kind: string;
|
|
102
111
|
nullable?: true;
|
|
@@ -208,12 +217,14 @@ declare class ConfigSyntaxError extends Error {}
|
|
|
208
217
|
*
|
|
209
218
|
* Read from the config's syntax rather than by running it: the generated file
|
|
210
219
|
* imports the same names the config did, and a config that loads is not a
|
|
211
|
-
* prerequisite for typing the routes it serves.
|
|
220
|
+
* prerequisite for typing the routes it serves. What
|
|
221
|
+
* `typescript.extend` adds exists only once the config has run, so the caller passes
|
|
222
|
+
* it in.
|
|
212
223
|
*
|
|
213
224
|
* `outputPath` is what the imports are written relative to. Left out, they stay
|
|
214
225
|
* relative to the config.
|
|
215
226
|
*/
|
|
216
|
-
declare const generateConfigTypes: (configSource: string, fileName?: string, outputPath?: string) => string;
|
|
227
|
+
declare const generateConfigTypes: (configSource: string, fileName?: string, outputPath?: string, extensions?: readonly ConfigExtension[]) => string;
|
|
217
228
|
//#endregion
|
|
218
229
|
//#region src/diff-schemas.d.ts
|
|
219
230
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _ as loadConfig, a as ConfigSyntaxError, c as formatStale, f as formatChange, g as diffSchemas, l as writeFiles, n as routeMap, o as generateConfigTypes, p as hasBreakingChange, r as diffAgainst, s as checkFiles, t as formatRoutes, u as diffApis } from "./route-map-
|
|
1
|
+
import { _ as loadConfig, a as ConfigSyntaxError, c as formatStale, f as formatChange, g as diffSchemas, l as writeFiles, n as routeMap, o as generateConfigTypes, p as hasBreakingChange, r as diffAgainst, s as checkFiles, t as formatRoutes, u as diffApis } from "./route-map-CnQV3Vwj.mjs";
|
|
2
2
|
import { walkApi } from "kizunajs/generator";
|
|
3
3
|
//#region src/api-notices.ts
|
|
4
4
|
const readSunset = (route) => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createJiti } from "jiti";
|
|
2
2
|
import { apiEntries } from "kizunajs";
|
|
3
|
-
import { deriveToolNames, flattenJobs, readDef, readObjectShape, resolveResponseBody, toToolName, unwrapOptionalWrappers, walkApi } from "kizunajs/generator";
|
|
3
|
+
import { deriveToolNames, flattenJobs, readDef, readMetaId, readModelSchema, readObjectShape, resolveResponseBody, toToolName, unwrapOptionalWrappers, walkApi } from "kizunajs/generator";
|
|
4
4
|
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
5
5
|
import { parse, stringify } from "yaml";
|
|
6
6
|
import { flattenRoutes } from "kizunajs/adapter";
|
|
@@ -27,6 +27,7 @@ const loadConfig = async (configPath) => {
|
|
|
27
27
|
clients: entry.clients ?? [],
|
|
28
28
|
generators: entry.generators ?? [],
|
|
29
29
|
typesOutput: entry.typescript?.outputFile,
|
|
30
|
+
typesExtensions: entry.typescript?.extend ?? [],
|
|
30
31
|
diff: entry.diff ?? {}
|
|
31
32
|
}));
|
|
32
33
|
};
|
|
@@ -36,12 +37,13 @@ const joinPath = (parent, key) => parent === "" ? key : `${parent}.${key}`;
|
|
|
36
37
|
const fieldsOf = (schema) => schema.kind === "object" ? schema.fields : void 0;
|
|
37
38
|
const elementOf = (schema) => schema.kind === "array" ? schema.element : void 0;
|
|
38
39
|
const valuesOf = (schema) => schema.kind === "enum" ? schema.values : void 0;
|
|
40
|
+
const modelOf = (schema) => schema.kind === "model" ? schema.model : void 0;
|
|
39
41
|
/**
|
|
40
42
|
* What a reader sees when the kinds differ, so `array` reads as `array<string>`.
|
|
41
43
|
*/
|
|
42
44
|
const describeKind = (schema) => {
|
|
43
45
|
const element = elementOf(schema);
|
|
44
|
-
return element ? `array<${describeKind(element)}>` : schema.kind;
|
|
46
|
+
return element ? `array<${describeKind(element)}>` : modelOf(schema) ?? schema.kind;
|
|
45
47
|
};
|
|
46
48
|
/**
|
|
47
49
|
* What changed between two versions of one schema, and whether it breaks the
|
|
@@ -59,7 +61,7 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
59
61
|
path,
|
|
60
62
|
summary: `${path || "the body"} is gone`
|
|
61
63
|
}];
|
|
62
|
-
if (before.kind !== after.kind) return [{
|
|
64
|
+
if (before.kind !== after.kind || modelOf(before) !== modelOf(after)) return [{
|
|
63
65
|
breaking: true,
|
|
64
66
|
path,
|
|
65
67
|
summary: `${path || "the body"} is ${describeKind(after)} instead of ${describeKind(before)}`
|
|
@@ -154,26 +156,36 @@ const refinementCount = (schema) => {
|
|
|
154
156
|
return custom === 0 ? void 0 : custom;
|
|
155
157
|
};
|
|
156
158
|
/**
|
|
157
|
-
* A Zod schema reduced to the shape a comparison reads.
|
|
159
|
+
* A Zod schema reduced to the shape a comparison reads. A model that holds
|
|
160
|
+
* itself is written out once, and inside itself as a reference by its title.
|
|
161
|
+
*
|
|
162
|
+
* `inside` is the objects the walk is in, so it can tell when it has gone round.
|
|
158
163
|
*/
|
|
159
|
-
const toSchemaNode = (schema) => {
|
|
164
|
+
const toSchemaNode = (schema, inside = []) => {
|
|
160
165
|
const { inner, nullable } = unwrapOptionalWrappers(schema);
|
|
161
166
|
const def = readDef(inner);
|
|
167
|
+
const nullability = nullable ? { nullable: true } : {};
|
|
168
|
+
const identity = readModelSchema(inner) ?? inner;
|
|
169
|
+
if (inside.includes(identity)) return {
|
|
170
|
+
kind: "model",
|
|
171
|
+
...nullability,
|
|
172
|
+
model: readMetaId(inner) ?? ""
|
|
173
|
+
};
|
|
162
174
|
const options = def.type === "union" ? def.options ?? [] : [];
|
|
163
175
|
const nonNull = options.filter((option) => readDef(option).type !== "null");
|
|
164
176
|
if (nonNull.length === 1 && options.length > 1) return {
|
|
165
|
-
...toSchemaNode(nonNull[0]),
|
|
177
|
+
...toSchemaNode(nonNull[0], inside),
|
|
166
178
|
nullable: true
|
|
167
179
|
};
|
|
168
|
-
const nullability = nullable ? { nullable: true } : {};
|
|
169
180
|
const refinements = refinementCount(inner);
|
|
170
181
|
const carry = refinements === void 0 ? {} : { refinements };
|
|
171
182
|
const shape = readObjectShape(inner);
|
|
172
183
|
if (shape) {
|
|
173
184
|
const fields = {};
|
|
185
|
+
const within = [...inside, identity];
|
|
174
186
|
for (const [key, field] of Object.entries(shape)) fields[key] = {
|
|
175
187
|
optional: unwrapOptionalWrappers(field).optional,
|
|
176
|
-
schema: toSchemaNode(field)
|
|
188
|
+
schema: toSchemaNode(field, within)
|
|
177
189
|
};
|
|
178
190
|
return {
|
|
179
191
|
kind: "object",
|
|
@@ -185,7 +197,7 @@ const toSchemaNode = (schema) => {
|
|
|
185
197
|
if (def.type === "array" && def.element) return {
|
|
186
198
|
kind: "array",
|
|
187
199
|
...nullability,
|
|
188
|
-
element: toSchemaNode(def.element),
|
|
200
|
+
element: toSchemaNode(def.element, inside),
|
|
189
201
|
...carry
|
|
190
202
|
};
|
|
191
203
|
if (def.type === "enum") return {
|
|
@@ -679,6 +691,32 @@ const authLines = (expression, used) => {
|
|
|
679
691
|
}
|
|
680
692
|
return lines;
|
|
681
693
|
};
|
|
694
|
+
/**
|
|
695
|
+
* A string as Prettier writes it: single quotes, unless the string holds more
|
|
696
|
+
* of them than double quotes.
|
|
697
|
+
*/
|
|
698
|
+
const stringLiteral = (value) => {
|
|
699
|
+
const quote = value.split("'").length > value.split("\"").length ? "\"" : "'";
|
|
700
|
+
return `${quote}${value.replaceAll("\\", "\\\\").replaceAll(quote, `\\${quote}`)}${quote}`;
|
|
701
|
+
};
|
|
702
|
+
const propertyName = (name) => /^[A-Za-z_$][\w$]*$/.test(name) ? name : stringLiteral(name);
|
|
703
|
+
/**
|
|
704
|
+
* One entry under `typescript.extend`. A list of strings becomes a
|
|
705
|
+
* union, wrapped one member per line when it would run past
|
|
706
|
+
* {@link PRINT_WIDTH}, the way Prettier does.
|
|
707
|
+
*/
|
|
708
|
+
const typeOfExtension = (values, indent) => {
|
|
709
|
+
const lines = Object.entries(values).map(([key, value]) => {
|
|
710
|
+
const prefix = `${indent} ${propertyName(key)}:`;
|
|
711
|
+
if (!Array.isArray(value)) return `${prefix} ${typeOfExtension(value, `${indent} `)};`;
|
|
712
|
+
const members = [...new Set(value)].map(stringLiteral);
|
|
713
|
+
if (members.length === 0) return `${prefix} never;`;
|
|
714
|
+
const inline = `${prefix} ${members.join(" | ")};`;
|
|
715
|
+
if (members.length === 1 || inline.length <= PRINT_WIDTH) return inline;
|
|
716
|
+
return `${prefix}\n${members.map((member) => `${indent} | ${member}`).join("\n")};`;
|
|
717
|
+
});
|
|
718
|
+
return lines.length === 0 ? "{}" : `{\n${lines.join("\n")}\n${indent}}`;
|
|
719
|
+
};
|
|
682
720
|
const HEADER = `/**
|
|
683
721
|
* This file was automatically generated by kizuna.
|
|
684
722
|
* DO NOT MODIFY IT BY HAND. Instead, modify your source kizuna config,
|
|
@@ -721,12 +759,14 @@ const renderImports = (imports, used, rebase) => {
|
|
|
721
759
|
*
|
|
722
760
|
* Read from the config's syntax rather than by running it: the generated file
|
|
723
761
|
* imports the same names the config did, and a config that loads is not a
|
|
724
|
-
* prerequisite for typing the routes it serves.
|
|
762
|
+
* prerequisite for typing the routes it serves. What
|
|
763
|
+
* `typescript.extend` adds exists only once the config has run, so the caller passes
|
|
764
|
+
* it in.
|
|
725
765
|
*
|
|
726
766
|
* `outputPath` is what the imports are written relative to. Left out, they stay
|
|
727
767
|
* relative to the config.
|
|
728
768
|
*/
|
|
729
|
-
const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", outputPath) => {
|
|
769
|
+
const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", outputPath, extensions = []) => {
|
|
730
770
|
const source = ts.createSourceFile(fileName, configSource, ts.ScriptTarget.Latest, true);
|
|
731
771
|
const imports = collectImports(source);
|
|
732
772
|
const configObject = findConfigObject(source);
|
|
@@ -753,6 +793,7 @@ const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", output
|
|
|
753
793
|
} else if (key === "plugins") lines.push(` ${key}: ${typeOfPluginList(value, used, ` ${key}: `)};`);
|
|
754
794
|
else lines.push(` ${key}: ${typeOfValue(value, used, key)};`);
|
|
755
795
|
}
|
|
796
|
+
for (const extension of extensions) for (const [name, values] of Object.entries(extension)) lines.push(` ${propertyName(name)}: ${typeOfExtension(values, " ")};`);
|
|
756
797
|
const configDir = dirname(resolve(fileName));
|
|
757
798
|
const outputDir = outputPath === void 0 ? configDir : dirname(resolve(outputPath));
|
|
758
799
|
const importBlock = renderImports(imports, used, (specifier) => outputDir === configDir ? specifier : rebaseSpecifier(specifier, configDir, outputDir));
|
|
@@ -50,6 +50,7 @@ const loadConfig = async (configPath) => {
|
|
|
50
50
|
clients: entry.clients ?? [],
|
|
51
51
|
generators: entry.generators ?? [],
|
|
52
52
|
typesOutput: entry.typescript?.outputFile,
|
|
53
|
+
typesExtensions: entry.typescript?.extend ?? [],
|
|
53
54
|
diff: entry.diff ?? {}
|
|
54
55
|
}));
|
|
55
56
|
};
|
|
@@ -59,12 +60,13 @@ const joinPath = (parent, key) => parent === "" ? key : `${parent}.${key}`;
|
|
|
59
60
|
const fieldsOf = (schema) => schema.kind === "object" ? schema.fields : void 0;
|
|
60
61
|
const elementOf = (schema) => schema.kind === "array" ? schema.element : void 0;
|
|
61
62
|
const valuesOf = (schema) => schema.kind === "enum" ? schema.values : void 0;
|
|
63
|
+
const modelOf = (schema) => schema.kind === "model" ? schema.model : void 0;
|
|
62
64
|
/**
|
|
63
65
|
* What a reader sees when the kinds differ, so `array` reads as `array<string>`.
|
|
64
66
|
*/
|
|
65
67
|
const describeKind = (schema) => {
|
|
66
68
|
const element = elementOf(schema);
|
|
67
|
-
return element ? `array<${describeKind(element)}>` : schema.kind;
|
|
69
|
+
return element ? `array<${describeKind(element)}>` : modelOf(schema) ?? schema.kind;
|
|
68
70
|
};
|
|
69
71
|
/**
|
|
70
72
|
* What changed between two versions of one schema, and whether it breaks the
|
|
@@ -82,7 +84,7 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
82
84
|
path,
|
|
83
85
|
summary: `${path || "the body"} is gone`
|
|
84
86
|
}];
|
|
85
|
-
if (before.kind !== after.kind) return [{
|
|
87
|
+
if (before.kind !== after.kind || modelOf(before) !== modelOf(after)) return [{
|
|
86
88
|
breaking: true,
|
|
87
89
|
path,
|
|
88
90
|
summary: `${path || "the body"} is ${describeKind(after)} instead of ${describeKind(before)}`
|
|
@@ -177,26 +179,36 @@ const refinementCount = (schema) => {
|
|
|
177
179
|
return custom === 0 ? void 0 : custom;
|
|
178
180
|
};
|
|
179
181
|
/**
|
|
180
|
-
* A Zod schema reduced to the shape a comparison reads.
|
|
182
|
+
* A Zod schema reduced to the shape a comparison reads. A model that holds
|
|
183
|
+
* itself is written out once, and inside itself as a reference by its title.
|
|
184
|
+
*
|
|
185
|
+
* `inside` is the objects the walk is in, so it can tell when it has gone round.
|
|
181
186
|
*/
|
|
182
|
-
const toSchemaNode = (schema) => {
|
|
187
|
+
const toSchemaNode = (schema, inside = []) => {
|
|
183
188
|
const { inner, nullable } = (0, kizunajs_generator.unwrapOptionalWrappers)(schema);
|
|
184
189
|
const def = (0, kizunajs_generator.readDef)(inner);
|
|
190
|
+
const nullability = nullable ? { nullable: true } : {};
|
|
191
|
+
const identity = (0, kizunajs_generator.readModelSchema)(inner) ?? inner;
|
|
192
|
+
if (inside.includes(identity)) return {
|
|
193
|
+
kind: "model",
|
|
194
|
+
...nullability,
|
|
195
|
+
model: (0, kizunajs_generator.readMetaId)(inner) ?? ""
|
|
196
|
+
};
|
|
185
197
|
const options = def.type === "union" ? def.options ?? [] : [];
|
|
186
198
|
const nonNull = options.filter((option) => (0, kizunajs_generator.readDef)(option).type !== "null");
|
|
187
199
|
if (nonNull.length === 1 && options.length > 1) return {
|
|
188
|
-
...toSchemaNode(nonNull[0]),
|
|
200
|
+
...toSchemaNode(nonNull[0], inside),
|
|
189
201
|
nullable: true
|
|
190
202
|
};
|
|
191
|
-
const nullability = nullable ? { nullable: true } : {};
|
|
192
203
|
const refinements = refinementCount(inner);
|
|
193
204
|
const carry = refinements === void 0 ? {} : { refinements };
|
|
194
205
|
const shape = (0, kizunajs_generator.readObjectShape)(inner);
|
|
195
206
|
if (shape) {
|
|
196
207
|
const fields = {};
|
|
208
|
+
const within = [...inside, identity];
|
|
197
209
|
for (const [key, field] of Object.entries(shape)) fields[key] = {
|
|
198
210
|
optional: (0, kizunajs_generator.unwrapOptionalWrappers)(field).optional,
|
|
199
|
-
schema: toSchemaNode(field)
|
|
211
|
+
schema: toSchemaNode(field, within)
|
|
200
212
|
};
|
|
201
213
|
return {
|
|
202
214
|
kind: "object",
|
|
@@ -208,7 +220,7 @@ const toSchemaNode = (schema) => {
|
|
|
208
220
|
if (def.type === "array" && def.element) return {
|
|
209
221
|
kind: "array",
|
|
210
222
|
...nullability,
|
|
211
|
-
element: toSchemaNode(def.element),
|
|
223
|
+
element: toSchemaNode(def.element, inside),
|
|
212
224
|
...carry
|
|
213
225
|
};
|
|
214
226
|
if (def.type === "enum") return {
|
|
@@ -702,6 +714,32 @@ const authLines = (expression, used) => {
|
|
|
702
714
|
}
|
|
703
715
|
return lines;
|
|
704
716
|
};
|
|
717
|
+
/**
|
|
718
|
+
* A string as Prettier writes it: single quotes, unless the string holds more
|
|
719
|
+
* of them than double quotes.
|
|
720
|
+
*/
|
|
721
|
+
const stringLiteral = (value) => {
|
|
722
|
+
const quote = value.split("'").length > value.split("\"").length ? "\"" : "'";
|
|
723
|
+
return `${quote}${value.replaceAll("\\", "\\\\").replaceAll(quote, `\\${quote}`)}${quote}`;
|
|
724
|
+
};
|
|
725
|
+
const propertyName = (name) => /^[A-Za-z_$][\w$]*$/.test(name) ? name : stringLiteral(name);
|
|
726
|
+
/**
|
|
727
|
+
* One entry under `typescript.extend`. A list of strings becomes a
|
|
728
|
+
* union, wrapped one member per line when it would run past
|
|
729
|
+
* {@link PRINT_WIDTH}, the way Prettier does.
|
|
730
|
+
*/
|
|
731
|
+
const typeOfExtension = (values, indent) => {
|
|
732
|
+
const lines = Object.entries(values).map(([key, value]) => {
|
|
733
|
+
const prefix = `${indent} ${propertyName(key)}:`;
|
|
734
|
+
if (!Array.isArray(value)) return `${prefix} ${typeOfExtension(value, `${indent} `)};`;
|
|
735
|
+
const members = [...new Set(value)].map(stringLiteral);
|
|
736
|
+
if (members.length === 0) return `${prefix} never;`;
|
|
737
|
+
const inline = `${prefix} ${members.join(" | ")};`;
|
|
738
|
+
if (members.length === 1 || inline.length <= PRINT_WIDTH) return inline;
|
|
739
|
+
return `${prefix}\n${members.map((member) => `${indent} | ${member}`).join("\n")};`;
|
|
740
|
+
});
|
|
741
|
+
return lines.length === 0 ? "{}" : `{\n${lines.join("\n")}\n${indent}}`;
|
|
742
|
+
};
|
|
705
743
|
const HEADER = `/**
|
|
706
744
|
* This file was automatically generated by kizuna.
|
|
707
745
|
* DO NOT MODIFY IT BY HAND. Instead, modify your source kizuna config,
|
|
@@ -744,12 +782,14 @@ const renderImports = (imports, used, rebase) => {
|
|
|
744
782
|
*
|
|
745
783
|
* Read from the config's syntax rather than by running it: the generated file
|
|
746
784
|
* imports the same names the config did, and a config that loads is not a
|
|
747
|
-
* prerequisite for typing the routes it serves.
|
|
785
|
+
* prerequisite for typing the routes it serves. What
|
|
786
|
+
* `typescript.extend` adds exists only once the config has run, so the caller passes
|
|
787
|
+
* it in.
|
|
748
788
|
*
|
|
749
789
|
* `outputPath` is what the imports are written relative to. Left out, they stay
|
|
750
790
|
* relative to the config.
|
|
751
791
|
*/
|
|
752
|
-
const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", outputPath) => {
|
|
792
|
+
const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", outputPath, extensions = []) => {
|
|
753
793
|
const source = typescript.default.createSourceFile(fileName, configSource, typescript.default.ScriptTarget.Latest, true);
|
|
754
794
|
const imports = collectImports(source);
|
|
755
795
|
const configObject = findConfigObject(source);
|
|
@@ -776,6 +816,7 @@ const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", output
|
|
|
776
816
|
} else if (key === "plugins") lines.push(` ${key}: ${typeOfPluginList(value, used, ` ${key}: `)};`);
|
|
777
817
|
else lines.push(` ${key}: ${typeOfValue(value, used, key)};`);
|
|
778
818
|
}
|
|
819
|
+
for (const extension of extensions) for (const [name, values] of Object.entries(extension)) lines.push(` ${propertyName(name)}: ${typeOfExtension(values, " ")};`);
|
|
779
820
|
const configDir = (0, node_path.dirname)((0, node_path.resolve)(fileName));
|
|
780
821
|
const outputDir = outputPath === void 0 ? configDir : (0, node_path.dirname)((0, node_path.resolve)(outputPath));
|
|
781
822
|
const importBlock = renderImports(imports, used, (specifier) => outputDir === configDir ? specifier : rebaseSpecifier(specifier, configDir, outputDir));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kizunajs/cli",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.34",
|
|
4
4
|
"description": "Command line tooling for Kizuna.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"kizunajs",
|
|
@@ -61,13 +61,13 @@
|
|
|
61
61
|
"yaml": "^2.9.1"
|
|
62
62
|
},
|
|
63
63
|
"peerDependencies": {
|
|
64
|
-
"kizunajs": "2.0.0-beta.
|
|
64
|
+
"kizunajs": "2.0.0-beta.34",
|
|
65
65
|
"zod": "^4.0.0"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
68
|
"@types/node": "^24.19.2",
|
|
69
69
|
"@types/react": "^19.3.0",
|
|
70
|
-
"kizunajs": "2.0.0-beta.
|
|
70
|
+
"kizunajs": "2.0.0-beta.34",
|
|
71
71
|
"tsdown": "^0.21.10",
|
|
72
72
|
"zod": "^4.6.7"
|
|
73
73
|
},
|