@kaito-http/core 2.6.0 → 2.7.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/res.d.ts +4 -4
- package/dist/declarations/src/route.d.ts +5 -5
- package/dist/declarations/src/router.d.ts +18 -4
- package/dist/declarations/src/server.d.ts +5 -5
- package/dist/declarations/src/util.d.ts +9 -9
- package/dist/kaito-http-core.cjs.dev.js +38 -102
- package/dist/kaito-http-core.cjs.prod.js +38 -102
- package/dist/kaito-http-core.esm.js +38 -102
- package/package.json +4 -4
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type { ServerResponse } from 'node:http';
|
|
3
3
|
import type { CookieSerializeOptions } from 'cookie';
|
|
4
|
-
export
|
|
4
|
+
export type ErroredAPIResponse = {
|
|
5
5
|
success: false;
|
|
6
6
|
data: null;
|
|
7
7
|
message: string;
|
|
8
8
|
};
|
|
9
|
-
export
|
|
9
|
+
export type SuccessfulAPIResponse<T> = {
|
|
10
10
|
success: true;
|
|
11
11
|
data: T;
|
|
12
12
|
message: 'OK';
|
|
13
13
|
};
|
|
14
|
-
export
|
|
15
|
-
export
|
|
14
|
+
export type APIResponse<T> = ErroredAPIResponse | SuccessfulAPIResponse<T>;
|
|
15
|
+
export type AnyResponse = APIResponse<unknown>;
|
|
16
16
|
export declare class KaitoResponse<T = unknown> {
|
|
17
17
|
readonly raw: ServerResponse;
|
|
18
18
|
constructor(raw: ServerResponse);
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import type { z } from 'zod';
|
|
2
2
|
import type { ExtractRouteParams, KaitoMethod } from './util';
|
|
3
|
-
export
|
|
3
|
+
export type RouteArgument<Path extends string, Context, QueryOutput, BodyOutput> = {
|
|
4
4
|
ctx: Context;
|
|
5
5
|
body: BodyOutput;
|
|
6
6
|
query: QueryOutput;
|
|
7
7
|
params: ExtractRouteParams<Path>;
|
|
8
8
|
};
|
|
9
|
-
export
|
|
10
|
-
export
|
|
9
|
+
export type AnyQueryDefinition = Record<string, z.ZodTypeAny>;
|
|
10
|
+
export type Route<Context, Result, Path extends string, Method extends KaitoMethod, Query extends AnyQueryDefinition, BodyOutput, BodyDef extends z.ZodTypeDef, BodyInput> = {
|
|
11
11
|
body?: z.ZodType<BodyOutput, BodyDef, BodyInput>;
|
|
12
12
|
query?: Query;
|
|
13
13
|
path: Path;
|
|
14
14
|
method: Method;
|
|
15
|
-
run(args: RouteArgument<Path, Context, z.infer<z.ZodObject<Query>>, BodyOutput>): Promise<Result
|
|
15
|
+
run(args: RouteArgument<Path, Context, z.infer<z.ZodObject<Query>>, BodyOutput>): Promise<Result> | Result;
|
|
16
16
|
};
|
|
17
|
-
export
|
|
17
|
+
export type AnyRoute<Context = any> = Route<Context, any, any, any, AnyQueryDefinition, any, any, any>;
|
|
@@ -3,9 +3,9 @@ import { z } from 'zod';
|
|
|
3
3
|
import type { AnyQueryDefinition, AnyRoute, Route } from './route';
|
|
4
4
|
import type { ServerConfig } from './server';
|
|
5
5
|
import type { KaitoMethod } from './util';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
type Routes = readonly AnyRoute[];
|
|
7
|
+
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
|
+
type PrefixRoutesPath<Prefix extends `/${string}`, R extends Routes> = R extends [infer First, ...infer Rest] ? [
|
|
9
9
|
RemapRoutePrefix<Extract<First, AnyRoute>, Prefix>,
|
|
10
10
|
...PrefixRoutesPath<Prefix, Extract<Rest, readonly AnyRoute[]>>
|
|
11
11
|
] : [];
|
|
@@ -14,7 +14,21 @@ export declare class Router<Context, R extends Routes> {
|
|
|
14
14
|
static create: <Context_1>() => Router<Context_1, []>;
|
|
15
15
|
private static handle;
|
|
16
16
|
constructor(routes: R);
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Adds a new route to the router
|
|
19
|
+
* @param route The route specification to add to this router
|
|
20
|
+
* @returns A new router with this route added
|
|
21
|
+
* @deprecated Use `Router#add` instead
|
|
22
|
+
*/
|
|
23
|
+
old_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>]>;
|
|
24
|
+
/**
|
|
25
|
+
* Adds a new route to the router
|
|
26
|
+
* @param method The HTTP method to add a route for
|
|
27
|
+
* @param path The path to add a route for
|
|
28
|
+
* @param route The route specification to add to this router
|
|
29
|
+
* @returns A new router with this route added
|
|
30
|
+
*/
|
|
31
|
+
add: <Result, Path extends string, Method extends KaitoMethod, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(method: Method, path: Path, route: ((args: import("./route").RouteArgument<Path, Context, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Result | Promise<Result>) | (Method extends "GET" ? Omit<Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>, "path" | "body" | "method"> : Omit<Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">)) => Router<Context, [...R, Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>]>;
|
|
18
32
|
merge: <PathPrefix extends `/${string}`, OtherRoutes extends Routes>(pathPrefix: PathPrefix, other: Router<Context, OtherRoutes>) => Router<Context, [...R, ...PrefixRoutesPath<PathPrefix, OtherRoutes>]>;
|
|
19
33
|
toFindMyWay: (server: ServerConfig<Context, any>) => fmw.Instance<fmw.HTTPVersion.V1>;
|
|
20
34
|
}
|
|
@@ -5,8 +5,8 @@ import type { KaitoRequest } from './req';
|
|
|
5
5
|
import type { KaitoResponse } from './res';
|
|
6
6
|
import type { Router } from './router';
|
|
7
7
|
import type { GetContext, KaitoMethod } from './util';
|
|
8
|
-
export
|
|
9
|
-
export
|
|
8
|
+
export type Before<BeforeAfterContext> = (req: http.IncomingMessage, res: http.ServerResponse) => Promise<BeforeAfterContext>;
|
|
9
|
+
export type HandlerResult = {
|
|
10
10
|
success: true;
|
|
11
11
|
data: unknown;
|
|
12
12
|
} | {
|
|
@@ -16,14 +16,14 @@ export declare type HandlerResult = {
|
|
|
16
16
|
message: string;
|
|
17
17
|
};
|
|
18
18
|
};
|
|
19
|
-
export
|
|
20
|
-
export
|
|
19
|
+
export type After<BeforeAfterContext> = (ctx: BeforeAfterContext, result: HandlerResult) => Promise<void>;
|
|
20
|
+
export type ServerConfigWithBefore<BeforeAfterContext> = {
|
|
21
21
|
before: Before<BeforeAfterContext>;
|
|
22
22
|
after?: After<BeforeAfterContext>;
|
|
23
23
|
} | {
|
|
24
24
|
before?: undefined;
|
|
25
25
|
};
|
|
26
|
-
export
|
|
26
|
+
export type ServerConfig<Context, BeforeAfterContext> = ServerConfigWithBefore<BeforeAfterContext> & {
|
|
27
27
|
router: Router<Context, any>;
|
|
28
28
|
getContext: GetContext<Context>;
|
|
29
29
|
rawRoutes?: Partial<Record<KaitoMethod, Array<{
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import type { HTTPMethod } from 'find-my-way';
|
|
2
2
|
import type { KaitoRequest } from './req';
|
|
3
3
|
import type { KaitoResponse } from './res';
|
|
4
|
-
export
|
|
4
|
+
export 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}` ? {
|
|
7
7
|
[k in Param]: string;
|
|
8
8
|
} : {};
|
|
9
|
-
export
|
|
10
|
-
export
|
|
9
|
+
export type KaitoMethod = HTTPMethod | '*';
|
|
10
|
+
export type GetContext<Result> = (req: KaitoRequest, res: KaitoResponse) => Promise<Result>;
|
|
11
11
|
export declare function createGetContext<Context>(callback: GetContext<Context>): GetContext<Context>;
|
|
12
|
-
export
|
|
12
|
+
export type InferContext<T> = T extends (req: KaitoRequest, res: KaitoResponse) => Promise<infer U> ? U : never;
|
|
13
13
|
export declare function getLastEntryInMultiHeaderValue(headerValue: string | string[]): string;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export
|
|
17
|
-
export
|
|
18
|
-
export
|
|
14
|
+
type RemoveEndSlashes<T extends string> = T extends `${infer U}/` ? U : T;
|
|
15
|
+
type AddStartSlashes<T extends string> = T extends `/${infer U}` ? `/${U}` : `/${T}`;
|
|
16
|
+
export type NormalizePath<T extends string> = AddStartSlashes<RemoveEndSlashes<T>>;
|
|
17
|
+
export type Values<T> = T[keyof T];
|
|
18
|
+
export type NoEmpty<T> = [keyof T] extends [never] ? never : T;
|
|
19
19
|
export declare function getBody(req: KaitoRequest): Promise<unknown>;
|
|
20
20
|
export {};
|
|
@@ -41,26 +41,21 @@ class WrappedError extends Error {
|
|
|
41
41
|
if (maybeError instanceof Error) {
|
|
42
42
|
return maybeError;
|
|
43
43
|
}
|
|
44
|
-
|
|
45
44
|
return WrappedError.from(maybeError);
|
|
46
45
|
}
|
|
47
|
-
|
|
48
46
|
static from(data) {
|
|
49
47
|
return new WrappedError(data);
|
|
50
48
|
}
|
|
51
|
-
|
|
52
49
|
constructor(data) {
|
|
53
50
|
super('Something was thrown, but it was not an instance of Error, so a WrappedError was created.');
|
|
54
51
|
this.data = data;
|
|
55
52
|
}
|
|
56
|
-
|
|
57
53
|
}
|
|
58
54
|
class KaitoError extends Error {
|
|
59
55
|
constructor(status, message) {
|
|
60
56
|
super(message);
|
|
61
57
|
this.status = status;
|
|
62
58
|
}
|
|
63
|
-
|
|
64
59
|
}
|
|
65
60
|
|
|
66
61
|
function _defineProperty(obj, key, value) {
|
|
@@ -74,7 +69,6 @@ function _defineProperty(obj, key, value) {
|
|
|
74
69
|
} else {
|
|
75
70
|
obj[key] = value;
|
|
76
71
|
}
|
|
77
|
-
|
|
78
72
|
return obj;
|
|
79
73
|
}
|
|
80
74
|
|
|
@@ -86,29 +80,24 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
|
86
80
|
reject(error);
|
|
87
81
|
return;
|
|
88
82
|
}
|
|
89
|
-
|
|
90
83
|
if (info.done) {
|
|
91
84
|
resolve(value);
|
|
92
85
|
} else {
|
|
93
86
|
Promise.resolve(value).then(_next, _throw);
|
|
94
87
|
}
|
|
95
88
|
}
|
|
96
|
-
|
|
97
89
|
function _asyncToGenerator(fn) {
|
|
98
90
|
return function () {
|
|
99
91
|
var self = this,
|
|
100
|
-
|
|
92
|
+
args = arguments;
|
|
101
93
|
return new Promise(function (resolve, reject) {
|
|
102
94
|
var gen = fn.apply(self, args);
|
|
103
|
-
|
|
104
95
|
function _next(value) {
|
|
105
96
|
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
106
97
|
}
|
|
107
|
-
|
|
108
98
|
function _throw(err) {
|
|
109
99
|
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
110
100
|
}
|
|
111
|
-
|
|
112
101
|
_next(undefined);
|
|
113
102
|
});
|
|
114
103
|
};
|
|
@@ -125,31 +114,26 @@ function getLastEntryInMultiHeaderValue(headerValue) {
|
|
|
125
114
|
function getBody(_x) {
|
|
126
115
|
return _getBody.apply(this, arguments);
|
|
127
116
|
}
|
|
128
|
-
|
|
129
117
|
function _getBody() {
|
|
130
118
|
_getBody = _asyncToGenerator(function* (req) {
|
|
131
119
|
if (!req.headers['content-type']) {
|
|
132
120
|
return null;
|
|
133
121
|
}
|
|
134
|
-
|
|
135
122
|
var buffer = yield getRawBody__default["default"](req.raw);
|
|
136
123
|
var {
|
|
137
124
|
type
|
|
138
125
|
} = contentType.parse(req.headers['content-type']);
|
|
139
|
-
|
|
140
126
|
switch (type) {
|
|
141
127
|
case 'application/json':
|
|
142
128
|
{
|
|
143
129
|
return consumers.json(node_stream.Readable.from(buffer));
|
|
144
130
|
}
|
|
145
|
-
|
|
146
131
|
default:
|
|
147
132
|
{
|
|
148
133
|
if (process.env.NODE_ENV === 'development') {
|
|
149
134
|
console.warn('[kaito] Unsupported content type:', type);
|
|
150
135
|
console.warn('[kaito] This message is only shown in development mode.');
|
|
151
136
|
}
|
|
152
|
-
|
|
153
137
|
return null;
|
|
154
138
|
}
|
|
155
139
|
}
|
|
@@ -160,105 +144,91 @@ function _getBody() {
|
|
|
160
144
|
class KaitoRequest {
|
|
161
145
|
constructor(raw) {
|
|
162
146
|
_defineProperty(this, "_url", null);
|
|
163
|
-
|
|
164
147
|
this.raw = raw;
|
|
165
148
|
}
|
|
149
|
+
|
|
166
150
|
/**
|
|
167
151
|
* The full URL of the request, including the protocol, hostname, and path.
|
|
168
152
|
* Note: does not include the query string or hash
|
|
169
153
|
*/
|
|
170
|
-
|
|
171
|
-
|
|
172
154
|
get fullURL() {
|
|
173
155
|
var _this$raw$url;
|
|
174
|
-
|
|
175
156
|
return "".concat(this.protocol, "://").concat(this.hostname).concat((_this$raw$url = this.raw.url) !== null && _this$raw$url !== void 0 ? _this$raw$url : '');
|
|
176
157
|
}
|
|
158
|
+
|
|
177
159
|
/**
|
|
178
160
|
* A new URL instance for the full URL of the request.
|
|
179
161
|
*/
|
|
180
|
-
|
|
181
|
-
|
|
182
162
|
get url() {
|
|
183
163
|
if (this._url) {
|
|
184
164
|
return this._url;
|
|
185
165
|
}
|
|
186
|
-
|
|
187
166
|
this._url = new URL(this.fullURL);
|
|
188
167
|
return this._url;
|
|
189
168
|
}
|
|
169
|
+
|
|
190
170
|
/**
|
|
191
171
|
* The HTTP method of the request.
|
|
192
172
|
*/
|
|
193
|
-
|
|
194
|
-
|
|
195
173
|
get method() {
|
|
196
174
|
if (!this.raw.method) {
|
|
197
175
|
throw new Error('Request method is not defined, somehow...');
|
|
198
176
|
}
|
|
199
|
-
|
|
200
177
|
return this.raw.method;
|
|
201
178
|
}
|
|
179
|
+
|
|
202
180
|
/**
|
|
203
181
|
* The protocol of the request, either `http` or `https`.
|
|
204
182
|
*/
|
|
205
|
-
|
|
206
|
-
|
|
207
183
|
get protocol() {
|
|
208
184
|
if (this.raw.socket instanceof node_tls.TLSSocket) {
|
|
209
185
|
return this.raw.socket.encrypted ? 'https' : 'http';
|
|
210
186
|
}
|
|
211
|
-
|
|
212
187
|
return 'http';
|
|
213
188
|
}
|
|
189
|
+
|
|
214
190
|
/**
|
|
215
191
|
* The request headers
|
|
216
192
|
*/
|
|
217
|
-
|
|
218
|
-
|
|
219
193
|
get headers() {
|
|
220
194
|
return this.raw.headers;
|
|
221
195
|
}
|
|
196
|
+
|
|
222
197
|
/**
|
|
223
198
|
* The hostname of the request.
|
|
224
199
|
*/
|
|
225
|
-
|
|
226
|
-
|
|
227
200
|
get hostname() {
|
|
228
201
|
var _this$raw$headers$hos, _this$raw$headers$Au;
|
|
229
|
-
|
|
230
202
|
return (_this$raw$headers$hos = this.raw.headers.host) !== null && _this$raw$headers$hos !== void 0 ? _this$raw$headers$hos : getLastEntryInMultiHeaderValue((_this$raw$headers$Au = this.raw.headers[':authority']) !== null && _this$raw$headers$Au !== void 0 ? _this$raw$headers$Au : []);
|
|
231
203
|
}
|
|
232
|
-
|
|
233
204
|
}
|
|
234
205
|
|
|
235
206
|
class KaitoResponse {
|
|
236
207
|
constructor(raw) {
|
|
237
208
|
this.raw = raw;
|
|
238
209
|
}
|
|
210
|
+
|
|
239
211
|
/**
|
|
240
212
|
* Send a response
|
|
241
213
|
* @param key The key of the header
|
|
242
214
|
* @param value The value of the header
|
|
243
215
|
* @returns The response object
|
|
244
216
|
*/
|
|
245
|
-
|
|
246
|
-
|
|
247
217
|
header(key, value) {
|
|
248
218
|
this.raw.setHeader(key, value);
|
|
249
219
|
return this;
|
|
250
220
|
}
|
|
221
|
+
|
|
251
222
|
/**
|
|
252
223
|
* Set the status code of the response
|
|
253
224
|
* @param code The status code
|
|
254
225
|
* @returns The response object
|
|
255
226
|
*/
|
|
256
|
-
|
|
257
|
-
|
|
258
227
|
status(code) {
|
|
259
228
|
this.raw.statusCode = code;
|
|
260
229
|
return this;
|
|
261
230
|
}
|
|
231
|
+
|
|
262
232
|
/**
|
|
263
233
|
* Set a cookie
|
|
264
234
|
* @param name The name of the cookie
|
|
@@ -266,19 +236,16 @@ class KaitoResponse {
|
|
|
266
236
|
* @param options The options for the cookie
|
|
267
237
|
* @returns The response object
|
|
268
238
|
*/
|
|
269
|
-
|
|
270
|
-
|
|
271
239
|
cookie(name, value, options) {
|
|
272
240
|
this.raw.setHeader('Set-Cookie', cookie.serialize(name, value, options));
|
|
273
241
|
return this;
|
|
274
242
|
}
|
|
243
|
+
|
|
275
244
|
/**
|
|
276
245
|
* Send a JSON APIResponse body
|
|
277
246
|
* @param data The data to send
|
|
278
247
|
* @returns The response object
|
|
279
248
|
*/
|
|
280
|
-
|
|
281
|
-
|
|
282
249
|
json(data) {
|
|
283
250
|
var json = JSON.stringify(data);
|
|
284
251
|
this.raw.setHeader('Content-Type', 'application/json');
|
|
@@ -286,44 +253,27 @@ class KaitoResponse {
|
|
|
286
253
|
this.raw.end(json);
|
|
287
254
|
return this;
|
|
288
255
|
}
|
|
289
|
-
|
|
290
256
|
}
|
|
291
257
|
|
|
292
258
|
function ownKeys(object, enumerableOnly) {
|
|
293
259
|
var keys = Object.keys(object);
|
|
294
|
-
|
|
295
260
|
if (Object.getOwnPropertySymbols) {
|
|
296
261
|
var symbols = Object.getOwnPropertySymbols(object);
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
keys.push.apply(keys, symbols);
|
|
262
|
+
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
263
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
264
|
+
})), keys.push.apply(keys, symbols);
|
|
305
265
|
}
|
|
306
|
-
|
|
307
266
|
return keys;
|
|
308
267
|
}
|
|
309
|
-
|
|
310
268
|
function _objectSpread2(target) {
|
|
311
269
|
for (var i = 1; i < arguments.length; i++) {
|
|
312
|
-
var source = arguments[i]
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
} else if (Object.getOwnPropertyDescriptors) {
|
|
319
|
-
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
320
|
-
} else {
|
|
321
|
-
ownKeys(Object(source)).forEach(function (key) {
|
|
322
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
323
|
-
});
|
|
324
|
-
}
|
|
270
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
|
271
|
+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
|
272
|
+
_defineProperty(target, key, source[key]);
|
|
273
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
|
274
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
275
|
+
});
|
|
325
276
|
}
|
|
326
|
-
|
|
327
277
|
return target;
|
|
328
278
|
}
|
|
329
279
|
|
|
@@ -332,7 +282,6 @@ class Router {
|
|
|
332
282
|
return _asyncToGenerator(function* () {
|
|
333
283
|
try {
|
|
334
284
|
var _yield$route$body$par, _route$body;
|
|
335
|
-
|
|
336
285
|
var ctx = yield server.getContext(options.req, options.res);
|
|
337
286
|
var body = (_yield$route$body$par = yield (_route$body = route.body) === null || _route$body === void 0 ? void 0 : _route$body.parse(yield getBody(options.req))) !== null && _yield$route$body$par !== void 0 ? _yield$route$body$par : undefined;
|
|
338
287
|
var query = route.query ? zod.z.object(route.query).parse(Object.fromEntries(options.req.url.searchParams.entries())) : {};
|
|
@@ -353,7 +302,6 @@ class Router {
|
|
|
353
302
|
};
|
|
354
303
|
} catch (e) {
|
|
355
304
|
var error = WrappedError.maybe(e);
|
|
356
|
-
|
|
357
305
|
if (error instanceof KaitoError) {
|
|
358
306
|
options.res.status(error.status).json({
|
|
359
307
|
success: false,
|
|
@@ -362,7 +310,6 @@ class Router {
|
|
|
362
310
|
});
|
|
363
311
|
return;
|
|
364
312
|
}
|
|
365
|
-
|
|
366
313
|
var {
|
|
367
314
|
status,
|
|
368
315
|
message
|
|
@@ -389,25 +336,29 @@ class Router {
|
|
|
389
336
|
}
|
|
390
337
|
})();
|
|
391
338
|
}
|
|
392
|
-
|
|
393
339
|
constructor(routes) {
|
|
394
|
-
_defineProperty(this, "
|
|
395
|
-
|
|
340
|
+
_defineProperty(this, "old_add", route => new Router([...this.routes, route]));
|
|
341
|
+
_defineProperty(this, "add", (method, path, route) => {
|
|
342
|
+
var merged = _objectSpread2(_objectSpread2({}, typeof route === 'object' ? route : {
|
|
343
|
+
run: route
|
|
344
|
+
}), {}, {
|
|
345
|
+
method,
|
|
346
|
+
path
|
|
347
|
+
});
|
|
348
|
+
return new Router([...this.routes, merged]);
|
|
349
|
+
});
|
|
396
350
|
_defineProperty(this, "merge", (pathPrefix, other) => {
|
|
397
351
|
var newRoutes = other.routes.map(route => _objectSpread2(_objectSpread2({}, route), {}, {
|
|
398
352
|
path: "".concat(pathPrefix).concat(route.path)
|
|
399
353
|
}));
|
|
400
354
|
return new Router([...this.routes, ...newRoutes]);
|
|
401
355
|
});
|
|
402
|
-
|
|
403
356
|
_defineProperty(this, "toFindMyWay", server => {
|
|
404
357
|
var instance = fmw__default["default"]({
|
|
405
358
|
ignoreTrailingSlash: true,
|
|
406
|
-
|
|
407
359
|
defaultRoute(req, serverResponse) {
|
|
408
360
|
return _asyncToGenerator(function* () {
|
|
409
361
|
var _req$url;
|
|
410
|
-
|
|
411
362
|
var res = new KaitoResponse(serverResponse);
|
|
412
363
|
var message = "Cannot ".concat(req.method, " ").concat((_req$url = req.url) !== null && _req$url !== void 0 ? _req$url : '/');
|
|
413
364
|
res.status(404).json({
|
|
@@ -424,9 +375,7 @@ class Router {
|
|
|
424
375
|
};
|
|
425
376
|
})();
|
|
426
377
|
}
|
|
427
|
-
|
|
428
378
|
});
|
|
429
|
-
|
|
430
379
|
var _loop = function _loop(route) {
|
|
431
380
|
var handler = /*#__PURE__*/function () {
|
|
432
381
|
var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
|
|
@@ -438,85 +387,72 @@ class Router {
|
|
|
438
387
|
res
|
|
439
388
|
});
|
|
440
389
|
});
|
|
441
|
-
|
|
442
390
|
return function handler(_x, _x2, _x3) {
|
|
443
391
|
return _ref.apply(this, arguments);
|
|
444
392
|
};
|
|
445
393
|
}();
|
|
446
|
-
|
|
447
394
|
if (route.method === '*') {
|
|
448
395
|
instance.all(route.path, handler);
|
|
449
396
|
return "continue";
|
|
450
397
|
}
|
|
451
|
-
|
|
452
398
|
instance.on(route.method, route.path, handler);
|
|
453
399
|
};
|
|
454
|
-
|
|
455
400
|
for (var route of this.routes) {
|
|
456
401
|
var _ret = _loop(route);
|
|
457
|
-
|
|
458
402
|
if (_ret === "continue") continue;
|
|
459
403
|
}
|
|
460
|
-
|
|
461
404
|
return instance;
|
|
462
405
|
});
|
|
463
|
-
|
|
464
406
|
this.routes = routes;
|
|
465
407
|
}
|
|
466
408
|
|
|
409
|
+
/**
|
|
410
|
+
* Adds a new route to the router
|
|
411
|
+
* @param route The route specification to add to this router
|
|
412
|
+
* @returns A new router with this route added
|
|
413
|
+
* @deprecated Use `Router#add` instead
|
|
414
|
+
*/
|
|
467
415
|
}
|
|
468
|
-
|
|
469
416
|
_defineProperty(Router, "create", () => new Router([]));
|
|
470
417
|
|
|
471
418
|
function createFMWServer(config) {
|
|
472
419
|
var _config$rawRoutes;
|
|
473
|
-
|
|
474
420
|
var fmw = config.router.toFindMyWay(config);
|
|
475
421
|
var rawRoutes = (_config$rawRoutes = config.rawRoutes) !== null && _config$rawRoutes !== void 0 ? _config$rawRoutes : {};
|
|
476
|
-
|
|
477
422
|
for (var method in rawRoutes) {
|
|
478
423
|
if (!Object.prototype.hasOwnProperty.call(rawRoutes, method)) {
|
|
479
424
|
continue;
|
|
480
425
|
}
|
|
481
|
-
|
|
482
426
|
var routes = rawRoutes[method];
|
|
483
|
-
|
|
484
427
|
if (!routes || routes.length === 0) {
|
|
485
428
|
continue;
|
|
486
429
|
}
|
|
487
|
-
|
|
488
430
|
for (var route of routes) {
|
|
489
431
|
if (method === '*') {
|
|
490
432
|
fmw.all(route.path, route.handler);
|
|
491
433
|
continue;
|
|
492
434
|
}
|
|
493
|
-
|
|
494
435
|
fmw[method.toLowerCase()](route.path, route.handler);
|
|
495
436
|
}
|
|
496
437
|
}
|
|
497
|
-
|
|
498
438
|
var server = http__namespace.createServer( /*#__PURE__*/function () {
|
|
499
439
|
var _ref = _asyncToGenerator(function* (req, res) {
|
|
500
440
|
var before;
|
|
501
|
-
|
|
502
441
|
if (config.before) {
|
|
503
442
|
before = yield config.before(req, res);
|
|
504
443
|
} else {
|
|
505
444
|
before = undefined;
|
|
506
|
-
}
|
|
507
|
-
|
|
445
|
+
}
|
|
508
446
|
|
|
447
|
+
// If the user has sent a response (e.g. replying to CORS), we don't want to do anything else.
|
|
509
448
|
if (res.headersSent) {
|
|
510
449
|
return;
|
|
511
450
|
}
|
|
512
|
-
|
|
513
451
|
var result = yield fmw.lookup(req, res);
|
|
514
|
-
|
|
515
452
|
if ('after' in config && config.after) {
|
|
516
453
|
yield config.after(before, result);
|
|
517
454
|
}
|
|
518
455
|
});
|
|
519
|
-
|
|
520
456
|
return function (_x, _x2) {
|
|
521
457
|
return _ref.apply(this, arguments);
|
|
522
458
|
};
|