@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
|
@@ -13,26 +13,21 @@ class WrappedError extends Error {
|
|
|
13
13
|
if (maybeError instanceof Error) {
|
|
14
14
|
return maybeError;
|
|
15
15
|
}
|
|
16
|
-
|
|
17
16
|
return WrappedError.from(maybeError);
|
|
18
17
|
}
|
|
19
|
-
|
|
20
18
|
static from(data) {
|
|
21
19
|
return new WrappedError(data);
|
|
22
20
|
}
|
|
23
|
-
|
|
24
21
|
constructor(data) {
|
|
25
22
|
super('Something was thrown, but it was not an instance of Error, so a WrappedError was created.');
|
|
26
23
|
this.data = data;
|
|
27
24
|
}
|
|
28
|
-
|
|
29
25
|
}
|
|
30
26
|
class KaitoError extends Error {
|
|
31
27
|
constructor(status, message) {
|
|
32
28
|
super(message);
|
|
33
29
|
this.status = status;
|
|
34
30
|
}
|
|
35
|
-
|
|
36
31
|
}
|
|
37
32
|
|
|
38
33
|
function _defineProperty(obj, key, value) {
|
|
@@ -46,7 +41,6 @@ function _defineProperty(obj, key, value) {
|
|
|
46
41
|
} else {
|
|
47
42
|
obj[key] = value;
|
|
48
43
|
}
|
|
49
|
-
|
|
50
44
|
return obj;
|
|
51
45
|
}
|
|
52
46
|
|
|
@@ -58,29 +52,24 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
|
58
52
|
reject(error);
|
|
59
53
|
return;
|
|
60
54
|
}
|
|
61
|
-
|
|
62
55
|
if (info.done) {
|
|
63
56
|
resolve(value);
|
|
64
57
|
} else {
|
|
65
58
|
Promise.resolve(value).then(_next, _throw);
|
|
66
59
|
}
|
|
67
60
|
}
|
|
68
|
-
|
|
69
61
|
function _asyncToGenerator(fn) {
|
|
70
62
|
return function () {
|
|
71
63
|
var self = this,
|
|
72
|
-
|
|
64
|
+
args = arguments;
|
|
73
65
|
return new Promise(function (resolve, reject) {
|
|
74
66
|
var gen = fn.apply(self, args);
|
|
75
|
-
|
|
76
67
|
function _next(value) {
|
|
77
68
|
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
78
69
|
}
|
|
79
|
-
|
|
80
70
|
function _throw(err) {
|
|
81
71
|
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
82
72
|
}
|
|
83
|
-
|
|
84
73
|
_next(undefined);
|
|
85
74
|
});
|
|
86
75
|
};
|
|
@@ -97,31 +86,26 @@ function getLastEntryInMultiHeaderValue(headerValue) {
|
|
|
97
86
|
function getBody(_x) {
|
|
98
87
|
return _getBody.apply(this, arguments);
|
|
99
88
|
}
|
|
100
|
-
|
|
101
89
|
function _getBody() {
|
|
102
90
|
_getBody = _asyncToGenerator(function* (req) {
|
|
103
91
|
if (!req.headers['content-type']) {
|
|
104
92
|
return null;
|
|
105
93
|
}
|
|
106
|
-
|
|
107
94
|
var buffer = yield getRawBody(req.raw);
|
|
108
95
|
var {
|
|
109
96
|
type
|
|
110
97
|
} = parse(req.headers['content-type']);
|
|
111
|
-
|
|
112
98
|
switch (type) {
|
|
113
99
|
case 'application/json':
|
|
114
100
|
{
|
|
115
101
|
return json(Readable.from(buffer));
|
|
116
102
|
}
|
|
117
|
-
|
|
118
103
|
default:
|
|
119
104
|
{
|
|
120
105
|
if (process.env.NODE_ENV === 'development') {
|
|
121
106
|
console.warn('[kaito] Unsupported content type:', type);
|
|
122
107
|
console.warn('[kaito] This message is only shown in development mode.');
|
|
123
108
|
}
|
|
124
|
-
|
|
125
109
|
return null;
|
|
126
110
|
}
|
|
127
111
|
}
|
|
@@ -132,105 +116,91 @@ function _getBody() {
|
|
|
132
116
|
class KaitoRequest {
|
|
133
117
|
constructor(raw) {
|
|
134
118
|
_defineProperty(this, "_url", null);
|
|
135
|
-
|
|
136
119
|
this.raw = raw;
|
|
137
120
|
}
|
|
121
|
+
|
|
138
122
|
/**
|
|
139
123
|
* The full URL of the request, including the protocol, hostname, and path.
|
|
140
124
|
* Note: does not include the query string or hash
|
|
141
125
|
*/
|
|
142
|
-
|
|
143
|
-
|
|
144
126
|
get fullURL() {
|
|
145
127
|
var _this$raw$url;
|
|
146
|
-
|
|
147
128
|
return "".concat(this.protocol, "://").concat(this.hostname).concat((_this$raw$url = this.raw.url) !== null && _this$raw$url !== void 0 ? _this$raw$url : '');
|
|
148
129
|
}
|
|
130
|
+
|
|
149
131
|
/**
|
|
150
132
|
* A new URL instance for the full URL of the request.
|
|
151
133
|
*/
|
|
152
|
-
|
|
153
|
-
|
|
154
134
|
get url() {
|
|
155
135
|
if (this._url) {
|
|
156
136
|
return this._url;
|
|
157
137
|
}
|
|
158
|
-
|
|
159
138
|
this._url = new URL(this.fullURL);
|
|
160
139
|
return this._url;
|
|
161
140
|
}
|
|
141
|
+
|
|
162
142
|
/**
|
|
163
143
|
* The HTTP method of the request.
|
|
164
144
|
*/
|
|
165
|
-
|
|
166
|
-
|
|
167
145
|
get method() {
|
|
168
146
|
if (!this.raw.method) {
|
|
169
147
|
throw new Error('Request method is not defined, somehow...');
|
|
170
148
|
}
|
|
171
|
-
|
|
172
149
|
return this.raw.method;
|
|
173
150
|
}
|
|
151
|
+
|
|
174
152
|
/**
|
|
175
153
|
* The protocol of the request, either `http` or `https`.
|
|
176
154
|
*/
|
|
177
|
-
|
|
178
|
-
|
|
179
155
|
get protocol() {
|
|
180
156
|
if (this.raw.socket instanceof TLSSocket) {
|
|
181
157
|
return this.raw.socket.encrypted ? 'https' : 'http';
|
|
182
158
|
}
|
|
183
|
-
|
|
184
159
|
return 'http';
|
|
185
160
|
}
|
|
161
|
+
|
|
186
162
|
/**
|
|
187
163
|
* The request headers
|
|
188
164
|
*/
|
|
189
|
-
|
|
190
|
-
|
|
191
165
|
get headers() {
|
|
192
166
|
return this.raw.headers;
|
|
193
167
|
}
|
|
168
|
+
|
|
194
169
|
/**
|
|
195
170
|
* The hostname of the request.
|
|
196
171
|
*/
|
|
197
|
-
|
|
198
|
-
|
|
199
172
|
get hostname() {
|
|
200
173
|
var _this$raw$headers$hos, _this$raw$headers$Au;
|
|
201
|
-
|
|
202
174
|
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 : []);
|
|
203
175
|
}
|
|
204
|
-
|
|
205
176
|
}
|
|
206
177
|
|
|
207
178
|
class KaitoResponse {
|
|
208
179
|
constructor(raw) {
|
|
209
180
|
this.raw = raw;
|
|
210
181
|
}
|
|
182
|
+
|
|
211
183
|
/**
|
|
212
184
|
* Send a response
|
|
213
185
|
* @param key The key of the header
|
|
214
186
|
* @param value The value of the header
|
|
215
187
|
* @returns The response object
|
|
216
188
|
*/
|
|
217
|
-
|
|
218
|
-
|
|
219
189
|
header(key, value) {
|
|
220
190
|
this.raw.setHeader(key, value);
|
|
221
191
|
return this;
|
|
222
192
|
}
|
|
193
|
+
|
|
223
194
|
/**
|
|
224
195
|
* Set the status code of the response
|
|
225
196
|
* @param code The status code
|
|
226
197
|
* @returns The response object
|
|
227
198
|
*/
|
|
228
|
-
|
|
229
|
-
|
|
230
199
|
status(code) {
|
|
231
200
|
this.raw.statusCode = code;
|
|
232
201
|
return this;
|
|
233
202
|
}
|
|
203
|
+
|
|
234
204
|
/**
|
|
235
205
|
* Set a cookie
|
|
236
206
|
* @param name The name of the cookie
|
|
@@ -238,19 +208,16 @@ class KaitoResponse {
|
|
|
238
208
|
* @param options The options for the cookie
|
|
239
209
|
* @returns The response object
|
|
240
210
|
*/
|
|
241
|
-
|
|
242
|
-
|
|
243
211
|
cookie(name, value, options) {
|
|
244
212
|
this.raw.setHeader('Set-Cookie', serialize(name, value, options));
|
|
245
213
|
return this;
|
|
246
214
|
}
|
|
215
|
+
|
|
247
216
|
/**
|
|
248
217
|
* Send a JSON APIResponse body
|
|
249
218
|
* @param data The data to send
|
|
250
219
|
* @returns The response object
|
|
251
220
|
*/
|
|
252
|
-
|
|
253
|
-
|
|
254
221
|
json(data) {
|
|
255
222
|
var json = JSON.stringify(data);
|
|
256
223
|
this.raw.setHeader('Content-Type', 'application/json');
|
|
@@ -258,44 +225,27 @@ class KaitoResponse {
|
|
|
258
225
|
this.raw.end(json);
|
|
259
226
|
return this;
|
|
260
227
|
}
|
|
261
|
-
|
|
262
228
|
}
|
|
263
229
|
|
|
264
230
|
function ownKeys(object, enumerableOnly) {
|
|
265
231
|
var keys = Object.keys(object);
|
|
266
|
-
|
|
267
232
|
if (Object.getOwnPropertySymbols) {
|
|
268
233
|
var symbols = Object.getOwnPropertySymbols(object);
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
keys.push.apply(keys, symbols);
|
|
234
|
+
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
235
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
236
|
+
})), keys.push.apply(keys, symbols);
|
|
277
237
|
}
|
|
278
|
-
|
|
279
238
|
return keys;
|
|
280
239
|
}
|
|
281
|
-
|
|
282
240
|
function _objectSpread2(target) {
|
|
283
241
|
for (var i = 1; i < arguments.length; i++) {
|
|
284
|
-
var source = arguments[i]
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
} else if (Object.getOwnPropertyDescriptors) {
|
|
291
|
-
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
292
|
-
} else {
|
|
293
|
-
ownKeys(Object(source)).forEach(function (key) {
|
|
294
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
295
|
-
});
|
|
296
|
-
}
|
|
242
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
|
243
|
+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
|
244
|
+
_defineProperty(target, key, source[key]);
|
|
245
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
|
246
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
247
|
+
});
|
|
297
248
|
}
|
|
298
|
-
|
|
299
249
|
return target;
|
|
300
250
|
}
|
|
301
251
|
|
|
@@ -304,7 +254,6 @@ class Router {
|
|
|
304
254
|
return _asyncToGenerator(function* () {
|
|
305
255
|
try {
|
|
306
256
|
var _yield$route$body$par, _route$body;
|
|
307
|
-
|
|
308
257
|
var ctx = yield server.getContext(options.req, options.res);
|
|
309
258
|
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;
|
|
310
259
|
var query = route.query ? z.object(route.query).parse(Object.fromEntries(options.req.url.searchParams.entries())) : {};
|
|
@@ -325,7 +274,6 @@ class Router {
|
|
|
325
274
|
};
|
|
326
275
|
} catch (e) {
|
|
327
276
|
var error = WrappedError.maybe(e);
|
|
328
|
-
|
|
329
277
|
if (error instanceof KaitoError) {
|
|
330
278
|
options.res.status(error.status).json({
|
|
331
279
|
success: false,
|
|
@@ -334,7 +282,6 @@ class Router {
|
|
|
334
282
|
});
|
|
335
283
|
return;
|
|
336
284
|
}
|
|
337
|
-
|
|
338
285
|
var {
|
|
339
286
|
status,
|
|
340
287
|
message
|
|
@@ -361,25 +308,29 @@ class Router {
|
|
|
361
308
|
}
|
|
362
309
|
})();
|
|
363
310
|
}
|
|
364
|
-
|
|
365
311
|
constructor(routes) {
|
|
366
|
-
_defineProperty(this, "
|
|
367
|
-
|
|
312
|
+
_defineProperty(this, "old_add", route => new Router([...this.routes, route]));
|
|
313
|
+
_defineProperty(this, "add", (method, path, route) => {
|
|
314
|
+
var merged = _objectSpread2(_objectSpread2({}, typeof route === 'object' ? route : {
|
|
315
|
+
run: route
|
|
316
|
+
}), {}, {
|
|
317
|
+
method,
|
|
318
|
+
path
|
|
319
|
+
});
|
|
320
|
+
return new Router([...this.routes, merged]);
|
|
321
|
+
});
|
|
368
322
|
_defineProperty(this, "merge", (pathPrefix, other) => {
|
|
369
323
|
var newRoutes = other.routes.map(route => _objectSpread2(_objectSpread2({}, route), {}, {
|
|
370
324
|
path: "".concat(pathPrefix).concat(route.path)
|
|
371
325
|
}));
|
|
372
326
|
return new Router([...this.routes, ...newRoutes]);
|
|
373
327
|
});
|
|
374
|
-
|
|
375
328
|
_defineProperty(this, "toFindMyWay", server => {
|
|
376
329
|
var instance = fmw({
|
|
377
330
|
ignoreTrailingSlash: true,
|
|
378
|
-
|
|
379
331
|
defaultRoute(req, serverResponse) {
|
|
380
332
|
return _asyncToGenerator(function* () {
|
|
381
333
|
var _req$url;
|
|
382
|
-
|
|
383
334
|
var res = new KaitoResponse(serverResponse);
|
|
384
335
|
var message = "Cannot ".concat(req.method, " ").concat((_req$url = req.url) !== null && _req$url !== void 0 ? _req$url : '/');
|
|
385
336
|
res.status(404).json({
|
|
@@ -396,9 +347,7 @@ class Router {
|
|
|
396
347
|
};
|
|
397
348
|
})();
|
|
398
349
|
}
|
|
399
|
-
|
|
400
350
|
});
|
|
401
|
-
|
|
402
351
|
var _loop = function _loop(route) {
|
|
403
352
|
var handler = /*#__PURE__*/function () {
|
|
404
353
|
var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
|
|
@@ -410,85 +359,72 @@ class Router {
|
|
|
410
359
|
res
|
|
411
360
|
});
|
|
412
361
|
});
|
|
413
|
-
|
|
414
362
|
return function handler(_x, _x2, _x3) {
|
|
415
363
|
return _ref.apply(this, arguments);
|
|
416
364
|
};
|
|
417
365
|
}();
|
|
418
|
-
|
|
419
366
|
if (route.method === '*') {
|
|
420
367
|
instance.all(route.path, handler);
|
|
421
368
|
return "continue";
|
|
422
369
|
}
|
|
423
|
-
|
|
424
370
|
instance.on(route.method, route.path, handler);
|
|
425
371
|
};
|
|
426
|
-
|
|
427
372
|
for (var route of this.routes) {
|
|
428
373
|
var _ret = _loop(route);
|
|
429
|
-
|
|
430
374
|
if (_ret === "continue") continue;
|
|
431
375
|
}
|
|
432
|
-
|
|
433
376
|
return instance;
|
|
434
377
|
});
|
|
435
|
-
|
|
436
378
|
this.routes = routes;
|
|
437
379
|
}
|
|
438
380
|
|
|
381
|
+
/**
|
|
382
|
+
* Adds a new route to the router
|
|
383
|
+
* @param route The route specification to add to this router
|
|
384
|
+
* @returns A new router with this route added
|
|
385
|
+
* @deprecated Use `Router#add` instead
|
|
386
|
+
*/
|
|
439
387
|
}
|
|
440
|
-
|
|
441
388
|
_defineProperty(Router, "create", () => new Router([]));
|
|
442
389
|
|
|
443
390
|
function createFMWServer(config) {
|
|
444
391
|
var _config$rawRoutes;
|
|
445
|
-
|
|
446
392
|
var fmw = config.router.toFindMyWay(config);
|
|
447
393
|
var rawRoutes = (_config$rawRoutes = config.rawRoutes) !== null && _config$rawRoutes !== void 0 ? _config$rawRoutes : {};
|
|
448
|
-
|
|
449
394
|
for (var method in rawRoutes) {
|
|
450
395
|
if (!Object.prototype.hasOwnProperty.call(rawRoutes, method)) {
|
|
451
396
|
continue;
|
|
452
397
|
}
|
|
453
|
-
|
|
454
398
|
var routes = rawRoutes[method];
|
|
455
|
-
|
|
456
399
|
if (!routes || routes.length === 0) {
|
|
457
400
|
continue;
|
|
458
401
|
}
|
|
459
|
-
|
|
460
402
|
for (var route of routes) {
|
|
461
403
|
if (method === '*') {
|
|
462
404
|
fmw.all(route.path, route.handler);
|
|
463
405
|
continue;
|
|
464
406
|
}
|
|
465
|
-
|
|
466
407
|
fmw[method.toLowerCase()](route.path, route.handler);
|
|
467
408
|
}
|
|
468
409
|
}
|
|
469
|
-
|
|
470
410
|
var server = http.createServer( /*#__PURE__*/function () {
|
|
471
411
|
var _ref = _asyncToGenerator(function* (req, res) {
|
|
472
412
|
var before;
|
|
473
|
-
|
|
474
413
|
if (config.before) {
|
|
475
414
|
before = yield config.before(req, res);
|
|
476
415
|
} else {
|
|
477
416
|
before = undefined;
|
|
478
|
-
}
|
|
479
|
-
|
|
417
|
+
}
|
|
480
418
|
|
|
419
|
+
// If the user has sent a response (e.g. replying to CORS), we don't want to do anything else.
|
|
481
420
|
if (res.headersSent) {
|
|
482
421
|
return;
|
|
483
422
|
}
|
|
484
|
-
|
|
485
423
|
var result = yield fmw.lookup(req, res);
|
|
486
|
-
|
|
487
424
|
if ('after' in config && config.after) {
|
|
488
425
|
yield config.after(before, result);
|
|
489
426
|
}
|
|
490
427
|
});
|
|
491
|
-
|
|
492
428
|
return function (_x, _x2) {
|
|
493
429
|
return _ref.apply(this, arguments);
|
|
494
430
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaito-http/core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.1",
|
|
4
4
|
"description": "Functional HTTP Framework for TypeScript",
|
|
5
5
|
"repository": "https://github.com/kaito-http/kaito",
|
|
6
6
|
"author": "Alistair Smith <hi@alistair.sh>",
|
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"@types/content-type": "^1.1.5",
|
|
13
13
|
"@types/cookie": "^0.5.1",
|
|
14
|
-
"@types/node": "^18.
|
|
15
|
-
"typescript": "4.
|
|
14
|
+
"@types/node": "^18.11.9",
|
|
15
|
+
"typescript": "4.9",
|
|
16
16
|
"zod": "^3.19.1"
|
|
17
17
|
},
|
|
18
18
|
"files": [
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"content-type": "^1.0.4",
|
|
37
37
|
"cookie": "^0.5.0",
|
|
38
|
-
"find-my-way": "^7.1
|
|
38
|
+
"find-my-way": "^7.3.1",
|
|
39
39
|
"raw-body": "^2.5.1"
|
|
40
40
|
}
|
|
41
41
|
}
|