routup 2.0.0 → 3.0.0-alpha.2

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.
Files changed (83) hide show
  1. package/README.md +88 -35
  2. package/dist/dispatcher/adapters/node/module.d.ts +2 -1
  3. package/dist/dispatcher/type.d.ts +8 -6
  4. package/dist/dispatcher/utils.d.ts +2 -1
  5. package/dist/error/create.d.ts +11 -0
  6. package/dist/error/index.d.ts +3 -0
  7. package/dist/error/is.d.ts +2 -0
  8. package/dist/error/module.d.ts +3 -0
  9. package/dist/handler/constants.d.ts +4 -0
  10. package/dist/handler/core/define.d.ts +3 -0
  11. package/dist/handler/core/index.d.ts +2 -0
  12. package/dist/handler/core/types.d.ts +10 -0
  13. package/dist/handler/error/define.d.ts +3 -0
  14. package/dist/handler/error/index.d.ts +2 -0
  15. package/dist/handler/error/types.d.ts +11 -0
  16. package/dist/handler/index.d.ts +6 -0
  17. package/dist/handler/is.d.ts +2 -0
  18. package/dist/handler/types-base.d.ts +6 -0
  19. package/dist/handler/types.d.ts +5 -0
  20. package/dist/index.cjs +734 -698
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.ts +3 -1
  23. package/dist/index.mjs +726 -696
  24. package/dist/index.mjs.map +1 -1
  25. package/dist/layer/constants.d.ts +1 -0
  26. package/dist/layer/module.d.ts +9 -7
  27. package/dist/layer/type.d.ts +6 -3
  28. package/dist/layer/utils.d.ts +1 -1
  29. package/dist/path/matcher.d.ts +4 -4
  30. package/dist/plugin/index.d.ts +2 -0
  31. package/dist/plugin/is.d.ts +2 -0
  32. package/dist/plugin/types.d.ts +28 -0
  33. package/dist/request/helpers/body.d.ts +1 -1
  34. package/dist/request/helpers/cache.d.ts +1 -1
  35. package/dist/request/helpers/cookie.d.ts +1 -1
  36. package/dist/request/helpers/env.d.ts +1 -1
  37. package/dist/request/helpers/header-accept-charset.d.ts +1 -1
  38. package/dist/request/helpers/header-accept-language.d.ts +1 -1
  39. package/dist/request/helpers/header-accept.d.ts +1 -1
  40. package/dist/request/helpers/header-content-type.d.ts +1 -1
  41. package/dist/request/helpers/header.d.ts +1 -1
  42. package/dist/request/helpers/hostname.d.ts +1 -1
  43. package/dist/request/helpers/ip.d.ts +1 -1
  44. package/dist/request/helpers/mount-path.d.ts +1 -1
  45. package/dist/request/helpers/negotiator.d.ts +1 -1
  46. package/dist/request/helpers/params.d.ts +1 -1
  47. package/dist/request/helpers/path.d.ts +1 -1
  48. package/dist/request/helpers/protocol.d.ts +1 -1
  49. package/dist/request/helpers/query.d.ts +1 -1
  50. package/dist/request/helpers/router.d.ts +3 -3
  51. package/dist/request/types.d.ts +4 -0
  52. package/dist/response/helpers/cache.d.ts +1 -1
  53. package/dist/response/helpers/gone.d.ts +1 -1
  54. package/dist/response/helpers/header-attachment.d.ts +1 -1
  55. package/dist/response/helpers/header-content-type.d.ts +1 -1
  56. package/dist/response/helpers/header.d.ts +1 -1
  57. package/dist/response/helpers/send-accepted.d.ts +1 -1
  58. package/dist/response/helpers/send-created.d.ts +1 -1
  59. package/dist/response/helpers/send-file.d.ts +1 -1
  60. package/dist/response/helpers/send-format.d.ts +1 -1
  61. package/dist/response/helpers/send-redirect.d.ts +1 -1
  62. package/dist/response/helpers/send-stream.d.ts +2 -1
  63. package/dist/response/helpers/send-web-blob.d.ts +2 -1
  64. package/dist/response/helpers/send-web-response.d.ts +2 -1
  65. package/dist/response/helpers/send.d.ts +1 -1
  66. package/dist/response/helpers/utils.d.ts +1 -1
  67. package/dist/response/index.d.ts +1 -0
  68. package/dist/response/module.d.ts +2 -1
  69. package/dist/response/types.d.ts +4 -0
  70. package/dist/router/constants.d.ts +1 -0
  71. package/dist/router/index.d.ts +1 -0
  72. package/dist/router/module.d.ts +27 -23
  73. package/dist/router/utils.d.ts +2 -0
  74. package/dist/router-options/module.d.ts +1 -1
  75. package/dist/router-options/type.d.ts +3 -12
  76. package/dist/types.d.ts +0 -9
  77. package/dist/utils/is-instance.d.ts +1 -1
  78. package/package.json +15 -14
  79. package/dist/error.d.ts +0 -1
  80. package/dist/route/index.d.ts +0 -3
  81. package/dist/route/module.d.ts +0 -28
  82. package/dist/route/type.d.ts +0 -6
  83. package/dist/route/utils.d.ts +0 -2
