kizunajs 2.0.0-beta.21 → 2.0.0-beta.23

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 (37) hide show
  1. package/README.md +1 -1
  2. package/dist/{adapter-D4bTPElA.d.mts → adapter-BdkB6ql9.d.mts} +5 -1
  3. package/dist/{adapter-ClaFZoab.cjs → adapter-CK8ccDOC.cjs} +78 -8
  4. package/dist/{adapter-BSqNrxhq.mjs → adapter-DU3Ljl0P.mjs} +73 -9
  5. package/dist/{adapter-C3WYdSfx.d.cts → adapter-nQIkQwsM.d.cts} +5 -1
  6. package/dist/adapter.cjs +4 -3
  7. package/dist/adapter.d.cts +2 -2
  8. package/dist/adapter.d.mts +2 -2
  9. package/dist/adapter.mjs +4 -4
  10. package/dist/{error-response-CNQaKVYd.mjs → error-response-D7urgxeq.mjs} +16 -3
  11. package/dist/{error-response-C9ymqSU4.cjs → error-response-DN5Dsf8b.cjs} +21 -2
  12. package/dist/generator.cjs +5 -3
  13. package/dist/generator.d.cts +6 -2
  14. package/dist/generator.d.mts +6 -2
  15. package/dist/generator.mjs +5 -4
  16. package/dist/index.cjs +4 -4
  17. package/dist/index.d.cts +1 -1
  18. package/dist/index.d.mts +1 -1
  19. package/dist/index.mjs +4 -4
  20. package/dist/jobs-entry.cjs +2 -2
  21. package/dist/jobs-entry.d.cts +1 -1
  22. package/dist/jobs-entry.d.mts +1 -1
  23. package/dist/jobs-entry.mjs +2 -2
  24. package/dist/{plugin-server-kUsK7thV.cjs → plugin-server-CfF82yZL.cjs} +11 -6
  25. package/dist/{plugin-server-BztlE1fz.mjs → plugin-server-DNQ9_DL7.mjs} +11 -6
  26. package/dist/plugin.d.cts +1 -1
  27. package/dist/plugin.d.mts +1 -1
  28. package/dist/{schemas-DNPVDvkd.cjs → schemas-Bre1FVHq.cjs} +1 -1
  29. package/dist/{schemas-HW2FDsvz.mjs → schemas-DBX8DwFb.mjs} +1 -1
  30. package/dist/schemas.cjs +2 -2
  31. package/dist/schemas.mjs +2 -2
  32. package/package.json +1 -1
  33. package/src/adapter.ts +8 -5
  34. package/src/coercion.ts +25 -16
  35. package/src/generator.ts +4 -2
  36. package/src/routes.ts +97 -1
  37. package/src/zod-internals.ts +17 -2
package/src/routes.ts CHANGED
@@ -1,7 +1,15 @@
1
1
  import type { z } from 'zod';
2
2
  import { ROUTES_GROUP, type Routes, type RouteDefinition, type ResponseDefinition } from './types.js';
3
3
  import { isRouteDefinition } from './handler-pipeline.js';
4
- import { findCoercedSchemaPath, readObjectShape, resolveBaseType } from './zod-internals.js';
4
+ import {
5
+ findCoercedSchemaPath,
6
+ isBinarySchema,
7
+ isFileSchema,
8
+ readDef,
9
+ readObjectShape,
10
+ resolveBaseType,
11
+ unwrapOptionalWrappers,
12
+ } from './zod-internals.js';
5
13
  import { resolveCoercionPlans } from './coercion.js';
6
14
  import { parsePath, type PathParamsCheck } from './path-params.js';
7
15
  import { isStreamResponse, isZodSchema } from './generator-utils.js';
@@ -90,6 +98,91 @@ const assertPathParamsAreScalar = (route: RouteDefinition, routeKey: string): vo
90
98
  }
91
99
  };
92
100
 
