astro-routify 1.4.0 → 1.5.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 +213 -91
- package/dist/core/RouteTrie.js +52 -24
- package/dist/core/RouterBuilder.js +42 -9
- package/dist/core/defineGroup.d.ts +1 -0
- package/dist/core/defineGroup.js +1 -0
- package/dist/core/defineHandler.d.ts +19 -11
- package/dist/core/defineHandler.js +2 -13
- package/dist/core/defineRoute.d.ts +6 -1
- package/dist/core/defineRoute.js +8 -3
- package/dist/core/defineRouter.d.ts +2 -2
- package/dist/core/defineRouter.js +18 -2
- package/dist/core/internal/createJsonStreamRoute.d.ts +1 -1
- package/dist/core/middlewares.d.ts +1 -1
- package/dist/core/middlewares.js +4 -4
- package/dist/core/openapi.js +32 -5
- package/dist/core/responseHelpers.d.ts +9 -5
- package/dist/core/responseHelpers.js +92 -17
- package/dist/core/stream.d.ts +3 -3
- package/dist/core/stream.js +3 -3
- package/dist/core/streamJsonArray.d.ts +1 -1
- package/dist/core/streamJsonArray.js +1 -1
- package/dist/index.d.ts +42 -24
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -15,6 +15,10 @@ declare const ALLOWED_HTTP_METHODS: Set<string>;
|
|
|
15
15
|
*/
|
|
16
16
|
declare function normalizeMethod(method: string): HttpMethod;
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Supported types that can be returned from a route handler.
|
|
20
|
+
*/
|
|
21
|
+
type HandlerResult = Response | ResultResponse | string | number | boolean | object | ArrayBuffer | Uint8Array | ReadableStream<Uint8Array> | Blob | FormData | URLSearchParams | null | undefined;
|
|
18
22
|
/**
|
|
19
23
|
* A standardized shape for internal route result objects.
|
|
20
24
|
* These are later converted into native `Response` instances.
|
|
@@ -86,7 +90,7 @@ declare const internalError: (err: unknown, headers?: HeadersInit) => ResultResp
|
|
|
86
90
|
/**
|
|
87
91
|
* Sends a binary or stream-based file response with optional content-disposition.
|
|
88
92
|
*
|
|
89
|
-
* @param content - The file
|
|
93
|
+
* @param content - The file state (Blob, ArrayBuffer, or stream)
|
|
90
94
|
* @param contentType - MIME type of the file
|
|
91
95
|
* @param fileName - Optional download filename
|
|
92
96
|
* @param headers - Optional extra headers
|
|
@@ -101,40 +105,48 @@ declare const fileResponse: (content: Blob | ArrayBuffer | ReadableStream<Uint8A
|
|
|
101
105
|
*/
|
|
102
106
|
declare function isReadableStream(value: unknown): value is ReadableStream<Uint8Array>;
|
|
103
107
|
/**
|
|
104
|
-
* Converts an internal `ResultResponse` into a native `Response` object
|
|
108
|
+
* Converts an internal `ResultResponse` or any `HandlerResult` into a native `Response` object
|
|
105
109
|
* for use inside Astro API routes.
|
|
106
110
|
*
|
|
107
|
-
* Automatically applies
|
|
111
|
+
* Automatically applies appropriate Content-Type headers.
|
|
108
112
|
*
|
|
109
|
-
* @param result - A ResultResponse returned from route handler
|
|
113
|
+
* @param result - A ResultResponse or other supported type returned from route handler
|
|
110
114
|
* @returns A native Response
|
|
111
115
|
*/
|
|
112
|
-
declare function toAstroResponse(result:
|
|
116
|
+
declare function toAstroResponse(result: HandlerResult): Response;
|
|
113
117
|
|
|
114
118
|
/**
|
|
115
119
|
* Enhanced Astro context for Routify.
|
|
116
120
|
*/
|
|
117
|
-
interface RoutifyContext extends APIContext {
|
|
121
|
+
interface RoutifyContext<State = Record<string, any>> extends APIContext {
|
|
118
122
|
/**
|
|
119
123
|
* Parsed query parameters from the URL.
|
|
124
|
+
* Note: If multiple parameters have the same key, only the last one is included.
|
|
125
|
+
* Use `searchParams` for full multi-value support.
|
|
126
|
+
*/
|
|
127
|
+
query: Record<string, string | string[]>;
|
|
128
|
+
/**
|
|
129
|
+
* Full URLSearchParams object for the incoming request.
|
|
120
130
|
*/
|
|
121
|
-
|
|
131
|
+
searchParams: URLSearchParams;
|
|
122
132
|
/**
|
|
123
|
-
* Shared
|
|
133
|
+
* Shared state container for middlewares and handlers (e.g., validation results).
|
|
134
|
+
* This is where middlewares can store information for subsequent handlers.
|
|
124
135
|
*/
|
|
125
|
-
|
|
136
|
+
state: State;
|
|
126
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Convenience type alias for RoutifyContext.
|
|
140
|
+
*/
|
|
141
|
+
type Context<State = Record<string, any>> = RoutifyContext<State>;
|
|
127
142
|
/**
|
|
128
143
|
* A middleware function that can modify the context or short-circuit the response.
|
|
129
144
|
*/
|
|
130
|
-
type Middleware = (ctx: RoutifyContext
|
|
145
|
+
type Middleware<State = any> = (ctx: RoutifyContext<State>, next: () => Promise<Response>) => Promise<Response> | Response;
|
|
131
146
|
/**
|
|
132
|
-
* A flexible route handler that can return
|
|
133
|
-
* - a native `Response` object,
|
|
134
|
-
* - a structured `ResultResponse` object,
|
|
135
|
-
* - or a file stream (Blob, ArrayBuffer, or ReadableStream).
|
|
147
|
+
* A flexible route handler that can return various types.
|
|
136
148
|
*/
|
|
137
|
-
type Handler = (ctx: RoutifyContext) => Promise<
|
|
149
|
+
type Handler<State = any> = (ctx: RoutifyContext<State>) => Promise<HandlerResult> | HandlerResult;
|
|
138
150
|
/**
|
|
139
151
|
* Wraps a `Handler` function into an `APIRoute` that:
|
|
140
152
|
* - logs requests and responses,
|
|
@@ -168,9 +180,14 @@ interface Route {
|
|
|
168
180
|
*/
|
|
169
181
|
middlewares?: Middleware[];
|
|
170
182
|
/**
|
|
171
|
-
* Optional metadata for the route (e.g
|
|
183
|
+
* Optional metadata for the route (e.g. for OpenAPI generation).
|
|
172
184
|
*/
|
|
173
185
|
metadata?: Record<string, any>;
|
|
186
|
+
/**
|
|
187
|
+
* Internal marker to identify the object type during module discovery.
|
|
188
|
+
* @internal
|
|
189
|
+
*/
|
|
190
|
+
_routifyType?: 'route';
|
|
174
191
|
}
|
|
175
192
|
/**
|
|
176
193
|
* Defines a route using a `Route` object.
|
|
@@ -232,7 +249,7 @@ interface RouterOptions {
|
|
|
232
249
|
/**
|
|
233
250
|
* Custom error handler for the router.
|
|
234
251
|
*/
|
|
235
|
-
onError?: (error: unknown, ctx: RoutifyContext) =>
|
|
252
|
+
onError?: (error: unknown, ctx: RoutifyContext) => HandlerResult | Response;
|
|
236
253
|
}
|
|
237
254
|
/**
|
|
238
255
|
* Defines a router that dynamically matches registered routes based on method and path.
|
|
@@ -270,6 +287,7 @@ declare function defineRouter(routes: Route[], options?: RouterOptions): APIRout
|
|
|
270
287
|
* .addPost('/', createUser);
|
|
271
288
|
*/
|
|
272
289
|
declare class RouteGroup {
|
|
290
|
+
readonly _routifyType = "group";
|
|
273
291
|
private basePath;
|
|
274
292
|
private routes;
|
|
275
293
|
private middlewares;
|
|
@@ -616,7 +634,7 @@ declare const Patch: (path: string) => (target: any, propertyKey: string, descri
|
|
|
616
634
|
declare function Group(basePath: string): (constructor: Function) => void;
|
|
617
635
|
|
|
618
636
|
/**
|
|
619
|
-
* A writer for streaming raw
|
|
637
|
+
* A writer for streaming raw state to the response body.
|
|
620
638
|
*
|
|
621
639
|
* This is used inside the `stream()` route handler to emit bytes
|
|
622
640
|
* or strings directly to the client with backpressure awareness.
|
|
@@ -660,7 +678,7 @@ interface StreamOptions {
|
|
|
660
678
|
keepAlive?: boolean;
|
|
661
679
|
}
|
|
662
680
|
/**
|
|
663
|
-
* Defines a generic streaming route that can write raw chunks of
|
|
681
|
+
* Defines a generic streaming route that can write raw chunks of state
|
|
664
682
|
* to the response in real time using a `ReadableStream`.
|
|
665
683
|
*
|
|
666
684
|
* Suitable for Server-Sent Events (SSE), long-polling, streamed HTML,
|
|
@@ -669,7 +687,7 @@ interface StreamOptions {
|
|
|
669
687
|
* @example
|
|
670
688
|
* stream('/clock', async ({ response }) => {
|
|
671
689
|
* const timer = setInterval(() => {
|
|
672
|
-
* response.write(`
|
|
690
|
+
* response.write(`state: ${new Date().toISOString()}\n\n`);
|
|
673
691
|
* }, 1000);
|
|
674
692
|
*
|
|
675
693
|
* setTimeout(() => {
|
|
@@ -689,7 +707,7 @@ declare function stream(path: string, handler: (ctx: APIContext & {
|
|
|
689
707
|
|
|
690
708
|
type JsonValue = any;
|
|
691
709
|
/**
|
|
692
|
-
* A writer interface for streaming JSON
|
|
710
|
+
* A writer interface for streaming JSON state to the response body.
|
|
693
711
|
* Supports both NDJSON and array formats.
|
|
694
712
|
*/
|
|
695
713
|
interface JsonStreamWriter {
|
|
@@ -753,7 +771,7 @@ declare function streamJsonND(path: string, handler: (ctx: APIContext & {
|
|
|
753
771
|
* Defines a JSON streaming route that emits a valid JSON array.
|
|
754
772
|
*
|
|
755
773
|
* This helper returns a valid `application/json` response containing
|
|
756
|
-
* a streamable array of JSON values. Useful for large
|
|
774
|
+
* a streamable array of JSON values. Useful for large state exports
|
|
757
775
|
* or APIs where the full array can be streamed as it's generated.
|
|
758
776
|
*
|
|
759
777
|
* Unlike `streamJsonND()`, this wraps all values in `[` and `]`
|
|
@@ -820,7 +838,7 @@ interface ValidationSchema {
|
|
|
820
838
|
* Middleware for request validation.
|
|
821
839
|
* Supports any schema library with a `safeParse` method (like Zod).
|
|
822
840
|
*
|
|
823
|
-
* Validated
|
|
841
|
+
* Validated state is stored in `ctx.state.body`, `ctx.state.query`, etc.
|
|
824
842
|
*/
|
|
825
843
|
declare function validate(schema: ValidationSchema): Middleware;
|
|
826
844
|
|
|
@@ -843,4 +861,4 @@ interface OpenAPIOptions {
|
|
|
843
861
|
declare function generateOpenAPI(router: any, options: OpenAPIOptions): any;
|
|
844
862
|
|
|
845
863
|
export { ALLOWED_HTTP_METHODS, Delete, Get, Group, HttpMethod, Patch, Post, Put, RouteGroup, RouteTrie, RouterBuilder, badRequest, cors, createRouter, created, defineGroup, defineHandler, defineRoute, defineRouter, fileResponse, forbidden, generateOpenAPI, globalRegistry, internalError, isReadableStream, isRoute, json, methodNotAllowed, noContent, normalizeMethod, notFound, notModified, ok, securityHeaders, stream, streamJsonArray, streamJsonND, toAstroResponse, tooManyRequests, unauthorized, validate, validateRoute };
|
|
846
|
-
export type { CorsOptions, Handler, JsonStreamWriter, Middleware, OpenAPIOptions, ResultResponse, Route, RouterOptions, RoutifyContext, StreamOptions, StreamWriter, Validatable, ValidationSchema };
|
|
864
|
+
export type { Context, CorsOptions, Handler, HandlerResult, JsonStreamWriter, Middleware, OpenAPIOptions, ResultResponse, Route, RouterOptions, RoutifyContext, StreamOptions, StreamWriter, Validatable, ValidationSchema };
|