@kaito-http/core 2.3.7 → 2.4.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,13 +1,13 @@
1
- import { HTTPMethod } from 'find-my-way';
2
1
  import { z } from 'zod';
3
- import { ExtractRouteParams } from './util';
2
+ import { ExtractRouteParams, KaitoMethod } from './util';
4
3
  export declare type RouteArgument<Path extends string, Context, Input extends z.ZodSchema> = {
5
4
  ctx: Context;
6
5
  input: z.infer<Input>;
7
6
  params: ExtractRouteParams<Path>;
8
7
  };
9
- export interface Route<Result, Path extends string, Method extends HTTPMethod, Context, Input extends z.ZodSchema> {
8
+ export declare type Route<Context, Result, Path extends string, Method extends KaitoMethod, Input extends z.ZodSchema> = {
10
9
  input?: Input;
10
+ path: Path;
11
11
  method: Method;
12
- run(arg: RouteArgument<Path, Context, Input>): Promise<Result>;
13
- }
12
+ run(args: RouteArgument<Path, Context, Input>): Promise<Result>;
13
+ };
@@ -2,57 +2,24 @@ import fmw, { HTTPMethod, Instance } from 'find-my-way';
2
2
  import { z } from 'zod';
3
3
  import { Route } from './route';
4
4
  import { ServerConfig } from './server';
5
- import { NoEmpty, NormalizePath, Values } from './util';
6
- export declare type RoutesInit<Context> = {
7
- [Path in string]: Route<any, Path, HTTPMethod, Context, z.ZodSchema>;
8
- };
5
+ import { KaitoMethod } from './util';
6
+ export declare type RoutesInit<Context> = Array<Route<Context, unknown, string, KaitoMethod, z.ZodSchema>>;
7
+ export declare type MergePaths<Routes extends RoutesInit<unknown>, Prefix extends string> = Routes extends [
8
+ infer R,
9
+ ...infer Rest
10
+ ] ? R extends Route<infer Context, infer Result, infer Path, infer Method, infer Input> ? [Route<Context, Result, `${Prefix}${Path}`, Method, Input>, ...MergePaths<Extract<Rest, RoutesInit<any>>, Prefix>] : MergePaths<Extract<Rest, RoutesInit<any>>, Prefix> : [];
9
11
  export declare class Router<Context, Routes extends RoutesInit<Context>> {
10
- static create<Context = null>(): Router<Context, {}>;
12
+ static create<Context = null>(): Router<Context, []>;
11
13
  private static handle;
12
14
  readonly 'routes': Routes;
13
- readonly 'acl': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "ACL", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "ACL", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "ACL", Context, Input>; }>;
14
- readonly 'bind': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "BIND", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "BIND", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "BIND", Context, Input>; }>;
15
- readonly 'checkout': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "CHECKOUT", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "CHECKOUT", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "CHECKOUT", Context, Input>; }>;
16
- readonly 'connect': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "CONNECT", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "CONNECT", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "CONNECT", Context, Input>; }>;
17
- readonly 'copy': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "COPY", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "COPY", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "COPY", Context, Input>; }>;
18
- readonly 'delete': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "DELETE", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "DELETE", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "DELETE", Context, Input>; }>;
19
- readonly 'get': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "GET", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "GET", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "GET", Context, Input>; }>;
20
- readonly 'head': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "HEAD", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "HEAD", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "HEAD", Context, Input>; }>;
21
- readonly 'link': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "LINK", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "LINK", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "LINK", Context, Input>; }>;
22
- readonly 'lock': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "LOCK", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "LOCK", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "LOCK", Context, Input>; }>;
23
- readonly 'm_search': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "M-SEARCH", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "M-SEARCH", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "M-SEARCH", Context, Input>; }>;
24
- readonly 'mkactivity': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "MKACTIVITY", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "MKACTIVITY", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "MKACTIVITY", Context, Input>; }>;
25
- readonly 'mkcalendar': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "MKCALENDAR", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "MKCALENDAR", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "MKCALENDAR", Context, Input>; }>;
26
- readonly 'mkcol': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "MKCOL", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "MKCOL", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "MKCOL", Context, Input>; }>;
27
- readonly 'move': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "MOVE", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "MOVE", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "MOVE", Context, Input>; }>;
28
- readonly 'notify': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "NOTIFY", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "NOTIFY", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "NOTIFY", Context, Input>; }>;
29
- readonly 'options': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "OPTIONS", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "OPTIONS", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "OPTIONS", Context, Input>; }>;
30
- readonly 'patch': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "PATCH", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "PATCH", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "PATCH", Context, Input>; }>;
31
- readonly 'post': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "POST", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "POST", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "POST", Context, Input>; }>;
32
- readonly 'propfind': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "PROPFIND", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "PROPFIND", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "PROPFIND", Context, Input>; }>;
33
- readonly 'proppatch': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "PROPPATCH", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "PROPPATCH", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "PROPPATCH", Context, Input>; }>;
34
- readonly 'purge': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "PURGE", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "PURGE", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "PURGE", Context, Input>; }>;
35
- readonly 'put': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "PUT", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "PUT", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "PUT", Context, Input>; }>;
36
- readonly 'rebind': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "REBIND", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "REBIND", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "REBIND", Context, Input>; }>;
37
- readonly 'report': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "REPORT", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "REPORT", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "REPORT", Context, Input>; }>;
38
- readonly 'search': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "SEARCH", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "SEARCH", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "SEARCH", Context, Input>; }>;
39
- readonly 'source': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "SOURCE", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "SOURCE", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "SOURCE", Context, Input>; }>;
40
- readonly 'subscribe': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "SUBSCRIBE", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "SUBSCRIBE", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "SUBSCRIBE", Context, Input>; }>;
41
- readonly 'trace': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "TRACE", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "TRACE", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "TRACE", Context, Input>; }>;
42
- readonly 'unbind': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "UNBIND", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "UNBIND", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "UNBIND", Context, Input>; }>;
43
- readonly 'unlink': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "UNLINK", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "UNLINK", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "UNLINK", Context, Input>; }>;
44
- readonly 'unlock': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "UNLOCK", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "UNLOCK", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "UNLOCK", Context, Input>; }>;
45
- readonly 'unsubscribe': <Result, Path extends `/${string}`, Input extends z.ZodType<any, z.ZodTypeDef, any> = never>(path: Path, route: Omit<Route<Result, Path, "UNSUBSCRIBE", Context, Input>, "method">) => Router<Context, Path extends keyof Routes ? NoEmpty<Pick<Routes, Path>> | { [key in Path]: Route<Result, Path, "UNSUBSCRIBE", Context, Input>; } : Routes & { [key_1 in Path]: Route<Result, Path, "UNSUBSCRIBE", Context, Input>; }>;
46
15
  private constructor();
