@kaito-http/core 2.2.7 → 2.3.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/error.d.ts +4 -0
- package/dist/declarations/src/index.d.ts +6 -0
- package/dist/declarations/src/req.d.ts +2 -2
- package/dist/declarations/src/route.d.ts +13 -0
- package/dist/declarations/src/router.d.ts +28 -0
- package/dist/declarations/src/server.d.ts +15 -71
- package/dist/declarations/src/util.d.ts +9 -1
- package/dist/kaito-http-core.cjs.dev.js +194 -175
- package/dist/kaito-http-core.cjs.prod.js +194 -175
- package/dist/kaito-http-core.esm.js +170 -177
- package/package.json +2 -1
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
import { HTTPMethod } from 'find-my-way';
|
|
2
3
|
import { IncomingMessage } from 'http';
|
|
3
|
-
import { Method } from './util';
|
|
4
4
|
export declare class KaitoRequest {
|
|
5
5
|
readonly raw: IncomingMessage;
|
|
6
6
|
private _url;
|
|
7
7
|
constructor(raw: IncomingMessage);
|
|
8
8
|
get fullURL(): string;
|
|
9
9
|
get url(): URL;
|
|
10
|
-
get method():
|
|
10
|
+
get method(): HTTPMethod;
|
|
11
11
|
get protocol(): 'http' | 'https';
|
|
12
12
|
get headers(): import("http").IncomingHttpHeaders;
|
|
13
13
|
get hostname(): string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { HTTPMethod } from 'find-my-way';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { ExtractRouteParams } from './util';
|
|
4
|
+
export declare type RouteArgument<Path extends string, Context, Input extends z.ZodSchema> = {
|
|
5
|
+
ctx: Context;
|
|
6
|
+
input: z.infer<Input>;
|
|
7
|
+
params: ExtractRouteParams<Path>;
|
|
8
|
+
};
|
|
9
|
+
export interface Route<Result, Path extends string, Method extends HTTPMethod, Context, Input extends z.ZodSchema = never> {
|
|
10
|
+
input?: Input;
|
|
11
|
+
method: Method;
|
|
12
|
+
run(arg: RouteArgument<Path, Context, Input>): Promise<Result>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import fmw, { HTTPMethod, Instance } from 'find-my-way';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { Route } from './route';
|
|
4
|
+
import { ServerConfig } from './server';
|
|
5
|
+
import { NormalizePath } from './util';
|
|
6
|
+
export declare type RoutesInit<Context, Paths extends string = string> = {
|
|
7
|
+
[Path in Paths]: Route<unknown, Path, HTTPMethod, Context, z.ZodSchema>;
|
|
8
|
+
};
|
|
9
|
+
export declare class Router<Context, Routes extends RoutesInit<Context>> {
|
|
10
|
+
static create<Context>(): Router<Context, {}>;
|
|
11
|
+
private static handle;
|
|
12
|
+
readonly routes: Routes;
|
|
13
|
+
readonly get: <Result, Path extends string, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, route: Omit<Route<Result, (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "GET", Context, Input>, "method">) => Router<Context, Routes & { [P in Path as (P extends `${infer U}/` ? U : P) extends `/${infer U_1}` ? `/${U_1}` : `/${P extends `${infer U}/` ? U : P}`]: Route<Result, (P extends `${infer U}/` ? U : P) extends `/${infer U_1}` ? `/${U_1}` : `/${P extends `${infer U}/` ? U : P}`, "GET", Context, Input>; }>;
|
|
14
|
+
readonly post: <Result, Path extends string, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, route: Omit<Route<Result, (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "POST", Context, Input>, "method">) => Router<Context, Routes & { [P in Path as (P extends `${infer U}/` ? U : P) extends `/${infer U_1}` ? `/${U_1}` : `/${P extends `${infer U}/` ? U : P}`]: Route<Result, (P extends `${infer U}/` ? U : P) extends `/${infer U_1}` ? `/${U_1}` : `/${P extends `${infer U}/` ? U : P}`, "POST", Context, Input>; }>;
|
|
15
|
+
readonly head: <Result, Path extends string, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, route: Omit<Route<Result, (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "HEAD", Context, Input>, "method">) => Router<Context, Routes & { [P in Path as (P extends `${infer U}/` ? U : P) extends `/${infer U_1}` ? `/${U_1}` : `/${P extends `${infer U}/` ? U : P}`]: Route<Result, (P extends `${infer U}/` ? U : P) extends `/${infer U_1}` ? `/${U_1}` : `/${P extends `${infer U}/` ? U : P}`, "HEAD", Context, Input>; }>;
|
|
16
|
+
readonly put: <Result, Path extends string, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, route: Omit<Route<Result, (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "PUT", Context, Input>, "method">) => Router<Context, Routes & { [P in Path as (P extends `${infer U}/` ? U : P) extends `/${infer U_1}` ? `/${U_1}` : `/${P extends `${infer U}/` ? U : P}`]: Route<Result, (P extends `${infer U}/` ? U : P) extends `/${infer U_1}` ? `/${U_1}` : `/${P extends `${infer U}/` ? U : P}`, "PUT", Context, Input>; }>;
|
|
17
|
+
readonly patch: <Result, Path extends string, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, route: Omit<Route<Result, (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "PATCH", Context, Input>, "method">) => Router<Context, Routes & { [P in Path as (P extends `${infer U}/` ? U : P) extends `/${infer U_1}` ? `/${U_1}` : `/${P extends `${infer U}/` ? U : P}`]: Route<Result, (P extends `${infer U}/` ? U : P) extends `/${infer U_1}` ? `/${U_1}` : `/${P extends `${infer U}/` ? U : P}`, "PATCH", Context, Input>; }>;
|
|
18
|
+
private constructor();
|
|
19
|
+
merge<Prefix extends string, NewRoutes extends RoutesInit<Context>>(prefix: NormalizePath<Prefix>, router: Router<Context, NewRoutes>): Router<Context, Routes & { [Path in Extract<keyof NewRoutes, string> as (`${Prefix}${Path}` extends `${infer U}/` ? U : `${Prefix}${Path}`) extends `/${infer U_1}` ? `/${U_1}` : `/${`${Prefix}${Path}` extends `${infer U}/` ? U : `${Prefix}${Path}`}`]: NewRoutes[Path] & {
|
|
20
|
+
path: Path;
|
|
21
|
+
}; }>;
|
|
22
|
+
toFindMyWay(server: ServerConfig<Context>): Instance<fmw.HTTPVersion.V1>;
|
|
23
|
+
private make;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* @deprecated Please use Router#create instead
|
|
27
|
+
*/
|
|
28
|
+
export declare const createRouter: typeof Router.create;
|
|
@@ -1,80 +1,24 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import { Router, RoutesInit } from './router';
|
|
3
|
+
import * as http from 'http';
|
|
4
4
|
import { KaitoRequest } from './req';
|
|
5
5
|
import { KaitoResponse } from './res';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export declare type GetContext<T> = (req: KaitoRequest, res: KaitoResponse) => Promise<T>;
|
|
13
|
-
declare type Never = [never];
|
|
14
|
-
export declare function createGetContext<T>(getContext: GetContext<T>): GetContext<T>;
|
|
15
|
-
export declare type InferContext<T> = T extends GetContext<infer Value> ? Value : never;
|
|
16
|
-
export declare type ContextWithInput<Ctx, Params extends Record<string, string>, Input> = {
|
|
17
|
-
ctx: Ctx;
|
|
18
|
-
params: Params;
|
|
19
|
-
input: Input;
|
|
20
|
-
};
|
|
21
|
-
declare type Values<T> = T[keyof T];
|
|
22
|
-
export declare type Proc<Ctx, Result, Params extends Record<string, string> = Record<never, string>, Input extends z.ZodTypeAny | Never = Never> = {
|
|
23
|
-
input?: Input;
|
|
24
|
-
run(arg: ContextWithInput<Ctx, Params, Input extends ZodTypeAny ? z.infer<Input> : undefined>): Promise<Result>;
|
|
25
|
-
};
|
|
26
|
-
export interface RouterProc<Path extends string, M extends Method> {
|
|
27
|
-
method: M;
|
|
28
|
-
path: Path;
|
|
29
|
-
pattern: RegExp;
|
|
30
|
-
}
|
|
31
|
-
export declare type AnyProcs<Ctx> = {
|
|
32
|
-
[Path in string]: Proc<Ctx, unknown, Record<string, string>, z.ZodTypeAny> & RouterProc<Path, Method>;
|
|
33
|
-
};
|
|
34
|
-
export declare type AnyRouter<Ctx> = Router<Ctx, AnyProcs<Ctx>>;
|
|
35
|
-
export declare class Router<Ctx, Procs extends AnyProcs<Ctx>> {
|
|
36
|
-
private readonly procs;
|
|
37
|
-
private readonly _procsArray;
|
|
38
|
-
private static patternize;
|
|
39
|
-
constructor(procs: Procs);
|
|
40
|
-
getProcs(): Procs;
|
|
41
|
-
find(method: Method, url: string): (Proc<Ctx, unknown, Record<string, string>, z.ZodTypeAny> & RouterProc<string, Method>) | null;
|
|
42
|
-
private readonly create;
|
|
43
|
-
readonly merge: <Prefix extends string, NewCtx, NewProcs extends AnyProcs<NewCtx>>(prefix: (Prefix extends `${infer U}/` ? U : Prefix) extends `/${infer U_1}` ? `/${U_1}` : `/${Prefix extends `${infer U}/` ? U : Prefix}`, router: Router<NewCtx, NewProcs>) => Router<NewCtx & Ctx, Procs & { [P in Extract<keyof NewProcs, string> as `/${Prefix}${P}`]: Omit<NewProcs[P], "path"> & {
|
|
44
|
-
path: P;
|
|
45
|
-
}; }>;
|
|
46
|
-
readonly get: <Path extends string, Result, Input extends z.ZodTypeAny>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, proc: Proc<Ctx, Result, ExtractRouteParams<Path>, Input>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Proc<Ctx, Result, ExtractRouteParams<Path>, Input> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "GET">>>;
|
|
47
|
-
readonly post: <Path extends string, Result, Input extends z.ZodTypeAny>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, proc: Proc<Ctx, Result, ExtractRouteParams<Path>, Input>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Proc<Ctx, Result, ExtractRouteParams<Path>, Input> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "POST">>>;
|
|
48
|
-
readonly put: <Path extends string, Result, Input extends z.ZodTypeAny>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, proc: Proc<Ctx, Result, ExtractRouteParams<Path>, Input>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Proc<Ctx, Result, ExtractRouteParams<Path>, Input> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "PUT">>>;
|
|
49
|
-
readonly patch: <Path extends string, Result, Input extends z.ZodTypeAny>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, proc: Proc<Ctx, Result, ExtractRouteParams<Path>, Input>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Proc<Ctx, Result, ExtractRouteParams<Path>, Input> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "PATCH">>>;
|
|
50
|
-
readonly delete: <Path extends string, Result, Input extends z.ZodTypeAny>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, proc: Proc<Ctx, Result, ExtractRouteParams<Path>, Input>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Proc<Ctx, Result, ExtractRouteParams<Path>, Input> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "DELETE">>>;
|
|
51
|
-
readonly head: <Path extends string, Result, Input extends z.ZodTypeAny>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, proc: Proc<Ctx, Result, ExtractRouteParams<Path>, Input>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Proc<Ctx, Result, ExtractRouteParams<Path>, Input> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "HEAD">>>;
|
|
52
|
-
readonly options: <Path extends string, Result, Input extends z.ZodTypeAny>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, proc: Proc<Ctx, Result, ExtractRouteParams<Path>, Input>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Proc<Ctx, Result, ExtractRouteParams<Path>, Input> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "OPTIONS">>>;
|
|
53
|
-
readonly connect: <Path extends string, Result, Input extends z.ZodTypeAny>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, proc: Proc<Ctx, Result, ExtractRouteParams<Path>, Input>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Proc<Ctx, Result, ExtractRouteParams<Path>, Input> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "CONNECT">>>;
|
|
54
|
-
readonly trace: <Path extends string, Result, Input extends z.ZodTypeAny>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, proc: Proc<Ctx, Result, ExtractRouteParams<Path>, Input>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Proc<Ctx, Result, ExtractRouteParams<Path>, Input> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "TRACE">>>;
|
|
55
|
-
readonly acl: <Path extends string, Result, Input extends z.ZodTypeAny>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, proc: Proc<Ctx, Result, ExtractRouteParams<Path>, Input>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Proc<Ctx, Result, ExtractRouteParams<Path>, Input> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "ACL">>>;
|
|
56
|
-
readonly bind: <Path extends string, Result, Input extends z.ZodTypeAny>(path: (Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, proc: Proc<Ctx, Result, ExtractRouteParams<Path>, Input>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Proc<Ctx, Result, ExtractRouteParams<Path>, Input> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "BIND">>>;
|
|
57
|
-
}
|
|
58
|
-
export declare class KaitoError extends Error {
|
|
59
|
-
readonly status: number;
|
|
60
|
-
readonly cause?: Error | undefined;
|
|
61
|
-
constructor(status: number, message: string, cause?: Error | undefined);
|
|
62
|
-
}
|
|
63
|
-
export declare function createRouter<Ctx>(): Router<Ctx, {}>;
|
|
64
|
-
export declare type InferAPIResponseType<R extends AnyRouter<unknown>, M extends Method, Path extends Extract<Values<ReturnType<R['getProcs']>>, {
|
|
65
|
-
method: M;
|
|
66
|
-
}>['path']> = ReturnType<ReturnType<R['getProcs']>[Path]['run']> extends Promise<infer V> ? V : never;
|
|
67
|
-
export declare function createServer<Ctx, R extends Router<Ctx, AnyProcs<Ctx>>>(config: {
|
|
68
|
-
getContext: GetContext<Ctx>;
|
|
69
|
-
router: R;
|
|
70
|
-
onError(error: {
|
|
6
|
+
import { KaitoError } from './error';
|
|
7
|
+
import { GetContext } from './util';
|
|
8
|
+
export interface ServerConfig<Context> {
|
|
9
|
+
router: Router<Context, RoutesInit<Context>>;
|
|
10
|
+
getContext: GetContext<Context>;
|
|
11
|
+
onError(arg: {
|
|
71
12
|
error: Error;
|
|
72
13
|
req: KaitoRequest;
|
|
73
14
|
res: KaitoResponse;
|
|
74
|
-
}): Promise<{
|
|
15
|
+
}): Promise<KaitoError | {
|
|
75
16
|
status: number;
|
|
76
17
|
message: string;
|
|
77
18
|
}>;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
19
|
+
}
|
|
20
|
+
export declare function createFMWServer<Context>(config: ServerConfig<Context>): {
|
|
21
|
+
server: http.Server;
|
|
22
|
+
fmw: import("find-my-way").Instance<import("find-my-way").HTTPVersion.V1>;
|
|
23
|
+
};
|
|
24
|
+
export declare function createServer<Context>(config: ServerConfig<Context>): http.Server;
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import { KaitoRequest } from './req';
|
|
2
|
+
import { KaitoResponse } from './res';
|
|
3
|
+
export declare type ExtractRouteParams<T extends string> = string extends T ? Record<string, string> : T extends `${string}:${infer Param}/${infer Rest}` ? {
|
|
4
|
+
[k in Param | keyof ExtractRouteParams<Rest>]: string;
|
|
5
|
+
} : T extends `${string}:${infer Param}` ? {
|
|
6
|
+
[k in Param]: string;
|
|
7
|
+
} : {};
|
|
8
|
+
export declare type GetContext<Result> = (req: KaitoRequest, res: KaitoResponse) => Promise<Result>;
|
|
9
|
+
export declare function createGetContext<Context>(callback: GetContext<Context>): GetContext<Context>;
|
|
10
|
+
export declare type InferContext<T> = T extends (req: KaitoRequest, res: KaitoResponse) => Promise<infer U> ? U : never;
|
|
2
11
|
export declare function getLastEntryInMultiHeaderValue(headerValue: string | string[]): string;
|
|
3
12
|
declare type RemoveEndSlashes<T extends string> = T extends `${infer U}/` ? U : T;
|
|
4
13
|
declare type AddStartSlashes<T extends string> = T extends `/${infer U}` ? `/${U}` : `/${T}`;
|
|
5
14
|
export declare type NormalizePath<T extends string> = AddStartSlashes<RemoveEndSlashes<T>>;
|
|
6
15
|
export declare function normalizePath<T extends string>(path: T): NormalizePath<T>;
|
|
7
|
-
export declare type Method = 'ACL' | 'BIND' | 'CHECKOUT' | 'CONNECT' | 'COPY' | 'DELETE' | 'GET' | 'HEAD' | 'LINK' | 'LOCK' | 'M-SEARCH' | 'MERGE' | 'MKACTIVITY' | 'MKCALENDAR' | 'MKCOL' | 'MOVE' | 'NOTIFY' | 'OPTIONS' | 'PATCH' | 'POST' | 'PRI' | 'PROPFIND' | 'PROPPATCH' | 'PURGE' | 'PUT' | 'REBIND' | 'REPORT' | 'SEARCH' | 'SOURCE' | 'SUBSCRIBE' | 'TRACE' | 'UNBIND' | 'UNLINK' | 'UNLOCK' | 'UNSUBSCRIBE';
|
|
8
16
|
export declare function getInput(req: KaitoRequest): Promise<unknown>;
|
|
9
17
|
export {};
|
|
@@ -3,49 +3,47 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var http = require('http');
|
|
6
|
+
var fmw = require('find-my-way');
|
|
6
7
|
var tls = require('tls');
|
|
7
8
|
var getRawBody = require('raw-body');
|
|
8
9
|
|
|
9
10
|
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
12
|
+
function _interopNamespace(e) {
|
|
13
|
+
if (e && e.__esModule) return e;
|
|
14
|
+
var n = Object.create(null);
|
|
15
|
+
if (e) {
|
|
16
|
+
Object.keys(e).forEach(function (k) {
|
|
17
|
+
if (k !== 'default') {
|
|
18
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () { return e[k]; }
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
n["default"] = e;
|
|
27
|
+
return Object.freeze(n);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
args = arguments;
|
|
34
|
-
return new Promise(function (resolve, reject) {
|
|
35
|
-
var gen = fn.apply(self, args);
|
|
36
|
-
|
|
37
|
-
function _next(value) {
|
|
38
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function _throw(err) {
|
|
42
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
43
|
-
}
|
|
30
|
+
var http__namespace = /*#__PURE__*/_interopNamespace(http);
|
|
31
|
+
var fmw__default = /*#__PURE__*/_interopDefault(fmw);
|
|
32
|
+
var getRawBody__default = /*#__PURE__*/_interopDefault(getRawBody);
|
|
44
33
|
|
|
45
|
-
|
|
46
|
-
|
|
34
|
+
function createFMWServer(config) {
|
|
35
|
+
var fmw = config.router.toFindMyWay(config);
|
|
36
|
+
var server = http__namespace.createServer((req, res) => {
|
|
37
|
+
fmw.lookup(req, res);
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
server,
|
|
41
|
+
fmw
|
|
47
42
|
};
|
|
48
43
|
}
|
|
44
|
+
function createServer(config) {
|
|
45
|
+
return createFMWServer(config).server;
|
|
46
|
+
}
|
|
49
47
|
|
|
50
48
|
function _defineProperty(obj, key, value) {
|
|
51
49
|
if (key in obj) {
|
|
@@ -100,6 +98,42 @@ function _objectSpread2(target) {
|
|
|
100
98
|
return target;
|
|
101
99
|
}
|
|
102
100
|
|
|
101
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
102
|
+
try {
|
|
103
|
+
var info = gen[key](arg);
|
|
104
|
+
var value = info.value;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
reject(error);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (info.done) {
|
|
111
|
+
resolve(value);
|
|
112
|
+
} else {
|
|
113
|
+
Promise.resolve(value).then(_next, _throw);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function _asyncToGenerator(fn) {
|
|
118
|
+
return function () {
|
|
119
|
+
var self = this,
|
|
120
|
+
args = arguments;
|
|
121
|
+
return new Promise(function (resolve, reject) {
|
|
122
|
+
var gen = fn.apply(self, args);
|
|
123
|
+
|
|
124
|
+
function _next(value) {
|
|
125
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function _throw(err) {
|
|
129
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
_next(undefined);
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
103
137
|
class WrappedError extends Error {
|
|
104
138
|
static maybe(maybeError) {
|
|
105
139
|
if (maybeError instanceof Error) {
|
|
@@ -118,8 +152,18 @@ class WrappedError extends Error {
|
|
|
118
152
|
this.data = data;
|
|
119
153
|
}
|
|
120
154
|
|
|
155
|
+
}
|
|
156
|
+
class KaitoError extends Error {
|
|
157
|
+
constructor(status, message) {
|
|
158
|
+
super(message);
|
|
159
|
+
this.status = status;
|
|
160
|
+
}
|
|
161
|
+
|
|
121
162
|
}
|
|
122
163
|
|
|
164
|
+
function createGetContext(callback) {
|
|
165
|
+
return callback;
|
|
166
|
+
}
|
|
123
167
|
function getLastEntryInMultiHeaderValue(headerValue) {
|
|
124
168
|
var normalized = Array.isArray(headerValue) ? headerValue.join(',') : headerValue;
|
|
125
169
|
var lastIndex = normalized.lastIndexOf(',');
|
|
@@ -137,8 +181,7 @@ function normalizePath(path) {
|
|
|
137
181
|
}
|
|
138
182
|
|
|
139
183
|
return result;
|
|
140
|
-
}
|
|
141
|
-
|
|
184
|
+
}
|
|
142
185
|
function getInput(_x) {
|
|
143
186
|
return _getInput.apply(this, arguments);
|
|
144
187
|
}
|
|
@@ -247,142 +290,34 @@ class KaitoResponse {
|
|
|
247
290
|
|
|
248
291
|
}
|
|
249
292
|
|
|
250
|
-
function createGetContext(getContext) {
|
|
251
|
-
return getContext;
|
|
252
|
-
}
|
|
253
293
|
class Router {
|
|
254
|
-
static
|
|
255
|
-
|
|
256
|
-
return new RegExp("^".concat(normalized, "/?$"), 'i');
|
|
294
|
+
static create() {
|
|
295
|
+
return new Router({});
|
|
257
296
|
}
|
|
258
297
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
var pattern = Router.patternize(path);
|
|
262
|
-
|
|
263
|
-
var merged = _objectSpread2(_objectSpread2({}, this.procs), {}, {
|
|
264
|
-
[path]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
265
|
-
method,
|
|
266
|
-
path,
|
|
267
|
-
pattern
|
|
268
|
-
})
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
return new Router(merged);
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
_defineProperty(this, "merge", (prefix, router) => {
|
|
275
|
-
var newProcs = Object.entries(router.getProcs()).reduce((all, entry) => {
|
|
276
|
-
var [path, proc] = entry;
|
|
277
|
-
var newPath = "".concat(prefix).concat(path);
|
|
278
|
-
return _objectSpread2(_objectSpread2({}, all), {}, {
|
|
279
|
-
["".concat(prefix).concat(path)]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
280
|
-
path: newPath,
|
|
281
|
-
pattern: Router.patternize(newPath)
|
|
282
|
-
})
|
|
283
|
-
});
|
|
284
|
-
}, {});
|
|
285
|
-
|
|
286
|
-
var mergedProcs = _objectSpread2(_objectSpread2({}, this.procs), newProcs);
|
|
287
|
-
|
|
288
|
-
return new Router(mergedProcs);
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
_defineProperty(this, "get", this.create('GET'));
|
|
292
|
-
|
|
293
|
-
_defineProperty(this, "post", this.create('POST'));
|
|
294
|
-
|
|
295
|
-
_defineProperty(this, "put", this.create('PUT'));
|
|
296
|
-
|
|
297
|
-
_defineProperty(this, "patch", this.create('PATCH'));
|
|
298
|
-
|
|
299
|
-
_defineProperty(this, "delete", this.create('DELETE'));
|
|
300
|
-
|
|
301
|
-
_defineProperty(this, "head", this.create('HEAD'));
|
|
302
|
-
|
|
303
|
-
_defineProperty(this, "options", this.create('OPTIONS'));
|
|
304
|
-
|
|
305
|
-
_defineProperty(this, "connect", this.create('CONNECT'));
|
|
306
|
-
|
|
307
|
-
_defineProperty(this, "trace", this.create('TRACE'));
|
|
308
|
-
|
|
309
|
-
_defineProperty(this, "acl", this.create('ACL'));
|
|
310
|
-
|
|
311
|
-
_defineProperty(this, "bind", this.create('BIND'));
|
|
312
|
-
|
|
313
|
-
this.procs = procs;
|
|
314
|
-
this._procsArray = Object.values(procs);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
getProcs() {
|
|
318
|
-
return this.procs;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
find(method, url) {
|
|
322
|
-
for (var proc of this._procsArray) {
|
|
323
|
-
if (proc.method !== method) {
|
|
324
|
-
continue;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
if (proc.pattern.test(url)) {
|
|
328
|
-
return proc;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
return null;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
}
|
|
336
|
-
class KaitoError extends Error {
|
|
337
|
-
constructor(status, message, cause) {
|
|
338
|
-
super(message);
|
|
339
|
-
this.status = status;
|
|
340
|
-
this.cause = cause;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
}
|
|
344
|
-
function createRouter() {
|
|
345
|
-
return new Router({});
|
|
346
|
-
}
|
|
347
|
-
function createServer(config) {
|
|
348
|
-
var log = message => {
|
|
349
|
-
if (config.log === undefined) {
|
|
350
|
-
console.log(message);
|
|
351
|
-
} else if (config.log) {
|
|
352
|
-
config.log(message);
|
|
353
|
-
}
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
return http__default["default"].createServer( /*#__PURE__*/function () {
|
|
357
|
-
var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse) {
|
|
358
|
-
var start = Date.now();
|
|
359
|
-
var req = new KaitoRequest(incomingMessage);
|
|
360
|
-
var res = new KaitoResponse(serverResponse);
|
|
361
|
-
|
|
298
|
+
static handle(server, options) {
|
|
299
|
+
return _asyncToGenerator(function* () {
|
|
362
300
|
try {
|
|
363
|
-
var
|
|
364
|
-
|
|
365
|
-
var handler = config.router.find(req.method, req.raw.url);
|
|
301
|
+
var _options$route$input$, _options$route$input;
|
|
366
302
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
var input = (_handler$input = handler.input) === null || _handler$input === void 0 ? void 0 : _handler$input.parse((_yield$getInput = yield getInput(req)) !== null && _yield$getInput !== void 0 ? _yield$getInput : undefined);
|
|
372
|
-
var context = yield config.getContext(req, res);
|
|
373
|
-
var data = yield handler.run({
|
|
303
|
+
var context = yield server.getContext(options.req, options.res);
|
|
304
|
+
var body = yield getInput(options.req);
|
|
305
|
+
var input = (_options$route$input$ = (_options$route$input = options.route.input) === null || _options$route$input === void 0 ? void 0 : _options$route$input.parse(body)) !== null && _options$route$input$ !== void 0 ? _options$route$input$ : undefined;
|
|
306
|
+
var result = yield options.route.run({
|
|
374
307
|
ctx: context,
|
|
375
308
|
input,
|
|
376
|
-
params:
|
|
309
|
+
params: options.params
|
|
377
310
|
});
|
|
378
|
-
res.json({
|
|
311
|
+
options.res.status(200).json({
|
|
379
312
|
success: true,
|
|
380
|
-
data,
|
|
313
|
+
data: result,
|
|
381
314
|
message: 'OK'
|
|
382
315
|
});
|
|
383
|
-
} catch (
|
|
316
|
+
} catch (e) {
|
|
317
|
+
var error = WrappedError.maybe(e);
|
|
318
|
+
|
|
384
319
|
if (error instanceof KaitoError) {
|
|
385
|
-
res.status(error.status).json({
|
|
320
|
+
options.res.status(error.status).json({
|
|
386
321
|
success: false,
|
|
387
322
|
data: null,
|
|
388
323
|
message: error.message
|
|
@@ -391,35 +326,119 @@ function createServer(config) {
|
|
|
391
326
|
}
|
|
392
327
|
|
|
393
328
|
var {
|
|
394
|
-
status
|
|
395
|
-
message
|
|
396
|
-
} = yield
|
|
397
|
-
error
|
|
398
|
-
req,
|
|
399
|
-
res
|
|
329
|
+
status,
|
|
330
|
+
message
|
|
331
|
+
} = yield server.onError({
|
|
332
|
+
error,
|
|
333
|
+
req: options.req,
|
|
334
|
+
res: options.res
|
|
400
335
|
}).catch(() => ({
|
|
401
336
|
status: 500,
|
|
402
|
-
message: '
|
|
337
|
+
message: 'Internal Server Error'
|
|
403
338
|
}));
|
|
404
|
-
res.status(
|
|
339
|
+
options.res.status(status).json({
|
|
405
340
|
success: false,
|
|
406
341
|
data: null,
|
|
407
|
-
message
|
|
342
|
+
message
|
|
408
343
|
});
|
|
409
|
-
} finally {
|
|
410
|
-
var finish = Date.now();
|
|
411
|
-
log("".concat(req.method, " ").concat(req.fullURL, " ").concat(res.raw.statusCode, " ").concat(finish - start, "ms"));
|
|
412
344
|
}
|
|
413
|
-
});
|
|
345
|
+
})();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
constructor(routes) {
|
|
349
|
+
_defineProperty(this, "get", this.make('GET'));
|
|
350
|
+
|
|
351
|
+
_defineProperty(this, "post", this.make('POST'));
|
|
352
|
+
|
|
353
|
+
_defineProperty(this, "head", this.make('HEAD'));
|
|
354
|
+
|
|
355
|
+
_defineProperty(this, "put", this.make('PUT'));
|
|
356
|
+
|
|
357
|
+
_defineProperty(this, "patch", this.make('PATCH'));
|
|
358
|
+
|
|
359
|
+
this.routes = routes;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
merge(prefix, router) {
|
|
363
|
+
var newRoutes = Object.fromEntries(Object.entries(router.routes).map(_ref => {
|
|
364
|
+
var [k, v] = _ref;
|
|
365
|
+
return ["".concat(prefix).concat(k), v];
|
|
366
|
+
}));
|
|
367
|
+
return new Router(_objectSpread2(_objectSpread2({}, this.routes), newRoutes));
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
toFindMyWay(server) {
|
|
371
|
+
var _this = this;
|
|
414
372
|
|
|
415
|
-
|
|
416
|
-
|
|
373
|
+
var instance = fmw__default["default"]({
|
|
374
|
+
defaultRoute(req, serverResponse) {
|
|
375
|
+
var _req$url;
|
|
376
|
+
|
|
377
|
+
var res = new KaitoResponse(serverResponse);
|
|
378
|
+
res.status(404).json({
|
|
379
|
+
success: false,
|
|
380
|
+
data: null,
|
|
381
|
+
message: "Cannot ".concat(req.method, " ").concat((_req$url = req.url) !== null && _req$url !== void 0 ? _req$url : '/')
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
}); // eslint-disable-next-line guard-for-in
|
|
386
|
+
|
|
387
|
+
var _loop = function _loop(path) {
|
|
388
|
+
var route = _this.routes[path];
|
|
389
|
+
instance.on(route.method, path, /*#__PURE__*/function () {
|
|
390
|
+
var _ref2 = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
|
|
391
|
+
var req = new KaitoRequest(incomingMessage);
|
|
392
|
+
var res = new KaitoResponse(serverResponse);
|
|
393
|
+
yield Router.handle(server, {
|
|
394
|
+
route,
|
|
395
|
+
params,
|
|
396
|
+
req,
|
|
397
|
+
res
|
|
398
|
+
});
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
return function (_x, _x2, _x3) {
|
|
402
|
+
return _ref2.apply(this, arguments);
|
|
403
|
+
};
|
|
404
|
+
}());
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
for (var path in this.routes) {
|
|
408
|
+
_loop(path);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return instance;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
make(method) {
|
|
415
|
+
return (path, route) => {
|
|
416
|
+
var mergedRoute = _objectSpread2(_objectSpread2({}, route), {}, {
|
|
417
|
+
method
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
return new Router(_objectSpread2(_objectSpread2({}, this.routes), {}, {
|
|
421
|
+
[path]: mergedRoute
|
|
422
|
+
}));
|
|
417
423
|
};
|
|
418
|
-
}
|
|
424
|
+
}
|
|
425
|
+
|
|
419
426
|
}
|
|
427
|
+
/**
|
|
428
|
+
* @deprecated Please use Router#create instead
|
|
429
|
+
*/
|
|
430
|
+
|
|
431
|
+
var createRouter = Router.create;
|
|
420
432
|
|
|
421
433
|
exports.KaitoError = KaitoError;
|
|
434
|
+
exports.KaitoRequest = KaitoRequest;
|
|
435
|
+
exports.KaitoResponse = KaitoResponse;
|
|
422
436
|
exports.Router = Router;
|
|
437
|
+
exports.WrappedError = WrappedError;
|
|
438
|
+
exports.createFMWServer = createFMWServer;
|
|
423
439
|
exports.createGetContext = createGetContext;
|
|
424
440
|
exports.createRouter = createRouter;
|
|
425
441
|
exports.createServer = createServer;
|
|
442
|
+
exports.getInput = getInput;
|
|
443
|
+
exports.getLastEntryInMultiHeaderValue = getLastEntryInMultiHeaderValue;
|
|
444
|
+
exports.normalizePath = normalizePath;
|