@float.js/core 2.0.1
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/ai/index.d.ts +103 -0
- package/dist/ai/index.js +293 -0
- package/dist/ai/index.js.map +1 -0
- package/dist/analytics/index.d.ts +193 -0
- package/dist/analytics/index.js +499 -0
- package/dist/analytics/index.js.map +1 -0
- package/dist/api/index.d.ts +172 -0
- package/dist/api/index.js +643 -0
- package/dist/api/index.js.map +1 -0
- package/dist/cli/index.js +2367 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/devtools/index.d.ts +82 -0
- package/dist/devtools/index.js +839 -0
- package/dist/devtools/index.js.map +1 -0
- package/dist/image/index.d.ts +114 -0
- package/dist/image/index.js +242 -0
- package/dist/image/index.js.map +1 -0
- package/dist/index.d.ts +400 -0
- package/dist/index.js +6511 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/index.d.ts +179 -0
- package/dist/middleware/index.js +362 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/realtime/index.d.ts +198 -0
- package/dist/realtime/index.js +555 -0
- package/dist/realtime/index.js.map +1 -0
- package/dist/router/index.d.ts +43 -0
- package/dist/router/index.js +143 -0
- package/dist/router/index.js.map +1 -0
- package/dist/server/index.d.ts +51 -0
- package/dist/server/index.js +2163 -0
- package/dist/server/index.js.map +1 -0
- package/dist/ssg/index.d.ts +178 -0
- package/dist/ssg/index.js +399 -0
- package/dist/ssg/index.js.map +1 -0
- package/package.json +69 -0
|
@@ -0,0 +1,643 @@
|
|
|
1
|
+
// src/api/index.ts
|
|
2
|
+
function createStringSchema() {
|
|
3
|
+
const validators = [];
|
|
4
|
+
const transforms = [];
|
|
5
|
+
const schema = {
|
|
6
|
+
_type: "string",
|
|
7
|
+
_output: "",
|
|
8
|
+
min(length, message) {
|
|
9
|
+
validators.push(
|
|
10
|
+
(val) => val.length >= length ? null : message || `Must be at least ${length} characters`
|
|
11
|
+
);
|
|
12
|
+
return schema;
|
|
13
|
+
},
|
|
14
|
+
max(length, message) {
|
|
15
|
+
validators.push(
|
|
16
|
+
(val) => val.length <= length ? null : message || `Must be at most ${length} characters`
|
|
17
|
+
);
|
|
18
|
+
return schema;
|
|
19
|
+
},
|
|
20
|
+
email(message) {
|
|
21
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
22
|
+
validators.push(
|
|
23
|
+
(val) => emailRegex.test(val) ? null : message || "Invalid email address"
|
|
24
|
+
);
|
|
25
|
+
return schema;
|
|
26
|
+
},
|
|
27
|
+
url(message) {
|
|
28
|
+
validators.push((val) => {
|
|
29
|
+
try {
|
|
30
|
+
new URL(val);
|
|
31
|
+
return null;
|
|
32
|
+
} catch {
|
|
33
|
+
return message || "Invalid URL";
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
return schema;
|
|
37
|
+
},
|
|
38
|
+
uuid(message) {
|
|
39
|
+
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
40
|
+
validators.push(
|
|
41
|
+
(val) => uuidRegex.test(val) ? null : message || "Invalid UUID"
|
|
42
|
+
);
|
|
43
|
+
return schema;
|
|
44
|
+
},
|
|
45
|
+
regex(pattern, message) {
|
|
46
|
+
validators.push(
|
|
47
|
+
(val) => pattern.test(val) ? null : message || `Must match pattern ${pattern}`
|
|
48
|
+
);
|
|
49
|
+
return schema;
|
|
50
|
+
},
|
|
51
|
+
trim() {
|
|
52
|
+
transforms.push((val) => val.trim());
|
|
53
|
+
return schema;
|
|
54
|
+
},
|
|
55
|
+
toLowerCase() {
|
|
56
|
+
transforms.push((val) => val.toLowerCase());
|
|
57
|
+
return schema;
|
|
58
|
+
},
|
|
59
|
+
toUpperCase() {
|
|
60
|
+
transforms.push((val) => val.toUpperCase());
|
|
61
|
+
return schema;
|
|
62
|
+
},
|
|
63
|
+
optional() {
|
|
64
|
+
return createOptionalSchema(schema);
|
|
65
|
+
},
|
|
66
|
+
parse(value) {
|
|
67
|
+
const result = schema.safeParse(value);
|
|
68
|
+
if (!result.success) {
|
|
69
|
+
throw new FloatValidationError([result.error]);
|
|
70
|
+
}
|
|
71
|
+
return result.data;
|
|
72
|
+
},
|
|
73
|
+
safeParse(value) {
|
|
74
|
+
if (typeof value !== "string") {
|
|
75
|
+
return {
|
|
76
|
+
success: false,
|
|
77
|
+
error: {
|
|
78
|
+
path: [],
|
|
79
|
+
message: "Expected string",
|
|
80
|
+
received: value,
|
|
81
|
+
expected: "string"
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
let transformed = value;
|
|
86
|
+
for (const transform of transforms) {
|
|
87
|
+
transformed = transform(transformed);
|
|
88
|
+
}
|
|
89
|
+
for (const validator of validators) {
|
|
90
|
+
const error2 = validator(transformed);
|
|
91
|
+
if (error2) {
|
|
92
|
+
return {
|
|
93
|
+
success: false,
|
|
94
|
+
error: {
|
|
95
|
+
path: [],
|
|
96
|
+
message: error2,
|
|
97
|
+
received: value,
|
|
98
|
+
expected: "string"
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return { success: true, data: transformed };
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
return schema;
|
|
107
|
+
}
|
|
108
|
+
function createNumberSchema() {
|
|
109
|
+
const validators = [];
|
|
110
|
+
const schema = {
|
|
111
|
+
_type: "number",
|
|
112
|
+
_output: 0,
|
|
113
|
+
min(value, message) {
|
|
114
|
+
validators.push(
|
|
115
|
+
(val) => val >= value ? null : message || `Must be at least ${value}`
|
|
116
|
+
);
|
|
117
|
+
return schema;
|
|
118
|
+
},
|
|
119
|
+
max(value, message) {
|
|
120
|
+
validators.push(
|
|
121
|
+
(val) => val <= value ? null : message || `Must be at most ${value}`
|
|
122
|
+
);
|
|
123
|
+
return schema;
|
|
124
|
+
},
|
|
125
|
+
int(message) {
|
|
126
|
+
validators.push(
|
|
127
|
+
(val) => Number.isInteger(val) ? null : message || "Must be an integer"
|
|
128
|
+
);
|
|
129
|
+
return schema;
|
|
130
|
+
},
|
|
131
|
+
positive(message) {
|
|
132
|
+
validators.push(
|
|
133
|
+
(val) => val > 0 ? null : message || "Must be positive"
|
|
134
|
+
);
|
|
135
|
+
return schema;
|
|
136
|
+
},
|
|
137
|
+
negative(message) {
|
|
138
|
+
validators.push(
|
|
139
|
+
(val) => val < 0 ? null : message || "Must be negative"
|
|
140
|
+
);
|
|
141
|
+
return schema;
|
|
142
|
+
},
|
|
143
|
+
finite(message) {
|
|
144
|
+
validators.push(
|
|
145
|
+
(val) => Number.isFinite(val) ? null : message || "Must be finite"
|
|
146
|
+
);
|
|
147
|
+
return schema;
|
|
148
|
+
},
|
|
149
|
+
optional() {
|
|
150
|
+
return createOptionalSchema(schema);
|
|
151
|
+
},
|
|
152
|
+
parse(value) {
|
|
153
|
+
const result = schema.safeParse(value);
|
|
154
|
+
if (!result.success) {
|
|
155
|
+
throw new FloatValidationError([result.error]);
|
|
156
|
+
}
|
|
157
|
+
return result.data;
|
|
158
|
+
},
|
|
159
|
+
safeParse(value) {
|
|
160
|
+
const num = typeof value === "string" ? parseFloat(value) : value;
|
|
161
|
+
if (typeof num !== "number" || isNaN(num)) {
|
|
162
|
+
return {
|
|
163
|
+
success: false,
|
|
164
|
+
error: {
|
|
165
|
+
path: [],
|
|
166
|
+
message: "Expected number",
|
|
167
|
+
received: value,
|
|
168
|
+
expected: "number"
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
for (const validator of validators) {
|
|
173
|
+
const error2 = validator(num);
|
|
174
|
+
if (error2) {
|
|
175
|
+
return {
|
|
176
|
+
success: false,
|
|
177
|
+
error: {
|
|
178
|
+
path: [],
|
|
179
|
+
message: error2,
|
|
180
|
+
received: value,
|
|
181
|
+
expected: "number"
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return { success: true, data: num };
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
return schema;
|
|
190
|
+
}
|
|
191
|
+
function createBooleanSchema() {
|
|
192
|
+
const schema = {
|
|
193
|
+
_type: "boolean",
|
|
194
|
+
_output: false,
|
|
195
|
+
optional() {
|
|
196
|
+
return createOptionalSchema(schema);
|
|
197
|
+
},
|
|
198
|
+
parse(value) {
|
|
199
|
+
const result = schema.safeParse(value);
|
|
200
|
+
if (!result.success) {
|
|
201
|
+
throw new FloatValidationError([result.error]);
|
|
202
|
+
}
|
|
203
|
+
return result.data;
|
|
204
|
+
},
|
|
205
|
+
safeParse(value) {
|
|
206
|
+
if (value === "true") return { success: true, data: true };
|
|
207
|
+
if (value === "false") return { success: true, data: false };
|
|
208
|
+
if (typeof value !== "boolean") {
|
|
209
|
+
return {
|
|
210
|
+
success: false,
|
|
211
|
+
error: {
|
|
212
|
+
path: [],
|
|
213
|
+
message: "Expected boolean",
|
|
214
|
+
received: value,
|
|
215
|
+
expected: "boolean"
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
return { success: true, data: value };
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
return schema;
|
|
223
|
+
}
|
|
224
|
+
function createArraySchema(itemSchema) {
|
|
225
|
+
const validators = [];
|
|
226
|
+
const schema = {
|
|
227
|
+
_type: "array",
|
|
228
|
+
_output: [],
|
|
229
|
+
min(length, message) {
|
|
230
|
+
validators.push(
|
|
231
|
+
(val) => val.length >= length ? null : message || `Must have at least ${length} items`
|
|
232
|
+
);
|
|
233
|
+
return schema;
|
|
234
|
+
},
|
|
235
|
+
max(length, message) {
|
|
236
|
+
validators.push(
|
|
237
|
+
(val) => val.length <= length ? null : message || `Must have at most ${length} items`
|
|
238
|
+
);
|
|
239
|
+
return schema;
|
|
240
|
+
},
|
|
241
|
+
nonempty(message) {
|
|
242
|
+
validators.push(
|
|
243
|
+
(val) => val.length > 0 ? null : message || "Array must not be empty"
|
|
244
|
+
);
|
|
245
|
+
return schema;
|
|
246
|
+
},
|
|
247
|
+
optional() {
|
|
248
|
+
return createOptionalSchema(schema);
|
|
249
|
+
},
|
|
250
|
+
parse(value) {
|
|
251
|
+
const result = schema.safeParse(value);
|
|
252
|
+
if (!result.success) {
|
|
253
|
+
throw new FloatValidationError([result.error]);
|
|
254
|
+
}
|
|
255
|
+
return result.data;
|
|
256
|
+
},
|
|
257
|
+
safeParse(value) {
|
|
258
|
+
if (!Array.isArray(value)) {
|
|
259
|
+
return {
|
|
260
|
+
success: false,
|
|
261
|
+
error: {
|
|
262
|
+
path: [],
|
|
263
|
+
message: "Expected array",
|
|
264
|
+
received: value,
|
|
265
|
+
expected: "array"
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
for (const validator of validators) {
|
|
270
|
+
const error2 = validator(value);
|
|
271
|
+
if (error2) {
|
|
272
|
+
return {
|
|
273
|
+
success: false,
|
|
274
|
+
error: {
|
|
275
|
+
path: [],
|
|
276
|
+
message: error2,
|
|
277
|
+
received: value,
|
|
278
|
+
expected: "array"
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
const result = [];
|
|
284
|
+
for (let i = 0; i < value.length; i++) {
|
|
285
|
+
const itemResult = itemSchema.safeParse(value[i]);
|
|
286
|
+
if (!itemResult.success) {
|
|
287
|
+
return {
|
|
288
|
+
success: false,
|
|
289
|
+
error: {
|
|
290
|
+
...itemResult.error,
|
|
291
|
+
path: [String(i), ...itemResult.error.path]
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
result.push(itemResult.data);
|
|
296
|
+
}
|
|
297
|
+
return { success: true, data: result };
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
return schema;
|
|
301
|
+
}
|
|
302
|
+
function createObjectSchema(shape) {
|
|
303
|
+
let passthroughMode = false;
|
|
304
|
+
let strictMode = false;
|
|
305
|
+
const schema = {
|
|
306
|
+
_type: "object",
|
|
307
|
+
_output: {},
|
|
308
|
+
_shape: shape,
|
|
309
|
+
partial() {
|
|
310
|
+
const partialShape = {};
|
|
311
|
+
for (const key in shape) {
|
|
312
|
+
partialShape[key] = shape[key].optional();
|
|
313
|
+
}
|
|
314
|
+
return createObjectSchema(partialShape);
|
|
315
|
+
},
|
|
316
|
+
pick(...keys) {
|
|
317
|
+
const pickedShape = {};
|
|
318
|
+
for (const key of keys) {
|
|
319
|
+
pickedShape[key] = shape[key];
|
|
320
|
+
}
|
|
321
|
+
return createObjectSchema(pickedShape);
|
|
322
|
+
},
|
|
323
|
+
omit(...keys) {
|
|
324
|
+
const omittedShape = { ...shape };
|
|
325
|
+
for (const key of keys) {
|
|
326
|
+
delete omittedShape[key];
|
|
327
|
+
}
|
|
328
|
+
return createObjectSchema(omittedShape);
|
|
329
|
+
},
|
|
330
|
+
extend(extension) {
|
|
331
|
+
return createObjectSchema({ ...shape, ...extension });
|
|
332
|
+
},
|
|
333
|
+
merge(other) {
|
|
334
|
+
return createObjectSchema({ ...shape, ...other._shape });
|
|
335
|
+
},
|
|
336
|
+
passthrough() {
|
|
337
|
+
passthroughMode = true;
|
|
338
|
+
return schema;
|
|
339
|
+
},
|
|
340
|
+
strict() {
|
|
341
|
+
strictMode = true;
|
|
342
|
+
return schema;
|
|
343
|
+
},
|
|
344
|
+
optional() {
|
|
345
|
+
return createOptionalSchema(schema);
|
|
346
|
+
},
|
|
347
|
+
parse(value) {
|
|
348
|
+
const result = schema.safeParse(value);
|
|
349
|
+
if (!result.success) {
|
|
350
|
+
throw new FloatValidationError([result.error]);
|
|
351
|
+
}
|
|
352
|
+
return result.data;
|
|
353
|
+
},
|
|
354
|
+
safeParse(value) {
|
|
355
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
356
|
+
return {
|
|
357
|
+
success: false,
|
|
358
|
+
error: {
|
|
359
|
+
path: [],
|
|
360
|
+
message: "Expected object",
|
|
361
|
+
received: value,
|
|
362
|
+
expected: "object"
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
const obj = value;
|
|
367
|
+
const result = {};
|
|
368
|
+
if (strictMode) {
|
|
369
|
+
for (const key in obj) {
|
|
370
|
+
if (!(key in shape)) {
|
|
371
|
+
return {
|
|
372
|
+
success: false,
|
|
373
|
+
error: {
|
|
374
|
+
path: [key],
|
|
375
|
+
message: `Unknown key '${key}'`,
|
|
376
|
+
received: obj[key],
|
|
377
|
+
expected: "undefined"
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
for (const key in shape) {
|
|
384
|
+
const fieldSchema = shape[key];
|
|
385
|
+
const fieldResult = fieldSchema.safeParse(obj[key]);
|
|
386
|
+
if (!fieldResult.success) {
|
|
387
|
+
return {
|
|
388
|
+
success: false,
|
|
389
|
+
error: {
|
|
390
|
+
...fieldResult.error,
|
|
391
|
+
path: [key, ...fieldResult.error.path]
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
result[key] = fieldResult.data;
|
|
396
|
+
}
|
|
397
|
+
if (passthroughMode) {
|
|
398
|
+
for (const key in obj) {
|
|
399
|
+
if (!(key in shape)) {
|
|
400
|
+
result[key] = obj[key];
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return { success: true, data: result };
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
return schema;
|
|
408
|
+
}
|
|
409
|
+
function createOptionalSchema(innerSchema) {
|
|
410
|
+
const schema = {
|
|
411
|
+
_type: "optional",
|
|
412
|
+
_output: void 0,
|
|
413
|
+
_innerType: innerSchema,
|
|
414
|
+
default(defaultValue) {
|
|
415
|
+
return {
|
|
416
|
+
_type: "optional",
|
|
417
|
+
_output: defaultValue,
|
|
418
|
+
optional: () => schema,
|
|
419
|
+
parse(value) {
|
|
420
|
+
if (value === void 0 || value === null) {
|
|
421
|
+
return defaultValue;
|
|
422
|
+
}
|
|
423
|
+
return innerSchema.parse(value);
|
|
424
|
+
},
|
|
425
|
+
safeParse(value) {
|
|
426
|
+
if (value === void 0 || value === null) {
|
|
427
|
+
return { success: true, data: defaultValue };
|
|
428
|
+
}
|
|
429
|
+
return innerSchema.safeParse(value);
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
},
|
|
433
|
+
optional() {
|
|
434
|
+
return schema;
|
|
435
|
+
},
|
|
436
|
+
parse(value) {
|
|
437
|
+
const result = schema.safeParse(value);
|
|
438
|
+
if (!result.success) {
|
|
439
|
+
throw new FloatValidationError([result.error]);
|
|
440
|
+
}
|
|
441
|
+
return result.data;
|
|
442
|
+
},
|
|
443
|
+
safeParse(value) {
|
|
444
|
+
if (value === void 0 || value === null) {
|
|
445
|
+
return { success: true, data: void 0 };
|
|
446
|
+
}
|
|
447
|
+
return innerSchema.safeParse(value);
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
return schema;
|
|
451
|
+
}
|
|
452
|
+
function createEnumSchema(values) {
|
|
453
|
+
const schema = {
|
|
454
|
+
_type: "enum",
|
|
455
|
+
_output: values[0],
|
|
456
|
+
options: values,
|
|
457
|
+
optional() {
|
|
458
|
+
return createOptionalSchema(schema);
|
|
459
|
+
},
|
|
460
|
+
parse(value) {
|
|
461
|
+
const result = schema.safeParse(value);
|
|
462
|
+
if (!result.success) {
|
|
463
|
+
throw new FloatValidationError([result.error]);
|
|
464
|
+
}
|
|
465
|
+
return result.data;
|
|
466
|
+
},
|
|
467
|
+
safeParse(value) {
|
|
468
|
+
if (typeof value !== "string" || !values.includes(value)) {
|
|
469
|
+
return {
|
|
470
|
+
success: false,
|
|
471
|
+
error: {
|
|
472
|
+
path: [],
|
|
473
|
+
message: `Expected one of: ${values.join(", ")}`,
|
|
474
|
+
received: value,
|
|
475
|
+
expected: values.join(" | ")
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
return { success: true, data: value };
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
return schema;
|
|
483
|
+
}
|
|
484
|
+
function createUnionSchema(schemas) {
|
|
485
|
+
const schema = {
|
|
486
|
+
_type: "union",
|
|
487
|
+
_output: void 0,
|
|
488
|
+
optional() {
|
|
489
|
+
return createOptionalSchema(schema);
|
|
490
|
+
},
|
|
491
|
+
parse(value) {
|
|
492
|
+
const result = schema.safeParse(value);
|
|
493
|
+
if (!result.success) {
|
|
494
|
+
throw new FloatValidationError([result.error]);
|
|
495
|
+
}
|
|
496
|
+
return result.data;
|
|
497
|
+
},
|
|
498
|
+
safeParse(value) {
|
|
499
|
+
const errors = [];
|
|
500
|
+
for (const s of schemas) {
|
|
501
|
+
const result = s.safeParse(value);
|
|
502
|
+
if (result.success) {
|
|
503
|
+
return result;
|
|
504
|
+
}
|
|
505
|
+
errors.push(result.error);
|
|
506
|
+
}
|
|
507
|
+
return {
|
|
508
|
+
success: false,
|
|
509
|
+
error: {
|
|
510
|
+
path: [],
|
|
511
|
+
message: `Value did not match any schema in union`,
|
|
512
|
+
received: value,
|
|
513
|
+
expected: schemas.map((s) => s._type).join(" | ")
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
return schema;
|
|
519
|
+
}
|
|
520
|
+
var FloatValidationError = class extends Error {
|
|
521
|
+
errors;
|
|
522
|
+
status = 400;
|
|
523
|
+
constructor(errors) {
|
|
524
|
+
super("Validation failed");
|
|
525
|
+
this.name = "FloatValidationError";
|
|
526
|
+
this.errors = errors;
|
|
527
|
+
}
|
|
528
|
+
toJSON() {
|
|
529
|
+
return {
|
|
530
|
+
error: "Validation Error",
|
|
531
|
+
details: this.errors.map((e) => ({
|
|
532
|
+
path: e.path.join(".") || "root",
|
|
533
|
+
message: e.message,
|
|
534
|
+
received: typeof e.received
|
|
535
|
+
}))
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
toResponse() {
|
|
539
|
+
return new Response(JSON.stringify(this.toJSON()), {
|
|
540
|
+
status: 400,
|
|
541
|
+
headers: { "Content-Type": "application/json" }
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
var f = {
|
|
546
|
+
string: createStringSchema,
|
|
547
|
+
number: createNumberSchema,
|
|
548
|
+
boolean: createBooleanSchema,
|
|
549
|
+
array: createArraySchema,
|
|
550
|
+
object: createObjectSchema,
|
|
551
|
+
enum: createEnumSchema,
|
|
552
|
+
union: createUnionSchema
|
|
553
|
+
};
|
|
554
|
+
function typedRoute(options, handler) {
|
|
555
|
+
return async (request) => {
|
|
556
|
+
try {
|
|
557
|
+
const validated = { body: void 0, query: void 0, params: void 0 };
|
|
558
|
+
if (options.body) {
|
|
559
|
+
try {
|
|
560
|
+
const body = await request.json();
|
|
561
|
+
const bodySchema = options.body;
|
|
562
|
+
const result = bodySchema.safeParse(body);
|
|
563
|
+
if (!result.success) {
|
|
564
|
+
return new FloatValidationError([result.error]).toResponse();
|
|
565
|
+
}
|
|
566
|
+
validated.body = result.data;
|
|
567
|
+
} catch {
|
|
568
|
+
return new Response(
|
|
569
|
+
JSON.stringify({ error: "Invalid JSON body" }),
|
|
570
|
+
{ status: 400, headers: { "Content-Type": "application/json" } }
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
if (options.query) {
|
|
575
|
+
const url = new URL(request.url);
|
|
576
|
+
const queryObj = {};
|
|
577
|
+
url.searchParams.forEach((value, key) => {
|
|
578
|
+
queryObj[key] = value;
|
|
579
|
+
});
|
|
580
|
+
const querySchema = options.query;
|
|
581
|
+
const result = querySchema.safeParse(queryObj);
|
|
582
|
+
if (!result.success) {
|
|
583
|
+
return new FloatValidationError([result.error]).toResponse();
|
|
584
|
+
}
|
|
585
|
+
validated.query = result.data;
|
|
586
|
+
}
|
|
587
|
+
if (options.params) {
|
|
588
|
+
const params = request.params || {};
|
|
589
|
+
const paramsSchema = options.params;
|
|
590
|
+
const result = paramsSchema.safeParse(params);
|
|
591
|
+
if (!result.success) {
|
|
592
|
+
return new FloatValidationError([result.error]).toResponse();
|
|
593
|
+
}
|
|
594
|
+
validated.params = result.data;
|
|
595
|
+
}
|
|
596
|
+
const typedRequest = request;
|
|
597
|
+
typedRequest.validated = validated;
|
|
598
|
+
return await handler(typedRequest);
|
|
599
|
+
} catch (error2) {
|
|
600
|
+
if (error2 instanceof FloatValidationError) {
|
|
601
|
+
return error2.toResponse();
|
|
602
|
+
}
|
|
603
|
+
console.error("Route error:", error2);
|
|
604
|
+
return new Response(
|
|
605
|
+
JSON.stringify({ error: "Internal server error" }),
|
|
606
|
+
{ status: 500, headers: { "Content-Type": "application/json" } }
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
function json(data, options = {}) {
|
|
612
|
+
return new Response(JSON.stringify(data), {
|
|
613
|
+
status: options.status || 200,
|
|
614
|
+
headers: {
|
|
615
|
+
"Content-Type": "application/json",
|
|
616
|
+
...options.headers
|
|
617
|
+
}
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
function error(message, status = 500) {
|
|
621
|
+
return new Response(
|
|
622
|
+
JSON.stringify({ error: message }),
|
|
623
|
+
{
|
|
624
|
+
status,
|
|
625
|
+
headers: { "Content-Type": "application/json" }
|
|
626
|
+
}
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
function redirect(url, status = 302) {
|
|
630
|
+
return new Response(null, {
|
|
631
|
+
status,
|
|
632
|
+
headers: { Location: url }
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
export {
|
|
636
|
+
FloatValidationError,
|
|
637
|
+
error,
|
|
638
|
+
f,
|
|
639
|
+
json,
|
|
640
|
+
redirect,
|
|
641
|
+
typedRoute
|
|
642
|
+
};
|
|
643
|
+
//# sourceMappingURL=index.js.map
|