@kizunajs/fetch 2.0.0-beta.2 → 2.0.0-beta.4
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/server.cjs +23 -11
- package/dist/server.mjs +24 -12
- package/package.json +4 -4
package/dist/server.cjs
CHANGED
|
@@ -27,6 +27,15 @@ var TypeCollector = class {
|
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
29
|
const quote = (value) => JSON.stringify(value);
|
|
30
|
+
/**
|
|
31
|
+
* A literal as the type the JSON carries, so `z.literal(true)` is `true`.
|
|
32
|
+
*/
|
|
33
|
+
const literalType = (value) => {
|
|
34
|
+
if (typeof value === "string") return quote(value);
|
|
35
|
+
if (typeof value === "bigint") return `${value}n`;
|
|
36
|
+
if (value === void 0) return "undefined";
|
|
37
|
+
return String(value);
|
|
38
|
+
};
|
|
30
39
|
const indent$1 = (text, depth = 1) => text.split("\n").map((line) => line.length > 0 ? " ".repeat(depth) + line : line).join("\n");
|
|
31
40
|
/**
|
|
32
41
|
* A property name, quoted only when it is not a plain identifier.
|
|
@@ -159,24 +168,27 @@ const typeOf = (schema, collector, hint) => {
|
|
|
159
168
|
const def = (0, kizunajs_generator.readDef)(schema);
|
|
160
169
|
if (def.type === "nullable" && def.innerType) return `${typeOf(def.innerType, collector, hint)} | null`;
|
|
161
170
|
if (def.type === "nullish" && def.innerType) return `${typeOf(def.innerType, collector, hint)} | null`;
|
|
162
|
-
if (def.innerType && def.type !== "object")
|
|
163
|
-
const { inner } = (0, kizunajs_generator.unwrapOptionalWrappers)(schema);
|
|
164
|
-
if (inner !== schema) return typeOf(inner, collector, hint);
|
|
165
|
-
return typeOf(def.innerType, collector, hint);
|
|
166
|
-
}
|
|
171
|
+
if (def.innerType && def.type !== "object") return typeOf(def.innerType, collector, hint);
|
|
167
172
|
if (def.type === "pipe") {
|
|
168
173
|
const side = def.out ?? def.in;
|
|
169
174
|
return side ? typeOf(side, collector, hint) : "unknown";
|
|
170
175
|
}
|
|
171
176
|
const modelName = (0, kizunajs_generator.readMetaId)(schema);
|
|
172
|
-
if (modelName !== void 0
|
|
177
|
+
if (modelName !== void 0) {
|
|
173
178
|
if (collector.claim(modelName)) collector.add({
|
|
174
179
|
name: modelName,
|
|
175
180
|
description: (0, kizunajs_generator.readMetaDescription)(schema),
|
|
176
|
-
body:
|
|
181
|
+
body: structuralType(schema, collector, modelName)
|
|
177
182
|
});
|
|
178
183
|
return modelName;
|
|
179
184
|
}
|
|
185
|
+
return structuralType(schema, collector, hint);
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* The type a schema describes by its shape, its name already spent by the caller.
|
|
189
|
+
*/
|
|
190
|
+
const structuralType = (schema, collector, hint) => {
|
|
191
|
+
const def = (0, kizunajs_generator.readDef)(schema);
|
|
180
192
|
switch (def.type) {
|
|
181
193
|
case "string": return "string";
|
|
182
194
|
case "number":
|
|
@@ -192,11 +204,11 @@ const typeOf = (schema, collector, hint) => {
|
|
|
192
204
|
case "undefined": return "undefined";
|
|
193
205
|
case "literal": {
|
|
194
206
|
const values = def.values ?? [];
|
|
195
|
-
return values.length > 0 ? values.map(
|
|
207
|
+
return values.length > 0 ? values.map(literalType).join(" | ") : "never";
|
|
196
208
|
}
|
|
197
209
|
case "enum": {
|
|
198
210
|
const values = Object.values(def.entries ?? {});
|
|
199
|
-
return values.length > 0 ? values.map(
|
|
211
|
+
return values.length > 0 ? values.map(literalType).join(" | ") : "never";
|
|
200
212
|
}
|
|
201
213
|
case "array": return def.element ? `Array<${typeOf(def.element, collector, `${hint}Item`)}>` : "unknown[]";
|
|
202
214
|
case "object": return objectBody(schema, collector, hint);
|
|
@@ -247,7 +259,7 @@ const exampleCall = (routeKey, route) => {
|
|
|
247
259
|
entries.push(`params: {\n${indent(fields.join("\n"))}\n},`);
|
|
248
260
|
}
|
|
249
261
|
if (route.query && requiresArgument(route.query)) entries.push(`query: ${sampleObject(route.query)},`);
|
|
250
|
-
if (route.body && requiresArgument(route.body)) entries.push(`body: ${sampleValue(route.body)},`);
|
|
262
|
+
if (route.body && !(0, kizunajs_generator.isVoidSchema)(route.body) && requiresArgument(route.body)) entries.push(`body: ${sampleValue(route.body)},`);
|
|
251
263
|
return `const result = await client.${routeKey}(${entries.length > 0 ? `{\n${indent(entries.join("\n"))}\n}` : ""});`;
|
|
252
264
|
};
|
|
253
265
|
/**
|
|
@@ -293,7 +305,7 @@ const emitRoute = (routeKey, key, route, collector) => {
|
|
|
293
305
|
members.push(`export type Query = ${typeOf(route.query, collector, `${namespace}Query`)};`);
|
|
294
306
|
args.push(`query?: API.${namespace}.Query`);
|
|
295
307
|
}
|
|
296
|
-
if (route.body) {
|
|
308
|
+
if (route.body && !(0, kizunajs_generator.isVoidSchema)(route.body)) {
|
|
297
309
|
members.push(`export type Body = ${typeOf(route.body, collector, `${namespace}Body`)};`);
|
|
298
310
|
args.push(`body: API.${namespace}.Body`);
|
|
299
311
|
}
|
package/dist/server.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ValidationErrorSchema } from "kizunajs/schemas";
|
|
2
|
-
import { isBinarySchema, isFileSchema, isStreamResponse, readDef, readDeprecation, readDiscriminatedUnion, readMetaDescription, readMetaExamples, readMetaId, readObjectShape, resolveResponseBody, resolveResponseHeaders, streamContentType, toPascalCase, unwrapOptionalWrappers } from "kizunajs/generator";
|
|
2
|
+
import { isBinarySchema, isFileSchema, isStreamResponse, isVoidSchema, readDef, readDeprecation, readDiscriminatedUnion, readMetaDescription, readMetaExamples, readMetaId, readObjectShape, resolveResponseBody, resolveResponseHeaders, streamContentType, toPascalCase, unwrapOptionalWrappers } from "kizunajs/generator";
|
|
3
3
|
//#region src/zod-to-typescript.ts
|
|
4
4
|
/**
|
|
5
5
|
* Collects the named types a walk produces, so a model shared by many routes is
|
|
@@ -26,6 +26,15 @@ var TypeCollector = class {
|
|
|
26
26
|
}
|
|
27
27
|
};
|
|
28
28
|
const quote = (value) => JSON.stringify(value);
|
|
29
|
+
/**
|
|
30
|
+
* A literal as the type the JSON carries, so `z.literal(true)` is `true`.
|
|
31
|
+
*/
|
|
32
|
+
const literalType = (value) => {
|
|
33
|
+
if (typeof value === "string") return quote(value);
|
|
34
|
+
if (typeof value === "bigint") return `${value}n`;
|
|
35
|
+
if (value === void 0) return "undefined";
|
|
36
|
+
return String(value);
|
|
37
|
+
};
|
|
29
38
|
const indent$1 = (text, depth = 1) => text.split("\n").map((line) => line.length > 0 ? " ".repeat(depth) + line : line).join("\n");
|
|
30
39
|
/**
|
|
31
40
|
* A property name, quoted only when it is not a plain identifier.
|
|
@@ -158,24 +167,27 @@ const typeOf = (schema, collector, hint) => {
|
|
|
158
167
|
const def = readDef(schema);
|
|
159
168
|
if (def.type === "nullable" && def.innerType) return `${typeOf(def.innerType, collector, hint)} | null`;
|
|
160
169
|
if (def.type === "nullish" && def.innerType) return `${typeOf(def.innerType, collector, hint)} | null`;
|
|
161
|
-
if (def.innerType && def.type !== "object")
|
|
162
|
-
const { inner } = unwrapOptionalWrappers(schema);
|
|
163
|
-
if (inner !== schema) return typeOf(inner, collector, hint);
|
|
164
|
-
return typeOf(def.innerType, collector, hint);
|
|
165
|
-
}
|
|
170
|
+
if (def.innerType && def.type !== "object") return typeOf(def.innerType, collector, hint);
|
|
166
171
|
if (def.type === "pipe") {
|
|
167
172
|
const side = def.out ?? def.in;
|
|
168
173
|
return side ? typeOf(side, collector, hint) : "unknown";
|
|
169
174
|
}
|
|
170
175
|
const modelName = readMetaId(schema);
|
|
171
|
-
if (modelName !== void 0
|
|
176
|
+
if (modelName !== void 0) {
|
|
172
177
|
if (collector.claim(modelName)) collector.add({
|
|
173
178
|
name: modelName,
|
|
174
179
|
description: readMetaDescription(schema),
|
|
175
|
-
body:
|
|
180
|
+
body: structuralType(schema, collector, modelName)
|
|
176
181
|
});
|
|
177
182
|
return modelName;
|
|
178
183
|
}
|
|
184
|
+
return structuralType(schema, collector, hint);
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* The type a schema describes by its shape, its name already spent by the caller.
|
|
188
|
+
*/
|
|
189
|
+
const structuralType = (schema, collector, hint) => {
|
|
190
|
+
const def = readDef(schema);
|
|
179
191
|
switch (def.type) {
|
|
180
192
|
case "string": return "string";
|
|
181
193
|
case "number":
|
|
@@ -191,11 +203,11 @@ const typeOf = (schema, collector, hint) => {
|
|
|
191
203
|
case "undefined": return "undefined";
|
|
192
204
|
case "literal": {
|
|
193
205
|
const values = def.values ?? [];
|
|
194
|
-
return values.length > 0 ? values.map(
|
|
206
|
+
return values.length > 0 ? values.map(literalType).join(" | ") : "never";
|
|
195
207
|
}
|
|
196
208
|
case "enum": {
|
|
197
209
|
const values = Object.values(def.entries ?? {});
|
|
198
|
-
return values.length > 0 ? values.map(
|
|
210
|
+
return values.length > 0 ? values.map(literalType).join(" | ") : "never";
|
|
199
211
|
}
|
|
200
212
|
case "array": return def.element ? `Array<${typeOf(def.element, collector, `${hint}Item`)}>` : "unknown[]";
|
|
201
213
|
case "object": return objectBody(schema, collector, hint);
|
|
@@ -246,7 +258,7 @@ const exampleCall = (routeKey, route) => {
|
|
|
246
258
|
entries.push(`params: {\n${indent(fields.join("\n"))}\n},`);
|
|
247
259
|
}
|
|
248
260
|
if (route.query && requiresArgument(route.query)) entries.push(`query: ${sampleObject(route.query)},`);
|
|
249
|
-
if (route.body && requiresArgument(route.body)) entries.push(`body: ${sampleValue(route.body)},`);
|
|
261
|
+
if (route.body && !isVoidSchema(route.body) && requiresArgument(route.body)) entries.push(`body: ${sampleValue(route.body)},`);
|
|
250
262
|
return `const result = await client.${routeKey}(${entries.length > 0 ? `{\n${indent(entries.join("\n"))}\n}` : ""});`;
|
|
251
263
|
};
|
|
252
264
|
/**
|
|
@@ -292,7 +304,7 @@ const emitRoute = (routeKey, key, route, collector) => {
|
|
|
292
304
|
members.push(`export type Query = ${typeOf(route.query, collector, `${namespace}Query`)};`);
|
|
293
305
|
args.push(`query?: API.${namespace}.Query`);
|
|
294
306
|
}
|
|
295
|
-
if (route.body) {
|
|
307
|
+
if (route.body && !isVoidSchema(route.body)) {
|
|
296
308
|
members.push(`export type Body = ${typeOf(route.body, collector, `${namespace}Body`)};`);
|
|
297
309
|
args.push(`body: API.${namespace}.Body`);
|
|
298
310
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kizunajs/fetch",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.4",
|
|
4
4
|
"description": "Typed fetch client for Kizuna.js APIs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"kizunajs",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
70
|
"zod": "^4.0.0",
|
|
71
|
-
"kizunajs": "2.0.0-beta.
|
|
71
|
+
"kizunajs": "2.0.0-beta.4"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@types/express": "^5.0.0",
|
|
@@ -76,8 +76,8 @@
|
|
|
76
76
|
"tsdown": "^0.21.0",
|
|
77
77
|
"typescript": "^6.0.3",
|
|
78
78
|
"zod": "^4.0.0",
|
|
79
|
-
"kizunajs": "2.0.0-beta.
|
|
80
|
-
"@kizunajs/express": "2.0.0-beta.
|
|
79
|
+
"kizunajs": "2.0.0-beta.4",
|
|
80
|
+
"@kizunajs/express": "2.0.0-beta.4"
|
|
81
81
|
},
|
|
82
82
|
"scripts": {
|
|
83
83
|
"build": "tsdown src/index.ts src/server.ts --format esm,cjs --dts --clean --external kizunajs --external zod",
|