@kaito-http/core 2.2.0 → 2.2.1
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.
|
@@ -3,7 +3,7 @@ import http from 'http';
|
|
|
3
3
|
import { z, ZodTypeAny } from 'zod';
|
|
4
4
|
import { KaitoRequest } from './req';
|
|
5
5
|
import { KaitoResponse } from './res';
|
|
6
|
-
import { Method } from './util';
|
|
6
|
+
import { Method, NormalizePath } from './util';
|
|
7
7
|
export declare type GetContext<T> = (req: KaitoRequest, res: KaitoResponse) => Promise<T>;
|
|
8
8
|
declare type Never = [never];
|
|
9
9
|
export declare function createGetContext<T>(getContext: GetContext<T>): GetContext<T>;
|
|
@@ -17,24 +17,18 @@ export declare type Proc<Ctx, Result, Input extends z.ZodTypeAny | Never = Never
|
|
|
17
17
|
input?: Input;
|
|
18
18
|
run(arg: ContextWithInput<Ctx, Input extends ZodTypeAny ? z.infer<Input> : undefined>): Promise<Result>;
|
|
19
19
|
}>;
|
|
20
|
-
export interface RouterProc<Path extends string
|
|
20
|
+
export interface RouterProc<Path extends string, M extends Method> {
|
|
21
21
|
method: M;
|
|
22
22
|
path: Path;
|
|
23
23
|
pattern: RegExp;
|
|
24
24
|
}
|
|
25
25
|
export declare type AnyProcs<Ctx> = {
|
|
26
|
-
[Path in string]: Proc<Ctx, unknown, z.ZodTypeAny> & RouterProc<Path>;
|
|
26
|
+
[Path in string]: Proc<Ctx, unknown, z.ZodTypeAny> & RouterProc<Path, Method>;
|
|
27
27
|
};
|
|
28
28
|
export declare type AnyRouter<Ctx> = Router<Ctx, AnyProcs<Ctx>>;
|
|
29
29
|
export declare class Router<Ctx, Procs extends AnyProcs<Ctx>> {
|
|
30
30
|
private readonly procs;
|
|
31
31
|
private readonly _procsArray;
|
|
32
|
-
/**
|
|
33
|
-
* Ensures that the path does not start or end with a slash.
|
|
34
|
-
* @param path
|
|
35
|
-
* @private
|
|
36
|
-
*/
|
|
37
|
-
private static stripSlashes;
|
|
38
32
|
constructor(procs: Procs);
|
|
39
33
|
getProcs(): Procs;
|
|
40
34
|
find(method: Method, url: string): (Readonly<{
|
|
@@ -42,86 +36,86 @@ export declare class Router<Ctx, Procs extends AnyProcs<Ctx>> {
|
|
|
42
36
|
run(arg: ContextWithInput<Ctx, any>): Promise<unknown>;
|
|
43
37
|
}> & RouterProc<string, Method>) | null;
|
|
44
38
|
private readonly create;
|
|
45
|
-
readonly merge: <Prefix extends string, NewCtx, NewProcs extends AnyProcs<NewCtx>>(
|
|
39
|
+
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 `/${Prefix}${Extract<keyof NewProcs, string>}`]: Omit<NewProcs[P extends `/${Prefix}${infer Rest}` ? Rest : never], "path"> & {
|
|
46
40
|
path: P;
|
|
47
41
|
}; }>;
|
|
48
|
-
readonly get: <Path extends string, Result, Input extends z.ZodTypeAny>(path: Path
|
|
42
|
+
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: Readonly<{
|
|
49
43
|
input?: Input | undefined;
|
|
50
44
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
51
|
-
}>) => Router<Ctx, Procs & Record<Path
|
|
45
|
+
}>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Readonly<{
|
|
52
46
|
input?: Input | undefined;
|
|
53
47
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
54
|
-
}> & RouterProc<Path
|
|
55
|
-
readonly post: <Path extends string, Result, Input extends z.ZodTypeAny>(path: Path
|
|
48
|
+
}> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "GET">>>;
|
|
49
|
+
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: Readonly<{
|
|
56
50
|
input?: Input | undefined;
|
|
57
51
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
58
|
-
}>) => Router<Ctx, Procs & Record<Path
|
|
52
|
+
}>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Readonly<{
|
|
59
53
|
input?: Input | undefined;
|
|
60
54
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
61
|
-
}> & RouterProc<Path
|
|
62
|
-
readonly put: <Path extends string, Result, Input extends z.ZodTypeAny>(path: Path
|
|
55
|
+
}> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "POST">>>;
|
|
56
|
+
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: Readonly<{
|
|
63
57
|
input?: Input | undefined;
|
|
64
58
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
65
|
-
}>) => Router<Ctx, Procs & Record<Path
|
|
59
|
+
}>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Readonly<{
|
|
66
60
|
input?: Input | undefined;
|
|
67
61
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
68
|
-
}> & RouterProc<Path
|
|
69
|
-
readonly patch: <Path extends string, Result, Input extends z.ZodTypeAny>(path: Path
|
|
62
|
+
}> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "PUT">>>;
|
|
63
|
+
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: Readonly<{
|
|
70
64
|
input?: Input | undefined;
|
|
71
65
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
72
|
-
}>) => Router<Ctx, Procs & Record<Path
|
|
66
|
+
}>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Readonly<{
|
|
73
67
|
input?: Input | undefined;
|
|
74
68
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
75
|
-
}> & RouterProc<Path
|
|
76
|
-
readonly delete: <Path extends string, Result, Input extends z.ZodTypeAny>(path: Path
|
|
69
|
+
}> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "PATCH">>>;
|
|
70
|
+
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: Readonly<{
|
|
77
71
|
input?: Input | undefined;
|
|
78
72
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
79
|
-
}>) => Router<Ctx, Procs & Record<Path
|
|
73
|
+
}>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Readonly<{
|
|
80
74
|
input?: Input | undefined;
|
|
81
75
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
82
|
-
}> & RouterProc<Path
|
|
83
|
-
readonly head: <Path extends string, Result, Input extends z.ZodTypeAny>(path: Path
|
|
76
|
+
}> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "DELETE">>>;
|
|
77
|
+
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: Readonly<{
|
|
84
78
|
input?: Input | undefined;
|
|
85
79
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
86
|
-
}>) => Router<Ctx, Procs & Record<Path
|
|
80
|
+
}>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Readonly<{
|
|
87
81
|
input?: Input | undefined;
|
|
88
82
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
89
|
-
}> & RouterProc<Path
|
|
90
|
-
readonly options: <Path extends string, Result, Input extends z.ZodTypeAny>(path: Path
|
|
83
|
+
}> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "HEAD">>>;
|
|
84
|
+
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: Readonly<{
|
|
91
85
|
input?: Input | undefined;
|
|
92
86
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
93
|
-
}>) => Router<Ctx, Procs & Record<Path
|
|
87
|
+
}>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Readonly<{
|
|
94
88
|
input?: Input | undefined;
|
|
95
89
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
96
|
-
}> & RouterProc<Path
|
|
97
|
-
readonly connect: <Path extends string, Result, Input extends z.ZodTypeAny>(path: Path
|
|
90
|
+
}> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "OPTIONS">>>;
|
|
91
|
+
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: Readonly<{
|
|
98
92
|
input?: Input | undefined;
|
|
99
93
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
100
|
-
}>) => Router<Ctx, Procs & Record<Path
|
|
94
|
+
}>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Readonly<{
|
|
101
95
|
input?: Input | undefined;
|
|
102
96
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
103
|
-
}> & RouterProc<Path
|
|
104
|
-
readonly trace: <Path extends string, Result, Input extends z.ZodTypeAny>(path: Path
|
|
97
|
+
}> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "CONNECT">>>;
|
|
98
|
+
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: Readonly<{
|
|
105
99
|
input?: Input | undefined;
|
|
106
100
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
107
|
-
}>) => Router<Ctx, Procs & Record<Path
|
|
101
|
+
}>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Readonly<{
|
|
108
102
|
input?: Input | undefined;
|
|
109
103
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
110
|
-
}> & RouterProc<Path
|
|
111
|
-
readonly acl: <Path extends string, Result, Input extends z.ZodTypeAny>(path: Path
|
|
104
|
+
}> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "TRACE">>>;
|
|
105
|
+
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: Readonly<{
|
|
112
106
|
input?: Input | undefined;
|
|
113
107
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
114
|
-
}>) => Router<Ctx, Procs & Record<Path
|
|
108
|
+
}>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Readonly<{
|
|
115
109
|
input?: Input | undefined;
|
|
116
110
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
117
|
-
}> & RouterProc<Path
|
|
118
|
-
readonly bind: <Path extends string, Result, Input extends z.ZodTypeAny>(path: Path
|
|
111
|
+
}> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "ACL">>>;
|
|
112
|
+
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: Readonly<{
|
|
119
113
|
input?: Input | undefined;
|
|
120
114
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
121
|
-
}>) => Router<Ctx, Procs & Record<Path
|
|
115
|
+
}>) => Router<Ctx, Procs & Record<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, Readonly<{
|
|
122
116
|
input?: Input | undefined;
|
|
123
117
|
run(arg: ContextWithInput<Ctx, Input extends z.ZodTypeAny ? z.TypeOf<Input> : undefined>): Promise<Result>;
|
|
124
|
-
}> & RouterProc<Path
|
|
118
|
+
}> & RouterProc<(Path extends `${infer U}/` ? U : Path) extends `/${infer U_1}` ? `/${U_1}` : `/${Path extends `${infer U}/` ? U : Path}`, "BIND">>>;
|
|
125
119
|
}
|
|
126
120
|
export declare class KaitoError extends Error {
|
|
127
121
|
readonly status: number;
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { KaitoRequest } from './req';
|
|
2
2
|
export declare function getLastEntryInMultiHeaderValue(headerValue: string | string[]): string;
|
|
3
|
+
declare type RemoveEndSlashes<T extends string> = T extends `${infer U}/` ? U : T;
|
|
4
|
+
declare type AddStartSlashes<T extends string> = T extends `/${infer U}` ? `/${U}` : `/${T}`;
|
|
5
|
+
export declare type NormalizePath<T extends string> = AddStartSlashes<RemoveEndSlashes<T>>;
|
|
6
|
+
export declare function normalizePath<T extends string>(path: T): NormalizePath<T>;
|
|
3
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';
|
|
4
8
|
export declare function getInput(req: KaitoRequest): Promise<unknown>;
|
|
9
|
+
export {};
|
|
@@ -124,6 +124,19 @@ function getLastEntryInMultiHeaderValue(headerValue) {
|
|
|
124
124
|
var normalized = Array.isArray(headerValue) ? headerValue.join(',') : headerValue;
|
|
125
125
|
var lastIndex = normalized.lastIndexOf(',');
|
|
126
126
|
return lastIndex === -1 ? normalized.trim() : normalized.slice(lastIndex + 1).trim();
|
|
127
|
+
}
|
|
128
|
+
function normalizePath(path) {
|
|
129
|
+
var result = path;
|
|
130
|
+
|
|
131
|
+
if (!result.startsWith('/')) {
|
|
132
|
+
result = "/".concat(result);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (result.endsWith('/')) {
|
|
136
|
+
result = result.slice(-1);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return result;
|
|
127
140
|
} // Type for import('http').METHODS
|
|
128
141
|
|
|
129
142
|
function getInput(_x) {
|
|
@@ -231,27 +244,10 @@ function createGetContext(getContext) {
|
|
|
231
244
|
return getContext;
|
|
232
245
|
}
|
|
233
246
|
class Router {
|
|
234
|
-
/**
|
|
235
|
-
* Ensures that the path does not start or end with a slash.
|
|
236
|
-
* @param path
|
|
237
|
-
* @private
|
|
238
|
-
*/
|
|
239
|
-
static stripSlashes(path) {
|
|
240
|
-
if (path.startsWith('/')) {
|
|
241
|
-
path = path.slice(1);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
if (path.endsWith('/')) {
|
|
245
|
-
path = path.slice(-1);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return path;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
247
|
constructor(procs) {
|
|
252
248
|
_defineProperty(this, "create", method => (path, proc) => {
|
|
253
|
-
var stripped =
|
|
254
|
-
var pattern = new RegExp("
|
|
249
|
+
var stripped = normalizePath(path);
|
|
250
|
+
var pattern = new RegExp("^".concat(stripped, "/?$"), 'i');
|
|
255
251
|
|
|
256
252
|
var merged = _objectSpread2(_objectSpread2({}, this.procs), {}, {
|
|
257
253
|
[path]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
@@ -264,9 +260,11 @@ class Router {
|
|
|
264
260
|
return new Router(merged);
|
|
265
261
|
});
|
|
266
262
|
|
|
267
|
-
_defineProperty(this, "merge", (
|
|
263
|
+
_defineProperty(this, "merge", (_prefix, router) => {
|
|
264
|
+
var prefix = normalizePath(_prefix);
|
|
268
265
|
var newProcs = Object.entries(router.getProcs()).reduce((all, entry) => {
|
|
269
|
-
var [
|
|
266
|
+
var [_path, proc] = entry;
|
|
267
|
+
var path = normalizePath(_path);
|
|
270
268
|
return _objectSpread2(_objectSpread2({}, all), {}, {
|
|
271
269
|
["".concat(prefix).concat(path)]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
272
270
|
path: "".concat(prefix).concat(path)
|
|
@@ -124,6 +124,19 @@ function getLastEntryInMultiHeaderValue(headerValue) {
|
|
|
124
124
|
var normalized = Array.isArray(headerValue) ? headerValue.join(',') : headerValue;
|
|
125
125
|
var lastIndex = normalized.lastIndexOf(',');
|
|
126
126
|
return lastIndex === -1 ? normalized.trim() : normalized.slice(lastIndex + 1).trim();
|
|
127
|
+
}
|
|
128
|
+
function normalizePath(path) {
|
|
129
|
+
var result = path;
|
|
130
|
+
|
|
131
|
+
if (!result.startsWith('/')) {
|
|
132
|
+
result = "/".concat(result);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (result.endsWith('/')) {
|
|
136
|
+
result = result.slice(-1);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return result;
|
|
127
140
|
} // Type for import('http').METHODS
|
|
128
141
|
|
|
129
142
|
function getInput(_x) {
|
|
@@ -231,27 +244,10 @@ function createGetContext(getContext) {
|
|
|
231
244
|
return getContext;
|
|
232
245
|
}
|
|
233
246
|
class Router {
|
|
234
|
-
/**
|
|
235
|
-
* Ensures that the path does not start or end with a slash.
|
|
236
|
-
* @param path
|
|
237
|
-
* @private
|
|
238
|
-
*/
|
|
239
|
-
static stripSlashes(path) {
|
|
240
|
-
if (path.startsWith('/')) {
|
|
241
|
-
path = path.slice(1);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
if (path.endsWith('/')) {
|
|
245
|
-
path = path.slice(-1);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return path;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
247
|
constructor(procs) {
|
|
252
248
|
_defineProperty(this, "create", method => (path, proc) => {
|
|
253
|
-
var stripped =
|
|
254
|
-
var pattern = new RegExp("
|
|
249
|
+
var stripped = normalizePath(path);
|
|
250
|
+
var pattern = new RegExp("^".concat(stripped, "/?$"), 'i');
|
|
255
251
|
|
|
256
252
|
var merged = _objectSpread2(_objectSpread2({}, this.procs), {}, {
|
|
257
253
|
[path]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
@@ -264,9 +260,11 @@ class Router {
|
|
|
264
260
|
return new Router(merged);
|
|
265
261
|
});
|
|
266
262
|
|
|
267
|
-
_defineProperty(this, "merge", (
|
|
263
|
+
_defineProperty(this, "merge", (_prefix, router) => {
|
|
264
|
+
var prefix = normalizePath(_prefix);
|
|
268
265
|
var newProcs = Object.entries(router.getProcs()).reduce((all, entry) => {
|
|
269
|
-
var [
|
|
266
|
+
var [_path, proc] = entry;
|
|
267
|
+
var path = normalizePath(_path);
|
|
270
268
|
return _objectSpread2(_objectSpread2({}, all), {}, {
|
|
271
269
|
["".concat(prefix).concat(path)]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
272
270
|
path: "".concat(prefix).concat(path)
|
|
@@ -115,6 +115,19 @@ function getLastEntryInMultiHeaderValue(headerValue) {
|
|
|
115
115
|
var normalized = Array.isArray(headerValue) ? headerValue.join(',') : headerValue;
|
|
116
116
|
var lastIndex = normalized.lastIndexOf(',');
|
|
117
117
|
return lastIndex === -1 ? normalized.trim() : normalized.slice(lastIndex + 1).trim();
|
|
118
|
+
}
|
|
119
|
+
function normalizePath(path) {
|
|
120
|
+
var result = path;
|
|
121
|
+
|
|
122
|
+
if (!result.startsWith('/')) {
|
|
123
|
+
result = "/".concat(result);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (result.endsWith('/')) {
|
|
127
|
+
result = result.slice(-1);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return result;
|
|
118
131
|
} // Type for import('http').METHODS
|
|
119
132
|
|
|
120
133
|
function getInput(_x) {
|
|
@@ -222,27 +235,10 @@ function createGetContext(getContext) {
|
|
|
222
235
|
return getContext;
|
|
223
236
|
}
|
|
224
237
|
class Router {
|
|
225
|
-
/**
|
|
226
|
-
* Ensures that the path does not start or end with a slash.
|
|
227
|
-
* @param path
|
|
228
|
-
* @private
|
|
229
|
-
*/
|
|
230
|
-
static stripSlashes(path) {
|
|
231
|
-
if (path.startsWith('/')) {
|
|
232
|
-
path = path.slice(1);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
if (path.endsWith('/')) {
|
|
236
|
-
path = path.slice(-1);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
return path;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
238
|
constructor(procs) {
|
|
243
239
|
_defineProperty(this, "create", method => (path, proc) => {
|
|
244
|
-
var stripped =
|
|
245
|
-
var pattern = new RegExp("
|
|
240
|
+
var stripped = normalizePath(path);
|
|
241
|
+
var pattern = new RegExp("^".concat(stripped, "/?$"), 'i');
|
|
246
242
|
|
|
247
243
|
var merged = _objectSpread2(_objectSpread2({}, this.procs), {}, {
|
|
248
244
|
[path]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
@@ -255,9 +251,11 @@ class Router {
|
|
|
255
251
|
return new Router(merged);
|
|
256
252
|
});
|
|
257
253
|
|
|
258
|
-
_defineProperty(this, "merge", (
|
|
254
|
+
_defineProperty(this, "merge", (_prefix, router) => {
|
|
255
|
+
var prefix = normalizePath(_prefix);
|
|
259
256
|
var newProcs = Object.entries(router.getProcs()).reduce((all, entry) => {
|
|
260
|
-
var [
|
|
257
|
+
var [_path, proc] = entry;
|
|
258
|
+
var path = normalizePath(_path);
|
|
261
259
|
return _objectSpread2(_objectSpread2({}, all), {}, {
|
|
262
260
|
["".concat(prefix).concat(path)]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
263
261
|
path: "".concat(prefix).concat(path)
|