package/README.md CHANGED
@@ -12,14 +12,11 @@
12
12
  [![Known Vulnerabilities](https://snyk.io/test/github/Tada5hi/routup/badge.svg)](https://snyk.io/test/github/Tada5hi/routup)
13
13
  [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white)](https://conventionalcommits.org)
14
14
 
15
- **Routup** is a lightweight, runtime agnostic and extendable routing library.
16
- It uses node's vanilla request and response interfaces, which are injected into route handlers aka middlewares as function argument.
17
- Helpers provide additional functionalities to transform and interact with the request and manipulate the response upstream.
18
-
19
- Use the same routing framework for each project, regardless of the used runtime environment (Node.Js, Bun, ... ) 🎉.
20
- Besides, it is even **228%** faster than Express ([more](#benchmarks)).
21
-
15
+ **Routup** is a fast, lightweight, runtime agnostic and asynchronous routing framework.
16
+ Helpers provide additional functionalities to interact with the request and manipulate the response.
22
17
 
18
+ It can be used independently of the selected runtime environment (Node.Js, Bun, ... ) 🎉.
19
+ Moreover, it is even **228%** faster than Express ([more](#benchmarks)).
23
20
 
24
21
  **Table of Contents**
25
22
 
@@ -41,14 +38,14 @@ npm install routup --save
41
38
  ## Features
42
39
 
43
40
  - 🚀 runtime agnostic (Node.JS, Bun, Deno, ...)
44
- - 🧰 response & request composables/helpers
45
- - 💼 extendable & compact
46
- - 🛫 named route parameters
41
+ - 📝 different handler types (base & error)
42
+ - promise (async) support for core- & error-handlers
43
+ - 🔌 powerful plugin system
44
+ - 🧰 tree shakeable response & request helpers
45
+ - 🤝️ different handler declaration styles (shorthand & verbose)
47
46
  - 📁 nestable routers
48
- - 🤝️ define one or many (error-) middlewares (inc. express middlewares)
49
- - promise support for route- & middleware-handlers
50
- - 👕 TypeScript fully supported
51
- - 🤏 Minimalistic to fit into any solution with minimum overhead
47
+ - 👕 TypeScript support
48
+ - 🤏 minimalistic to fit into any solution with minimum overhead
52
49
  - & much more
53
50
 
54
51
  ## Documentation
@@ -57,36 +54,92 @@ To read the docs, visit [https://routup.net](https://routup.net)
57
54
 
58
55
  ## Usage
59
56
 
60
- **`NodeJs`**
57
+ The following examples are intended to give a small insight into the use of the framework.
58
+ However, it is highly recommended to read the [documentation](https://routup.net),
59
+ as all concepts and basics are taught there.
60
+
61
+ ### Handlers
62
+
63
+ Both core and error handlers, can be defined in two different ways.
64
+
65
+ **`Shorthand`**
66
+
67
+ With the shorthand variant,
68
+ only the handler function is passed as argument to the **coreHandler** & **errorHandler** function.
61
69
 
62
70
  ```typescript
63
- import {createServer} from 'node:http';
71
+ import { createServer } from 'node:http';
64
72
  import {
73
+ coreHandler,
65
74
  createNodeDispatcher,
75
+ errorHandler,
66
76
  Router,
67
- send
77
+ useRequestParam
68
78
  } from 'routup';
69
79
 
70
80
  const router = new Router();
71
81
 
72
- router.get('/', () => 'Hello World');
82
+ router.get('/', coreHandler(() => 'Hello, World!'));
83
+ router.get('/greet/:name', coreHandler((req) => `Hello, ${useRequestParam(req, 'name')}!`));
84
+ router.use(errorHandler((err) => `An error with statusCode ${err.statusCode} occured.`));
73
85
 
74
86
  const server = createServer(createNodeDispatcher(router));
75
87
  server.listen(3000)
76
88
  ```
77
89
 
90
+ **`Verbose`**
91
+
92
+ The verbose variant is more complex, but offers the possibility to set additional information
93
+ like **path**, **method**, ... in the handler definition.
94
+
95
+ ```typescript
96
+ import { createServer } from 'node:http';
97
+ import {
98
+ coreHandler,
99
+ createNodeDispatcher,
100
+ errorHandler,
101
+ Router,
102
+ useRequestParam
103
+ } from 'routup';
104
+
105
+ const router = new Router();
106
+
107
+ router.get(coreHandler({
108
+ path: '/',
109
+ fn: () => 'Hello, World!',
110
+ }));
111
+
112
+ router.get(coreHandler({
113
+ path: '/greet/:name',
114
+ fn: (req) => `Hello, ${useRequestParam(req, 'name')}!`
115
+ }))
116
+
117
+ router.use(errorHandler({
118
+ fn: (err) => `An error with statusCode ${err.statusCode} occured.`
119
+ }))
120
+
121
+ const server = createServer(createNodeDispatcher(router));
122
+ server.listen(3000)
123
+ ```
124
+
125
+ ### Runtimes
126
+
127
+ It is possible to use any javascript runtime environment. Below are examples for Bun and Deno.
128
+ These use the web dispatcher to submit requests based on the web api. Besides the node- & web-dispatcher,
129
+ there is also a plain dispatcher that underlies the web dispatcher, which can be controlled via a simple API.
130
+
78
131
  **`Bun`**
79
132
 
80
133
  ```typescript
81
134
  import {
135
+ coreHandler,
82
136
  createWebDispatcher,
83
- Router,
84
- send
137
+ Router
85
138
  } from 'routup';
86
139
 
87
140
  const router = new Router();
88
141
 
89
- router.get('/', () => 'Hello World');
142
+ router.get('/', coreHandler(() => 'Hello, World!'));
90
143
 
91
144
  const dispatch = createWebDispatcher(router);
92
145
 
@@ -102,14 +155,14 @@ Bun.serve({
102
155
 
103
156
  ```typescript
104
157
  import {
158
+ coreHandler,
105
159
  createWebDispatcher,
106
- Router,
107
- send
160
+ Router
108
161
  } from 'routup';
109
162
 
110
163
  const router = new Router();
111
164
 
112
- router.get('/', () => 'Hello World');
165
+ router.get('/', coreHandler(() => 'Hello, World!'));
113
166
 
114
167
  const dispatch = createWebDispatcher(router);
115
168
 
@@ -134,17 +187,17 @@ According to the fact that routup is a minimalistic framework,
134
187
  it depends on [plugins](https://github.com/routup/plugins) to cover some
135
188
  typically http framework functions, which are not integrated in the main package.
136
189
 
137
- | Name | Description |
138
- |----------------------------------------------------------------------------|------------------------------------------------------------------------|
139
- | [body](https://www.npmjs.com/package/@routup/body) | Read and parse the request body. |
140
- | [cookie](https://www.npmjs.com/package/@routup/cookie) | Read and parse request cookies and serialize cookies for the response. |
141
- | [decorators](https://www.npmjs.com/package/@routup/decorators) | Create request handlers with class-, method- & parameter-decorators. |
142
- | [prometheus](https://www.npmjs.com/package/@routup/prometheus) | Collect and serve metrics for prometheus. |
143
- | [query](https://www.npmjs.com/package/@routup/query) | Read and parse the query string of the request url. |
144
- | [rate-limit](https://www.npmjs.com/package/@routup/rate-limit) | Rate limit incoming requests. |
145
- | [rate-limit-redis](https://www.npmjs.com/package/@routup/rate-limit-redis) | Redis adapter for the rate-limit plugin. |
146
- | [static](https://www.npmjs.com/package/@routup/static) | Serve static files from a directory. |
147
- | [swagger](https://www.npmjs.com/package/@routup/swagger) | Serve generated docs from URL or based on a JSON file. |
190
+ | Name | Description |
191
+ |-----------------------------------------------------------------------------------------------|------------------------------------------------------------------------|
192
+ | [body](https://github.com/routup/plugins/tree/master/packages/body) | Read and parse the request body. |
193
+ | [cookie](https://github.com/routup/plugins/tree/master/packages/cookie) | Read and parse request cookies and serialize cookies for the response. |
194
+ | [decorators](https://github.com/routup/plugins/tree/master/packages/decorators) | Create request handlers with class-, method- & parameter-decorators. |
195
+ | [prometheus](https://github.com/routup/plugins/tree/master/packages/prometheus) | Collect and serve metrics for prometheus. |
196
+ | [query](https://github.com/routup/plugins/tree/master/packages/query) | Read and parse the query string of the request url. |
197
+ | [rate-limit](https://github.com/routup/plugins/tree/master/packages/rate-limit) | Rate limit incoming requests. |
198
+ | [rate-limit-redis](https://github.com/routup/plugins/tree/master/packages/rate-limit-redis) | Redis adapter for the rate-limit plugin. |
199
+ | [static](https://github.com/routup/plugins/tree/master/packages/static) | Serve static files from a directory. |
200
+ | [swagger](https://github.com/routup/plugins/tree/master/packages/swagger) | Serve generated docs from URL or based on a JSON file. |
148
201
 
149
202
  ## Benchmarks
150
203
 
@@ -1,6 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import type { RequestListener } from 'node:http';
3
+ import type { Request } from '../../../request';
4
+ import type { Response } from '../../../response';
3
5
  import type { Router } from '../../../router';
4
- import type { Request, Response } from '../../../types';
5
6
  export declare function dispatchNodeRequest(router: Router, req: Request, res: Response): Promise<void>;
6
7
  export declare function createNodeDispatcher(router: Router): RequestListener;
@@ -1,4 +1,6 @@
1
- import type { Request, Response } from '../types';
1
+ import type { ErrorProxy } from '../error';
2
+ import type { Request } from '../request';
3
+ import type { Response } from '../response';
2
4
  export interface Dispatcher {
3
5
  dispatch(event: DispatcherEvent, meta: DispatcherMeta): Promise<boolean>;
4
6
  }
@@ -6,23 +8,23 @@ export type DispatcherMeta = {
6
8
  /**
7
9
  * Params collected on path.
8
10
  */
9
- params?: Record<string, any>;
11
+ params: Record<string, any>;
10
12
  /**
11
13
  * Path to check for the current instance.
12
14
  */
13
- path?: string;
15
+ path: string;
14
16
  /**
15
17
  * The relative path on which the router is hung.
16
18
  */
17
- mountPath?: string;
19
+ mountPath: string;
18
20
  /**
19
21
  * The error which occurred during a previous handler.
20
22
  */
21
- error?: Error;
23
+ error?: ErrorProxy;
22
24
  /**
23
25
  * Ids of chained router instances.
24
26
  */
25
- routerIds?: number[];
27
+ routerPath: number[];
26
28
  };
27
29
  export type DispatcherEvent = {
28
30
  req: Request;
@@ -1,4 +1,5 @@
1
1
  import type { DispatcherMeta } from './type';
2
- export declare function cloneDispatcherMeta(input?: DispatcherMeta): DispatcherMeta;
2
+ export declare function buildDispatcherMeta(input: Partial<DispatcherMeta>): DispatcherMeta;
3
+ export declare function cloneDispatcherMeta(input: DispatcherMeta): DispatcherMeta;
3
4
  export declare function cloneDispatcherMetaParams(input?: Record<string, any>): Record<string, any>;
4
5
  export declare function mergeDispatcherMetaParams(t1?: Record<string, any>, t2?: Record<string, any>): Record<string, any>;
@@ -0,0 +1,11 @@
1
+ import type { Input } from '@ebec/http';
2
+ import { ErrorProxy } from './module';
3
+ /**
4
+ * Create an error proxy by
5
+ * - an existing error (accessible via cause property)
6
+ * - options
7
+ * - message
8
+ *
9
+ * @param input
10
+ */
11
+ export declare function createError(input: Input): ErrorProxy;
@@ -0,0 +1,3 @@
1
+ export * from './create';
2
+ export * from './is';
3
+ export * from './module';
@@ -0,0 +1,2 @@
1
+ import { ErrorProxy } from './module';
2
+ export declare function isError(input: unknown): input is ErrorProxy;
@@ -0,0 +1,3 @@
1
+ import { HTTPError } from '@ebec/http';
2
+ export declare class ErrorProxy extends HTTPError {
3
+ }
@@ -0,0 +1,4 @@
1
+ export declare enum HandlerType {
2
+ CORE = "core",
3
+ ERROR = "error"
4
+ }
@@ -0,0 +1,3 @@
1
+ import type { CoreHandler, CoreHandlerFn } from './types';
2
+ export declare function coreHandler(input: Omit<CoreHandler, 'type'>): CoreHandler;
3
+ export declare function coreHandler(input: CoreHandlerFn): CoreHandler;
@@ -0,0 +1,2 @@
1
+ export * from './define';
2
+ export * from './types';
@@ -0,0 +1,10 @@
1
+ import type { Request } from '../../request';
2
+ import type { Response } from '../../response';
3
+ import type { HandlerType } from '../constants';
4
+ import type { Next } from '../types';
5
+ import type { HandlerBase } from '../types-base';
6
+ export type CoreHandlerFn = (req: Request, res: Response, next: Next) => unknown | Promise<unknown>;
7
+ export type CoreHandler = HandlerBase & {
8
+ type: `${HandlerType.CORE}`;
9
+ fn: CoreHandlerFn;
10
+ };
@@ -0,0 +1,3 @@
1
+ import type { ErrorHandler, ErrorHandlerFn } from './types';
2
+ export declare function errorHandler(input: Omit<ErrorHandler, 'type'>): ErrorHandler;
3
+ export declare function errorHandler(input: ErrorHandlerFn): ErrorHandler;
@@ -0,0 +1,2 @@
1
+ export * from './define';
2
+ export * from './types';
@@ -0,0 +1,11 @@
1
+ import type { ErrorProxy } from '../../error';
2
+ import type { Request } from '../../request';
3
+ import type { Response } from '../../response';
4
+ import type { HandlerType } from '../constants';
5
+ import type { Next } from '../types';
6
+ import type { HandlerBase } from '../types-base';
7
+ export type ErrorHandlerFn = (err: ErrorProxy, req: Request, res: Response, next: Next) => unknown | Promise<unknown>;
8
+ export type ErrorHandler = HandlerBase & {
9
+ type: `${HandlerType.ERROR}`;
10
+ fn: ErrorHandlerFn;
11
+ };
@@ -0,0 +1,6 @@
1
+ export * from './core';
2
+ export * from './error';
3
+ export * from './constants';
4
+ export * from './is';
5
+ export * from './types';
6
+ export * from './types-base';
@@ -0,0 +1,2 @@
1
+ import type { Handler } from './types';
2
+ export declare function isHandler(input: unknown): input is Handler;
@@ -0,0 +1,6 @@
1
+ import type { MethodName } from '../constants';
2
+ import type { Path } from '../path';
3
+ export type HandlerBase = {
4
+ method?: `${MethodName}` | `${Uppercase<MethodName>}`;
5
+ path?: Path;
6
+ };
@@ -0,0 +1,5 @@
1
+ import type { CoreHandler, CoreHandlerFn } from './core';
2
+ import type { ErrorHandler, ErrorHandlerFn } from './error';
3
+ export type Next = (err?: Error) => void;
4
+ export type Handler = CoreHandler | ErrorHandler;
5
+ export type HandlerFn = CoreHandlerFn | ErrorHandlerFn;