47
- merge<Prefix extends string, NewRoutes extends RoutesInit<Context>>(prefix: NormalizePath<Prefix>, router: Router<Context, NewRoutes>): Router<Context, Routes & { [Path in Extract<keyof NewRoutes, string> as `/${Prefix}${Path}`]: Values<{ [M in NewRoutes[Path]["method"]]: Omit<Extract<NewRoutes[Path], {
48
- method: M;
49
- }>, "method" | "path"> & {
50
- path: `/${Prefix}${Path}`;
51
- method: M;
52
- }; }>; }>;
53
16
  toFindMyWay(server: ServerConfig<Context>): Instance<fmw.HTTPVersion.V1>;
54
- _copy<NewRoutes extends RoutesInit<Context>>(routes: NewRoutes): Router<Context, NewRoutes>;
55
- private make;
17
+ add<Method extends HTTPMethod, Path extends string, Result, Input extends z.ZodSchema>(route: Route<Context, Result, Path, Method, Input>): Router<Context, [...Routes, Route<Context, Result, Path, Method, Input>]>;
18
+ map(): { [Method in Routes[number]["method"]]: { [R in Extract<Routes[number], {
19
+ method: Method;
20
+ }> as R["path"]]: R; }; };
21
+ merge<Prefix extends string, NewRoutes extends RoutesInit<Context>>(prefix: Prefix, router: Router<Context, NewRoutes>): Router<Context, [...Routes, ...MergePaths<NewRoutes, Prefix>]>;
22
+ private copyContext;
56
23
  }
57
24
  /**
58
25
  * @deprecated Please use Router#create instead
@@ -1,13 +1,15 @@
1
1
  /// <reference types="node" />
2
- import { Router, RoutesInit } from './router';
2
+ import { Router } from './router';
3
3
  import * as http from 'http';
4
4
  import { KaitoRequest } from './req';
5
5
  import { KaitoResponse } from './res';
6
6
  import { KaitoError } from './error';
7
7
  import { GetContext } from './util';
8
+ export declare type Before = (req: http.IncomingMessage, res: http.ServerResponse) => Promise<void>;
8
9
  export interface ServerConfig<Context> {
9
- router: Router<Context, RoutesInit<Context>>;
10
+ router: Router<Context, any>;
10
11
  getContext: GetContext<Context>;
12
+ before?: Before[];
11
13
  onError(arg: {
12
14
  error: Error;
13
15
  req: KaitoRequest;
@@ -1,3 +1,4 @@
1
+ import { HTTPMethod } from 'find-my-way';
1
2
  import { KaitoRequest } from './req';
2
3
  import { KaitoResponse } from './res';
3
4
  export declare type ExtractRouteParams<T extends string> = string extends T ? Record<string, string> : T extends `${string}:${infer Param}/${infer Rest}` ? {
@@ -5,6 +6,7 @@ export declare type ExtractRouteParams<T extends string> = string extends T ? Re
5
6
  } : T extends `${string}:${infer Param}` ? {
6
7
  [k in Param]: string;
7
8
  } : {};
9
+ export declare type KaitoMethod = HTTPMethod | '*';
8
10
  export declare type GetContext<Result> = (req: KaitoRequest, res: KaitoResponse) => Promise<Result>;
9
11
  export declare function createGetContext<Context>(callback: GetContext<Context>): GetContext<Context>;
10
12
  export declare type InferContext<T> = T extends (req: KaitoRequest, res: KaitoResponse) => Promise<infer U> ? U : never;
@@ -11,32 +11,86 @@ var getRawBody = require('raw-body');
11
11
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
12
12
 
13
13
  function _interopNamespace(e) {
14
- if (e && e.__esModule) return e;
15
- var n = Object.create(null);
16
- if (e) {
17
- Object.keys(e).forEach(function (k) {
18
- if (k !== 'default') {
19
- var d = Object.getOwnPropertyDescriptor(e, k);
20
- Object.defineProperty(n, k, d.get ? d : {
21
- enumerable: true,
22
- get: function () { return e[k]; }
23
- });
24
- }
25
- });
26
- }
27
- n["default"] = e;
28
- return Object.freeze(n);
14
+ if (e && e.__esModule) return e;
15
+ var n = Object.create(null);
16
+ if (e) {
17
+ Object.keys(e).forEach(function (k) {
18
+ if (k !== 'default') {
19
+ var d = Object.getOwnPropertyDescriptor(e, k);
20
+ Object.defineProperty(n, k, d.get ? d : {
21
+ enumerable: true,
22
+ get: function () { return e[k]; }
23
+ });
24
+ }
25
+ });
26
+ }
27
+ n["default"] = e;
28
+ return Object.freeze(n);
29
29
  }
30
30
 
31
31
  var http__namespace = /*#__PURE__*/_interopNamespace(http);