101
+ /**
102
+ * What a field holds once optional and default wrappers are peeled away: a schema kind, or a file or bytes.
103
+ */
104
+ const fieldKind = (schema: z.core.$ZodType): string => {
105
+ const { inner } = unwrapOptionalWrappers(schema);
106
+ if (isFileSchema(inner)) return 'file';
107
+ if (isBinarySchema(inner)) return 'BinarySchema';
108
+ return resolveBaseType(inner);
109
+ };
110
+
111
+ const listElement = (schema: z.core.$ZodType): z.core.$ZodType | undefined => {
112
+ const def = readDef(unwrapOptionalWrappers(schema).inner);
113
+ return def.type === 'array' ? def.element : undefined;
114
+ };
115
+
116
+ const isValue = (kind: string): boolean => !STRUCTURED_TYPES.has(kind) && kind !== 'file' && kind !== 'BinarySchema';
117
+
118
+ /**
119
+ * Names a field the way an error shows it: its kind, or what a list holds.
120
+ */
121
+ const describeField = (schema: z.core.$ZodType): string => {
122
+ const element = listElement(schema);
123
+ return element === undefined ? fieldKind(schema) : `array of ${fieldKind(element)}`;
124
+ };
125
+
126
+ const fieldsOf = (schema: z.ZodType | undefined): [string, z.core.$ZodType][] => Object.entries((schema && readObjectShape(schema)) ?? {});
127
+
128
+ /**
129
+ * A query string carries values and lists of values, the way OpenAPI's default `form` style sends them.
130
+ */
131
+ const assertQuerySendable = (route: RouteDefinition, routeKey: string): void => {
132
+ for (const [name, field] of fieldsOf(route.query)) {
133
+ const element = listElement(field);
134
+ if (element === undefined ? isValue(fieldKind(field)) : isValue(fieldKind(element))) continue;
135
+ throw new Error(
136
+ `Route "${routeKey}" declares query field "${name}" as ${describeField(field)}. ` +
137
+ 'A query string carries values and lists of values, so this cannot be sent.\n' +
138
+ 'Split it into fields of their own, or move it to a body.'
139
+ );
140
+ }
141
+ };
142
+
143
+ /**
144
+ * A header carries one value.
145
+ */
146
+ const assertHeadersSendable = (route: RouteDefinition, routeKey: string): void => {
147
+ for (const [name, field] of fieldsOf(route.headers)) {
148
+ if (listElement(field) === undefined && isValue(fieldKind(field))) continue;
149
+ throw new Error(
150
+ `Route "${routeKey}" declares header "${name}" as ${describeField(field)}. ` +
151
+ 'A header carries one value, so this cannot be sent.\n' +
152
+ 'Use a scalar schema (z.string(), z.int(), z.enum([...])), or split it into headers of their own.'
153
+ );
154
+ }
155
+ };
156
+
157
+ /**
158
+ * A form carries values and lists of values, and a multipart one carries files too (RFC 7578).
159
+ */
160
+ const assertFormFieldsSendable = (route: RouteDefinition, routeKey: string): void => {
161
+ if (route.contentType !== 'multipart/form-data' && route.contentType !== 'application/x-www-form-urlencoded') return;
162
+ const multipart = route.contentType === 'multipart/form-data';
163
+ for (const [name, field] of fieldsOf(route.body)) {
164
+ const kind = fieldKind(listElement(field) ?? field);
165
+ if (kind === 'BinarySchema') {
166
+ throw new Error(
167
+ `Route "${routeKey}" declares form field "${name}" as BinarySchema, but a form sends a file part as a File. ` +
168
+ 'Declare it with FileSchema.'
169
+ );
170
+ }
171
+ if (kind === 'file' && !multipart) {
172
+ throw new Error(
173
+ `Route "${routeKey}" declares form field "${name}" as a file, which a urlencoded body cannot carry. ` +
174
+ "Send it as contentType: 'multipart/form-data'."
175
+ );
176
+ }
177
+ if (kind === 'file' || isValue(kind)) continue;
178
+ throw new Error(
179
+ `Route "${routeKey}" declares form field "${name}" as ${describeField(field)}. ` +
180
+ 'A form carries values, lists of values and files, so this cannot be sent.\n' +
181
+ 'Split it into fields of their own, or take the body as JSON.'
182
+ );
183
+ }
184
+ };
185
+
93
186
  /**
94
187
  * A tool's description is the one thing a model reads before choosing it, so a
95
188
  * route that publishes as one has to carry it.
@@ -114,6 +207,9 @@ const validateRoutes = (routes: Routes, prefix?: string): void => {
114
207
  expandStreamTools(value, fullKey);
115
208
  assertPathParamsMatchPath(value, fullKey);
116
209
  assertPathParamsAreScalar(value, fullKey);
210
+ assertQuerySendable(value, fullKey);
211
+ assertHeadersSendable(value, fullKey);
212
+ assertFormFieldsSendable(value, fullKey);
117
213
  assertNoCoercion(value, fullKey);
118
214
  assertValidStreams(value, fullKey);
119
215
  assertToolDescribed(value, fullKey);
@@ -248,12 +248,27 @@ export const markModelSchema = (schema: z.core.$ZodType): void => {
248
248
  modelSchemas.add(schema);
249
249
  };
250
250
 
251
+ /**
252
+ * Returns the `Kizuna.model` a schema is, or was copied from by `.describe()` or `.meta()`.
253
+ */
254
+ export const readModelSchema = (schema: z.core.$ZodType): z.core.$ZodType | undefined => {
255
+ let current: z.core.$ZodType | undefined = schema;
256
+ while (current !== undefined) {
257
+ if (modelSchemas.has(current)) return current;
258
+ const parent: z.core.$ZodType | undefined = current._zod.parent;
259
+ if (parent === undefined || parent._zod.def !== current._zod.def) return undefined;
260
+ current = parent;
261
+ }
262
+ return undefined;
263
+ };
264
+
251
265
  /**
252
266
  * Returns a `Kizuna.model` schema's `id` metadata, or undefined.
253
267
  */
254
268
  export const readMetaId = (schema: z.core.$ZodType): string | undefined => {
255
- if (!modelSchemas.has(schema)) return undefined;
256
- const id = readMeta(schema)?.id;
269
+ const model = readModelSchema(schema);
270
+ if (model === undefined) return undefined;
271
+ const id = readMeta(model)?.id;
257
272
  return typeof id === 'string' ? id : undefined;
258
273
  };
259
274