@sdk-it/hono 0.46.11 → 0.47.0
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/README.md +65 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +114 -159
- package/dist/index.js.map +3 -3
- package/dist/lib/constant-value.d.ts +3 -2
- package/dist/lib/constant-value.d.ts.map +1 -1
- package/dist/lib/evlog.d.ts +28 -0
- package/dist/lib/evlog.d.ts.map +1 -0
- package/dist/lib/evlog.js +152 -0
- package/dist/lib/evlog.js.map +7 -0
- package/dist/lib/response-analyzer.d.ts +8 -6
- package/dist/lib/response-analyzer.d.ts.map +1 -1
- package/dist/lib/runtime/index.d.ts.map +1 -1
- package/dist/lib/runtime/index.js +48 -23
- package/dist/lib/runtime/index.js.map +2 -2
- package/dist/lib/runtime/output.d.ts +5 -4
- package/dist/lib/runtime/output.d.ts.map +1 -1
- package/dist/lib/runtime/validator.d.ts.map +1 -1
- package/package.json +23 -8
- package/src/lib/runtime/output.ts +8 -8
- package/src/lib/runtime/validator.ts +72 -29
|
@@ -59,7 +59,7 @@ type InferIn<T extends ValidatorConfig> = (keyof InferTarget<
|
|
|
59
59
|
? never
|
|
60
60
|
: { cookie: InferTarget<T, CookieSelect, 'cookie'> });
|
|
61
61
|
|
|
62
|
-
//
|
|
62
|
+
// Markers distinguish request sources; `fieldMarkers` builds them at runtime.
|
|
63
63
|
type Select<Target extends string> = {
|
|
64
64
|
readonly validationTarget: Target;
|
|
65
65
|
};
|
|
@@ -71,6 +71,47 @@ type ParamsSelect = Select<'params'>;
|
|
|
71
71
|
type HeadersSelect = Select<'headers'>;
|
|
72
72
|
type CookieSelect = Select<'cookie'>;
|
|
73
73
|
|
|
74
|
+
const fieldSourceNames = [
|
|
75
|
+
'body',
|
|
76
|
+
'query',
|
|
77
|
+
'queries',
|
|
78
|
+
'params',
|
|
79
|
+
'headers',
|
|
80
|
+
] as const;
|
|
81
|
+
type FieldSource = (typeof fieldSourceNames)[number];
|
|
82
|
+
type FieldMarker = Select<FieldSource> & { readonly name: string };
|
|
83
|
+
|
|
84
|
+
/** `payload.<source>.<name>` in a selector reads a marker that names the field, not its value. */
|
|
85
|
+
function fieldMarkers<Target extends FieldSource>(
|
|
86
|
+
validationTarget: Target,
|
|
87
|
+
): Record<string, Select<Target>> {
|
|
88
|
+
return new Proxy<Record<string, Select<Target>>>(
|
|
89
|
+
{},
|
|
90
|
+
{
|
|
91
|
+
get: (_target, name) =>
|
|
92
|
+
typeof name === 'string' ? { validationTarget, name } : undefined,
|
|
93
|
+
},
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const fieldSources: readonly string[] = fieldSourceNames;
|
|
98
|
+
|
|
99
|
+
function isFieldMarker(value: unknown): value is FieldMarker {
|
|
100
|
+
return (
|
|
101
|
+
typeof value === 'object' &&
|
|
102
|
+
value !== null &&
|
|
103
|
+
'validationTarget' in value &&
|
|
104
|
+
typeof value.validationTarget === 'string' &&
|
|
105
|
+
fieldSources.includes(value.validationTarget) &&
|
|
106
|
+
'name' in value &&
|
|
107
|
+
typeof value.name === 'string'
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
112
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
113
|
+
}
|
|
114
|
+
|
|
74
115
|
type SelectorFn<T> = (payload: {
|
|
75
116
|
body: Record<string, BodySelect>;
|
|
76
117
|
query: Record<string, QuerySelect>;
|
|
@@ -153,47 +194,49 @@ export function validate<T extends ValidatorConfig>(
|
|
|
153
194
|
body = {};
|
|
154
195
|
}
|
|
155
196
|
|
|
156
|
-
// Whole-body schemas may accept any JSON value. A field selector, however,
|
|
157
|
-
// requires an object: guard the property read itself rather than catching
|
|
158
|
-
// arbitrary errors thrown by the application's selector.
|
|
159
|
-
const selectedBody =
|
|
160
|
-
body !== null && typeof body === 'object' && !Array.isArray(body)
|
|
161
|
-
? body
|
|
162
|
-
: new Proxy(
|
|
163
|
-
{},
|
|
164
|
-
{
|
|
165
|
-
get() {
|
|
166
|
-
throw new HTTPException(400, {
|
|
167
|
-
message: 'Invalid request body',
|
|
168
|
-
cause: {
|
|
169
|
-
code: 'api/invalid-body',
|
|
170
|
-
detail:
|
|
171
|
-
'Request body must be an object when selecting fields.',
|
|
172
|
-
},
|
|
173
|
-
});
|
|
174
|
-
},
|
|
175
|
-
},
|
|
176
|
-
);
|
|
177
197
|
const payload = {
|
|
178
|
-
body:
|
|
198
|
+
body: fieldMarkers('body'),
|
|
199
|
+
query: fieldMarkers('query'),
|
|
200
|
+
queries: fieldMarkers('queries'),
|
|
201
|
+
params: fieldMarkers('params'),
|
|
202
|
+
headers: fieldMarkers('headers'),
|
|
203
|
+
};
|
|
204
|
+
const sources: Record<
|
|
205
|
+
Exclude<FieldSource, 'body'>,
|
|
206
|
+
Record<string, string | string[] | undefined>
|
|
207
|
+
> = {
|
|
179
208
|
query: c.req.query(),
|
|
180
209
|
queries: c.req.queries(),
|
|
181
210
|
params: c.req.param(),
|
|
182
211
|
headers: c.req.header(),
|
|
183
212
|
};
|
|
213
|
+
const read = (select: unknown) => {
|
|
214
|
+
if (select === payload.body) return body;
|
|
215
|
+
if (!isFieldMarker(select)) return select;
|
|
216
|
+
if (select.validationTarget !== 'body') {
|
|
217
|
+
return sources[select.validationTarget][select.name];
|
|
218
|
+
}
|
|
219
|
+
// Whole-body schemas may accept any JSON value; a field selector requires an object.
|
|
220
|
+
if (!isRecord(body)) {
|
|
221
|
+
throw new HTTPException(400, {
|
|
222
|
+
message: 'Invalid request body',
|
|
223
|
+
cause: {
|
|
224
|
+
code: 'api/invalid-body',
|
|
225
|
+
detail: 'Request body must be an object when selecting fields.',
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
return body[select.name];
|
|
230
|
+
};
|
|
184
231
|
|
|
185
|
-
const config = _selector(payload
|
|
232
|
+
const config = _selector(payload);
|
|
186
233
|
const schema = z.object(
|
|
187
234
|
Object.fromEntries(
|
|
188
235
|
Object.entries(config).map(([key, value]) => [key, value.against]),
|
|
189
236
|
),
|
|
190
237
|
);
|
|
191
|
-
|
|
192
238
|
const input = Object.fromEntries(
|
|
193
|
-
Object.entries(config).map(([key, value]) => [
|
|
194
|
-
key,
|
|
195
|
-
value.select === selectedBody ? body : value.select,
|
|
196
|
-
]),
|
|
239
|
+
Object.entries(config).map(([key, value]) => [key, read(value.select)]),
|
|
197
240
|
);
|
|
198
241
|
|
|
199
242
|
const parsed = await parse(schema, input);
|