@kaito-http/core 2.6.0 → 2.7.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.
- package/dist/declarations/src/res.d.ts +4 -4
- package/dist/declarations/src/route.d.ts +4 -4
- 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
|
@@ -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,27 +114,22 @@ 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
|
-
|
|
149
133
|
return null;
|
|
150
134
|
}
|
|
151
135
|
}
|
|
@@ -156,105 +140,91 @@ function _getBody() {
|
|
|
156
140
|
class KaitoRequest {
|
|
157
141
|
constructor(raw) {
|
|
158
142
|
_defineProperty(this, "_url", null);
|
|
159
|
-
|
|
160
143
|
this.raw = raw;
|
|
161
144
|
}
|
|
145
|
+
|
|
162
146
|
/**
|
|
163
147
|
* The full URL of the request, including the protocol, hostname, and path.
|
|
164
148
|
* Note: does not include the query string or hash
|
|
165
149
|
*/
|
|
166
|
-
|
|
167
|
-
|
|
168
150
|
get fullURL() {
|
|
169
151
|
var _this$raw$url;
|
|
170
|
-
|
|
171
152
|
return "".concat(this.protocol, "://").concat(this.hostname).concat((_this$raw$url = this.raw.url) !== null && _this$raw$url !== void 0 ? _this$raw$url : '');
|
|
172
153
|
}
|
|
154
|
+
|
|
173
155
|
/**
|
|
174
156
|
* A new URL instance for the full URL of the request.
|
|
175
157
|
*/
|
|
176
|
-
|
|
177
|
-
|
|
178
158
|
get url() {
|
|
179
159
|
if (this._url) {
|
|
180
160
|
return this._url;
|
|
181
161
|
}
|
|
182
|
-
|
|
183
162
|
this._url = new URL(this.fullURL);
|
|
184
163
|
return this._url;
|
|
185
164
|
}
|
|
165
|
+
|
|
186
166
|
/**
|
|
187
167
|
* The HTTP method of the request.
|
|
188
168
|
*/
|
|
189
|
-
|
|
190
|
-
|
|
191
169
|
get method() {
|
|
192
170
|
if (!this.raw.method) {
|
|
193
171
|
throw new Error('Request method is not defined, somehow...');
|
|
194
172
|
}
|
|
195
|
-
|
|
196
173
|
return this.raw.method;
|
|
197
174
|
}
|
|
175
|
+
|
|
198
176
|
/**
|
|
199
177
|
* The protocol of the request, either `http` or `https`.
|
|
200
178
|
*/
|
|
201
|
-
|
|
202
|
-
|
|
203
179
|
get protocol() {
|
|
204
180
|
if (this.raw.socket instanceof node_tls.TLSSocket) {
|
|
205
181
|
return this.raw.socket.encrypted ? 'https' : 'http';
|
|
206
182
|
}
|
|
207
|
-
|
|
208
183
|
return 'http';
|
|
209
184
|
}
|
|
185
|
+
|
|
210
186
|
/**
|
|
211
187
|
* The request headers
|
|
212
188
|
*/
|
|
213
|
-
|
|
214
|
-
|
|
215
189
|
get headers() {
|
|
216
190
|
return this.raw.headers;
|
|
217
191
|
}
|
|
192
|
+
|
|
218
193
|
/**
|
|
219
194
|
* The hostname of the request.
|
|
220
195
|
*/
|
|
221
|
-
|
|
222
|
-
|
|
223
196
|
get hostname() {
|
|
224
197
|
var _this$raw$headers$hos, _this$raw$headers$Au;
|
|
225
|
-
|
|
226
198
|
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 : []);
|
|
227
199
|
}
|
|
228
|
-
|
|
229
200
|
}
|
|
230
201
|
|
|
231
202
|
class KaitoResponse {
|
|
232
203
|
constructor(raw) {
|
|
233
204
|
this.raw = raw;
|
|
234
205
|
}
|
|
206
|
+
|
|
235
207
|
/**
|
|
236
208
|
* Send a response
|
|
237
209
|
* @param key The key of the header
|
|
238
210
|
* @param value The value of the header
|
|
239
211
|
* @returns The response object
|
|
240
212
|
*/
|
|
241
|
-
|
|
242
|
-
|
|
243
213
|
header(key, value) {
|
|
244
214
|
this.raw.setHeader(key, value);
|
|
245
215
|
return this;
|
|
246
216
|
}
|
|
217
|
+
|
|
247
218
|
/**
|
|
248
219
|
* Set the status code of the response
|
|
249
220
|
* @param code The status code
|
|
250
221
|
* @returns The response object
|
|
251
222
|
*/
|
|
252
|
-
|
|
253
|
-
|
|
254
223
|
status(code) {
|
|
255
224
|
this.raw.statusCode = code;
|
|
256
225
|
return this;
|
|
257
226
|
}
|
|
227
|
+
|
|
258
228
|
/**
|
|
259
229
|
* Set a cookie
|
|
260
230
|
* @param name The name of the cookie
|
|
@@ -262,19 +232,16 @@ class KaitoResponse {
|
|
|
262
232
|
* @param options The options for the cookie
|
|
263
233
|
* @returns The response object
|
|
264
234
|
*/
|
|
265
|
-
|
|
266
|
-
|
|
267
235
|
cookie(name, value, options) {
|
|
268
236
|
this.raw.setHeader('Set-Cookie', cookie.serialize(name, value, options));
|
|
269
237
|
return this;
|
|
270
238
|
}
|
|
239
|
+
|
|
271
240
|
/**
|
|
272
241
|
* Send a JSON APIResponse body
|
|
273
242
|
* @param data The data to send
|
|
274
243
|
* @returns The response object
|
|
275
244
|
*/
|
|
276
|
-
|
|
277
|
-
|
|
278
245
|
json(data) {
|
|
279
246
|
var json = JSON.stringify(data);
|
|
280
247
|
this.raw.setHeader('Content-Type', 'application/json');
|
|
@@ -282,44 +249,27 @@ class KaitoResponse {
|
|
|
282
249
|
this.raw.end(json);
|
|
283
250
|
return this;
|
|
284
251
|
}
|
|
285
|
-
|
|
286
252
|
}
|
|
287
253
|
|
|
288
254
|
function ownKeys(object, enumerableOnly) {
|
|
289
255
|
var keys = Object.keys(object);
|
|
290
|
-
|
|
291
256
|
if (Object.getOwnPropertySymbols) {
|
|
292
257
|
var symbols = Object.getOwnPropertySymbols(object);
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
keys.push.apply(keys, symbols);
|
|
258
|
+
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
259
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
260
|
+
})), keys.push.apply(keys, symbols);
|
|
301
261
|
}
|
|
302
|
-
|
|
303
262
|
return keys;
|
|
304
263
|
}
|
|
305
|
-
|
|
306
264
|
function _objectSpread2(target) {
|
|
307
265
|
for (var i = 1; i < arguments.length; i++) {
|
|
308
|
-
var source = arguments[i]
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
} else if (Object.getOwnPropertyDescriptors) {
|
|
315
|
-
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
316
|
-
} else {
|
|
317
|
-
ownKeys(Object(source)).forEach(function (key) {
|
|
318
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
319
|
-
});
|
|
320
|
-
}
|
|
266
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
|
267
|
+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
|
268
|
+
_defineProperty(target, key, source[key]);
|
|
269
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
|
270
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
271
|
+
});
|
|
321
272
|
}
|
|
322
|
-
|
|
323
273
|
return target;
|
|
324
274
|
}
|
|
325
275
|
|
|
@@ -328,7 +278,6 @@ class Router {
|
|
|
328
278
|
return _asyncToGenerator(function* () {
|
|
329
279
|
try {
|
|
330
280
|
var _yield$route$body$par, _route$body;
|
|
331
|
-
|
|
332
281
|
var ctx = yield server.getContext(options.req, options.res);
|
|
333
282
|
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;
|
|
334
283
|
var query = route.query ? zod.z.object(route.query).parse(Object.fromEntries(options.req.url.searchParams.entries())) : {};
|
|
@@ -349,7 +298,6 @@ class Router {
|
|
|
349
298
|
};
|
|
350
299
|
} catch (e) {
|
|
351
300
|
var error = WrappedError.maybe(e);
|
|
352
|
-
|
|
353
301
|
if (error instanceof KaitoError) {
|
|
354
302
|
options.res.status(error.status).json({
|
|
355
303
|
success: false,
|
|
@@ -358,7 +306,6 @@ class Router {
|
|
|
358
306
|
});
|
|
359
307
|
return;
|
|
360
308
|
}
|
|
361
|
-
|
|
362
309
|
var {
|
|
363
310
|
status,
|
|
364
311
|
message
|
|
@@ -385,25 +332,29 @@ class Router {
|
|
|
385
332
|
}
|
|
386
333
|
})();
|
|
387
334
|
}
|
|
388
|
-
|
|
389
335
|
constructor(routes) {
|
|
390
|
-
_defineProperty(this, "
|
|
391
|
-
|
|
336
|
+
_defineProperty(this, "old_add", route => new Router([...this.routes, route]));
|
|
337
|
+
_defineProperty(this, "add", (method, path, route) => {
|
|
338
|
+
var merged = _objectSpread2(_objectSpread2({}, typeof route === 'object' ? route : {
|
|
339
|
+
run: route
|
|
340
|
+
}), {}, {
|
|
341
|
+
method,
|
|
342
|
+
path
|
|
343
|
+
});
|
|
344
|
+
return new Router([...this.routes, merged]);
|
|
345
|
+
});
|
|
392
346
|
_defineProperty(this, "merge", (pathPrefix, other) => {
|
|
393
347
|
var newRoutes = other.routes.map(route => _objectSpread2(_objectSpread2({}, route), {}, {
|
|
394
348
|
path: "".concat(pathPrefix).concat(route.path)
|
|
395
349
|
}));
|
|
396
350
|
return new Router([...this.routes, ...newRoutes]);
|
|
397
351
|
});
|
|
398
|
-
|
|
399
352
|
_defineProperty(this, "toFindMyWay", server => {
|
|
400
353
|
var instance = fmw__default["default"]({
|
|
401
354
|
ignoreTrailingSlash: true,
|
|
402
|
-
|
|
403
355
|
defaultRoute(req, serverResponse) {
|
|
404
356
|
return _asyncToGenerator(function* () {
|
|
405
357
|
var _req$url;
|
|
406
|
-
|
|
407
358
|
var res = new KaitoResponse(serverResponse);
|
|
408
359
|
var message = "Cannot ".concat(req.method, " ").concat((_req$url = req.url) !== null && _req$url !== void 0 ? _req$url : '/');
|
|
409
360
|
res.status(404).json({
|
|
@@ -420,9 +371,7 @@ class Router {
|
|
|
420
371
|
};
|
|
421
372
|
})();
|
|
422
373
|
}
|
|
423
|
-
|
|
424
374
|
});
|
|
425
|
-
|
|
426
375
|
var _loop = function _loop(route) {
|
|
427
376
|
var handler = /*#__PURE__*/function () {
|
|
428
377
|
var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
|
|
@@ -434,85 +383,72 @@ class Router {
|
|
|
434
383
|
res
|
|
435
384
|
});
|
|
436
385
|
});
|
|
437
|
-
|
|
438
386
|
return function handler(_x, _x2, _x3) {
|
|
439
387
|
return _ref.apply(this, arguments);
|
|
440
388
|
};
|
|
441
389
|
}();
|
|
442
|
-
|
|
443
390
|
if (route.method === '*') {
|
|
444
391
|
instance.all(route.path, handler);
|
|
445
392
|
return "continue";
|
|
446
393
|
}
|
|
447
|
-
|
|
448
394
|
instance.on(route.method, route.path, handler);
|
|
449
395
|
};
|
|
450
|
-
|
|
451
396
|
for (var route of this.routes) {
|
|
452
397
|
var _ret = _loop(route);
|
|
453
|
-
|
|
454
398
|
if (_ret === "continue") continue;
|
|
455
399
|
}
|
|
456
|
-
|
|
457
400
|
return instance;
|
|
458
401
|
});
|
|
459
|
-
|
|
460
402
|
this.routes = routes;
|
|
461
403
|
}
|
|
462
404
|
|
|
405
|
+
/**
|
|
406
|
+
* Adds a new route to the router
|
|
407
|
+
* @param route The route specification to add to this router
|
|
408
|
+
* @returns A new router with this route added
|
|
409
|
+
* @deprecated Use `Router#add` instead
|
|
410
|
+
*/
|
|
463
411
|
}
|
|
464
|
-
|
|
465
412
|
_defineProperty(Router, "create", () => new Router([]));
|
|
466
413
|
|
|
467
414
|
function createFMWServer(config) {
|
|
468
415
|
var _config$rawRoutes;
|
|
469
|
-
|
|
470
416
|
var fmw = config.router.toFindMyWay(config);
|
|
471
417
|
var rawRoutes = (_config$rawRoutes = config.rawRoutes) !== null && _config$rawRoutes !== void 0 ? _config$rawRoutes : {};
|
|
472
|
-
|
|
473
418
|
for (var method in rawRoutes) {
|
|
474
419
|
if (!Object.prototype.hasOwnProperty.call(rawRoutes, method)) {
|
|
475
420
|
continue;
|
|
476
421
|
}
|
|
477
|
-
|
|
478
422
|
var routes = rawRoutes[method];
|
|
479
|
-
|
|
480
423
|
if (!routes || routes.length === 0) {
|
|
481
424
|
continue;
|
|
482
425
|
}
|
|
483
|
-
|
|
484
426
|
for (var route of routes) {
|
|
485
427
|
if (method === '*') {
|
|
486
428
|
fmw.all(route.path, route.handler);
|
|
487
429
|
continue;
|
|
488
430
|
}
|
|
489
|
-
|
|
490
431
|
fmw[method.toLowerCase()](route.path, route.handler);
|
|
491
432
|
}
|
|
492
433
|
}
|
|
493
|
-
|
|
494
434
|
var server = http__namespace.createServer( /*#__PURE__*/function () {
|
|
495
435
|
var _ref = _asyncToGenerator(function* (req, res) {
|
|
496
436
|
var before;
|
|
497
|
-
|
|
498
437
|
if (config.before) {
|
|
499
438
|
before = yield config.before(req, res);
|
|
500
439
|
} else {
|
|
501
440
|
before = undefined;
|
|
502
|
-
}
|
|
503
|
-
|
|
441
|
+
}
|
|
504
442
|
|
|
443
|
+
// If the user has sent a response (e.g. replying to CORS), we don't want to do anything else.
|
|
505
444
|
if (res.headersSent) {
|
|
506
445
|
return;
|
|
507
446
|
}
|
|
508
|
-
|
|
509
447
|
var result = yield fmw.lookup(req, res);
|
|
510
|
-
|
|
511
448
|
if ('after' in config && config.after) {
|
|
512
449
|
yield config.after(before, result);
|
|
513
450
|
}
|
|
514
451
|
});
|
|
515
|
-
|
|
516
452
|
return function (_x, _x2) {
|
|
517
453
|
return _ref.apply(this, arguments);
|
|
518
454
|
};
|