@kaito-http/core 2.5.0 → 2.6.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.
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
- import { HTTPMethod } from 'find-my-way';
3
- import { IncomingMessage } from 'node:http';
2
+ import type { HTTPMethod } from 'find-my-way';
3
+ import type { IncomingMessage } from 'node:http';
4
4
  export declare class KaitoRequest {
5
5
  readonly raw: IncomingMessage;
6
6
  private _url;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
- import { ServerResponse } from 'node:http';
3
- import { CookieSerializeOptions } from 'cookie';
2
+ import type { ServerResponse } from 'node:http';
3
+ import type { CookieSerializeOptions } from 'cookie';
4
4
  export declare type ErroredAPIResponse = {
5
5
  success: false;
6
6
  data: null;
@@ -1,14 +1,17 @@
1
- import { z } from 'zod';
2
- import { ExtractRouteParams, KaitoMethod } from './util';
3
- export declare type RouteArgument<Path extends string, Context, InputOutput> = {
1
+ import type { z } from 'zod';
2
+ import type { ExtractRouteParams, KaitoMethod } from './util';
3
+ export declare type RouteArgument<Path extends string, Context, QueryOutput, BodyOutput> = {
4
4
  ctx: Context;
5
- input: InputOutput;
5
+ body: BodyOutput;
6
+ query: QueryOutput;
6
7
  params: ExtractRouteParams<Path>;
7
8
  };
8
- export declare type Route<Context, Result, Path extends string, Method extends KaitoMethod, InputOutput = never, InputDef extends z.ZodTypeDef = z.ZodTypeDef, InputInput = InputOutput> = {
9
- input?: z.ZodType<InputOutput, InputDef, InputInput>;
9
+ export declare type AnyQueryDefinition = Record<string, z.ZodTypeAny>;
10
+ export declare type Route<Context, Result, Path extends string, Method extends KaitoMethod, Query extends AnyQueryDefinition, BodyOutput, BodyDef extends z.ZodTypeDef, BodyInput> = {
11
+ body?: z.ZodType<BodyOutput, BodyDef, BodyInput>;
12
+ query?: Query;
10
13
  path: Path;
11
14
  method: Method;
12
- run(args: RouteArgument<Path, Context, InputOutput>): Promise<Result>;
15
+ run(args: RouteArgument<Path, Context, z.infer<z.ZodObject<Query>>, BodyOutput>): Promise<Result>;
13
16
  };
14
- export declare type AnyRoute<Context = any> = Route<Context, any, any, any, any, z.ZodTypeDef, any>;
17
+ export declare type AnyRoute<Context = any> = Route<Context, any, any, any, AnyQueryDefinition, any, any, any>;
@@ -1,20 +1,20 @@
1
1
  import fmw from 'find-my-way';
2
2
  import { z } from 'zod';
3
- import { AnyRoute, Route } from './route';
4
- import { ServerConfig } from './server';
5
- import { KaitoMethod } from './util';
3
+ import type { AnyQueryDefinition, AnyRoute, Route } from './route';
4
+ import type { ServerConfig } from './server';
5
+ import type { KaitoMethod } from './util';
6
6
  declare type Routes = readonly AnyRoute[];
7
- declare type RemapRoutePrefix<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer Context, infer Result, infer Path, infer Method, infer InputOutput, infer InputDef, infer InputInput> ? Route<Context, Result, `${Prefix}${Path}`, Method, InputOutput, InputDef, InputInput> : never;
7
+ declare 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
8
  declare type PrefixRoutesPath<Prefix extends `/${string}`, R extends Routes> = R extends [infer First, ...infer Rest] ? [
9
- RemapRoutePrefix<Extract<First, AnyRoute<any>>, Prefix>,
10
- ...PrefixRoutesPath<Prefix, Extract<Rest, readonly AnyRoute<any>[]>>
9
+ RemapRoutePrefix<Extract<First, AnyRoute>, Prefix>,
10
+ ...PrefixRoutesPath<Prefix, Extract<Rest, readonly AnyRoute[]>>
11
11
  ] : [];
12
12
  export declare class Router<Context, R extends Routes> {
13
13
  readonly routes: R;
14
- constructor(routes: R);
15
- private static handle;
16
14
  static create: <Context_1>() => Router<Context_1, []>;
17
- add: <Result, Path extends string, Method extends KaitoMethod, InputOutput = never, InputDef extends z.ZodTypeDef = z.ZodTypeDef, InputInput = InputOutput>(route: Route<Context, Result, Path, Method, InputOutput, InputDef, InputInput>) => Router<Context, [...R, Route<Context, Result, Path, Method, InputOutput, InputDef, InputInput>]>;
15
+ private static handle;
16
+ constructor(routes: R);
17
+ 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>]>;
18
18
  merge: <PathPrefix extends `/${string}`, OtherRoutes extends Routes>(pathPrefix: PathPrefix, other: Router<Context, OtherRoutes>) => Router<Context, [...R, ...PrefixRoutesPath<PathPrefix, OtherRoutes>]>;
19
19
  toFindMyWay: (server: ServerConfig<Context, any>) => fmw.Instance<fmw.HTTPVersion.V1>;
20
20
  }
@@ -1,10 +1,10 @@
1
1
  /// <reference types="node" />
2
2
  import * as http from 'node:http';
3
- import { KaitoError } from './error';
4
- import { KaitoRequest } from './req';
5
- import { KaitoResponse } from './res';
6
- import { Router } from './router';
7
- import { GetContext, KaitoMethod } from './util';
3
+ import type { KaitoError } from './error';
4
+ import type { KaitoRequest } from './req';
5
+ import type { KaitoResponse } from './res';
6
+ import type { Router } from './router';
7
+ import type { GetContext, KaitoMethod } from './util';
8
8
  export declare type Before<BeforeAfterContext> = (req: http.IncomingMessage, res: http.ServerResponse) => Promise<BeforeAfterContext>;
9
9
  export declare type HandlerResult = {
10
10
  success: true;
@@ -26,10 +26,10 @@ export declare type ServerConfigWithBefore<BeforeAfterContext> = {
26
26
  export declare type ServerConfig<Context, BeforeAfterContext> = ServerConfigWithBefore<BeforeAfterContext> & {
27
27
  router: Router<Context, any>;
28
28
  getContext: GetContext<Context>;
29
- rawRoutes?: Partial<Record<KaitoMethod, {
29
+ rawRoutes?: Partial<Record<KaitoMethod, Array<{
30
30
  path: string;
31
31
  handler: (request: http.IncomingMessage, response: http.ServerResponse) => unknown;
32
- }[]>>;
32
+ }>>>;
33
33
  onError(arg: {
34
34
  error: Error;
35
35
  req: KaitoRequest;
@@ -1,6 +1,6 @@
1
- import { HTTPMethod } from 'find-my-way';
2
- import { KaitoRequest } from './req';
3
- import { KaitoResponse } from './res';
1
+ import type { HTTPMethod } from 'find-my-way';
2
+ import type { KaitoRequest } from './req';
3
+ import type { KaitoResponse } from './res';
4
4
  export declare 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}` ? {
@@ -16,5 +16,5 @@ declare type AddStartSlashes<T extends string> = T extends `/${infer U}` ? `/${U
16
16
  export declare type NormalizePath<T extends string> = AddStartSlashes<RemoveEndSlashes<T>>;
17
17
  export declare type Values<T> = T[keyof T];
18
18
  export declare type NoEmpty<T> = [keyof T] extends [never] ? never : T;
19
- export declare function getInput(req: KaitoRequest): Promise<unknown>;
19
+ export declare function getBody(req: KaitoRequest): Promise<unknown>;
20
20
  export {};
@@ -4,9 +4,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var node_tls = require('node:tls');
6
6
  var contentType = require('content-type');
7
+ var node_stream = require('node:stream');
8
+ var consumers = require('node:stream/consumers');
7
9
  var getRawBody = require('raw-body');
8
10
  var cookie = require('cookie');
9
11
  var fmw = require('find-my-way');
12
+ var zod = require('zod');
10
13
  var http = require('node:http');
11
14
 
12
15
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
@@ -119,28 +122,17 @@ function getLastEntryInMultiHeaderValue(headerValue) {
119
122
  var lastIndex = normalized.lastIndexOf(',');
120
123
  return lastIndex === -1 ? normalized.trim() : normalized.slice(lastIndex + 1).trim();
121
124
  }
122
- function getInput(_x) {
123
- return _getInput.apply(this, arguments);
125
+ function getBody(_x) {
126
+ return _getBody.apply(this, arguments);
124
127
  }
125
128
 
126
- function _getInput() {
127
- _getInput = _asyncToGenerator(function* (req) {
128
- if (req.method === 'GET') {
129
- var input = req.url.searchParams.get('input');
130
-
131
- if (!input) {
132
- return null;
133
- }
134
-
135
- return JSON.parse(input);
136
- }
137
-
138
- var buffer = yield getRawBody__default["default"](req.raw);
139
-
129
+ function _getBody() {
130
+ _getBody = _asyncToGenerator(function* (req) {
140
131
  if (!req.headers['content-type']) {
141
132
  return null;
142
133
  }
143
134
 
135
+ var buffer = yield getRawBody__default["default"](req.raw);
144
136
  var {
145
137
  type
146
138
  } = contentType.parse(req.headers['content-type']);
@@ -148,7 +140,7 @@ function _getInput() {
148
140
  switch (type) {
149
141
  case 'application/json':
150
142
  {
151
- return JSON.parse(buffer.toString());
143
+ return consumers.json(node_stream.Readable.from(buffer));
152
144
  }
153
145
 
154
146
  default:
@@ -162,7 +154,7 @@ function _getInput() {
162
154
  }
163
155
  }
164
156
  });
165
- return _getInput.apply(this, arguments);
157
+ return _getBody.apply(this, arguments);
166
158
  }
167
159
 
168
160
  class KaitoRequest {
@@ -336,6 +328,68 @@ function _objectSpread2(target) {
336
328
  }
337
329
 
338
330
  class Router {
331
+ static handle(server, route, options) {
332
+ return _asyncToGenerator(function* () {
333
+ try {
334
+ var _yield$route$body$par, _route$body;
335
+
336
+ var ctx = yield server.getContext(options.req, options.res);
337
+ 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
+ var query = route.query ? zod.z.object(route.query).parse(Object.fromEntries(options.req.url.searchParams.entries())) : {};
339
+ var result = yield route.run({
340
+ ctx,
341
+ body,
342
+ query,
343
+ params: options.params
344
+ });
345
+ options.res.status(200).json({
346
+ success: true,
347
+ data: result,
348
+ message: 'OK'
349
+ });
350
+ return {
351
+ success: true,
352
+ data: result
353
+ };
354
+ } catch (e) {
355
+ var error = WrappedError.maybe(e);
356
+
357
+ if (error instanceof KaitoError) {
358
+ options.res.status(error.status).json({
359
+ success: false,
360
+ data: null,
361
+ message: error.message
362
+ });
363
+ return;
364
+ }
365
+
366
+ var {
367
+ status,
368
+ message
369
+ } = yield server.onError({
370
+ error,
371
+ req: options.req,
372
+ res: options.res
373
+ }).catch(() => ({
374
+ status: 500,
375
+ message: 'Internal Server Error'
376
+ }));
377
+ options.res.status(status).json({
378
+ success: false,
379
+ data: null,
380
+ message
381
+ });
382
+ return {
383
+ success: false,
384
+ data: {
385
+ status,
386
+ message
387
+ }
388
+ };
389
+ }
390
+ })();
391
+ }
392
+
339
393
  constructor(routes) {
340
394
  _defineProperty(this, "add", route => new Router([...this.routes, route]));
341
395
 
@@ -410,67 +464,6 @@ class Router {
410
464
  this.routes = routes;
411
465
  }
412
466
 
413
- static handle(server, route, options) {
414
- return _asyncToGenerator(function* () {
415
- try {
416
- var _route$input$parse, _route$input;
417
-
418
- var ctx = yield server.getContext(options.req, options.res);
419
- var body = yield getInput(options.req);
420
- var input = (_route$input$parse = (_route$input = route.input) === null || _route$input === void 0 ? void 0 : _route$input.parse(body)) !== null && _route$input$parse !== void 0 ? _route$input$parse : undefined;
421
- var result = yield route.run({
422
- ctx,
423
- input,
424
- params: options.params
425
- });
426
- options.res.status(200).json({
427
- success: true,
428
- data: result,
429
- message: 'OK'
430
- });
431
- return {
432
- success: true,
433
- data: result
434
- };
435
- } catch (e) {
436
- var error = WrappedError.maybe(e);
437
-
438
- if (error instanceof KaitoError) {
439
- options.res.status(error.status).json({
440
- success: false,
441
- data: null,
442
- message: error.message
443
- });
444
- return;
445
- }
446
-
447
- var {
448
- status,
449
- message
450
- } = yield server.onError({
451
- error,
452
- req: options.req,
453
- res: options.res
454
- }).catch(() => ({
455
- status: 500,
456
- message: 'Internal Server Error'
457
- }));
458
- options.res.status(status).json({
459
- success: false,
460
- data: null,
461
- message
462
- });
463
- return {
464
- success: false,
465
- data: {
466
- status,
467
- message
468
- }
469
- };
470
- }
471
- })();
472
- }
473
-
474
467
  }
475
468
 
476
469
  _defineProperty(Router, "create", () => new Router([]));
@@ -509,7 +502,7 @@ function createFMWServer(config) {
509
502
  if (config.before) {
510
503
  before = yield config.before(req, res);
511
504
  } else {
512
- before = null;
505
+ before = undefined;
513
506
  } // If the user has sent a response (e.g. replying to CORS), we don't want to do anything else.
514
507
 
515
508
 
@@ -545,5 +538,5 @@ exports.WrappedError = WrappedError;
545
538
  exports.createFMWServer = createFMWServer;
546
539
  exports.createGetContext = createGetContext;
547
540
  exports.createServer = createServer;
548
- exports.getInput = getInput;
541
+ exports.getBody = getBody;
549
542
  exports.getLastEntryInMultiHeaderValue = getLastEntryInMultiHeaderValue;
@@ -4,9 +4,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var node_tls = require('node:tls');
6
6
  var contentType = require('content-type');
7
+ var node_stream = require('node:stream');
8
+ var consumers = require('node:stream/consumers');
7
9
  var getRawBody = require('raw-body');
8
10
  var cookie = require('cookie');
9
11
  var fmw = require('find-my-way');
12
+ var zod = require('zod');
10
13
  var http = require('node:http');
11
14
 
12
15
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
@@ -119,28 +122,17 @@ function getLastEntryInMultiHeaderValue(headerValue) {
119
122
  var lastIndex = normalized.lastIndexOf(',');
120
123
  return lastIndex === -1 ? normalized.trim() : normalized.slice(lastIndex + 1).trim();
121
124
  }
122
- function getInput(_x) {
123
- return _getInput.apply(this, arguments);
125
+ function getBody(_x) {
126
+ return _getBody.apply(this, arguments);
124
127
  }
125
128
 
126
- function _getInput() {
127
- _getInput = _asyncToGenerator(function* (req) {
128
- if (req.method === 'GET') {
129
- var input = req.url.searchParams.get('input');
130
-
131
- if (!input) {
132
- return null;
133
- }
134
-
135
- return JSON.parse(input);
136
- }
137
-
138
- var buffer = yield getRawBody__default["default"](req.raw);
139
-
129
+ function _getBody() {
130
+ _getBody = _asyncToGenerator(function* (req) {
140
131
  if (!req.headers['content-type']) {
141
132
  return null;
142
133
  }
143
134
 
135
+ var buffer = yield getRawBody__default["default"](req.raw);
144
136
  var {
145
137
  type
146
138
  } = contentType.parse(req.headers['content-type']);
@@ -148,7 +140,7 @@ function _getInput() {
148
140
  switch (type) {
149
141
  case 'application/json':
150
142
  {
151
- return JSON.parse(buffer.toString());
143
+ return consumers.json(node_stream.Readable.from(buffer));
152
144
  }
153
145
 
154
146
  default:
@@ -158,7 +150,7 @@ function _getInput() {
158
150
  }
159
151
  }
160
152
  });
161
- return _getInput.apply(this, arguments);
153
+ return _getBody.apply(this, arguments);
162
154
  }
163
155
 
164
156
  class KaitoRequest {
@@ -332,6 +324,68 @@ function _objectSpread2(target) {
332
324
  }
333
325
 
334
326
  class Router {
327
+ static handle(server, route, options) {
328
+ return _asyncToGenerator(function* () {
329
+ try {
330
+ var _yield$route$body$par, _route$body;
331
+
332
+ var ctx = yield server.getContext(options.req, options.res);
333
+ 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
+ var query = route.query ? zod.z.object(route.query).parse(Object.fromEntries(options.req.url.searchParams.entries())) : {};
335
+ var result = yield route.run({
336
+ ctx,
337
+ body,
338
+ query,
339
+ params: options.params
340
+ });
341
+ options.res.status(200).json({
342
+ success: true,
343
+ data: result,
344
+ message: 'OK'
345
+ });
346
+ return {
347
+ success: true,
348
+ data: result
349
+ };
350
+ } catch (e) {
351
+ var error = WrappedError.maybe(e);
352
+
353
+ if (error instanceof KaitoError) {
354
+ options.res.status(error.status).json({
355
+ success: false,
356
+ data: null,
357
+ message: error.message
358
+ });
359
+ return;
360
+ }
361
+
362
+ var {
363
+ status,
364
+ message
365
+ } = yield server.onError({
366
+ error,
367
+ req: options.req,
368
+ res: options.res
369
+ }).catch(() => ({
370
+ status: 500,
371
+ message: 'Internal Server Error'
372
+ }));
373
+ options.res.status(status).json({
374
+ success: false,
375
+ data: null,
376
+ message
377
+ });
378
+ return {
379
+ success: false,
380
+ data: {
381
+ status,
382
+ message
383
+ }
384
+ };
385
+ }
386
+ })();
387
+ }
388
+
335
389
  constructor(routes) {
336
390
  _defineProperty(this, "add", route => new Router([...this.routes, route]));
337
391
 
@@ -406,67 +460,6 @@ class Router {
406
460
  this.routes = routes;
407
461
  }
408
462
 
409
- static handle(server, route, options) {
410
- return _asyncToGenerator(function* () {
411
- try {
412
- var _route$input$parse, _route$input;
413
-
414
- var ctx = yield server.getContext(options.req, options.res);
415
- var body = yield getInput(options.req);
416
- var input = (_route$input$parse = (_route$input = route.input) === null || _route$input === void 0 ? void 0 : _route$input.parse(body)) !== null && _route$input$parse !== void 0 ? _route$input$parse : undefined;
417
- var result = yield route.run({
418
- ctx,
419
- input,
420
- params: options.params
421
- });
422
- options.res.status(200).json({
423
- success: true,
424
- data: result,
425
- message: 'OK'
426
- });
427
- return {
428
- success: true,
429
- data: result
430
- };
431
- } catch (e) {
432
- var error = WrappedError.maybe(e);
433
-
434
- if (error instanceof KaitoError) {
435
- options.res.status(error.status).json({
436
- success: false,
437
- data: null,
438
- message: error.message
439
- });
440
- return;
441
- }
442
-
443
- var {
444
- status,
445
- message
446
- } = yield server.onError({
447
- error,
448
- req: options.req,
449
- res: options.res
450
- }).catch(() => ({
451
- status: 500,
452
- message: 'Internal Server Error'
453
- }));
454
- options.res.status(status).json({
455
- success: false,
456
- data: null,
457
- message
458
- });
459
- return {
460
- success: false,
461
- data: {
462
- status,
463
- message
464
- }
465
- };
466
- }
467
- })();
468
- }
469
-
470
463
  }
471
464
 
472
465
  _defineProperty(Router, "create", () => new Router([]));
@@ -505,7 +498,7 @@ function createFMWServer(config) {
505
498
  if (config.before) {
506
499
  before = yield config.before(req, res);
507
500
  } else {
508
- before = null;
501
+ before = undefined;
509
502
  } // If the user has sent a response (e.g. replying to CORS), we don't want to do anything else.
510
503
 
511
504
 
@@ -541,5 +534,5 @@ exports.WrappedError = WrappedError;
541
534
  exports.createFMWServer = createFMWServer;
542
535
  exports.createGetContext = createGetContext;
543
536
  exports.createServer = createServer;
544
- exports.getInput = getInput;
537
+ exports.getBody = getBody;
545
538
  exports.getLastEntryInMultiHeaderValue = getLastEntryInMultiHeaderValue;
@@ -1,8 +1,11 @@
1
1
  import { TLSSocket } from 'node:tls';
2
2
  import { parse } from 'content-type';
3
+ import { Readable } from 'node:stream';
4
+ import { json } from 'node:stream/consumers';
3
5
  import getRawBody from 'raw-body';
4
6
  import { serialize } from 'cookie';
5
7
  import fmw from 'find-my-way';
8
+ import { z } from 'zod';
6
9
  import * as http from 'node:http';
7
10
 
8
11
  class WrappedError extends Error {
@@ -91,28 +94,17 @@ function getLastEntryInMultiHeaderValue(headerValue) {
91
94
  var lastIndex = normalized.lastIndexOf(',');
92
95
  return lastIndex === -1 ? normalized.trim() : normalized.slice(lastIndex + 1).trim();
93
96
  }
94
- function getInput(_x) {
95
- return _getInput.apply(this, arguments);
97
+ function getBody(_x) {
98
+ return _getBody.apply(this, arguments);
96
99
  }
97
100
 
98
- function _getInput() {
99
- _getInput = _asyncToGenerator(function* (req) {
100
- if (req.method === 'GET') {
101
- var input = req.url.searchParams.get('input');
102
-
103
- if (!input) {
104
- return null;
105
- }
106
-
107
- return JSON.parse(input);
108
- }
109
-
110
- var buffer = yield getRawBody(req.raw);
111
-
101
+ function _getBody() {
102
+ _getBody = _asyncToGenerator(function* (req) {
112
103
  if (!req.headers['content-type']) {
113
104
  return null;
114
105
  }
115
106
 
107
+ var buffer = yield getRawBody(req.raw);
116
108
  var {
117
109
  type
118
110
  } = parse(req.headers['content-type']);
@@ -120,7 +112,7 @@ function _getInput() {
120
112
  switch (type) {
121
113
  case 'application/json':
122
114
  {
123
- return JSON.parse(buffer.toString());
115
+ return json(Readable.from(buffer));
124
116
  }
125
117
 
126
118
  default:
@@ -134,7 +126,7 @@ function _getInput() {
134
126
  }
135
127
  }
136
128
  });
137
- return _getInput.apply(this, arguments);
129
+ return _getBody.apply(this, arguments);
138
130
  }
139
131
 
140
132
  class KaitoRequest {
@@ -308,6 +300,68 @@ function _objectSpread2(target) {
308
300
  }
309
301
 
310
302
  class Router {
303
+ static handle(server, route, options) {
304
+ return _asyncToGenerator(function* () {
305
+ try {
306
+ var _yield$route$body$par, _route$body;
307
+
308
+ var ctx = yield server.getContext(options.req, options.res);
309
+ 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
+ var query = route.query ? z.object(route.query).parse(Object.fromEntries(options.req.url.searchParams.entries())) : {};
311
+ var result = yield route.run({
312
+ ctx,
313
+ body,
314
+ query,
315
+ params: options.params
316
+ });
317
+ options.res.status(200).json({
318
+ success: true,
319
+ data: result,
320
+ message: 'OK'
321
+ });
322
+ return {
323
+ success: true,
324
+ data: result
325
+ };
326
+ } catch (e) {
327
+ var error = WrappedError.maybe(e);
328
+
329
+ if (error instanceof KaitoError) {
330
+ options.res.status(error.status).json({
331
+ success: false,
332
+ data: null,
333
+ message: error.message
334
+ });
335
+ return;
336
+ }
337
+
338
+ var {
339
+ status,
340
+ message
341
+ } = yield server.onError({
342
+ error,
343
+ req: options.req,
344
+ res: options.res
345
+ }).catch(() => ({
346
+ status: 500,
347
+ message: 'Internal Server Error'
348
+ }));
349
+ options.res.status(status).json({
350
+ success: false,
351
+ data: null,
352
+ message
353
+ });
354
+ return {
355
+ success: false,
356
+ data: {
357
+ status,
358
+ message
359
+ }
360
+ };
361
+ }
362
+ })();
363
+ }
364
+
311
365
  constructor(routes) {
312
366
  _defineProperty(this, "add", route => new Router([...this.routes, route]));
313
367
 
@@ -382,67 +436,6 @@ class Router {
382
436
  this.routes = routes;
383
437
  }
384
438
 
385
- static handle(server, route, options) {
386
- return _asyncToGenerator(function* () {
387
- try {
388
- var _route$input$parse, _route$input;
389
-
390
- var ctx = yield server.getContext(options.req, options.res);
391
- var body = yield getInput(options.req);
392
- var input = (_route$input$parse = (_route$input = route.input) === null || _route$input === void 0 ? void 0 : _route$input.parse(body)) !== null && _route$input$parse !== void 0 ? _route$input$parse : undefined;
393
- var result = yield route.run({
394
- ctx,
395
- input,
396
- params: options.params
397
- });
398
- options.res.status(200).json({
399
- success: true,
400
- data: result,
401
- message: 'OK'
402
- });
403
- return {
404
- success: true,
405
- data: result
406
- };
407
- } catch (e) {
408
- var error = WrappedError.maybe(e);
409
-
410
- if (error instanceof KaitoError) {
411
- options.res.status(error.status).json({
412
- success: false,
413
- data: null,
414
- message: error.message
415
- });
416
- return;
417
- }
418
-
419
- var {
420
- status,
421
- message
422
- } = yield server.onError({
423
- error,
424
- req: options.req,
425
- res: options.res
426
- }).catch(() => ({
427
- status: 500,
428
- message: 'Internal Server Error'
429
- }));
430
- options.res.status(status).json({
431
- success: false,
432
- data: null,
433
- message
434
- });
435
- return {
436
- success: false,
437
- data: {
438
- status,
439
- message
440
- }
441
- };
442
- }
443
- })();
444
- }
445
-
446
439
  }
447
440
 
448
441
  _defineProperty(Router, "create", () => new Router([]));
@@ -481,7 +474,7 @@ function createFMWServer(config) {
481
474
  if (config.before) {
482
475
  before = yield config.before(req, res);
483
476
  } else {
484
- before = null;
477
+ before = undefined;
485
478
  } // If the user has sent a response (e.g. replying to CORS), we don't want to do anything else.
486
479
 
487
480
 
@@ -509,4 +502,4 @@ function createServer(config) {
509
502
  return createFMWServer(config).server;
510
503
  }
511
504
 
512
- export { KaitoError, KaitoRequest, KaitoResponse, Router, WrappedError, createFMWServer, createGetContext, createServer, getInput, getLastEntryInMultiHeaderValue };
505
+ export { KaitoError, KaitoRequest, KaitoResponse, Router, WrappedError, createFMWServer, createGetContext, createServer, getBody, getLastEntryInMultiHeaderValue };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaito-http/core",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
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>",