32
32
  var fmw__default = /*#__PURE__*/_interopDefault(fmw);
33
33
  var getRawBody__default = /*#__PURE__*/_interopDefault(getRawBody);
34
34
 
35
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
36
+ try {
37
+ var info = gen[key](arg);
38
+ var value = info.value;
39
+ } catch (error) {
40
+ reject(error);
41
+ return;
42
+ }
43
+
44
+ if (info.done) {
45
+ resolve(value);
46
+ } else {
47
+ Promise.resolve(value).then(_next, _throw);
48
+ }
49
+ }
50
+
51
+ function _asyncToGenerator(fn) {
52
+ return function () {
53
+ var self = this,
54
+ args = arguments;
55
+ return new Promise(function (resolve, reject) {
56
+ var gen = fn.apply(self, args);
57
+
58
+ function _next(value) {
59
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
60
+ }
61
+
62
+ function _throw(err) {
63
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
64
+ }
65
+
66
+ _next(undefined);
67
+ });
68
+ };
69
+ }
70
+
35
71
  function createFMWServer(config) {
36
72
  var fmw = config.router.toFindMyWay(config);
37
- var server = http__namespace.createServer((req, res) => {
38
- fmw.lookup(req, res);
39
- });
73
+ var server = http__namespace.createServer( /*#__PURE__*/function () {
74
+ var _ref = _asyncToGenerator(function* (req, res) {
75
+ for (var fn of (_config$before = config.before) !== null && _config$before !== void 0 ? _config$before : []) {
76
+ var _config$before;
77
+
78
+ // Disabled because we need these to run in order!
79
+ // eslint-disable-next-line no-await-in-loop
80
+ yield fn(req, res);
81
+ }
82
+
83
+ if (req.method === 'OPTIONS') {
84
+ return;
85
+ }
86
+
87
+ fmw.lookup(req, res);
88
+ });
89
+
90
+ return function (_x, _x2) {
91
+ return _ref.apply(this, arguments);
92
+ };
93
+ }());
40
94
  return {
41
95
  server,
42
96
  fmw
@@ -99,42 +153,6 @@ function _objectSpread2(target) {
99
153
  return target;
100
154
  }
101
155
 
102
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
103
- try {
104
- var info = gen[key](arg);
105
- var value = info.value;
106
- } catch (error) {
107
- reject(error);
108
- return;
109
- }
110
-
111
- if (info.done) {
112
- resolve(value);
113
- } else {
114
- Promise.resolve(value).then(_next, _throw);
115
- }
116
- }
117
-
118
- function _asyncToGenerator(fn) {
119
- return function () {
120
- var self = this,
121
- args = arguments;
122
- return new Promise(function (resolve, reject) {
123
- var gen = fn.apply(self, args);
124
-
125
- function _next(value) {
126
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
127
- }
128
-
129
- function _throw(err) {
130
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
131
- }
132
-
133
- _next(undefined);
134
- });
135
- };
136
- }
137
-
138
156
  class WrappedError extends Error {
139
157
  static maybe(maybeError) {
140
158
  if (maybeError instanceof Error) {
@@ -288,18 +306,18 @@ class KaitoResponse {
288
306
 
289
307
  class Router {
290
308
  static create() {
291
- return new Router({});
309
+ return new Router([]);
292
310
  }
293
311
 
294
- static handle(server, options) {
312
+ static handle(server, route, options) {
295
313
  return _asyncToGenerator(function* () {
296
314
  try {
297
- var _options$route$input$, _options$route$input;
315
+ var _route$input$parse, _route$input;
298
316
 
299
317
  var context = yield server.getContext(options.req, options.res);
300
318
  var body = yield getInput(options.req);
301
- var input = (_options$route$input$ = (_options$route$input = options.route.input) === null || _options$route$input === void 0 ? void 0 : _options$route$input.parse(body)) !== null && _options$route$input$ !== void 0 ? _options$route$input$ : undefined;
302
- var result = yield options.route.run({
319
+ 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;
320
+ var result = yield route.run({
303
321
  ctx: context,
304
322
  input,
305
323
  params: options.params
@@ -342,89 +360,10 @@ class Router {
342
360
  }
343
361
 
344
362
  constructor(routes) {
345
- _defineProperty(this, 'acl', this.make('ACL'));
346
-
347
- _defineProperty(this, 'bind', this.make('BIND'));
348
-
349
- _defineProperty(this, 'checkout', this.make('CHECKOUT'));
350
-
351
- _defineProperty(this, 'connect', this.make('CONNECT'));
352
-
353
- _defineProperty(this, 'copy', this.make('COPY'));
354
-
355
- _defineProperty(this, 'delete', this.make('DELETE'));
356
-
357
- _defineProperty(this, 'get', this.make('GET'));
358
-
359
- _defineProperty(this, 'head', this.make('HEAD'));
360
-
361
- _defineProperty(this, 'link', this.make('LINK'));
362
-
363
- _defineProperty(this, 'lock', this.make('LOCK'));
364
-
365
- _defineProperty(this, 'm_search', this.make('M-SEARCH'));
366
-
367
- _defineProperty(this, 'mkactivity', this.make('MKACTIVITY'));
368
-
369
- _defineProperty(this, 'mkcalendar', this.make('MKCALENDAR'));
370
-
371
- _defineProperty(this, 'mkcol', this.make('MKCOL'));
372
-
373
- _defineProperty(this, 'move', this.make('MOVE'));
374
-
375
- _defineProperty(this, 'notify', this.make('NOTIFY'));
376
-
377
- _defineProperty(this, 'options', this.make('OPTIONS'));
378
-
379
- _defineProperty(this, 'patch', this.make('PATCH'));
380
-
381
- _defineProperty(this, 'post', this.make('POST'));
382
-
383
- _defineProperty(this, 'propfind', this.make('PROPFIND'));
384
-
385
- _defineProperty(this, 'proppatch', this.make('PROPPATCH'));
386
-
387
- _defineProperty(this, 'purge', this.make('PURGE'));
388
-
389
- _defineProperty(this, 'put', this.make('PUT'));
390
-
391
- _defineProperty(this, 'rebind', this.make('REBIND'));
392
-
393
- _defineProperty(this, 'report', this.make('REPORT'));
394
-
395
- _defineProperty(this, 'search', this.make('SEARCH'));
396
-
397
- _defineProperty(this, 'source', this.make('SOURCE'));
398
-
399
- _defineProperty(this, 'subscribe', this.make('SUBSCRIBE'));
400
-
401
- _defineProperty(this, 'trace', this.make('TRACE'));
402
-
403
- _defineProperty(this, 'unbind', this.make('UNBIND'));
404
-
405
- _defineProperty(this, 'unlink', this.make('UNLINK'));
406
-
407
- _defineProperty(this, 'unlock', this.make('UNLOCK'));
408
-
409
- _defineProperty(this, 'unsubscribe', this.make('UNSUBSCRIBE'));
410
-
411
363
  this.routes = routes;
412
364
  }
413
365
 
414
- merge(prefix, router) {
415
- var newRoutes = Object.fromEntries(Object.entries(router.routes).map(_ref => {
416
- var [k, v] = _ref;
417
- return ["".concat(prefix).concat(k), v];
418
- }));
419
-
420
- var merged = _objectSpread2(_objectSpread2({}, this.routes), newRoutes);
421
-
422
- return this._copy(merged);
423
- }
424
-
425
366
  toFindMyWay(server) {
426
- var _this = this;
427
-
428
367
  var instance = fmw__default["default"]({
429
368
  ignoreTrailingSlash: true,
430
369
 
@@ -440,54 +379,69 @@ class Router {
440
379
  }
441
380
 
442
381
  });
443
- var paths = Object.keys(this.routes);
444
382
 
445
- var _loop = function _loop(path) {
446
- var route = _this.routes[path];
447
- instance.on(route.method, path, /*#__PURE__*/function () {
448
- var _ref2 = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
383
+ var _loop = function _loop(route) {
384
+ var handler = /*#__PURE__*/function () {
385
+ var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
449
386
  var req = new KaitoRequest(incomingMessage);
450
387
  var res = new KaitoResponse(serverResponse);
451
- yield Router.handle(server, {
452
- route,
388
+ yield Router.handle(server, route, {
453
389
  params,
454
390
  req,
455
391
  res
456
392
  });
457
393
  });
458
394
 
459
- return function (_x, _x2, _x3) {
460
- return _ref2.apply(this, arguments);
395
+ return function handler(_x, _x2, _x3) {
396
+ return _ref.apply(this, arguments);
461
397
  };
462
- }());
398
+ }();
399
+
400
+ if (route.method === '*') {
401
+ instance.all(route.path, handler);
402
+ return "continue";
403
+ }
404
+
405
+ instance.on(route.method, route.path, handler);
463
406
  };
464
407
 
465
- for (var path of paths) {
466
- _loop(path);
408
+ for (var route of this.routes) {
409
+ var _ret = _loop(route);
410
+
411
+ if (_ret === "continue") continue;
467
412
  }
468
413
 
469
414
  return instance;
470
415
  }
471
416
 
472
- _copy(routes) {
473
- return new Router(routes);
417
+ add(route) {
418
+ return new Router([...this.routes, route]);
474
419
  }
475
420
 
476
- make(method) {
477
- return (path, route) => {
478
- var addedRoute = _objectSpread2(_objectSpread2({}, route), {}, {
479
- method
480
- }); // `as unknown` is required because otherwise
481
- // this type just gets massive and too slow,
482
- // so we have to write it out specifically
421
+ map() {
422
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
423
+ var result = {};
483
424
 
425
+ for (var route of this.routes) {
426
+ var _result$method;
484
427
 
485
- var merged = _objectSpread2(_objectSpread2({}, this.routes), {}, {
486
- [path]: addedRoute
428
+ var method = route.method;
429
+ result[method] = _objectSpread2(_objectSpread2({}, (_result$method = result[method]) !== null && _result$method !== void 0 ? _result$method : {}), {}, {
430
+ [route.path]: route
487
431
  });
432
+ }
488
433
 
489
- return this._copy(merged);
490
- };
434
+ return result;
435
+ }
436
+
437
+ merge(prefix, router) {
438
+ return this.copyContext([...this.routes, ...router.routes.map(route => _objectSpread2(_objectSpread2({}, route), {}, {
439
+ path: prefix + route.path
440
+ }))]);
441
+ }
442
+
443
+ copyContext(routes) {
444
+ return new Router(routes);
491
445
  }
492
446
 
493
447
  }
@@ -11,32 +11,86 @@ var getRawBody = require('raw-body');
11
11
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
12
12
 
13
13
  function _interopNamespace(e) {
14
- if (e && e.__esModule) return e;
15
- var n = Object.create(null);
16
- if (e) {
17
- Object.keys(e).forEach(function (k) {
18
- if (k !== 'default') {
19
- var d = Object.getOwnPropertyDescriptor(e, k);
20
- Object.defineProperty(n, k, d.get ? d : {
21
- enumerable: true,
22
- get: function () { return e[k]; }
23
- });
24
- }
25
- });
26
- }
27
- n["default"] = e;
28
- return Object.freeze(n);
14
+ if (e && e.__esModule) return e;
15
+ var n = Object.create(null);
16
+ if (e) {
17
+ Object.keys(e).forEach(function (k) {
18
+ if (k !== 'default') {
19
+ var d = Object.getOwnPropertyDescriptor(e, k);
20
+ Object.defineProperty(n, k, d.get ? d : {
21
+ enumerable: true,
22
+ get: function () { return e[k]; }
23
+ });
24
+ }
25
+ });
26
+ }
27
+ n["default"] = e;
28
+ return Object.freeze(n);
29
29
  }
30
30
 
31
31
  var http__namespace = /*#__PURE__*/_interopNamespace(http);
32
32
  var fmw__default = /*#__PURE__*/_interopDefault(fmw);
33
33
  var getRawBody__default = /*#__PURE__*/_interopDefault(getRawBody);
34
34
 
35
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
36
+ try {
37
+ var info = gen[key](arg);
38
+ var value = info.value;
39
+ } catch (error) {
40
+ reject(error);
41
+ return;
42
+ }
43
+
44
+ if (info.done) {
45
+ resolve(value);
46
+ } else {
47
+ Promise.resolve(value).then(_next, _throw);
48
+ }
49
+ }
50
+
51
+ function _asyncToGenerator(fn) {
52
+ return function () {
53
+ var self = this,
54
+ args = arguments;
55
+ return new Promise(function (resolve, reject) {
56
+ var gen = fn.apply(self, args);
57
+
58
+ function _next(value) {
59
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
60
+ }
61
+
62
+ function _throw(err) {
63
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
64
+ }
65
+
66
+ _next(undefined);
67
+ });
68
+ };
69
+ }
70
+
35
71
  function createFMWServer(config) {
36
72
  var fmw = config.router.toFindMyWay(config);
37
- var server = http__namespace.createServer((req, res) => {
38
- fmw.lookup(req, res);
39
- });
73
+ var server = http__namespace.createServer( /*#__PURE__*/function () {
74
+ var _ref = _asyncToGenerator(function* (req, res) {
75
+ for (var fn of (_config$before = config.before) !== null && _config$before !== void 0 ? _config$before : []) {
76
+ var _config$before;
77
+
78
+ // Disabled because we need these to run in order!
79
+ // eslint-disable-next-line no-await-in-loop
80
+ yield fn(req, res);
81
+ }
82
+
83
+ if (req.method === 'OPTIONS') {
84
+ return;
85
+ }
86
+
87
+ fmw.lookup(req, res);
88
+ });
89
+
90
+ return function (_x, _x2) {
91
+ return _ref.apply(this, arguments);
92
+ };
93
+ }());
40
94
  return {
41
95
  server,
42
96
  fmw
@@ -99,42 +153,6 @@ function _objectSpread2(target) {
99
153
  return target;
100
154
  }
101
155
 
102
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
103
- try {
104
- var info = gen[key](arg);
105
- var value = info.value;
106
- } catch (error) {
107
- reject(error);
108
- return;
109
- }
110
-
111
- if (info.done) {
112
- resolve(value);
113
- } else {
114
- Promise.resolve(value).then(_next, _throw);
115
- }
116
- }
117
-
118
- function _asyncToGenerator(fn) {
119
- return function () {
120
- var self = this,
121
- args = arguments;
122
- return new Promise(function (resolve, reject) {
123
- var gen = fn.apply(self, args);
124
-
125
- function _next(value) {
126
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
127
- }
128
-
129
- function _throw(err) {
130
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
131
- }
132
-
133
- _next(undefined);
134
- });
135
- };
136
- }
137
-
138
156
  class WrappedError extends Error {
139
157
  static maybe(maybeError) {
140
158
  if (maybeError instanceof Error) {
@@ -288,18 +306,18 @@ class KaitoResponse {
288
306
 
289
307
  class Router {
290
308
  static create() {
291
- return new Router({});
309
+ return new Router([]);
292
310
  }
293
311
 
294
- static handle(server, options) {
312
+ static handle(server, route, options) {
295
313
  return _asyncToGenerator(function* () {
296
314
  try {
297
- var _options$route$input$, _options$route$input;
315
+ var _route$input$parse, _route$input;
298
316
 
299
317
  var context = yield server.getContext(options.req, options.res);
300
318
  var body = yield getInput(options.req);
301
- var input = (_options$route$input$ = (_options$route$input = options.route.input) === null || _options$route$input === void 0 ? void 0 : _options$route$input.parse(body)) !== null && _options$route$input$ !== void 0 ? _options$route$input$ : undefined;
302
- var result = yield options.route.run({
319
+ 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;
320
+ var result = yield route.run({
303
321
  ctx: context,
304
322
  input,
305
323
  params: options.params
@@ -342,89 +360,10 @@ class Router {
342
360
  }
343
361
 
344
362
  constructor(routes) {
345
- _defineProperty(this, 'acl', this.make('ACL'));
346
-
347
- _defineProperty(this, 'bind', this.make('BIND'));
348
-
349
- _defineProperty(this, 'checkout', this.make('CHECKOUT'));
350
-
351
- _defineProperty(this, 'connect', this.make('CONNECT'));
352
-
353
- _defineProperty(this, 'copy', this.make('COPY'));
354
-
355
- _defineProperty(this, 'delete', this.make('DELETE'));
356
-
357
- _defineProperty(this, 'get', this.make('GET'));
358
-
359
- _defineProperty(this, 'head', this.make('HEAD'));
360
-
361
- _defineProperty(this, 'link', this.make('LINK'));
362
-
363
- _defineProperty(this, 'lock', this.make('LOCK'));
364
-
365
- _defineProperty(this, 'm_search', this.make('M-SEARCH'));
366
-
367
- _defineProperty(this, 'mkactivity', this.make('MKACTIVITY'));
368
-
369
- _defineProperty(this, 'mkcalendar', this.make('MKCALENDAR'));
370
-
371
- _defineProperty(this, 'mkcol', this.make('MKCOL'));
372
-
373
- _defineProperty(this, 'move', this.make('MOVE'));
374
-
375
- _defineProperty(this, 'notify', this.make('NOTIFY'));
376
-
377
- _defineProperty(this, 'options', this.make('OPTIONS'));
378
-
379
- _defineProperty(this, 'patch', this.make('PATCH'));
380
-
381
- _defineProperty(this, 'post', this.make('POST'));
382
-
383
- _defineProperty(this, 'propfind', this.make('PROPFIND'));
384
-
385
- _defineProperty(this, 'proppatch', this.make('PROPPATCH'));
386
-
387
- _defineProperty(this, 'purge', this.make('PURGE'));
388
-
389
- _defineProperty(this, 'put', this.make('PUT'));
390
-
391
- _defineProperty(this, 'rebind', this.make('REBIND'));
392
-
393
- _defineProperty(this, 'report', this.make('REPORT'));
394
-
395
- _defineProperty(this, 'search', this.make('SEARCH'));
396
-
397
- _defineProperty(this, 'source', this.make('SOURCE'));
398
-
399
- _defineProperty(this, 'subscribe', this.make('SUBSCRIBE'));
400
-
401
- _defineProperty(this, 'trace', this.make('TRACE'));
402
-
403
- _defineProperty(this, 'unbind', this.make('UNBIND'));
404
-
405
- _defineProperty(this, 'unlink', this.make('UNLINK'));
406
-
407
- _defineProperty(this, 'unlock', this.make('UNLOCK'));
408
-
409
- _defineProperty(this, 'unsubscribe', this.make('UNSUBSCRIBE'));
410
-
411
363
  this.routes = routes;
412
364
  }
413
365
 
414
- merge(prefix, router) {
415
- var newRoutes = Object.fromEntries(Object.entries(router.routes).map(_ref => {
416
- var [k, v] = _ref;
417
- return ["".concat(prefix).concat(k), v];
418
- }));
419
-
420
- var merged = _objectSpread2(_objectSpread2({}, this.routes), newRoutes);
421
-
422
- return this._copy(merged);
423
- }
424
-
425
366
  toFindMyWay(server) {
426
- var _this = this;
427
-
428
367
  var instance = fmw__default["default"]({
429
368
  ignoreTrailingSlash: true,
430
369
 
@@ -440,54 +379,69 @@ class Router {
440
379
  }
441
380
 
442
381
  });
443
- var paths = Object.keys(this.routes);
444
382
 
445
- var _loop = function _loop(path) {
446
- var route = _this.routes[path];
447
- instance.on(route.method, path, /*#__PURE__*/function () {
448
- var _ref2 = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
383
+ var _loop = function _loop(route) {
384
+ var handler = /*#__PURE__*/function () {
385
+ var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
449
386
  var req = new KaitoRequest(incomingMessage);
450
387
  var res = new KaitoResponse(serverResponse);
451
- yield Router.handle(server, {
452
- route,
388
+ yield Router.handle(server, route, {
453
389
  params,
454
390
  req,
455
391
  res
456
392
  });
457
393
  });
458
394
 
459
- return function (_x, _x2, _x3) {
460
- return _ref2.apply(this, arguments);
395
+ return function handler(_x, _x2, _x3) {
396
+ return _ref.apply(this, arguments);
461
397
  };
462
- }());
398
+ }();
399
+
400
+ if (route.method === '*') {
401
+ instance.all(route.path, handler);
402
+ return "continue";
403
+ }
404
+
405
+ instance.on(route.method, route.path, handler);
463
406
  };
464
407
 
465
- for (var path of paths) {
466
- _loop(path);
408
+ for (var route of this.routes) {
409
+ var _ret = _loop(route);
410
+
411
+ if (_ret === "continue") continue;
467
412
  }
468
413
 
469
414
  return instance;
470
415
  }
471
416
 
472
- _copy(routes) {
473
- return new Router(routes);
417
+ add(route) {
418
+ return new Router([...this.routes, route]);
474
419
  }
475
420
 
476
- make(method) {
477
- return (path, route) => {
478
- var addedRoute = _objectSpread2(_objectSpread2({}, route), {}, {
479
- method
480
- }); // `as unknown` is required because otherwise
481
- // this type just gets massive and too slow,
482
- // so we have to write it out specifically
421
+ map() {
422
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
423
+ var result = {};
483
424
 
425
+ for (var route of this.routes) {
426
+ var _result$method;
484
427
 
485
- var merged = _objectSpread2(_objectSpread2({}, this.routes), {}, {
486
- [path]: addedRoute
428
+ var method = route.method;
429
+ result[method] = _objectSpread2(_objectSpread2({}, (_result$method = result[method]) !== null && _result$method !== void 0 ? _result$method : {}), {}, {
430
+ [route.path]: route
487
431
  });
432
+ }
488
433
 
489
- return this._copy(merged);
490
- };
434
+ return result;
435
+ }
436
+
437
+ merge(prefix, router) {
438
+ return this.copyContext([...this.routes, ...router.routes.map(route => _objectSpread2(_objectSpread2({}, route), {}, {
439
+ path: prefix + route.path
440
+ }))]);
441
+ }
442
+
443
+ copyContext(routes) {
444
+ return new Router(routes);
491
445
  }
492
446
 
493
447
  }
@@ -4,11 +4,65 @@ import { TLSSocket } from 'tls';
4
4
  import { parse } from 'content-type';
5
5
  import getRawBody from 'raw-body';
6
6
 
7
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
8
+ try {
9
+ var info = gen[key](arg);
10
+ var value = info.value;
11
+ } catch (error) {
12
+ reject(error);
13
+ return;
14
+ }
15
+
16
+ if (info.done) {
17
+ resolve(value);
18
+ } else {
19
+ Promise.resolve(value).then(_next, _throw);
20
+ }
21
+ }
22
+
23
+ function _asyncToGenerator(fn) {
24
+ return function () {
25
+ var self = this,
26
+ args = arguments;
27
+ return new Promise(function (resolve, reject) {
28
+ var gen = fn.apply(self, args);
29
+
30
+ function _next(value) {
31
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
32
+ }
33
+
34
+ function _throw(err) {
35
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
36
+ }
37
+
38
+ _next(undefined);
39
+ });
40
+ };
41
+ }
42
+
7
43
  function createFMWServer(config) {
8
44
  var fmw = config.router.toFindMyWay(config);
9
- var server = http.createServer((req, res) => {
10
- fmw.lookup(req, res);
11
- });
45
+ var server = http.createServer( /*#__PURE__*/function () {
46
+ var _ref = _asyncToGenerator(function* (req, res) {
47
+ for (var fn of (_config$before = config.before) !== null && _config$before !== void 0 ? _config$before : []) {
48
+ var _config$before;
49
+
50
+ // Disabled because we need these to run in order!
51
+ // eslint-disable-next-line no-await-in-loop
52
+ yield fn(req, res);
53
+ }
54
+
55
+ if (req.method === 'OPTIONS') {
56
+ return;
57
+ }
58
+
59
+ fmw.lookup(req, res);
60
+ });
61
+
62
+ return function (_x, _x2) {
63
+ return _ref.apply(this, arguments);
64
+ };
65
+ }());
12
66
  return {
13
67
  server,
14
68
  fmw
@@ -71,42 +125,6 @@ function _objectSpread2(target) {
71
125
  return target;
72
126
  }
73
127
 
74
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
75
- try {
76
- var info = gen[key](arg);
77
- var value = info.value;
78
- } catch (error) {
79
- reject(error);
80
- return;
81
- }
82
-
83
- if (info.done) {
84
- resolve(value);
85
- } else {
86
- Promise.resolve(value).then(_next, _throw);
87
- }
88
- }
89
-
90
- function _asyncToGenerator(fn) {
91
- return function () {
92
- var self = this,
93
- args = arguments;
94
- return new Promise(function (resolve, reject) {
95
- var gen = fn.apply(self, args);
96
-
97
- function _next(value) {
98
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
99
- }
100
-
101
- function _throw(err) {
102
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
103
- }
104
-
105
- _next(undefined);
106
- });
107
- };
108
- }
109
-
110
128
  class WrappedError extends Error {
111
129
  static maybe(maybeError) {
112
130
  if (maybeError instanceof Error) {
@@ -260,18 +278,18 @@ class KaitoResponse {
260
278
 
261
279
  class Router {
262
280
  static create() {
263
- return new Router({});
281
+ return new Router([]);
264
282
  }
265
283
 
266
- static handle(server, options) {
284
+ static handle(server, route, options) {
267
285
  return _asyncToGenerator(function* () {
268
286
  try {
269
- var _options$route$input$, _options$route$input;
287
+ var _route$input$parse, _route$input;
270
288
 
271
289
  var context = yield server.getContext(options.req, options.res);
272
290
  var body = yield getInput(options.req);
273
- var input = (_options$route$input$ = (_options$route$input = options.route.input) === null || _options$route$input === void 0 ? void 0 : _options$route$input.parse(body)) !== null && _options$route$input$ !== void 0 ? _options$route$input$ : undefined;
274
- var result = yield options.route.run({
291
+ 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;
292
+ var result = yield route.run({
275
293
  ctx: context,
276
294
  input,
277
295
  params: options.params
@@ -314,89 +332,10 @@ class Router {
314
332
  }
315
333
 
316
334
  constructor(routes) {
317
- _defineProperty(this, 'acl', this.make('ACL'));
318
-
319
- _defineProperty(this, 'bind', this.make('BIND'));
320
-
321
- _defineProperty(this, 'checkout', this.make('CHECKOUT'));
322
-
323
- _defineProperty(this, 'connect', this.make('CONNECT'));
324
-
325
- _defineProperty(this, 'copy', this.make('COPY'));
326
-
327
- _defineProperty(this, 'delete', this.make('DELETE'));
328
-
329
- _defineProperty(this, 'get', this.make('GET'));
330
-
331
- _defineProperty(this, 'head', this.make('HEAD'));
332
-
333
- _defineProperty(this, 'link', this.make('LINK'));
334
-
335
- _defineProperty(this, 'lock', this.make('LOCK'));
336
-
337
- _defineProperty(this, 'm_search', this.make('M-SEARCH'));
338
-
339
- _defineProperty(this, 'mkactivity', this.make('MKACTIVITY'));
340
-
341
- _defineProperty(this, 'mkcalendar', this.make('MKCALENDAR'));
342
-
343
- _defineProperty(this, 'mkcol', this.make('MKCOL'));
344
-
345
- _defineProperty(this, 'move', this.make('MOVE'));
346
-
347
- _defineProperty(this, 'notify', this.make('NOTIFY'));
348
-
349
- _defineProperty(this, 'options', this.make('OPTIONS'));
350
-
351
- _defineProperty(this, 'patch', this.make('PATCH'));
352
-
353
- _defineProperty(this, 'post', this.make('POST'));
354
-
355
- _defineProperty(this, 'propfind', this.make('PROPFIND'));
356
-
357
- _defineProperty(this, 'proppatch', this.make('PROPPATCH'));
358
-
359
- _defineProperty(this, 'purge', this.make('PURGE'));
360
-
361
- _defineProperty(this, 'put', this.make('PUT'));
362
-
363
- _defineProperty(this, 'rebind', this.make('REBIND'));
364
-
365
- _defineProperty(this, 'report', this.make('REPORT'));
366
-
367
- _defineProperty(this, 'search', this.make('SEARCH'));
368
-
369
- _defineProperty(this, 'source', this.make('SOURCE'));
370
-
371
- _defineProperty(this, 'subscribe', this.make('SUBSCRIBE'));
372
-
373
- _defineProperty(this, 'trace', this.make('TRACE'));
374
-
375
- _defineProperty(this, 'unbind', this.make('UNBIND'));
376
-
377
- _defineProperty(this, 'unlink', this.make('UNLINK'));
378
-
379
- _defineProperty(this, 'unlock', this.make('UNLOCK'));
380
-
381
- _defineProperty(this, 'unsubscribe', this.make('UNSUBSCRIBE'));
382
-
383
335
  this.routes = routes;
384
336
  }
385
337
 
386
- merge(prefix, router) {
387
- var newRoutes = Object.fromEntries(Object.entries(router.routes).map(_ref => {
388
- var [k, v] = _ref;
389
- return ["".concat(prefix).concat(k), v];
390
- }));
391
-
392
- var merged = _objectSpread2(_objectSpread2({}, this.routes), newRoutes);
393
-
394
- return this._copy(merged);
395
- }
396
-
397
338
  toFindMyWay(server) {
398
- var _this = this;
399
-
400
339
  var instance = fmw({
401
340
  ignoreTrailingSlash: true,
402
341
 
@@ -412,54 +351,69 @@ class Router {
412
351
  }
413
352
 
414
353
  });
415
- var paths = Object.keys(this.routes);
416
354
 
417
- var _loop = function _loop(path) {
418
- var route = _this.routes[path];
419
- instance.on(route.method, path, /*#__PURE__*/function () {
420
- var _ref2 = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
355
+ var _loop = function _loop(route) {
356
+ var handler = /*#__PURE__*/function () {
357
+ var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
421
358
  var req = new KaitoRequest(incomingMessage);
422
359
  var res = new KaitoResponse(serverResponse);
423
- yield Router.handle(server, {
424
- route,
360
+ yield Router.handle(server, route, {
425
361
  params,
426
362
  req,
427
363
  res
428
364
  });
429
365
  });
430
366
 
431
- return function (_x, _x2, _x3) {
432
- return _ref2.apply(this, arguments);
367
+ return function handler(_x, _x2, _x3) {
368
+ return _ref.apply(this, arguments);
433
369
  };
434
- }());
370
+ }();
371
+
372
+ if (route.method === '*') {
373
+ instance.all(route.path, handler);
374
+ return "continue";
375
+ }
376
+
377
+ instance.on(route.method, route.path, handler);
435
378
  };
436
379
 
437
- for (var path of paths) {
438
- _loop(path);
380
+ for (var route of this.routes) {
381
+ var _ret = _loop(route);
382
+
383
+ if (_ret === "continue") continue;
439
384
  }
440
385
 
441
386
  return instance;
442
387
  }
443
388
 
444
- _copy(routes) {
445
- return new Router(routes);
389
+ add(route) {
390
+ return new Router([...this.routes, route]);
446
391
  }
447
392
 
448
- make(method) {
449
- return (path, route) => {
450
- var addedRoute = _objectSpread2(_objectSpread2({}, route), {}, {
451
- method
452
- }); // `as unknown` is required because otherwise
453
- // this type just gets massive and too slow,
454
- // so we have to write it out specifically
393
+ map() {
394
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
395
+ var result = {};
455
396
 
397
+ for (var route of this.routes) {
398
+ var _result$method;
456
399
 
457
- var merged = _objectSpread2(_objectSpread2({}, this.routes), {}, {
458
- [path]: addedRoute
400
+ var method = route.method;
401
+ result[method] = _objectSpread2(_objectSpread2({}, (_result$method = result[method]) !== null && _result$method !== void 0 ? _result$method : {}), {}, {
402
+ [route.path]: route
459
403
  });
404
+ }
460
405
 
461
- return this._copy(merged);
462
- };
406
+ return result;
407
+ }
408
+
409
+ merge(prefix, router) {
410
+ return this.copyContext([...this.routes, ...router.routes.map(route => _objectSpread2(_objectSpread2({}, route), {}, {
411
+ path: prefix + route.path
412
+ }))]);
413
+ }
414
+
415
+ copyContext(routes) {
416
+ return new Router(routes);
463
417
  }
464
418
 
465
419
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaito-http/core",
3
- "version": "2.3.7",
3
+ "version": "2.4.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>",