@ozanarslan/corpus 0.1.4 → 0.1.6
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 +24 -25
- package/dist/index.cjs +2014 -0
- package/dist/index.d.ts +389 -226
- package/dist/index.js +1012 -527
- package/package.json +19 -6
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
/// <reference types="node" />
|
|
4
4
|
|
|
5
|
+
declare abstract class StoreAbstract<T> {
|
|
6
|
+
protected abstract value: T;
|
|
7
|
+
set(value: T): void;
|
|
8
|
+
get(): T;
|
|
9
|
+
}
|
|
10
|
+
declare class GlobalPrefixStore extends StoreAbstract<string> {
|
|
11
|
+
protected value: string;
|
|
12
|
+
}
|
|
5
13
|
export type CookieOptions = {
|
|
6
14
|
name: string;
|
|
7
15
|
value: string;
|
|
@@ -62,6 +70,8 @@ export declare const CommonHeaders: {
|
|
|
62
70
|
readonly ContentType: "Content-Type";
|
|
63
71
|
/** Indicates the size of the entity-body in bytes */
|
|
64
72
|
readonly ContentLength: "Content-Length";
|
|
73
|
+
/** Whether to display payload inline within the page or prompt the user to download it as an attachment. */
|
|
74
|
+
readonly ContentDisposition: "Content-Disposition";
|
|
65
75
|
/** Specifies the character encodings that are acceptable */
|
|
66
76
|
readonly AcceptEncoding: "Accept-Encoding";
|
|
67
77
|
/** Informs the server about the types of data that can be sent back */
|
|
@@ -106,29 +116,29 @@ export declare const CommonHeaders: {
|
|
|
106
116
|
};
|
|
107
117
|
export type CommonHeaders = ValueOf<typeof CommonHeaders>;
|
|
108
118
|
export type OrString<T> = T | (string & {});
|
|
109
|
-
type
|
|
110
|
-
type
|
|
119
|
+
export type HeaderKey = OrString<CommonHeaders>;
|
|
120
|
+
type CHeadersInit = Headers | CHeaders | [
|
|
111
121
|
string,
|
|
112
122
|
string
|
|
113
|
-
][] | (Record<string, string> & Partial<Record<
|
|
123
|
+
][] | (Record<string, string> & Partial<Record<HeaderKey, string>>);
|
|
114
124
|
/** Headers is extended to include helpers and intellisense for common header names. */
|
|
115
|
-
declare class
|
|
116
|
-
constructor(init?:
|
|
117
|
-
append(name:
|
|
118
|
-
set(name:
|
|
119
|
-
get(name:
|
|
120
|
-
has(name:
|
|
121
|
-
delete(name:
|
|
122
|
-
static combine(source:
|
|
123
|
-
innerCombine(source:
|
|
125
|
+
declare class CHeaders extends Headers {
|
|
126
|
+
constructor(init?: CHeadersInit);
|
|
127
|
+
append(name: HeaderKey, value: string): void;
|
|
128
|
+
set(name: HeaderKey, value: string): void;
|
|
129
|
+
get(name: HeaderKey): string | null;
|
|
130
|
+
has(name: HeaderKey): boolean;
|
|
131
|
+
delete(name: HeaderKey): void;
|
|
132
|
+
static combine(source: CHeaders, target: CHeaders): CHeaders;
|
|
133
|
+
innerCombine(source: CHeaders): void;
|
|
124
134
|
setMany(init: [
|
|
125
135
|
string,
|
|
126
136
|
string
|
|
127
|
-
][] | (Record<string, string> & Partial<Record<
|
|
137
|
+
][] | (Record<string, string> & Partial<Record<HeaderKey, string>>)): void;
|
|
128
138
|
/** @deprecated */
|
|
129
|
-
static findHeaderInInit(init:
|
|
139
|
+
static findHeaderInInit(init: CHeadersInit, name: HeaderKey): string | null;
|
|
130
140
|
}
|
|
131
|
-
type
|
|
141
|
+
type CRequestInfo = Request | string | CRequest | URL;
|
|
132
142
|
/** Commonly used HTTP verbs. */
|
|
133
143
|
export declare const Method: {
|
|
134
144
|
readonly GET: "GET";
|
|
@@ -142,19 +152,19 @@ export declare const Method: {
|
|
|
142
152
|
readonly TRACE: "TRACE";
|
|
143
153
|
};
|
|
144
154
|
export type Method = ValueOf<typeof Method>;
|
|
145
|
-
type
|
|
146
|
-
headers?:
|
|
155
|
+
type CRequestInit = Omit<RequestInit, "headers" | "method"> & {
|
|
156
|
+
headers?: CHeadersInit;
|
|
147
157
|
method?: Method;
|
|
148
158
|
};
|
|
149
|
-
/**
|
|
150
|
-
declare class
|
|
151
|
-
readonly info:
|
|
152
|
-
readonly init?:
|
|
153
|
-
constructor(info:
|
|
159
|
+
/** CRequest includes a cookie jar, better headers, and some utilities. */
|
|
160
|
+
declare class CRequest extends Request {
|
|
161
|
+
readonly info: CRequestInfo;
|
|
162
|
+
readonly init?: CRequestInit | undefined;
|
|
163
|
+
constructor(info: CRequestInfo, init?: CRequestInit | undefined);
|
|
154
164
|
readonly urlObject: URL;
|
|
155
165
|
readonly isPreflight: boolean;
|
|
156
166
|
readonly cookies: Cookies;
|
|
157
|
-
headers:
|
|
167
|
+
headers: CHeaders;
|
|
158
168
|
private resolveUrlObject;
|
|
159
169
|
private resolveHeaders;
|
|
160
170
|
/** Gets cookie header and collects cookies for the jar */
|
|
@@ -295,38 +305,81 @@ export declare const Status: {
|
|
|
295
305
|
readonly NETWORK_AUTHENTICATION_REQUIRED: 511;
|
|
296
306
|
};
|
|
297
307
|
export type Status = OrNumber<ValueOf<typeof Status>>;
|
|
298
|
-
type
|
|
299
|
-
type
|
|
308
|
+
type CResponseBody<R = unknown> = R | BodyInit | null | undefined;
|
|
309
|
+
type CResponseInit = {
|
|
300
310
|
cookies?: CookiesInit;
|
|
301
|
-
headers?:
|
|
311
|
+
headers?: CHeadersInit;
|
|
302
312
|
status?: Status;
|
|
303
313
|
statusText?: string;
|
|
304
314
|
};
|
|
315
|
+
export type SseData = {
|
|
316
|
+
data: unknown;
|
|
317
|
+
event?: string;
|
|
318
|
+
id?: string;
|
|
319
|
+
};
|
|
320
|
+
export type Func<Args extends any[] = any[], Return = any> = (...args: Args) => Return;
|
|
321
|
+
export type SseSource = Func<[
|
|
322
|
+
send: Func<[
|
|
323
|
+
event: SseData
|
|
324
|
+
], void>
|
|
325
|
+
], void | Func<[
|
|
326
|
+
], void>>;
|
|
327
|
+
export type NdjsonSource = Func<[
|
|
328
|
+
send: Func<[
|
|
329
|
+
item: unknown
|
|
330
|
+
], void>
|
|
331
|
+
], void | Func<[
|
|
332
|
+
], void>>;
|
|
305
333
|
/**
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
334
|
+
* Represents an HTTP response. Pass it a body and optional init to construct a response,
|
|
335
|
+
* or use the static methods for common patterns like redirects and streaming.
|
|
336
|
+
*
|
|
337
|
+
* The body is automatically serialized based on its type:
|
|
338
|
+
* - `null` / `undefined` → empty body with `text/plain`
|
|
339
|
+
* - Primitives (`string`, `number`, `boolean`, `bigint`) → string with `text/plain`
|
|
340
|
+
* - `Date` → ISO string with `text/plain`
|
|
341
|
+
* - Plain objects and arrays → JSON string with `application/json`
|
|
342
|
+
* - `ArrayBuffer` → binary with `application/octet-stream`
|
|
343
|
+
* - `Blob` → binary with the Blob's own mime type
|
|
344
|
+
* - `FormData` → multipart with `multipart/form-data`
|
|
345
|
+
* - `URLSearchParams` → encoded with `application/x-www-form-urlencoded`
|
|
346
|
+
* - `ReadableStream` → streamed as-is, set `Content-Type` manually via `init.headers`
|
|
347
|
+
* - Custom class instances → falls back to `.toString()`
|
|
348
|
+
*
|
|
349
|
+
* Use {@link CResponse.response} to get the native web `Response` to return from a route handler.
|
|
350
|
+
*
|
|
351
|
+
* Static helpers:
|
|
352
|
+
* - {@link CResponse.redirect} / {@link CResponse.permanentRedirect} / {@link CResponse.temporaryRedirect} / {@link CResponse.seeOther} — HTTP redirects
|
|
353
|
+
* - {@link CResponse.sse} — Server-Sent Events stream
|
|
354
|
+
* - {@link CResponse.ndjson} — Newline-delimited JSON stream
|
|
355
|
+
* - {@link CResponse.streamFile} — Stream a file from disk
|
|
356
|
+
* - {@link CResponse.file} — Respond with a static file
|
|
357
|
+
*/
|
|
358
|
+
declare class CResponse<R = unknown> {
|
|
359
|
+
protected readonly data?: CResponseBody<R>;
|
|
360
|
+
protected readonly init?: CResponseInit | undefined;
|
|
361
|
+
constructor(data?: CResponseBody<R>, init?: CResponseInit | undefined);
|
|
362
|
+
body: BodyInit;
|
|
363
|
+
headers: CHeaders;
|
|
316
364
|
status: Status;
|
|
317
365
|
statusText: string;
|
|
318
366
|
cookies: Cookies;
|
|
319
367
|
get response(): Response;
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
368
|
+
static redirect(url: string | URL, init?: CResponseInit): CResponse;
|
|
369
|
+
static permanentRedirect(url: string | URL, init?: Omit<CResponseInit, "status">): CResponse;
|
|
370
|
+
static temporaryRedirect(url: string | URL, init?: Omit<CResponseInit, "status">): CResponse;
|
|
371
|
+
static seeOther(url: string | URL, init?: Omit<CResponseInit, "status">): CResponse;
|
|
372
|
+
private static createStream;
|
|
373
|
+
static sse(source: SseSource, init?: Omit<CResponseInit, "status">, retry?: number): CResponse;
|
|
374
|
+
static ndjson(source: NdjsonSource, init?: Omit<CResponseInit, "status">): CResponse;
|
|
375
|
+
static streamFile(filePath: string, disposition?: "attachment" | "inline", init?: Omit<CResponseInit, "status">): Promise<CResponse>;
|
|
376
|
+
static file(filePath: string, init?: CResponseInit): Promise<CResponse>;
|
|
377
|
+
private resolveCookies;
|
|
378
|
+
private resolveHeaders;
|
|
379
|
+
private resolveStatus;
|
|
323
380
|
private setContentType;
|
|
324
|
-
private
|
|
381
|
+
private resolveBody;
|
|
325
382
|
private getDefaultStatusText;
|
|
326
|
-
static redirect(url: string | URL, init?: HttpResponseInit): HttpResponse;
|
|
327
|
-
static permanentRedirect(url: string | URL, init?: Omit<HttpResponseInit, "status">): HttpResponse;
|
|
328
|
-
static temporaryRedirect(url: string | URL, init?: Omit<HttpResponseInit, "status">): HttpResponse;
|
|
329
|
-
static seeOther(url: string | URL, init?: Omit<HttpResponseInit, "status">): HttpResponse;
|
|
330
383
|
}
|
|
331
384
|
declare const RouteVariant: {
|
|
332
385
|
readonly static: "static";
|
|
@@ -491,20 +544,19 @@ export type RouterModelData<B = unknown, S = unknown, P = unknown> = {
|
|
|
491
544
|
* res = To set the {@link HTTPResponse} data
|
|
492
545
|
* */
|
|
493
546
|
export declare class Context<B = unknown, S = unknown, P = unknown, R = unknown> {
|
|
494
|
-
constructor(req:
|
|
495
|
-
req:
|
|
547
|
+
constructor(req: CRequest, body: B, search: S, params: P, res?: CResponse<R>);
|
|
548
|
+
req: CRequest;
|
|
496
549
|
url: URL;
|
|
497
|
-
headers:
|
|
550
|
+
headers: CHeaders;
|
|
498
551
|
cookies: Cookies;
|
|
499
552
|
body: B;
|
|
500
553
|
search: S;
|
|
501
554
|
params: P;
|
|
502
|
-
res:
|
|
555
|
+
res: CResponse<R>;
|
|
503
556
|
data: ContextDataInterface;
|
|
504
|
-
static makeFromRequest(req:
|
|
505
|
-
static appendParsedData<
|
|
557
|
+
static makeFromRequest(req: CRequest): Context;
|
|
558
|
+
static appendParsedData<B = unknown, S = unknown, P = unknown, R = unknown>(ctx: Context<B, S, P, R>, req: CRequest, params: Record<string, string>, search: Record<string, string>, model?: RouterModelData<B, S, P>): Promise<void>;
|
|
506
559
|
}
|
|
507
|
-
export type Func<Args extends any[] = any[], Return = any> = (...args: Args) => Return;
|
|
508
560
|
export type MaybePromise<T> = Promise<T> | T;
|
|
509
561
|
export type RouteHandler<B = unknown, S = unknown, P = unknown, R = unknown> = Func<[
|
|
510
562
|
Context<B, S, P, R>
|
|
@@ -526,17 +578,34 @@ declare abstract class RouteAbstract<Path extends string = string, B = unknown,
|
|
|
526
578
|
abstract id: RouteId;
|
|
527
579
|
abstract handler: RouteHandler<B, S, P, R>;
|
|
528
580
|
abstract model?: RouteModel<B, S, P, R>;
|
|
529
|
-
resolveEndpoint(definition: RouteDefinition<Path>, variant: RouteVariant): Path;
|
|
530
|
-
resolveMethod(definition: RouteDefinition<Path>): Method;
|
|
531
|
-
resolvePattern(endpoint: Path): RegExp;
|
|
532
|
-
resolveId(method: string, endpoint: Path): RouteId;
|
|
581
|
+
protected resolveEndpoint(definition: RouteDefinition<Path>, variant: RouteVariant): Path;
|
|
582
|
+
protected resolveMethod(definition: RouteDefinition<Path>): Method;
|
|
583
|
+
protected resolvePattern(endpoint: Path): RegExp;
|
|
584
|
+
protected resolveId(method: string, endpoint: Path): RouteId;
|
|
533
585
|
}
|
|
534
586
|
/**
|
|
535
|
-
*
|
|
536
|
-
*
|
|
537
|
-
*
|
|
538
|
-
*
|
|
539
|
-
*
|
|
587
|
+
* Defines an HTTP endpoint. Accepts a {@link RouteDefinition} which can either be a plain
|
|
588
|
+
* path string (defaults to GET) or an object with a `method` and `path` for other HTTP methods.
|
|
589
|
+
*
|
|
590
|
+
* The handler receives a {@link Context} and can return any data, a {@link CResponse} directly,
|
|
591
|
+
* or a plain web `Response` for cases where full control over the response is needed.
|
|
592
|
+
* Returned data is automatically serialized by {@link CResponse} — plain objects become JSON,
|
|
593
|
+
* primitives become plain text, and so on.
|
|
594
|
+
*
|
|
595
|
+
* An optional {@link RouteModel} can be provided to validate and parse the request body,
|
|
596
|
+
* URL params, and search params — the parsed results are typed and available on the context.
|
|
597
|
+
*
|
|
598
|
+
* Route instantiation automatically registers to the router.
|
|
599
|
+
*
|
|
600
|
+
* @example
|
|
601
|
+
* // GET /users
|
|
602
|
+
* new Route("/users", () => [{ id: 1 }]);
|
|
603
|
+
*
|
|
604
|
+
* // POST /users with typed body
|
|
605
|
+
* new Route({ method: C.Method.POST, path: "/users" }, (c) => {
|
|
606
|
+
* return { created: c.body.name };
|
|
607
|
+
* }, { body: UserModel });
|
|
608
|
+
*/
|
|
540
609
|
export declare class Route<Path extends string = string, B = unknown, S = unknown, P = unknown, R = unknown> extends RouteAbstract<Path, B, S, P, R> {
|
|
541
610
|
constructor(definition: RouteDefinition<Path>, handler: RouteHandler<B, S, P, R>, model?: RouteModel<B, S, P, R>);
|
|
542
611
|
variant: RouteVariant;
|
|
@@ -547,52 +616,97 @@ export declare class Route<Path extends string = string, B = unknown, S = unknow
|
|
|
547
616
|
handler: RouteHandler<B, S, P, R>;
|
|
548
617
|
model?: RouteModel<B, S, P, R> | undefined;
|
|
549
618
|
static makeRouteId(method: string, endpoint: string): RouteId;
|
|
619
|
+
static makeRoutePattern(endpoint: string): RegExp;
|
|
550
620
|
}
|
|
551
621
|
export type AnyRoute = Route<string, any, any, any, any>;
|
|
552
|
-
export type AnyRouteModel = RouteModel<any, any, any, any>;
|
|
553
622
|
export type MiddlewareHandler = Func<[
|
|
554
623
|
Context
|
|
555
624
|
], MaybePromise<void>>;
|
|
556
625
|
export type StaticRouteHandler<B = unknown, S = unknown, P = unknown> = Func<[
|
|
557
|
-
Context<B, S, P, string>,
|
|
558
|
-
string
|
|
559
|
-
], MaybePromise<string>>;
|
|
626
|
+
context: Context<B, S, P, string | CResponse>,
|
|
627
|
+
content: string
|
|
628
|
+
], MaybePromise<string | CResponse>>;
|
|
629
|
+
export type StaticRouteDefinition = string | {
|
|
630
|
+
filePath: string;
|
|
631
|
+
stream: true;
|
|
632
|
+
disposition?: "attachment" | "inline";
|
|
633
|
+
};
|
|
560
634
|
/**
|
|
561
|
-
*
|
|
562
|
-
*
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
635
|
+
* Defines a route that serves a static file. Accepts a path and a {@link StaticRouteDefinition}
|
|
636
|
+
* which can either be a plain file path string for a standard file response, or an object
|
|
637
|
+
* with `stream: true` to stream the file directly from disk — useful for large files like
|
|
638
|
+
* videos, PDFs, or large assets where reading the entire file into memory is undesirable.
|
|
639
|
+
*
|
|
640
|
+
* An optional custom handler can be provided to intercept the file content before it is sent,
|
|
641
|
+
* for example to modify headers or transform the content. Route instantiation automatically
|
|
642
|
+
* registers to the router.
|
|
643
|
+
*
|
|
644
|
+
* @example
|
|
645
|
+
* // Serve a file normally
|
|
646
|
+
* new StaticRoute("/style", "assets/style.css");
|
|
647
|
+
*
|
|
648
|
+
* // Stream a large file
|
|
649
|
+
* new StaticRoute("/video", { filePath: "assets/video.mp4", stream: true });
|
|
650
|
+
*
|
|
651
|
+
* // Custom handler
|
|
652
|
+
* new StaticRoute("/doc", "assets/doc.txt", (c, content) => {
|
|
653
|
+
* c.res.headers.set("x-custom", "value");
|
|
654
|
+
* return content;
|
|
655
|
+
* });
|
|
656
|
+
*/
|
|
657
|
+
export declare class StaticRoute<Path extends string = string, B = unknown, S = unknown, P = unknown> extends RouteAbstract<Path, B, S, P, string | CResponse> {
|
|
658
|
+
constructor(path: Path, definition: StaticRouteDefinition, handler?: StaticRouteHandler<B, S, P>, model?: RouteModel<B, S, P, string | CResponse>);
|
|
569
659
|
id: RouteId;
|
|
570
660
|
variant: RouteVariant;
|
|
571
661
|
method: Method;
|
|
572
662
|
endpoint: Path;
|
|
573
663
|
pattern: RegExp;
|
|
574
|
-
model?: RouteModel<B, S, P, string> | undefined;
|
|
575
|
-
handler: RouteHandler<B, S, P, string>;
|
|
576
|
-
private
|
|
577
|
-
private
|
|
578
|
-
private
|
|
579
|
-
private mimeTypes;
|
|
580
|
-
private handleHtml;
|
|
581
|
-
private handleCss;
|
|
582
|
-
private handleJs;
|
|
583
|
-
private handleFile;
|
|
664
|
+
model?: RouteModel<B, S, P, string | CResponse> | undefined;
|
|
665
|
+
handler: RouteHandler<B, S, P, string | CResponse>;
|
|
666
|
+
private filePath;
|
|
667
|
+
private resolveFilePath;
|
|
668
|
+
private resolveHandler;
|
|
584
669
|
}
|
|
585
670
|
export type ControllerOptions = {
|
|
586
671
|
prefix?: string;
|
|
587
672
|
beforeEach?: (context: Context) => MaybePromise<void>;
|
|
588
673
|
};
|
|
589
|
-
/**
|
|
674
|
+
/**
|
|
675
|
+
* Base class for grouping related routes under a shared prefix and optional middleware.
|
|
676
|
+
* Extend this class to create your own controllers.
|
|
677
|
+
*
|
|
678
|
+
* All routes registered via {@link ControllerAbstract.route} and {@link ControllerAbstract.staticRoute}
|
|
679
|
+
* automatically inherit the controller's prefix and run `beforeEach` before the handler if set.
|
|
680
|
+
*
|
|
681
|
+
* @example
|
|
682
|
+
* class UserController extends ControllerAbstract {
|
|
683
|
+
* constructor() {
|
|
684
|
+
* super({ prefix: "/users" });
|
|
685
|
+
* }
|
|
686
|
+
*
|
|
687
|
+
* getAll = this.route("/", () => getAllUsers());
|
|
688
|
+
*
|
|
689
|
+
* create = this.route({ method: C.Method.POST, path: "/" }, (c) => createUser(c.body));
|
|
690
|
+
*
|
|
691
|
+
* avatar = this.staticRoute("/avatar", { filePath: "assets/avatar.png", stream: true });
|
|
692
|
+
* }
|
|
693
|
+
*
|
|
694
|
+
* new UserController();
|
|
695
|
+
*/
|
|
590
696
|
declare abstract class ControllerAbstract {
|
|
591
697
|
constructor(opts?: ControllerOptions);
|
|
592
698
|
routeIds: Set<RouteId>;
|
|
593
699
|
protected prefix?: string;
|
|
594
700
|
protected beforeEach?: MiddlewareHandler;
|
|
701
|
+
/**
|
|
702
|
+
* Registers a dynamic route under this controller. Behaves identically to {@link Route}
|
|
703
|
+
* but automatically prepends the controller prefix and runs `beforeEach` before the handler.
|
|
704
|
+
*/
|
|
595
705
|
protected route<Path extends string = string, B = unknown, S = unknown, P = unknown, R = unknown>(...args: ConstructorParameters<typeof Route<Path, B, S, P, R>>): Route<Path, B, S, P, R>;
|
|
706
|
+
/**
|
|
707
|
+
* Registers a static file route under this controller. Behaves identically to {@link StaticRoute}
|
|
708
|
+
* but automatically prepends the controller prefix.
|
|
709
|
+
*/
|
|
596
710
|
protected staticRoute<Path extends string = string, B = unknown, S = unknown, P = unknown>(...args: ConstructorParameters<typeof StaticRoute<Path, B, S, P>>): StaticRoute<Path, B, S, P>;
|
|
597
711
|
private resolveRouteDefinition;
|
|
598
712
|
}
|
|
@@ -610,37 +724,61 @@ export declare class Middleware {
|
|
|
610
724
|
useOn: MiddlewareUseOn;
|
|
611
725
|
handler: MiddlewareHandler;
|
|
612
726
|
}
|
|
613
|
-
export type
|
|
727
|
+
export type AnyRouteModel = RouteModel<any, any, any, any>;
|
|
614
728
|
export type RouterRouteData = Pick<AnyRoute, "id" | "endpoint" | "method" | "pattern" | "handler">;
|
|
729
|
+
export type RouterReturnData = {
|
|
730
|
+
route: RouterRouteData;
|
|
731
|
+
model: RouterModelData | undefined;
|
|
732
|
+
middleware: MiddlewareHandler | undefined;
|
|
733
|
+
params: Record<string, string>;
|
|
734
|
+
search: Record<string, string>;
|
|
735
|
+
};
|
|
736
|
+
export interface RouterAdapterInterface {
|
|
737
|
+
find(req: CRequest): RouterReturnData | null;
|
|
738
|
+
list(): Array<RouterRouteData>;
|
|
739
|
+
addRoute(data: RouterRouteData): void;
|
|
740
|
+
addModel(route: AnyRoute, model: AnyRouteModel): void;
|
|
741
|
+
addMiddleware(middleware: Middleware): void;
|
|
742
|
+
}
|
|
615
743
|
declare class Router {
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
private
|
|
619
|
-
private internFuncMap;
|
|
744
|
+
constructor(adapter?: RouterAdapterInterface);
|
|
745
|
+
models: AnyRouteModel[];
|
|
746
|
+
private _adapter;
|
|
620
747
|
private cache;
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
], Promise<HttpResponse>>;
|
|
748
|
+
checkPossibleCollision(n: RouterRouteData): boolean;
|
|
749
|
+
addModel(route: AnyRoute, model: AnyRouteModel): void;
|
|
750
|
+
addMiddleware(middleware: Middleware): void;
|
|
751
|
+
addRoute(r: AnyRoute): void;
|
|
752
|
+
findRouteHandler(req: CRequest): Func<[
|
|
753
|
+
], Promise<CResponse>>;
|
|
628
754
|
getRouteList(): Array<[
|
|
629
755
|
string,
|
|
630
756
|
string
|
|
631
757
|
]>;
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
758
|
+
}
|
|
759
|
+
declare class GlobalRouterStore extends StoreAbstract<Router | null> {
|
|
760
|
+
value: Router | null;
|
|
761
|
+
get(): Router;
|
|
762
|
+
}
|
|
763
|
+
export type CorsOptions = {
|
|
764
|
+
allowedOrigins?: string[];
|
|
765
|
+
allowedMethods?: string[];
|
|
766
|
+
allowedHeaders?: HeaderKey[];
|
|
767
|
+
credentials?: boolean;
|
|
768
|
+
};
|
|
769
|
+
/** Simple cors helper object to set cors headers */
|
|
770
|
+
export declare class Cors {
|
|
771
|
+
readonly opts: CorsOptions | undefined;
|
|
772
|
+
constructor(opts: CorsOptions | undefined);
|
|
773
|
+
private readonly originKey;
|
|
774
|
+
private readonly methodsKey;
|
|
775
|
+
private readonly headersKey;
|
|
776
|
+
private readonly credentialsKey;
|
|
777
|
+
getCorsHeaders(req: CRequest, res: CResponse): CHeaders;
|
|
778
|
+
apply(req: CRequest, res: CResponse): void;
|
|
779
|
+
}
|
|
780
|
+
declare class GlobalCorsStore extends StoreAbstract<Cors | null> {
|
|
781
|
+
protected value: Cors | null;
|
|
644
782
|
}
|
|
645
783
|
export type ConfigEnvKey = OrString<keyof Env>;
|
|
646
784
|
export type ConfigValueParser<T> = Func<[
|
|
@@ -658,117 +796,48 @@ export declare class Config {
|
|
|
658
796
|
}): T;
|
|
659
797
|
static set(key: string, value: string | number | boolean): void;
|
|
660
798
|
}
|
|
661
|
-
|
|
662
|
-
text(): Promise<string>;
|
|
663
|
-
};
|
|
664
|
-
declare class FileWalkerUsingBun {
|
|
665
|
-
static read(address: string): Promise<string | null>;
|
|
666
|
-
static exists(address: string): Promise<boolean>;
|
|
667
|
-
static getExtension(address: string): string;
|
|
668
|
-
static find(address: string): Promise<FileWalkerFile | null>;
|
|
669
|
-
}
|
|
670
|
-
export declare class FileWalker extends FileWalkerUsingBun {
|
|
671
|
-
}
|
|
672
|
-
declare class HttpError extends Error {
|
|
799
|
+
declare class CError extends Error {
|
|
673
800
|
message: string;
|
|
674
801
|
status: Status;
|
|
675
802
|
data?: unknown | undefined;
|
|
676
803
|
constructor(message: string, status: Status, data?: unknown | undefined);
|
|
677
|
-
toResponse():
|
|
804
|
+
toResponse(): CResponse;
|
|
678
805
|
isStatusOf(status: Status): boolean;
|
|
679
|
-
static internalServerError(msg?: string):
|
|
680
|
-
static badRequest(msg?: string):
|
|
681
|
-
static notFound(msg?: string):
|
|
682
|
-
static methodNotAllowed(msg?: string):
|
|
683
|
-
static unprocessableEntity(msg?: string):
|
|
806
|
+
static internalServerError(msg?: string): CError;
|
|
807
|
+
static badRequest(msg?: string): CError;
|
|
808
|
+
static notFound(msg?: string): CError;
|
|
809
|
+
static methodNotAllowed(msg?: string): CError;
|
|
810
|
+
static unprocessableEntity(msg?: string): CError;
|
|
684
811
|
}
|
|
685
812
|
export type InferSchema<T extends Schema> = StandardSchemaV1.InferOutput<T>;
|
|
686
|
-
export type
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
export type InferModel<T extends Record<string, any>> = {
|
|
691
|
-
[K in keyof T]: T[K] extends RouteModel<any, any, any, any> ? Prettify<(T[K]["body"] extends Schema ? {
|
|
692
|
-
body: InferSchema<T[K]["body"]>;
|
|
693
|
-
} : {}) & (T[K]["search"] extends Schema ? {
|
|
694
|
-
search: InferSchema<T[K]["search"]>;
|
|
695
|
-
} : {}) & (T[K]["params"] extends Schema ? {
|
|
696
|
-
params: InferSchema<T[K]["params"]>;
|
|
697
|
-
} : {}) & (T[K]["response"] extends Schema ? {
|
|
698
|
-
response: InferSchema<T[K]["response"]>;
|
|
699
|
-
} : {})> : T[K] extends Schema ? InferSchema<T[K]> : never;
|
|
813
|
+
export type ServerTlsOptions = {
|
|
814
|
+
cert: string | Buffer;
|
|
815
|
+
key: string | Buffer;
|
|
816
|
+
ca?: string | Buffer;
|
|
700
817
|
};
|
|
701
|
-
export type
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
static getSearch<S = UnknownObject>(url: URL, validate?: SchemaValidator<S>): Promise<S>;
|
|
706
|
-
static getBody<B = UnknownObject>(r: HttpRequest | HttpResponse | Response, validate?: SchemaValidator<B>): Promise<B>;
|
|
707
|
-
static getParams<P = UnknownObject>(endpoint: string, url: URL, validate?: SchemaValidator<P>): Promise<P>;
|
|
708
|
-
private static getUnknownBody;
|
|
709
|
-
private static getJsonBody;
|
|
710
|
-
private static getFormUrlEncodedBody;
|
|
711
|
-
private static getFormDataBody;
|
|
712
|
-
private static getTextBody;
|
|
713
|
-
static getNormalizedContentType(input: Request | Response): string;
|
|
714
|
-
static processString(value: string): string | boolean | number;
|
|
715
|
-
}
|
|
716
|
-
/** Abstract class for repository implementations */
|
|
717
|
-
declare abstract class RepositoryAbstract {
|
|
718
|
-
readonly db: DatabaseClientInterface;
|
|
719
|
-
constructor(db: DatabaseClientInterface);
|
|
720
|
-
}
|
|
721
|
-
export declare const DefaultStatusTexts: {
|
|
722
|
-
200: string;
|
|
723
|
-
201: string;
|
|
724
|
-
204: string;
|
|
725
|
-
301: string;
|
|
726
|
-
302: string;
|
|
727
|
-
303: string;
|
|
728
|
-
307: string;
|
|
729
|
-
308: string;
|
|
730
|
-
400: string;
|
|
731
|
-
401: string;
|
|
732
|
-
403: string;
|
|
733
|
-
404: string;
|
|
734
|
-
500: string;
|
|
818
|
+
export type ServerOptions = {
|
|
819
|
+
adapter?: RouterAdapterInterface;
|
|
820
|
+
idleTimeout?: number;
|
|
821
|
+
tls?: ServerTlsOptions;
|
|
735
822
|
};
|
|
736
|
-
export type
|
|
737
|
-
export type ServeOptions = {
|
|
823
|
+
export type ServeArgs = {
|
|
738
824
|
port: number;
|
|
739
825
|
hostname?: "0.0.0.0" | "127.0.0.1" | "localhost" | (string & {}) | undefined;
|
|
740
826
|
fetch: (request: Request) => Promise<Response>;
|
|
741
827
|
};
|
|
742
|
-
export type CorsOptions = {
|
|
743
|
-
allowedOrigins?: string[];
|
|
744
|
-
allowedMethods?: string[];
|
|
745
|
-
allowedHeaders?: HttpHeaderKey[];
|
|
746
|
-
credentials?: boolean;
|
|
747
|
-
};
|
|
748
|
-
declare class Cors {
|
|
749
|
-
readonly opts: CorsOptions;
|
|
750
|
-
constructor(opts: CorsOptions);
|
|
751
|
-
private readonly originKey;
|
|
752
|
-
private readonly methodsKey;
|
|
753
|
-
private readonly headersKey;
|
|
754
|
-
private readonly credentialsKey;
|
|
755
|
-
getCorsHeaders(req: HttpRequest, res: HttpResponse): HttpHeaders;
|
|
756
|
-
apply(req: HttpRequest, res: HttpResponse): void;
|
|
757
|
-
}
|
|
758
828
|
export type ErrorHandler<R = unknown> = Func<[
|
|
759
829
|
Error
|
|
760
|
-
], MaybePromise<
|
|
830
|
+
], MaybePromise<CResponse<R>>>;
|
|
761
831
|
export type RequestHandler<R = unknown> = Func<[
|
|
762
|
-
|
|
763
|
-
], MaybePromise<
|
|
832
|
+
CRequest
|
|
833
|
+
], MaybePromise<CResponse<R>>>;
|
|
764
834
|
export type AfterResponseHandler = Func<[
|
|
765
|
-
|
|
766
|
-
], MaybePromise<
|
|
835
|
+
CResponse
|
|
836
|
+
], MaybePromise<CResponse>>;
|
|
767
837
|
export interface ServerInterface {
|
|
768
|
-
serve(options:
|
|
838
|
+
serve(options: ServeArgs): void;
|
|
769
839
|
close(): Promise<void>;
|
|
770
840
|
setGlobalPrefix(value: string): void;
|
|
771
|
-
setCors(cors: CorsOptions): void;
|
|
772
841
|
/**
|
|
773
842
|
*
|
|
774
843
|
* Default error handler response will have a status of C.Error or 500 and json:
|
|
@@ -797,38 +866,48 @@ export interface ServerInterface {
|
|
|
797
866
|
setOnBeforeListen(handler: () => MaybePromise<void>): void;
|
|
798
867
|
setOnBeforeClose(handler: () => MaybePromise<void>): void;
|
|
799
868
|
setOnAfterResponse(handler: AfterResponseHandler): void;
|
|
800
|
-
listen(port:
|
|
869
|
+
listen(port: ServeArgs["port"], hostname: ServeArgs["hostname"]): Promise<void>;
|
|
801
870
|
handle(request: Request): Promise<Response>;
|
|
802
871
|
}
|
|
803
872
|
declare abstract class ServerAbstract implements ServerInterface {
|
|
804
|
-
|
|
873
|
+
protected readonly opts?: ServerOptions | undefined;
|
|
874
|
+
abstract serve(options: ServeArgs): void;
|
|
805
875
|
abstract close(): Promise<void>;
|
|
806
|
-
|
|
876
|
+
constructor(opts?: ServerOptions | undefined);
|
|
807
877
|
get routes(): Array<[
|
|
808
878
|
string,
|
|
809
879
|
string
|
|
810
880
|
]>;
|
|
811
881
|
setGlobalPrefix(value: string): void;
|
|
812
|
-
|
|
813
|
-
setOnError(handler: ErrorHandler): void;
|
|
814
|
-
setOnNotFound(handler: RequestHandler): void;
|
|
815
|
-
setOnBeforeListen(handler: () => MaybePromise<void>): void;
|
|
816
|
-
setOnBeforeClose(handler: () => MaybePromise<void>): void;
|
|
817
|
-
setOnAfterResponse(handler: AfterResponseHandler): void;
|
|
818
|
-
listen(port: ServeOptions["port"], hostname?: ServeOptions["hostname"]): Promise<void>;
|
|
882
|
+
listen(port: ServeArgs["port"], hostname?: ServeArgs["hostname"]): Promise<void>;
|
|
819
883
|
handle(request: Request): Promise<Response>;
|
|
820
884
|
private getResponse;
|
|
821
|
-
protected handleBeforeListen:
|
|
822
|
-
|
|
823
|
-
|
|
885
|
+
protected handleBeforeListen: Func<[
|
|
886
|
+
], MaybePromise<void>> | undefined;
|
|
887
|
+
setOnBeforeListen(handler: Func<[
|
|
888
|
+
], MaybePromise<void>> | undefined): void;
|
|
889
|
+
defaultOnBeforeListen: Func<[
|
|
890
|
+
], MaybePromise<void>> | undefined;
|
|
891
|
+
protected handleBeforeClose: Func<[
|
|
892
|
+
], MaybePromise<void>> | undefined;
|
|
893
|
+
setOnBeforeClose(handler: () => MaybePromise<void>): void;
|
|
894
|
+
defaultOnBeforeClose: Func<[
|
|
895
|
+
], MaybePromise<void>> | undefined;
|
|
896
|
+
protected handleAfterResponse: AfterResponseHandler | undefined;
|
|
897
|
+
setOnAfterResponse(handler: AfterResponseHandler | undefined): void;
|
|
898
|
+
defaultOnAfterResponse: AfterResponseHandler | undefined;
|
|
824
899
|
protected handleError: ErrorHandler;
|
|
900
|
+
setOnError(handler: ErrorHandler): void;
|
|
901
|
+
defaultErrorHandler: ErrorHandler;
|
|
825
902
|
protected handleNotFound: RequestHandler;
|
|
903
|
+
setOnNotFound(handler: RequestHandler): void;
|
|
904
|
+
defaultNotFoundHandler: RequestHandler;
|
|
826
905
|
protected handleMethodNotAllowed: RequestHandler;
|
|
906
|
+
defaultMethodNotFoundHandler: RequestHandler;
|
|
827
907
|
}
|
|
828
908
|
declare class ServerUsingBun extends ServerAbstract {
|
|
829
909
|
private app;
|
|
830
|
-
|
|
831
|
-
serve(options: ServeOptions): void;
|
|
910
|
+
serve(args: ServeArgs): void;
|
|
832
911
|
close(): Promise<void>;
|
|
833
912
|
private createApp;
|
|
834
913
|
}
|
|
@@ -838,28 +917,112 @@ declare class ServerUsingBun extends ServerAbstract {
|
|
|
838
917
|
*/
|
|
839
918
|
export declare class Server extends ServerUsingBun {
|
|
840
919
|
}
|
|
841
|
-
export
|
|
842
|
-
|
|
920
|
+
export interface XFileInterface {
|
|
921
|
+
get name(): string;
|
|
922
|
+
get extension(): string;
|
|
923
|
+
get mimeType(): string;
|
|
924
|
+
exists(): Promise<boolean>;
|
|
925
|
+
text(): Promise<string>;
|
|
926
|
+
stream(): ReadableStream;
|
|
927
|
+
}
|
|
928
|
+
declare abstract class XFileAbstract implements XFileInterface {
|
|
929
|
+
readonly path: string;
|
|
930
|
+
private readonly fallbackExtension?;
|
|
931
|
+
constructor(path: string, fallbackExtension?: string | undefined);
|
|
932
|
+
abstract text(): Promise<string>;
|
|
933
|
+
abstract stream(): ReadableStream;
|
|
934
|
+
abstract exists(): Promise<boolean>;
|
|
935
|
+
get name(): string;
|
|
936
|
+
get extension(): string;
|
|
937
|
+
get mimeType(): string;
|
|
938
|
+
}
|
|
939
|
+
declare class XFileUsingBun extends XFileAbstract implements XFileInterface {
|
|
940
|
+
constructor(...args: ConstructorParameters<typeof XFileAbstract>);
|
|
941
|
+
file: Bun.BunFile;
|
|
942
|
+
exists(): Promise<boolean>;
|
|
943
|
+
text(): Promise<string>;
|
|
944
|
+
stream(): ReadableStream;
|
|
945
|
+
}
|
|
946
|
+
declare class XFile extends XFileUsingBun {
|
|
947
|
+
}
|
|
948
|
+
/** Abstract class for repository implementations */
|
|
949
|
+
declare abstract class RepositoryAbstract {
|
|
950
|
+
readonly db: DatabaseClientInterface;
|
|
951
|
+
constructor(db: DatabaseClientInterface);
|
|
952
|
+
}
|
|
953
|
+
/** Router Adapter for the "memoirist" package. */
|
|
954
|
+
export declare class MemoiristAdapter implements RouterAdapterInterface {
|
|
955
|
+
private router;
|
|
956
|
+
private pendingMiddlewares;
|
|
957
|
+
find(req: CRequest): RouterReturnData | null;
|
|
958
|
+
list(): Array<RouterRouteData>;
|
|
959
|
+
addRoute(data: RouterRouteData): void;
|
|
960
|
+
addModel(route: AnyRoute, model: AnyRouteModel): void;
|
|
961
|
+
addMiddleware(middleware: Middleware): void;
|
|
962
|
+
}
|
|
963
|
+
export type UnknownObject = Record<string, unknown>;
|
|
964
|
+
export declare class Parser {
|
|
965
|
+
static parse<T = UnknownObject>(data: unknown, validate?: SchemaValidator<T>): Promise<T>;
|
|
966
|
+
static issuesToErrorMessage(issues: readonly StandardSchemaV1.Issue[]): string;
|
|
967
|
+
static parseUrlData<P = UnknownObject>(params: Record<string, string>, validate?: SchemaValidator<P>): Promise<P>;
|
|
968
|
+
/** This can be used for both request and response bodies */
|
|
969
|
+
static parseBody<B = UnknownObject>(r: CRequest | CResponse | Response, validate?: SchemaValidator<B>): Promise<B>;
|
|
970
|
+
private static getUnknownBody;
|
|
971
|
+
private static getJsonBody;
|
|
972
|
+
private static getFormUrlEncodedBody;
|
|
973
|
+
private static getFormDataBody;
|
|
974
|
+
private static getTextBody;
|
|
975
|
+
static getNormalizedContentType(input: Request | Response): string;
|
|
976
|
+
}
|
|
977
|
+
export type Prettify<T> = {
|
|
978
|
+
[K in keyof T]: T[K];
|
|
979
|
+
} & {};
|
|
980
|
+
/** If you prefer to put all schemas into a single object, this will be helpful */
|
|
981
|
+
export type InferModel<T extends Record<string, any>> = {
|
|
982
|
+
[K in keyof T]: T[K] extends RouteModel<any, any, any, any> ? Prettify<(T[K]["body"] extends Schema ? {
|
|
983
|
+
body: InferSchema<T[K]["body"]>;
|
|
984
|
+
} : {}) & (T[K]["search"] extends Schema ? {
|
|
985
|
+
search: InferSchema<T[K]["search"]>;
|
|
986
|
+
} : {}) & (T[K]["params"] extends Schema ? {
|
|
987
|
+
params: InferSchema<T[K]["params"]>;
|
|
988
|
+
} : {}) & (T[K]["response"] extends Schema ? {
|
|
989
|
+
response: InferSchema<T[K]["response"]>;
|
|
990
|
+
} : {})> : T[K] extends Schema ? InferSchema<T[K]> : never;
|
|
991
|
+
};
|
|
992
|
+
export declare const _prefixStore: GlobalPrefixStore;
|
|
993
|
+
export declare const _routerStore: GlobalRouterStore;
|
|
994
|
+
export declare const _corsStore: GlobalCorsStore;
|
|
843
995
|
|
|
996
|
+
declare namespace X {
|
|
997
|
+
export { Cors, CorsOptions, InferModel, MemoiristAdapter, Parser, RepositoryAbstract as Repository, XFile as File };
|
|
998
|
+
}
|
|
844
999
|
declare namespace C {
|
|
845
|
-
export {
|
|
1000
|
+
export { CError as Error, CHeaders as Headers, CHeadersInit as HeadersInit, CRequest as Request, CRequestInfo as RequestInfo, CRequestInit as RequestInit, CResponse as Response, CResponseBody as ResponseBody, CResponseInit as ResponseInit, CommonHeaders, Config, Context, ControllerAbstract as Controller, ControllerOptions, CookieOptions, Cookies, CookiesInit, HeaderKey, InferSchema, Method, Middleware, MiddlewareHandler, MiddlewareOptions, MiddlewareUseOn, Route, RouteDefinition, RouteHandler, Schema, SchemaValidator, ServeArgs, Server, ServerOptions, ServerTlsOptions, StaticRoute, StaticRouteHandler, Status };
|
|
1001
|
+
}
|
|
1002
|
+
declare namespace _default {
|
|
1003
|
+
export { CError as Error, CHeaders as Headers, CHeadersInit as HeadersInit, CRequest as Request, CRequestInfo as RequestInfo, CRequestInit as RequestInit, CResponse as Response, CResponseBody as ResponseBody, CResponseInit as ResponseInit, CommonHeaders, Config, Context, ControllerAbstract as Controller, ControllerOptions, CookieOptions, Cookies, CookiesInit, HeaderKey, InferSchema, Method, Middleware, MiddlewareHandler, MiddlewareOptions, MiddlewareUseOn, Route, RouteDefinition, RouteHandler, Schema, SchemaValidator, ServeArgs, Server, ServerOptions, ServerTlsOptions, StaticRoute, StaticRouteHandler, Status };
|
|
1004
|
+
}
|
|
1005
|
+
declare namespace Extra {
|
|
1006
|
+
export { Cors, CorsOptions, InferModel, MemoiristAdapter, Parser, RepositoryAbstract as Repository, XFile as File };
|
|
846
1007
|
}
|
|
847
1008
|
|
|
848
1009
|
export {
|
|
849
1010
|
C,
|
|
850
1011
|
C as default,
|
|
1012
|
+
CError as Error,
|
|
1013
|
+
CHeaders as Headers,
|
|
1014
|
+
CHeadersInit as HeadersInit,
|
|
1015
|
+
CRequest as Request,
|
|
1016
|
+
CRequestInfo as RequestInfo,
|
|
1017
|
+
CRequestInit as RequestInit,
|
|
1018
|
+
CResponse as Response,
|
|
1019
|
+
CResponseBody as ResponseBody,
|
|
1020
|
+
CResponseInit as ResponseInit,
|
|
851
1021
|
ControllerAbstract as Controller,
|
|
852
|
-
|
|
853
|
-
HttpHeaderKey as HeaderKey,
|
|
854
|
-
HttpHeaders as Headers,
|
|
855
|
-
HttpHeadersInit as HeadersInit,
|
|
856
|
-
HttpRequest as Request,
|
|
857
|
-
HttpRequestInfo as RequestInfo,
|
|
858
|
-
HttpRequestInit as RequestInit,
|
|
859
|
-
HttpResponse as Response,
|
|
860
|
-
HttpResponseBody as ResponseBody,
|
|
861
|
-
HttpResponseInit as ResponseInit,
|
|
1022
|
+
Extra,
|
|
862
1023
|
RepositoryAbstract as Repository,
|
|
1024
|
+
X,
|
|
1025
|
+
XFile as File,
|
|
863
1026
|
};
|
|
864
1027
|
|
|
865
1028
|
export {};
|