routup 2.0.0 → 3.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +88 -35
- package/dist/dispatcher/adapters/node/module.d.ts +2 -1
- package/dist/dispatcher/type.d.ts +8 -6
- package/dist/dispatcher/utils.d.ts +2 -1
- package/dist/error/create.d.ts +11 -0
- package/dist/error/index.d.ts +3 -0
- package/dist/error/is.d.ts +2 -0
- package/dist/error/module.d.ts +3 -0
- package/dist/handler/constants.d.ts +4 -0
- package/dist/handler/core/define.d.ts +3 -0
- package/dist/handler/core/index.d.ts +2 -0
- package/dist/handler/core/types.d.ts +10 -0
- package/dist/handler/error/define.d.ts +3 -0
- package/dist/handler/error/index.d.ts +2 -0
- package/dist/handler/error/types.d.ts +11 -0
- package/dist/handler/index.d.ts +6 -0
- package/dist/handler/is.d.ts +2 -0
- package/dist/handler/types-base.d.ts +6 -0
- package/dist/handler/types.d.ts +5 -0
- package/dist/index.cjs +732 -695
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.mjs +724 -693
- package/dist/index.mjs.map +1 -1
- package/dist/layer/constants.d.ts +1 -0
- package/dist/layer/module.d.ts +9 -7
- package/dist/layer/type.d.ts +6 -3
- package/dist/layer/utils.d.ts +1 -1
- package/dist/path/matcher.d.ts +4 -4
- package/dist/plugin/index.d.ts +2 -0
- package/dist/plugin/is.d.ts +2 -0
- package/dist/plugin/types.d.ts +32 -0
- package/dist/request/helpers/body.d.ts +1 -1
- package/dist/request/helpers/cache.d.ts +1 -1
- package/dist/request/helpers/cookie.d.ts +1 -1
- package/dist/request/helpers/env.d.ts +1 -1
- package/dist/request/helpers/header-accept-charset.d.ts +1 -1
- package/dist/request/helpers/header-accept-language.d.ts +1 -1
- package/dist/request/helpers/header-accept.d.ts +1 -1
- package/dist/request/helpers/header-content-type.d.ts +1 -1
- package/dist/request/helpers/header.d.ts +1 -1
- package/dist/request/helpers/hostname.d.ts +1 -1
- package/dist/request/helpers/ip.d.ts +1 -1
- package/dist/request/helpers/mount-path.d.ts +1 -1
- package/dist/request/helpers/negotiator.d.ts +1 -1
- package/dist/request/helpers/params.d.ts +1 -1
- package/dist/request/helpers/path.d.ts +1 -1
- package/dist/request/helpers/protocol.d.ts +1 -1
- package/dist/request/helpers/query.d.ts +1 -1
- package/dist/request/helpers/router.d.ts +3 -3
- package/dist/request/types.d.ts +4 -0
- package/dist/response/helpers/cache.d.ts +1 -1
- package/dist/response/helpers/gone.d.ts +1 -1
- package/dist/response/helpers/header-attachment.d.ts +1 -1
- package/dist/response/helpers/header-content-type.d.ts +1 -1
- package/dist/response/helpers/header.d.ts +1 -1
- package/dist/response/helpers/send-accepted.d.ts +1 -1
- package/dist/response/helpers/send-created.d.ts +1 -1
- package/dist/response/helpers/send-file.d.ts +1 -1
- package/dist/response/helpers/send-format.d.ts +1 -1
- package/dist/response/helpers/send-redirect.d.ts +1 -1
- package/dist/response/helpers/send-stream.d.ts +2 -1
- package/dist/response/helpers/send-web-blob.d.ts +2 -1
- package/dist/response/helpers/send-web-response.d.ts +2 -1
- package/dist/response/helpers/send.d.ts +1 -1
- package/dist/response/helpers/utils.d.ts +1 -1
- package/dist/response/index.d.ts +1 -0
- package/dist/response/module.d.ts +2 -1
- package/dist/response/types.d.ts +4 -0
- package/dist/router/constants.d.ts +1 -0
- package/dist/router/index.d.ts +1 -0
- package/dist/router/module.d.ts +26 -23
- package/dist/router/utils.d.ts +2 -0
- package/dist/router-options/module.d.ts +1 -1
- package/dist/router-options/type.d.ts +3 -12
- package/dist/types.d.ts +0 -9
- package/dist/utils/is-instance.d.ts +1 -1
- package/package.json +15 -14
- package/dist/error.d.ts +0 -1
- package/dist/route/index.d.ts +0 -3
- package/dist/route/module.d.ts +0 -28
- package/dist/route/type.d.ts +0 -6
- package/dist/route/utils.d.ts +0 -2
package/README.md
CHANGED
|
@@ -12,14 +12,11 @@
|
|
|
12
12
|
[](https://snyk.io/test/github/Tada5hi/routup)
|
|
13
13
|
[](https://conventionalcommits.org)
|
|
14
14
|
|
|
15
|
-
**Routup** is a lightweight, runtime agnostic and
|
|
16
|
-
|
|
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
|
-
-
|
|
45
|
-
-
|
|
46
|
-
-
|
|
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
|
-
-
|
|
49
|
-
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
138
|
-
|
|
139
|
-
| [body](https://
|
|
140
|
-
| [cookie](https://
|
|
141
|
-
| [decorators](https://
|
|
142
|
-
| [prometheus](https://
|
|
143
|
-
| [query](https://
|
|
144
|
-
| [rate-limit](https://
|
|
145
|
-
| [rate-limit-redis](https://
|
|
146
|
-
| [static](https://
|
|
147
|
-
| [swagger](https://
|
|
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 {
|
|
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
|
|
11
|
+
params: Record<string, any>;
|
|
10
12
|
/**
|
|
11
13
|
* Path to check for the current instance.
|
|
12
14
|
*/
|
|
13
|
-
path
|
|
15
|
+
path: string;
|
|
14
16
|
/**
|
|
15
17
|
* The relative path on which the router is hung.
|
|
16
18
|
*/
|
|
17
|
-
mountPath
|
|
19
|
+
mountPath: string;
|
|
18
20
|
/**
|
|
19
21
|
* The error which occurred during a previous handler.
|
|
20
22
|
*/
|
|
21
|
-
error?:
|
|
23
|
+
error?: ErrorProxy;
|
|
22
24
|
/**
|
|
23
25
|
* Ids of chained router instances.
|
|
24
26
|
*/
|
|
25
|
-
|
|
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
|
|
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,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,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,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;
|