@kaito-http/core 2.4.0 → 2.6.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/dist/declarations/src/index.d.ts +5 -5
- package/dist/declarations/src/req.d.ts +21 -2
- package/dist/declarations/src/res.d.ts +26 -1
- package/dist/declarations/src/route.d.ts +11 -7
- package/dist/declarations/src/router.d.ts +18 -24
- package/dist/declarations/src/server.d.ts +34 -14
- package/dist/declarations/src/util.d.ts +4 -4
- package/dist/kaito-http-core.cjs.dev.js +306 -228
- package/dist/kaito-http-core.cjs.prod.js +302 -228
- package/dist/kaito-http-core.esm.js +288 -209
- package/package.json +7 -5
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export type { HTTPMethod } from 'find-my-way';
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './router';
|
|
4
|
-
export * from './route';
|
|
5
|
-
export * from './util';
|
|
2
|
+
export * from './error';
|
|
6
3
|
export * from './req';
|
|
7
4
|
export * from './res';
|
|
8
|
-
export * from './
|
|
5
|
+
export * from './route';
|
|
6
|
+
export * from './router';
|
|
7
|
+
export * from './server';
|
|
8
|
+
export * from './util';
|
|
@@ -1,14 +1,33 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { HTTPMethod } from 'find-my-way';
|
|
3
|
-
import { IncomingMessage } from 'http';
|
|
2
|
+
import type { HTTPMethod } from 'find-my-way';
|
|
3
|
+
import type { IncomingMessage } from 'node:http';
|
|
4
4
|
export declare class KaitoRequest {
|
|
5
5
|
readonly raw: IncomingMessage;
|
|
6
6
|
private _url;
|
|
7
7
|
constructor(raw: IncomingMessage);
|
|
8
|
+
/**
|
|
9
|
+
* The full URL of the request, including the protocol, hostname, and path.
|
|
10
|
+
* Note: does not include the query string or hash
|
|
11
|
+
*/
|
|
8
12
|
get fullURL(): string;
|
|
13
|
+
/**
|
|
14
|
+
* A new URL instance for the full URL of the request.
|
|
15
|
+
*/
|
|
9
16
|
get url(): URL;
|
|
17
|
+
/**
|
|
18
|
+
* The HTTP method of the request.
|
|
19
|
+
*/
|
|
10
20
|
get method(): HTTPMethod;
|
|
21
|
+
/**
|
|
22
|
+
* The protocol of the request, either `http` or `https`.
|
|
23
|
+
*/
|
|
11
24
|
get protocol(): 'http' | 'https';
|
|
25
|
+
/**
|
|
26
|
+
* The request headers
|
|
27
|
+
*/
|
|
12
28
|
get headers(): import("http").IncomingHttpHeaders;
|
|
29
|
+
/**
|
|
30
|
+
* The hostname of the request.
|
|
31
|
+
*/
|
|
13
32
|
get hostname(): string;
|
|
14
33
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { ServerResponse } from 'http';
|
|
2
|
+
import type { ServerResponse } from 'node:http';
|
|
3
|
+
import type { CookieSerializeOptions } from 'cookie';
|
|
3
4
|
export declare type ErroredAPIResponse = {
|
|
4
5
|
success: false;
|
|
5
6
|
data: null;
|
|
@@ -15,7 +16,31 @@ export declare type AnyResponse = APIResponse<unknown>;
|
|
|
15
16
|
export declare class KaitoResponse<T = unknown> {
|
|
16
17
|
readonly raw: ServerResponse;
|
|
17
18
|
constructor(raw: ServerResponse);
|
|
19
|
+
/**
|
|
20
|
+
* Send a response
|
|
21
|
+
* @param key The key of the header
|
|
22
|
+
* @param value The value of the header
|
|
23
|
+
* @returns The response object
|
|
24
|
+
*/
|
|
18
25
|
header(key: string, value: string | readonly string[]): this;
|
|
26
|
+
/**
|
|
27
|
+
* Set the status code of the response
|
|
28
|
+
* @param code The status code
|
|
29
|
+
* @returns The response object
|
|
30
|
+
*/
|
|
19
31
|
status(code: number): this;
|
|
32
|
+
/**
|
|
33
|
+
* Set a cookie
|
|
34
|
+
* @param name The name of the cookie
|
|
35
|
+
* @param value The value of the cookie
|
|
36
|
+
* @param options The options for the cookie
|
|
37
|
+
* @returns The response object
|
|
38
|
+
*/
|
|
39
|
+
cookie(name: string, value: string, options: CookieSerializeOptions): this;
|
|
40
|
+
/**
|
|
41
|
+
* Send a JSON APIResponse body
|
|
42
|
+
* @param data The data to send
|
|
43
|
+
* @returns The response object
|
|
44
|
+
*/
|
|
20
45
|
json(data: APIResponse<T>): this;
|
|
21
46
|
}
|
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { ExtractRouteParams, KaitoMethod } from './util';
|
|
3
|
-
export declare type RouteArgument<Path extends string, Context,
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import type { ExtractRouteParams, KaitoMethod } from './util';
|
|
3
|
+
export declare type RouteArgument<Path extends string, Context, QueryOutput, BodyOutput> = {
|
|
4
4
|
ctx: Context;
|
|
5
|
-
|
|
5
|
+
body: BodyOutput;
|
|
6
|
+
query: QueryOutput;
|
|
6
7
|
params: ExtractRouteParams<Path>;
|
|
7
8
|
};
|
|
8
|
-
export declare type
|
|
9
|
-
|
|
9
|
+
export declare type AnyQueryDefinition = Record<string, z.ZodTypeAny>;
|
|
10
|
+
export declare type Route<Context, Result, Path extends string, Method extends KaitoMethod, Query extends AnyQueryDefinition, BodyOutput, BodyDef extends z.ZodTypeDef, BodyInput> = {
|
|
11
|
+
body?: z.ZodType<BodyOutput, BodyDef, BodyInput>;
|
|
12
|
+
query?: Query;
|
|
10
13
|
path: Path;
|
|
11
14
|
method: Method;
|
|
12
|
-
run(args: RouteArgument<Path, Context,
|
|
15
|
+
run(args: RouteArgument<Path, Context, z.infer<z.ZodObject<Query>>, BodyOutput>): Promise<Result>;
|
|
13
16
|
};
|
|
17
|
+
export declare type AnyRoute<Context = any> = Route<Context, any, any, any, AnyQueryDefinition, any, any, any>;
|
|
@@ -1,27 +1,21 @@
|
|
|
1
|
-
import fmw
|
|
1
|
+
import fmw from 'find-my-way';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
-
import { Route } from './route';
|
|
4
|
-
import { ServerConfig } from './server';
|
|
5
|
-
import { KaitoMethod } from './util';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
import type { AnyQueryDefinition, AnyRoute, Route } from './route';
|
|
4
|
+
import type { ServerConfig } from './server';
|
|
5
|
+
import type { KaitoMethod } from './util';
|
|
6
|
+
declare type Routes = readonly AnyRoute[];
|
|
7
|
+
declare type RemapRoutePrefix<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer Context, infer Result, infer Path, infer Method, infer Query, infer BodyOutput, infer BodyDef, infer BodyInput> ? Route<Context, Result, `${Prefix}${Path}`, Method, Query, BodyOutput, BodyDef, BodyInput> : never;
|
|
8
|
+
declare type PrefixRoutesPath<Prefix extends `/${string}`, R extends Routes> = R extends [infer First, ...infer Rest] ? [
|
|
9
|
+
RemapRoutePrefix<Extract<First, AnyRoute>, Prefix>,
|
|
10
|
+
...PrefixRoutesPath<Prefix, Extract<Rest, readonly AnyRoute[]>>
|
|
11
|
+
] : [];
|
|
12
|
+
export declare class Router<Context, R extends Routes> {
|
|
13
|
+
readonly routes: R;
|
|
14
|
+
static create: <Context_1>() => Router<Context_1, []>;
|
|
13
15
|
private static handle;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
map(): { [Method in Routes[number]["method"]]: { [R in Extract<Routes[number], {
|
|
19
|
-
method: Method;
|
|
20
|
-
}> as R["path"]]: R; }; };
|
|
21
|
-
merge<Prefix extends string, NewRoutes extends RoutesInit<Context>>(prefix: Prefix, router: Router<Context, NewRoutes>): Router<Context, [...Routes, ...MergePaths<NewRoutes, Prefix>]>;
|
|
22
|
-
private copyContext;
|
|
16
|
+
constructor(routes: R);
|
|
17
|
+
add: <Result, Path extends string, Method extends KaitoMethod, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(route: Method extends "GET" ? Omit<Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>, "body"> : Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>) => Router<Context, [...R, Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>]>;
|
|
18
|
+
merge: <PathPrefix extends `/${string}`, OtherRoutes extends Routes>(pathPrefix: PathPrefix, other: Router<Context, OtherRoutes>) => Router<Context, [...R, ...PrefixRoutesPath<PathPrefix, OtherRoutes>]>;
|
|
19
|
+
toFindMyWay: (server: ServerConfig<Context, any>) => fmw.Instance<fmw.HTTPVersion.V1>;
|
|
23
20
|
}
|
|
24
|
-
|
|
25
|
-
* @deprecated Please use Router#create instead
|
|
26
|
-
*/
|
|
27
|
-
export declare const createRouter: typeof Router.create;
|
|
21
|
+
export {};
|
|
@@ -1,15 +1,35 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import { KaitoRequest } from './req';
|
|
5
|
-
import { KaitoResponse } from './res';
|
|
6
|
-
import {
|
|
7
|
-
import { GetContext } from './util';
|
|
8
|
-
export declare type Before = (req: http.IncomingMessage, res: http.ServerResponse) => Promise<
|
|
9
|
-
export
|
|
2
|
+
import * as http from 'node:http';
|
|
3
|
+
import type { KaitoError } from './error';
|
|
4
|
+
import type { KaitoRequest } from './req';
|
|
5
|
+
import type { KaitoResponse } from './res';
|
|
6
|
+
import type { Router } from './router';
|
|
7
|
+
import type { GetContext, KaitoMethod } from './util';
|
|
8
|
+
export declare type Before<BeforeAfterContext> = (req: http.IncomingMessage, res: http.ServerResponse) => Promise<BeforeAfterContext>;
|
|
9
|
+
export declare type HandlerResult = {
|
|
10
|
+
success: true;
|
|
11
|
+
data: unknown;
|
|
12
|
+
} | {
|
|
13
|
+
success: false;
|
|
14
|
+
data: {
|
|
15
|
+
status: number;
|
|
16
|
+
message: string;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export declare type After<BeforeAfterContext> = (ctx: BeforeAfterContext, result: HandlerResult) => Promise<void>;
|
|
20
|
+
export declare type ServerConfigWithBefore<BeforeAfterContext> = {
|
|
21
|
+
before: Before<BeforeAfterContext>;
|
|
22
|
+
after?: After<BeforeAfterContext>;
|
|
23
|
+
} | {
|
|
24
|
+
before?: undefined;
|
|
25
|
+
};
|
|
26
|
+
export declare type ServerConfig<Context, BeforeAfterContext> = ServerConfigWithBefore<BeforeAfterContext> & {
|
|
10
27
|
router: Router<Context, any>;
|
|
11
28
|
getContext: GetContext<Context>;
|
|
12
|
-
|
|
29
|
+
rawRoutes?: Partial<Record<KaitoMethod, Array<{
|
|
30
|
+
path: string;
|
|
31
|
+
handler: (request: http.IncomingMessage, response: http.ServerResponse) => unknown;
|
|
32
|
+
}>>>;
|
|
13
33
|
onError(arg: {
|
|
14
34
|
error: Error;
|
|
15
35
|
req: KaitoRequest;
|
|
@@ -18,9 +38,9 @@ export interface ServerConfig<Context> {
|
|
|
18
38
|
status: number;
|
|
19
39
|
message: string;
|
|
20
40
|
}>;
|
|
21
|
-
}
|
|
22
|
-
export declare function createFMWServer<Context>(config: ServerConfig<Context>): {
|
|
23
|
-
server: http.Server;
|
|
24
|
-
fmw: import("find-my-way").Instance<import("find-my-way").HTTPVersion.V1>;
|
|
25
41
|
};
|
|
26
|
-
export declare function
|
|
42
|
+
export declare function createFMWServer<Context, BeforeAfterContext = null>(config: ServerConfig<Context, BeforeAfterContext>): {
|
|
43
|
+
readonly server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
44
|
+
readonly fmw: import("find-my-way").Instance<import("find-my-way").HTTPVersion.V1>;
|
|
45
|
+
};
|
|
46
|
+
export declare function createServer<Context, BeforeAfterContext = null>(config: ServerConfig<Context, BeforeAfterContext>): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { HTTPMethod } from 'find-my-way';
|
|
2
|
-
import { KaitoRequest } from './req';
|
|
3
|
-
import { KaitoResponse } from './res';
|
|
1
|
+
import type { HTTPMethod } from 'find-my-way';
|
|
2
|
+
import type { KaitoRequest } from './req';
|
|
3
|
+
import type { KaitoResponse } from './res';
|
|
4
4
|
export declare type ExtractRouteParams<T extends string> = string extends T ? Record<string, string> : T extends `${string}:${infer Param}/${infer Rest}` ? {
|
|
5
5
|
[k in Param | keyof ExtractRouteParams<Rest>]: string;
|
|
6
6
|
} : T extends `${string}:${infer Param}` ? {
|
|
@@ -16,5 +16,5 @@ declare type AddStartSlashes<T extends string> = T extends `/${infer U}` ? `/${U
|
|
|
16
16
|
export declare type NormalizePath<T extends string> = AddStartSlashes<RemoveEndSlashes<T>>;
|
|
17
17
|
export declare type Values<T> = T[keyof T];
|
|
18
18
|
export declare type NoEmpty<T> = [keyof T] extends [never] ? never : T;
|
|
19
|
-
export declare function
|
|
19
|
+
export declare function getBody(req: KaitoRequest): Promise<unknown>;
|
|
20
20
|
export {};
|