-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.ts
130 lines (122 loc) · 2.85 KB
/
schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { z } from 'zod';
import type { HttpMethod } from './src/tea/types/config';
/**
* Schema definition for API endpoints.
* @template TResponse - The response type from Zod schema
* @template TParams - The path parameters type from Zod schema
* @template TBody - The request body type from Zod schema
* @template TQuery - The query parameters type from Zod schema
*/
export interface ApiEndpointSchema<
TResponse extends z.ZodType,
TParams extends z.ZodType<Record<string, string | number>> = z.ZodType<
Record<string, string | number>
>,
TBody extends z.ZodType = z.ZodType,
TQuery extends z.ZodType = z.ZodType
> {
/** Zod schema for validating the response */
response: TResponse;
/** Optional Zod schema for validating request body */
body?: TBody;
/** Optional Zod schema for validating query parameters */
query?: TQuery;
/** Optional Zod schema for validating path parameters */
params?: TParams;
}
/**
* Definition for an API route.
*/
export interface RouteDefinition<
TMethod extends HttpMethod,
TPath extends string,
TSchema extends ApiEndpointSchema<z.ZodType, z.ZodType, z.ZodType, z.ZodType>
> {
method: TMethod;
path: TPath;
schema: TSchema;
}
/**
* Schema Definition for Tea Schema
* @example
* const userSchema = {
* getUsers: {
* method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
* path: string,
* schema: {
* response: z.array(
* z.object({
* id: z.number(),
* name: z.string(),
* email: z.string().email(),
* })
* ),
* query: z.object({
* page: z.number().optional(),
* limit: z.number().optional(),
* }),
* }
* }
* } satisfies TeaSchema
*/
export type TeaSchema = Record<
string,
{
method: HttpMethod;
path: string;
schema: ApiEndpointSchema<z.ZodType, z.ZodType, z.ZodType, z.ZodType>;
}
>;
/**
* Extracts the response type from a Zod schema
*/
export type InferResponseType<T> = T extends ApiEndpointSchema<
infer R extends z.ZodType,
any,
any,
any
>
? z.infer<R>
: never;
/**
* Extracts the path parameters type from a Zod schema
*/
export type InferParamsType<T> = T extends ApiEndpointSchema<
any,
infer P extends z.ZodType,
any,
any
>
? z.infer<P>
: never;
/**
* Extracts the request body type from a Zod schema
*/
export type InferBodyType<T> = T extends ApiEndpointSchema<
any,
any,
infer B extends z.ZodType,
any
>
? z.infer<B>
: never;
/**
* Extracts the query parameters type from a Zod schema
*/
export type InferQueryType<T> = T extends ApiEndpointSchema<
any,
any,
any,
infer Q extends z.ZodType
>
? z.infer<Q>
: never;
/**
* Extracts route keys from a schema definition
*/
export type ExtractRouteKeys<T> = T extends Record<
infer K,
RouteDefinition<HttpMethod, string, ApiEndpointSchema<z.ZodType>>
>
? K
: never;