@aura-stack/router 0.2.0 → 0.4.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 +2 -2
- package/dist/assert.cjs +61 -3
- package/dist/assert.d.ts +21 -1
- package/dist/assert.js +8 -3
- package/dist/chunk-6PZEXNTS.js +31 -0
- package/dist/chunk-CFAIW6YL.js +41 -0
- package/dist/chunk-CL5D7UJU.js +149 -0
- package/dist/{chunk-RFYOPPMW.js → chunk-GJC3ODME.js} +15 -6
- package/dist/{chunk-JRJKKBSH.js → chunk-JNMXLKDG.js} +12 -2
- package/dist/chunk-PT4GU6PH.js +78 -0
- package/dist/context.cjs +50 -47
- package/dist/context.d.ts +6 -5
- package/dist/context.js +3 -4
- package/dist/endpoint.cjs +29 -29
- package/dist/endpoint.d.ts +17 -16
- package/dist/endpoint.js +5 -7
- package/dist/error.cjs +19 -7
- package/dist/error.d.ts +28 -3
- package/dist/error.js +9 -3
- package/dist/index.cjs +218 -130
- package/dist/index.d.ts +3 -1
- package/dist/index.js +19 -9
- package/dist/middlewares.cjs +23 -17
- package/dist/middlewares.d.ts +4 -3
- package/dist/middlewares.js +2 -2
- package/dist/router.cjs +206 -118
- package/dist/router.d.ts +24 -2
- package/dist/router.js +13 -8
- package/dist/types.d.ts +91 -13
- package/package.json +32 -13
- package/dist/assert.d.cts +0 -32
- package/dist/chunk-DR4C6QTF.js +0 -72
- package/dist/chunk-O6SY753N.js +0 -41
- package/dist/chunk-OXDCFAMF.js +0 -78
- package/dist/chunk-YUX3YHXF.js +0 -36
- package/dist/context.d.cts +0 -84
- package/dist/endpoint.d.cts +0 -59
- package/dist/error.d.cts +0 -40
- package/dist/index.d.cts +0 -4
- package/dist/middlewares.d.cts +0 -22
- package/dist/router.d.cts +0 -15
- package/dist/types.d.cts +0 -134
package/dist/types.d.cts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import { ZodObject, z } from 'zod';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Route pattern must start with a slash and can contain parameters prefixed with a colon.
|
|
5
|
-
* @example
|
|
6
|
-
* const getUser:RoutePattern = "/users/:userId"
|
|
7
|
-
* const getPostsComments:RoutePattern = "/posts/:postId/comments/:commentId"
|
|
8
|
-
*/
|
|
9
|
-
type RoutePattern = `/${string}` | `/${string}/:${string}`;
|
|
10
|
-
/**
|
|
11
|
-
* HTTP methods supported by the router.
|
|
12
|
-
*/
|
|
13
|
-
type HTTPMethod = "GET" | "POST" | "DELETE" | "PUT" | "PATCH";
|
|
14
|
-
/**
|
|
15
|
-
* Content types supported by the router.
|
|
16
|
-
*/
|
|
17
|
-
type ContentType = "application/json" | "application/x-www-form-urlencoded" | "text/plain";
|
|
18
|
-
type Prettify<Obj extends object> = {
|
|
19
|
-
[Key in keyof Obj]: Obj[Key];
|
|
20
|
-
} & {};
|
|
21
|
-
/**
|
|
22
|
-
* Extracts route parameters from a given route pattern through colon-prefixed segments.
|
|
23
|
-
* Returns an object type where keys are parameter names and values are strings.
|
|
24
|
-
* If no parameters are found, returns an empty object type.
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* // Expected: { userId: string }
|
|
28
|
-
* type UserParams = Params<"/users/:userId">;
|
|
29
|
-
*
|
|
30
|
-
* // Expected: { postId: string; commentId: string }
|
|
31
|
-
* type PostCommentParams = Params<"/posts/:postId/comments/:commentId">;
|
|
32
|
-
*
|
|
33
|
-
* // Expected: {}
|
|
34
|
-
* type NoParams = Params<"/about">;
|
|
35
|
-
*/
|
|
36
|
-
type Params<Route extends RoutePattern> = Route extends `/${string}/:${infer Param}/${infer Str}` ? Prettify<{
|
|
37
|
-
[K in Param]: string;
|
|
38
|
-
} & Params<`/${Str}`>> : Route extends `/${string}/:${infer Param}` ? Prettify<{
|
|
39
|
-
[K in Param]: string;
|
|
40
|
-
}> : Route extends `/:${infer Param}` ? Prettify<{
|
|
41
|
-
[K in Param]: string;
|
|
42
|
-
}> : {};
|
|
43
|
-
type GetRouteParams<T extends RoutePattern> = Params<T>;
|
|
44
|
-
/**
|
|
45
|
-
* Available schemas validation for an endpoint. It can include body and searchParams schemas.
|
|
46
|
-
*/
|
|
47
|
-
interface EndpointSchemas {
|
|
48
|
-
body?: ZodObject<any>;
|
|
49
|
-
searchParams?: ZodObject<any>;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Configuration for an endpoint, including optional schemas for request validation and middlewares.
|
|
53
|
-
*/
|
|
54
|
-
type EndpointConfig<RouteParams extends RoutePattern = RoutePattern, Schemas extends EndpointSchemas = EndpointSchemas> = Prettify<{
|
|
55
|
-
schemas?: Schemas;
|
|
56
|
-
middlewares?: MiddlewareFunction<GetRouteParams<RouteParams>, {
|
|
57
|
-
schemas: Schemas;
|
|
58
|
-
}>[];
|
|
59
|
-
}>;
|
|
60
|
-
/**
|
|
61
|
-
* Infer the type of search parameters from the provided value in the `EndpointConfig`.
|
|
62
|
-
*/
|
|
63
|
-
type ContextSearchParams<Schemas extends EndpointConfig["schemas"]> = Schemas extends {
|
|
64
|
-
searchParams: ZodObject;
|
|
65
|
-
} ? {
|
|
66
|
-
searchParams: z.infer<Schemas["searchParams"]>;
|
|
67
|
-
} : {
|
|
68
|
-
searchParams: URLSearchParams;
|
|
69
|
-
};
|
|
70
|
-
/**
|
|
71
|
-
* Infer the type of body from the provided value in the `EndpointConfig`.
|
|
72
|
-
*/
|
|
73
|
-
type ContextBody<Schemas extends EndpointConfig["schemas"]> = Schemas extends {
|
|
74
|
-
body: ZodObject;
|
|
75
|
-
} ? {
|
|
76
|
-
body: z.infer<Schemas["body"]>;
|
|
77
|
-
} : {
|
|
78
|
-
body: undefined;
|
|
79
|
-
};
|
|
80
|
-
/**
|
|
81
|
-
* Context object passed to route handlers and middlewares defined in the
|
|
82
|
-
* `createEndpoint/createEndpointConfig` function or globally in the `createRouter` function.
|
|
83
|
-
*/
|
|
84
|
-
interface RequestContext<RouteParams = Record<string, string>, Config extends EndpointConfig = EndpointConfig> {
|
|
85
|
-
params: RouteParams;
|
|
86
|
-
headers: Headers;
|
|
87
|
-
body: ContextBody<Config["schemas"]>["body"];
|
|
88
|
-
searchParams: ContextSearchParams<Config["schemas"]>["searchParams"];
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Global middleware function type that represent a function that runs before the route matching.
|
|
92
|
-
*/
|
|
93
|
-
type GlobalMiddleware = (request: Request) => Promise<Request | Response>;
|
|
94
|
-
/**
|
|
95
|
-
* Middleware function type that represent a function that runs before the route handler
|
|
96
|
-
* defined in the `createEndpoint/createEndpointConfig` function or globally in the `createRouter` function.
|
|
97
|
-
*/
|
|
98
|
-
type MiddlewareFunction<RouteParams = Record<string, string>, Config extends EndpointConfig = EndpointConfig> = (request: Request, ctx: Prettify<RequestContext<RouteParams, Config>>) => Promise<RequestContext<RouteParams, Config>>;
|
|
99
|
-
/**
|
|
100
|
-
* Defines a route handler function that processes an incoming request and returns a response.
|
|
101
|
-
* The handler receives the request object and a context containing route parameters, headers,
|
|
102
|
-
* and optionally validated body and search parameters based on the endpoint configuration.
|
|
103
|
-
*/
|
|
104
|
-
type RouteHandler<Route extends RoutePattern, Config extends EndpointConfig> = (request: Request, ctx: Prettify<RequestContext<GetRouteParams<Route>, Config>>) => Promise<Response>;
|
|
105
|
-
/**
|
|
106
|
-
* Represents a route endpoint definition, specifying the HTTP method, route pattern,
|
|
107
|
-
* handler function with inferred context types, and associated configuration.
|
|
108
|
-
*/
|
|
109
|
-
interface RouteEndpoint<Method extends HTTPMethod = HTTPMethod, Route extends RoutePattern = RoutePattern, Config extends EndpointConfig = EndpointConfig> {
|
|
110
|
-
method: Method;
|
|
111
|
-
route: Route;
|
|
112
|
-
handler: RouteHandler<Route, Config>;
|
|
113
|
-
config: Config;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Infer the HTTP methods defined in the provided array of route endpoints.
|
|
117
|
-
*/
|
|
118
|
-
type InferMethod<Endpoints extends RouteEndpoint[]> = Endpoints extends unknown[] ? Endpoints[number]["method"] : "unknown";
|
|
119
|
-
/**
|
|
120
|
-
* Generates an object with HTTP methods available by the router from `createRouter` function.
|
|
121
|
-
* Each method is a function that takes a request and context, returning a promise of a response.
|
|
122
|
-
*/
|
|
123
|
-
type GetHttpHandlers<Endpoints extends RouteEndpoint[]> = {
|
|
124
|
-
[Method in InferMethod<Endpoints>]: (req: Request) => Promise<Response>;
|
|
125
|
-
};
|
|
126
|
-
/**
|
|
127
|
-
* Configuration options for `createRouter` function.
|
|
128
|
-
*/
|
|
129
|
-
interface RouterConfig {
|
|
130
|
-
basePath?: RoutePattern;
|
|
131
|
-
middlewares?: GlobalMiddleware[];
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export type { ContentType, ContextBody, ContextSearchParams, EndpointConfig, EndpointSchemas, GetHttpHandlers, GetRouteParams, GlobalMiddleware, HTTPMethod, InferMethod, MiddlewareFunction, Params, Prettify, RequestContext, RouteEndpoint, RouteHandler, RoutePattern, RouterConfig };
|