@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.
@@ -0,0 +1,172 @@
1
+ /**
2
+ * Float.js Type-Safe API Module
3
+ * Automatic validation and type inference for API routes
4
+ *
5
+ * This is a MAJOR differentiator from Next.js - zero boilerplate API validation!
6
+ */
7
+ type SchemaType = 'string' | 'number' | 'boolean' | 'array' | 'object' | 'enum' | 'union' | 'optional';
8
+ interface BaseSchema<T = unknown> {
9
+ _type: SchemaType;
10
+ _output: T;
11
+ optional(): OptionalSchema<T>;
12
+ parse(value: unknown): T;
13
+ safeParse(value: unknown): {
14
+ success: true;
15
+ data: T;
16
+ } | {
17
+ success: false;
18
+ error: ValidationError;
19
+ };
20
+ }
21
+ interface ValidationError {
22
+ path: string[];
23
+ message: string;
24
+ received: unknown;
25
+ expected: string;
26
+ }
27
+ interface StringSchema extends BaseSchema<string> {
28
+ _type: 'string';
29
+ min(length: number, message?: string): StringSchema;
30
+ max(length: number, message?: string): StringSchema;
31
+ email(message?: string): StringSchema;
32
+ url(message?: string): StringSchema;
33
+ uuid(message?: string): StringSchema;
34
+ regex(pattern: RegExp, message?: string): StringSchema;
35
+ trim(): StringSchema;
36
+ toLowerCase(): StringSchema;
37
+ toUpperCase(): StringSchema;
38
+ }
39
+ declare function createStringSchema(): StringSchema;
40
+ interface NumberSchema extends BaseSchema<number> {
41
+ _type: 'number';
42
+ min(value: number, message?: string): NumberSchema;
43
+ max(value: number, message?: string): NumberSchema;
44
+ int(message?: string): NumberSchema;
45
+ positive(message?: string): NumberSchema;
46
+ negative(message?: string): NumberSchema;
47
+ finite(message?: string): NumberSchema;
48
+ }
49
+ declare function createNumberSchema(): NumberSchema;
50
+ interface BooleanSchema extends BaseSchema<boolean> {
51
+ _type: 'boolean';
52
+ }
53
+ declare function createBooleanSchema(): BooleanSchema;
54
+ interface ArraySchema<T> extends BaseSchema<T[]> {
55
+ _type: 'array';
56
+ min(length: number, message?: string): ArraySchema<T>;
57
+ max(length: number, message?: string): ArraySchema<T>;
58
+ nonempty(message?: string): ArraySchema<T>;
59
+ }
60
+ declare function createArraySchema<S extends BaseSchema>(itemSchema: S): ArraySchema<S['_output']>;
61
+ type ObjectShape = Record<string, BaseSchema>;
62
+ type InferObject<T extends ObjectShape> = {
63
+ [K in keyof T]: T[K]['_output'];
64
+ };
65
+ interface ObjectSchema<T extends ObjectShape> extends BaseSchema<InferObject<T>> {
66
+ _type: 'object';
67
+ _shape: T;
68
+ partial(): ObjectSchema<{
69
+ [K in keyof T]: OptionalSchema<T[K]['_output']>;
70
+ }>;
71
+ pick<K extends keyof T>(...keys: K[]): ObjectSchema<Pick<T, K>>;
72
+ omit<K extends keyof T>(...keys: K[]): ObjectSchema<Omit<T, K>>;
73
+ extend<E extends ObjectShape>(extension: E): ObjectSchema<T & E>;
74
+ merge<E extends ObjectShape>(other: ObjectSchema<E>): ObjectSchema<T & E>;
75
+ passthrough(): ObjectSchema<T>;
76
+ strict(): ObjectSchema<T>;
77
+ }
78
+ declare function createObjectSchema<T extends ObjectShape>(shape: T): ObjectSchema<T>;
79
+ interface OptionalSchema<T> extends BaseSchema<T | undefined> {
80
+ _type: 'optional';
81
+ _innerType: BaseSchema<T>;
82
+ default(value: T): BaseSchema<T>;
83
+ }
84
+ interface EnumSchema<T extends readonly string[]> extends BaseSchema<T[number]> {
85
+ _type: 'enum';
86
+ options: T;
87
+ }
88
+ declare function createEnumSchema<T extends readonly string[]>(values: T): EnumSchema<T>;
89
+ interface UnionSchema<T extends BaseSchema[]> extends BaseSchema<T[number]['_output']> {
90
+ _type: 'union';
91
+ }
92
+ declare function createUnionSchema<T extends BaseSchema[]>(schemas: T): UnionSchema<T>;
93
+ declare class FloatValidationError extends Error {
94
+ readonly errors: ValidationError[];
95
+ readonly status = 400;
96
+ constructor(errors: ValidationError[]);
97
+ toJSON(): {
98
+ error: string;
99
+ details: {
100
+ path: string;
101
+ message: string;
102
+ received: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
103
+ }[];
104
+ };
105
+ toResponse(): Response;
106
+ }
107
+ declare const f: {
108
+ string: typeof createStringSchema;
109
+ number: typeof createNumberSchema;
110
+ boolean: typeof createBooleanSchema;
111
+ array: typeof createArraySchema;
112
+ object: typeof createObjectSchema;
113
+ enum: typeof createEnumSchema;
114
+ union: typeof createUnionSchema;
115
+ };
116
+ type Infer<T extends BaseSchema> = T['_output'];
117
+ interface TypedRouteOptions {
118
+ body?: ObjectSchema<any>;
119
+ query?: ObjectSchema<any>;
120
+ params?: ObjectSchema<any>;
121
+ }
122
+ interface ValidatedData {
123
+ body: any;
124
+ query: any;
125
+ params: any;
126
+ }
127
+ interface TypedRequest extends Request {
128
+ validated: ValidatedData;
129
+ }
130
+ type RouteHandler = (request: TypedRequest) => Promise<Response> | Response;
131
+ /**
132
+ * Create a type-safe API route with automatic validation
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * import { typedRoute, f } from '@float/core';
137
+ *
138
+ * export const POST = typedRoute({
139
+ * body: f.object({
140
+ * name: f.string().min(2),
141
+ * email: f.string().email(),
142
+ * age: f.number().min(18).optional(),
143
+ * }),
144
+ * }, async (req) => {
145
+ * // req.validated.body is fully typed!
146
+ * const { name, email, age } = req.validated.body;
147
+ *
148
+ * return new Response(JSON.stringify({
149
+ * message: `Hello ${name}!`
150
+ * }));
151
+ * });
152
+ * ```
153
+ */
154
+ declare function typedRoute(options: TypedRouteOptions, handler: RouteHandler): (request: Request) => Promise<Response>;
155
+ interface JsonResponseOptions {
156
+ status?: number;
157
+ headers?: Record<string, string>;
158
+ }
159
+ /**
160
+ * Create a JSON response with automatic serialization
161
+ */
162
+ declare function json<T>(data: T, options?: JsonResponseOptions): Response;
163
+ /**
164
+ * Create an error response
165
+ */
166
+ declare function error(message: string, status?: number): Response;
167
+ /**
168
+ * Create a redirect response
169
+ */
170
+ declare function redirect(url: string, status?: 301 | 302 | 307 | 308): Response;
171
+
172
+ export { FloatValidationError, type Infer, error, f, json, redirect, typedRoute };