@visulima/connect 4.0.0-alpha.2 → 4.0.0-alpha.21
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/CHANGELOG.md +121 -0
- package/LICENSE.md +1 -1
- package/dist/index.cjs +1 -22
- package/dist/index.d.cts +107 -12
- package/dist/index.d.mts +107 -12
- package/dist/index.d.ts +107 -12
- package/dist/index.mjs +1 -6
- package/dist/packem_shared/EdgeRouter-BcLsFhoc.mjs +1 -0
- package/dist/packem_shared/EdgeRouter-C2WRZNVK.cjs +1 -0
- package/dist/packem_shared/Router-CtUe2eVq.cjs +1 -0
- package/dist/packem_shared/Router-ZvrvfK8e.mjs +1 -0
- package/dist/packem_shared/createRouter-DgHlwXny.cjs +1 -0
- package/dist/packem_shared/createRouter-DrcL8lRU.mjs +1 -0
- package/dist/packem_shared/expressWrapper-BMEaodw_.mjs +1 -0
- package/dist/packem_shared/expressWrapper-C745BRwk.cjs +1 -0
- package/dist/packem_shared/sendJson-CIEUTdsi.mjs +1 -0
- package/dist/packem_shared/sendJson-CnJbUMZT.cjs +1 -0
- package/dist/packem_shared/withZod-B5rDrse4.mjs +1 -0
- package/dist/packem_shared/withZod-CQOb0zZR.cjs +1 -0
- package/package.json +40 -40
- package/dist/adapter/express.d.cts +0 -7
- package/dist/adapter/express.d.mts +0 -7
- package/dist/adapter/express.d.ts +0 -7
- package/dist/adapter/with-zod.d.cts +0 -8
- package/dist/adapter/with-zod.d.mts +0 -8
- package/dist/adapter/with-zod.d.ts +0 -8
- package/dist/edge.d.cts +0 -30
- package/dist/edge.d.mts +0 -30
- package/dist/edge.d.ts +0 -30
- package/dist/node.d.cts +0 -33
- package/dist/node.d.mts +0 -33
- package/dist/node.d.ts +0 -33
- package/dist/packem_shared/EdgeRouter-B-e2l9un.cjs +0 -96
- package/dist/packem_shared/EdgeRouter-CdtQHTwm.mjs +0 -90
- package/dist/packem_shared/Router-ChnTdYfl.mjs +0 -135
- package/dist/packem_shared/Router-Eg_OPxIL.cjs +0 -139
- package/dist/packem_shared/createRouter-C8hDVJcV.cjs +0 -102
- package/dist/packem_shared/createRouter-_G1XhaP4.mjs +0 -96
- package/dist/packem_shared/expressWrapper-CTExfWvM.cjs +0 -10
- package/dist/packem_shared/expressWrapper-spfs58is.mjs +0 -8
- package/dist/packem_shared/sendJson-DwLYLDbt.mjs +0 -7
- package/dist/packem_shared/sendJson-pJY2tT2n.cjs +0 -9
- package/dist/packem_shared/withZod-HYgoC7AX.mjs +0 -18
- package/dist/packem_shared/withZod-ULTsdyM5.cjs +0 -24
- package/dist/router.d.cts +0 -21
- package/dist/router.d.mts +0 -21
- package/dist/router.d.ts +0 -21
- package/dist/utils/send-json.d.cts +0 -9
- package/dist/utils/send-json.d.mts +0 -9
- package/dist/utils/send-json.d.ts +0 -9
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { parse } from 'regexparam';
|
|
2
|
-
|
|
3
|
-
class Router {
|
|
4
|
-
constructor(base = "/", routes = []) {
|
|
5
|
-
this.base = base;
|
|
6
|
-
this.routes = routes;
|
|
7
|
-
}
|
|
8
|
-
static async exec(fns, ...arguments_) {
|
|
9
|
-
let index = 0;
|
|
10
|
-
const next = () => {
|
|
11
|
-
const function_ = fns[++index];
|
|
12
|
-
if (function_ === void 0) {
|
|
13
|
-
return Promise.resolve();
|
|
14
|
-
}
|
|
15
|
-
return function_(...arguments_, next);
|
|
16
|
-
};
|
|
17
|
-
return fns[index](...arguments_, next);
|
|
18
|
-
}
|
|
19
|
-
add(method, route, ...fns) {
|
|
20
|
-
if (typeof route === "function") {
|
|
21
|
-
fns.unshift(route);
|
|
22
|
-
route = "";
|
|
23
|
-
}
|
|
24
|
-
if (route === "") {
|
|
25
|
-
this.routes.push({
|
|
26
|
-
fns,
|
|
27
|
-
isMiddleware: false,
|
|
28
|
-
matchAll: true,
|
|
29
|
-
method
|
|
30
|
-
});
|
|
31
|
-
} else {
|
|
32
|
-
const { keys, pattern } = parse(route);
|
|
33
|
-
this.routes.push({
|
|
34
|
-
fns,
|
|
35
|
-
isMiddleware: false,
|
|
36
|
-
keys,
|
|
37
|
-
method,
|
|
38
|
-
pattern
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
return this;
|
|
42
|
-
}
|
|
43
|
-
clone(base) {
|
|
44
|
-
return new Router(base, [...this.routes]);
|
|
45
|
-
}
|
|
46
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
47
|
-
find(method, pathname) {
|
|
48
|
-
let middleOnly = true;
|
|
49
|
-
const fns = [];
|
|
50
|
-
const parameters = {};
|
|
51
|
-
const isHead = method === "HEAD";
|
|
52
|
-
for (const route of this.routes) {
|
|
53
|
-
if (route.method !== method && route.method !== "" && !(isHead && route.method === "GET")) {
|
|
54
|
-
continue;
|
|
55
|
-
}
|
|
56
|
-
let matched = false;
|
|
57
|
-
if ("matchAll" in route) {
|
|
58
|
-
matched = true;
|
|
59
|
-
} else if (route.keys === false) {
|
|
60
|
-
const matches = route.pattern.exec(pathname);
|
|
61
|
-
if (matches === null) {
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
if (matches.groups !== void 0) {
|
|
65
|
-
Object.keys(matches.groups).forEach((key) => {
|
|
66
|
-
parameters[key] = matches.groups[key];
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
matched = true;
|
|
70
|
-
} else if (route.keys.length > 0) {
|
|
71
|
-
const matches = route.pattern.exec(pathname);
|
|
72
|
-
if (matches === null) {
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
for (let index = 0; index < route.keys.length; ) {
|
|
76
|
-
const parameterKey = route.keys[index];
|
|
77
|
-
parameters[parameterKey] = matches[++index];
|
|
78
|
-
}
|
|
79
|
-
matched = true;
|
|
80
|
-
} else if (route.pattern.test(pathname)) {
|
|
81
|
-
matched = true;
|
|
82
|
-
}
|
|
83
|
-
if (matched) {
|
|
84
|
-
fns.push(
|
|
85
|
-
...route.fns.flatMap((function_) => {
|
|
86
|
-
if (function_ instanceof Router) {
|
|
87
|
-
const { base } = function_;
|
|
88
|
-
let stripPathname = pathname.slice(base.length);
|
|
89
|
-
if (!stripPathname.startsWith("/")) {
|
|
90
|
-
stripPathname = `/${stripPathname}`;
|
|
91
|
-
}
|
|
92
|
-
const result = function_.find(method, stripPathname);
|
|
93
|
-
if (!result.middleOnly) {
|
|
94
|
-
middleOnly = false;
|
|
95
|
-
}
|
|
96
|
-
Object.assign(parameters, result.params);
|
|
97
|
-
return result.fns;
|
|
98
|
-
}
|
|
99
|
-
return function_;
|
|
100
|
-
})
|
|
101
|
-
);
|
|
102
|
-
if (!route.isMiddleware) {
|
|
103
|
-
middleOnly = false;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return { fns, middleOnly, params: parameters };
|
|
108
|
-
}
|
|
109
|
-
use(base, ...fns) {
|
|
110
|
-
if (typeof base === "function" || base instanceof Router) {
|
|
111
|
-
fns.unshift(base);
|
|
112
|
-
base = "/";
|
|
113
|
-
}
|
|
114
|
-
fns = fns.map((function_) => {
|
|
115
|
-
if (function_ instanceof Router) {
|
|
116
|
-
if (typeof base === "string") {
|
|
117
|
-
return function_.clone(base);
|
|
118
|
-
}
|
|
119
|
-
throw new Error("Mounting a router to RegExp base is not supported");
|
|
120
|
-
}
|
|
121
|
-
return function_;
|
|
122
|
-
});
|
|
123
|
-
const { keys, pattern } = parse(base, true);
|
|
124
|
-
this.routes.push({
|
|
125
|
-
fns,
|
|
126
|
-
isMiddleware: true,
|
|
127
|
-
keys,
|
|
128
|
-
method: "",
|
|
129
|
-
pattern
|
|
130
|
-
});
|
|
131
|
-
return this;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export { Router };
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
4
|
-
|
|
5
|
-
const regexparam = require('regexparam');
|
|
6
|
-
|
|
7
|
-
class Router {
|
|
8
|
-
constructor(base = "/", routes = []) {
|
|
9
|
-
this.base = base;
|
|
10
|
-
this.routes = routes;
|
|
11
|
-
}
|
|
12
|
-
static async exec(fns, ...arguments_) {
|
|
13
|
-
let index = 0;
|
|
14
|
-
const next = () => {
|
|
15
|
-
const function_ = fns[++index];
|
|
16
|
-
if (function_ === void 0) {
|
|
17
|
-
return Promise.resolve();
|
|
18
|
-
}
|
|
19
|
-
return function_(...arguments_, next);
|
|
20
|
-
};
|
|
21
|
-
return fns[index](...arguments_, next);
|
|
22
|
-
}
|
|
23
|
-
add(method, route, ...fns) {
|
|
24
|
-
if (typeof route === "function") {
|
|
25
|
-
fns.unshift(route);
|
|
26
|
-
route = "";
|
|
27
|
-
}
|
|
28
|
-
if (route === "") {
|
|
29
|
-
this.routes.push({
|
|
30
|
-
fns,
|
|
31
|
-
isMiddleware: false,
|
|
32
|
-
matchAll: true,
|
|
33
|
-
method
|
|
34
|
-
});
|
|
35
|
-
} else {
|
|
36
|
-
const { keys, pattern } = regexparam.parse(route);
|
|
37
|
-
this.routes.push({
|
|
38
|
-
fns,
|
|
39
|
-
isMiddleware: false,
|
|
40
|
-
keys,
|
|
41
|
-
method,
|
|
42
|
-
pattern
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
return this;
|
|
46
|
-
}
|
|
47
|
-
clone(base) {
|
|
48
|
-
return new Router(base, [...this.routes]);
|
|
49
|
-
}
|
|
50
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
51
|
-
find(method, pathname) {
|
|
52
|
-
let middleOnly = true;
|
|
53
|
-
const fns = [];
|
|
54
|
-
const parameters = {};
|
|
55
|
-
const isHead = method === "HEAD";
|
|
56
|
-
for (const route of this.routes) {
|
|
57
|
-
if (route.method !== method && route.method !== "" && !(isHead && route.method === "GET")) {
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
let matched = false;
|
|
61
|
-
if ("matchAll" in route) {
|
|
62
|
-
matched = true;
|
|
63
|
-
} else if (route.keys === false) {
|
|
64
|
-
const matches = route.pattern.exec(pathname);
|
|
65
|
-
if (matches === null) {
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
if (matches.groups !== void 0) {
|
|
69
|
-
Object.keys(matches.groups).forEach((key) => {
|
|
70
|
-
parameters[key] = matches.groups[key];
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
matched = true;
|
|
74
|
-
} else if (route.keys.length > 0) {
|
|
75
|
-
const matches = route.pattern.exec(pathname);
|
|
76
|
-
if (matches === null) {
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
for (let index = 0; index < route.keys.length; ) {
|
|
80
|
-
const parameterKey = route.keys[index];
|
|
81
|
-
parameters[parameterKey] = matches[++index];
|
|
82
|
-
}
|
|
83
|
-
matched = true;
|
|
84
|
-
} else if (route.pattern.test(pathname)) {
|
|
85
|
-
matched = true;
|
|
86
|
-
}
|
|
87
|
-
if (matched) {
|
|
88
|
-
fns.push(
|
|
89
|
-
...route.fns.flatMap((function_) => {
|
|
90
|
-
if (function_ instanceof Router) {
|
|
91
|
-
const { base } = function_;
|
|
92
|
-
let stripPathname = pathname.slice(base.length);
|
|
93
|
-
if (!stripPathname.startsWith("/")) {
|
|
94
|
-
stripPathname = `/${stripPathname}`;
|
|
95
|
-
}
|
|
96
|
-
const result = function_.find(method, stripPathname);
|
|
97
|
-
if (!result.middleOnly) {
|
|
98
|
-
middleOnly = false;
|
|
99
|
-
}
|
|
100
|
-
Object.assign(parameters, result.params);
|
|
101
|
-
return result.fns;
|
|
102
|
-
}
|
|
103
|
-
return function_;
|
|
104
|
-
})
|
|
105
|
-
);
|
|
106
|
-
if (!route.isMiddleware) {
|
|
107
|
-
middleOnly = false;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return { fns, middleOnly, params: parameters };
|
|
112
|
-
}
|
|
113
|
-
use(base, ...fns) {
|
|
114
|
-
if (typeof base === "function" || base instanceof Router) {
|
|
115
|
-
fns.unshift(base);
|
|
116
|
-
base = "/";
|
|
117
|
-
}
|
|
118
|
-
fns = fns.map((function_) => {
|
|
119
|
-
if (function_ instanceof Router) {
|
|
120
|
-
if (typeof base === "string") {
|
|
121
|
-
return function_.clone(base);
|
|
122
|
-
}
|
|
123
|
-
throw new Error("Mounting a router to RegExp base is not supported");
|
|
124
|
-
}
|
|
125
|
-
return function_;
|
|
126
|
-
});
|
|
127
|
-
const { keys, pattern } = regexparam.parse(base, true);
|
|
128
|
-
this.routes.push({
|
|
129
|
-
fns,
|
|
130
|
-
isMiddleware: true,
|
|
131
|
-
keys,
|
|
132
|
-
method: "",
|
|
133
|
-
pattern
|
|
134
|
-
});
|
|
135
|
-
return this;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
exports.Router = Router;
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
4
|
-
|
|
5
|
-
const withZod = require('./withZod-ULTsdyM5.cjs');
|
|
6
|
-
const Router = require('./Router-Eg_OPxIL.cjs');
|
|
7
|
-
|
|
8
|
-
const onNoMatch = async (request, response) => {
|
|
9
|
-
response.statusCode = 404;
|
|
10
|
-
response.end(request.method === "HEAD" ? void 0 : `Route ${request.method} ${request.url} not found`);
|
|
11
|
-
};
|
|
12
|
-
const onError = async (error, _request, response) => {
|
|
13
|
-
response.statusCode = 500;
|
|
14
|
-
console.error(error);
|
|
15
|
-
response.end("Internal Server Error");
|
|
16
|
-
};
|
|
17
|
-
const getPathname = (url) => {
|
|
18
|
-
const queryIndex = url.indexOf("?");
|
|
19
|
-
return queryIndex === -1 ? url : url.slice(0, Math.max(0, queryIndex));
|
|
20
|
-
};
|
|
21
|
-
class NodeRouter {
|
|
22
|
-
all = this.add.bind(this, "");
|
|
23
|
-
connect = this.add.bind(this, "CONNECT");
|
|
24
|
-
delete = this.add.bind(this, "DELETE");
|
|
25
|
-
get = this.add.bind(this, "GET");
|
|
26
|
-
head = this.add.bind(this, "HEAD");
|
|
27
|
-
options = this.add.bind(this, "OPTIONS");
|
|
28
|
-
patch = this.add.bind(this, "PATCH");
|
|
29
|
-
post = this.add.bind(this, "POST");
|
|
30
|
-
put = this.add.bind(this, "PUT");
|
|
31
|
-
trace = this.add.bind(this, "TRACE");
|
|
32
|
-
onError;
|
|
33
|
-
onNoMatch;
|
|
34
|
-
router = new Router.Router();
|
|
35
|
-
constructor(options = {}) {
|
|
36
|
-
this.onNoMatch = options.onNoMatch ?? onNoMatch;
|
|
37
|
-
this.onError = options.onError ?? onError;
|
|
38
|
-
}
|
|
39
|
-
clone() {
|
|
40
|
-
const r = new NodeRouter({ onError: this.onError, onNoMatch: this.onNoMatch });
|
|
41
|
-
r.router = this.router.clone();
|
|
42
|
-
return r;
|
|
43
|
-
}
|
|
44
|
-
handler() {
|
|
45
|
-
const { routes } = this.router;
|
|
46
|
-
return async (request, response) => {
|
|
47
|
-
const result = this.router.find(request.method, getPathname(request.url));
|
|
48
|
-
this.prepareRequest(request, result);
|
|
49
|
-
try {
|
|
50
|
-
await (result.fns.length === 0 || result.middleOnly ? this.onNoMatch(request, response, routes) : Router.Router.exec(result.fns, request, response));
|
|
51
|
-
} catch (error) {
|
|
52
|
-
await this.onError(error, request, response, routes);
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
async run(request, response) {
|
|
57
|
-
const result = this.router.find(request.method, getPathname(request.url));
|
|
58
|
-
if (result.fns.length === 0) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
this.prepareRequest(request, result);
|
|
62
|
-
return await Router.Router.exec(result.fns, request, response);
|
|
63
|
-
}
|
|
64
|
-
use(base, ...fns) {
|
|
65
|
-
if (typeof base === "function" || base instanceof NodeRouter) {
|
|
66
|
-
fns.unshift(base);
|
|
67
|
-
base = "/";
|
|
68
|
-
}
|
|
69
|
-
this.router.use(base, ...fns.map((function_) => function_ instanceof NodeRouter ? function_.router : function_));
|
|
70
|
-
return this;
|
|
71
|
-
}
|
|
72
|
-
add(method, routeOrFunction, zodOrRouteOrFunction, ...fns) {
|
|
73
|
-
if (typeof routeOrFunction === "string" && typeof zodOrRouteOrFunction === "function") {
|
|
74
|
-
fns = [zodOrRouteOrFunction];
|
|
75
|
-
} else if (typeof zodOrRouteOrFunction === "object") {
|
|
76
|
-
if (typeof routeOrFunction === "function") {
|
|
77
|
-
fns = [withZod(zodOrRouteOrFunction, routeOrFunction)];
|
|
78
|
-
} else {
|
|
79
|
-
fns = fns.map(
|
|
80
|
-
(function_) => withZod(zodOrRouteOrFunction, function_)
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
} else if (typeof zodOrRouteOrFunction === "function") {
|
|
84
|
-
fns = [zodOrRouteOrFunction];
|
|
85
|
-
}
|
|
86
|
-
this.router.add(method, routeOrFunction, ...fns);
|
|
87
|
-
return this;
|
|
88
|
-
}
|
|
89
|
-
// eslint-disable-next-line class-methods-use-this
|
|
90
|
-
prepareRequest(request, findResult) {
|
|
91
|
-
request.params = {
|
|
92
|
-
...findResult.params,
|
|
93
|
-
...request.params
|
|
94
|
-
// original params will take precedence
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
const createRouter = (options = {}) => new NodeRouter(options);
|
|
99
|
-
|
|
100
|
-
exports.NodeRouter = NodeRouter;
|
|
101
|
-
exports.createRouter = createRouter;
|
|
102
|
-
exports.getPathname = getPathname;
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import withZod from './withZod-HYgoC7AX.mjs';
|
|
2
|
-
import { Router } from './Router-ChnTdYfl.mjs';
|
|
3
|
-
|
|
4
|
-
const onNoMatch = async (request, response) => {
|
|
5
|
-
response.statusCode = 404;
|
|
6
|
-
response.end(request.method === "HEAD" ? void 0 : `Route ${request.method} ${request.url} not found`);
|
|
7
|
-
};
|
|
8
|
-
const onError = async (error, _request, response) => {
|
|
9
|
-
response.statusCode = 500;
|
|
10
|
-
console.error(error);
|
|
11
|
-
response.end("Internal Server Error");
|
|
12
|
-
};
|
|
13
|
-
const getPathname = (url) => {
|
|
14
|
-
const queryIndex = url.indexOf("?");
|
|
15
|
-
return queryIndex === -1 ? url : url.slice(0, Math.max(0, queryIndex));
|
|
16
|
-
};
|
|
17
|
-
class NodeRouter {
|
|
18
|
-
all = this.add.bind(this, "");
|
|
19
|
-
connect = this.add.bind(this, "CONNECT");
|
|
20
|
-
delete = this.add.bind(this, "DELETE");
|
|
21
|
-
get = this.add.bind(this, "GET");
|
|
22
|
-
head = this.add.bind(this, "HEAD");
|
|
23
|
-
options = this.add.bind(this, "OPTIONS");
|
|
24
|
-
patch = this.add.bind(this, "PATCH");
|
|
25
|
-
post = this.add.bind(this, "POST");
|
|
26
|
-
put = this.add.bind(this, "PUT");
|
|
27
|
-
trace = this.add.bind(this, "TRACE");
|
|
28
|
-
onError;
|
|
29
|
-
onNoMatch;
|
|
30
|
-
router = new Router();
|
|
31
|
-
constructor(options = {}) {
|
|
32
|
-
this.onNoMatch = options.onNoMatch ?? onNoMatch;
|
|
33
|
-
this.onError = options.onError ?? onError;
|
|
34
|
-
}
|
|
35
|
-
clone() {
|
|
36
|
-
const r = new NodeRouter({ onError: this.onError, onNoMatch: this.onNoMatch });
|
|
37
|
-
r.router = this.router.clone();
|
|
38
|
-
return r;
|
|
39
|
-
}
|
|
40
|
-
handler() {
|
|
41
|
-
const { routes } = this.router;
|
|
42
|
-
return async (request, response) => {
|
|
43
|
-
const result = this.router.find(request.method, getPathname(request.url));
|
|
44
|
-
this.prepareRequest(request, result);
|
|
45
|
-
try {
|
|
46
|
-
await (result.fns.length === 0 || result.middleOnly ? this.onNoMatch(request, response, routes) : Router.exec(result.fns, request, response));
|
|
47
|
-
} catch (error) {
|
|
48
|
-
await this.onError(error, request, response, routes);
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
async run(request, response) {
|
|
53
|
-
const result = this.router.find(request.method, getPathname(request.url));
|
|
54
|
-
if (result.fns.length === 0) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
this.prepareRequest(request, result);
|
|
58
|
-
return await Router.exec(result.fns, request, response);
|
|
59
|
-
}
|
|
60
|
-
use(base, ...fns) {
|
|
61
|
-
if (typeof base === "function" || base instanceof NodeRouter) {
|
|
62
|
-
fns.unshift(base);
|
|
63
|
-
base = "/";
|
|
64
|
-
}
|
|
65
|
-
this.router.use(base, ...fns.map((function_) => function_ instanceof NodeRouter ? function_.router : function_));
|
|
66
|
-
return this;
|
|
67
|
-
}
|
|
68
|
-
add(method, routeOrFunction, zodOrRouteOrFunction, ...fns) {
|
|
69
|
-
if (typeof routeOrFunction === "string" && typeof zodOrRouteOrFunction === "function") {
|
|
70
|
-
fns = [zodOrRouteOrFunction];
|
|
71
|
-
} else if (typeof zodOrRouteOrFunction === "object") {
|
|
72
|
-
if (typeof routeOrFunction === "function") {
|
|
73
|
-
fns = [withZod(zodOrRouteOrFunction, routeOrFunction)];
|
|
74
|
-
} else {
|
|
75
|
-
fns = fns.map(
|
|
76
|
-
(function_) => withZod(zodOrRouteOrFunction, function_)
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
} else if (typeof zodOrRouteOrFunction === "function") {
|
|
80
|
-
fns = [zodOrRouteOrFunction];
|
|
81
|
-
}
|
|
82
|
-
this.router.add(method, routeOrFunction, ...fns);
|
|
83
|
-
return this;
|
|
84
|
-
}
|
|
85
|
-
// eslint-disable-next-line class-methods-use-this
|
|
86
|
-
prepareRequest(request, findResult) {
|
|
87
|
-
request.params = {
|
|
88
|
-
...findResult.params,
|
|
89
|
-
...request.params
|
|
90
|
-
// original params will take precedence
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
const createRouter = (options = {}) => new NodeRouter(options);
|
|
95
|
-
|
|
96
|
-
export { NodeRouter, createRouter, getPathname };
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const expressWrapper = (function_) => async (request, response, next) => (
|
|
4
|
-
// eslint-disable-next-line compat/compat
|
|
5
|
-
await new Promise((resolve, reject) => {
|
|
6
|
-
function_(request, response, (error) => error ? reject(error) : resolve());
|
|
7
|
-
}).then(next)
|
|
8
|
-
);
|
|
9
|
-
|
|
10
|
-
module.exports = expressWrapper;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
const expressWrapper = (function_) => async (request, response, next) => (
|
|
2
|
-
// eslint-disable-next-line compat/compat
|
|
3
|
-
await new Promise((resolve, reject) => {
|
|
4
|
-
function_(request, response, (error) => error ? reject(error) : resolve());
|
|
5
|
-
}).then(next)
|
|
6
|
-
);
|
|
7
|
-
|
|
8
|
-
export { expressWrapper as default };
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const sendJson = (response, statusCode, jsonBody) => {
|
|
4
|
-
response.setHeader("content-type", "application/json; charset=utf-8");
|
|
5
|
-
response.statusCode = statusCode;
|
|
6
|
-
response.end(JSON.stringify(jsonBody, null, 2));
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
module.exports = sendJson;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import createHttpError from 'http-errors';
|
|
2
|
-
import { ZodError } from 'zod';
|
|
3
|
-
|
|
4
|
-
const withZod = (schema, handler) => async (request, response, next) => {
|
|
5
|
-
let transformedRequest = request;
|
|
6
|
-
try {
|
|
7
|
-
transformedRequest = await schema.parseAsync(request);
|
|
8
|
-
} catch (error) {
|
|
9
|
-
let { message } = error;
|
|
10
|
-
if (error instanceof ZodError && typeof error.format === "function") {
|
|
11
|
-
message = error.issues.map((issue) => `${issue.path.join("/")} - ${issue.message}`).join("/n");
|
|
12
|
-
}
|
|
13
|
-
throw createHttpError(422, message);
|
|
14
|
-
}
|
|
15
|
-
return handler(transformedRequest, response, next);
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export { withZod as default };
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const createHttpError = require('http-errors');
|
|
4
|
-
const zod = require('zod');
|
|
5
|
-
|
|
6
|
-
const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
|
|
7
|
-
|
|
8
|
-
const createHttpError__default = /*#__PURE__*/_interopDefaultCompat(createHttpError);
|
|
9
|
-
|
|
10
|
-
const withZod = (schema, handler) => async (request, response, next) => {
|
|
11
|
-
let transformedRequest = request;
|
|
12
|
-
try {
|
|
13
|
-
transformedRequest = await schema.parseAsync(request);
|
|
14
|
-
} catch (error) {
|
|
15
|
-
let { message } = error;
|
|
16
|
-
if (error instanceof zod.ZodError && typeof error.format === "function") {
|
|
17
|
-
message = error.issues.map((issue) => `${issue.path.join("/")} - ${issue.message}`).join("/n");
|
|
18
|
-
}
|
|
19
|
-
throw createHttpError__default(422, message);
|
|
20
|
-
}
|
|
21
|
-
return handler(transformedRequest, response, next);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
module.exports = withZod;
|
package/dist/router.d.cts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { FindResult, FunctionLike, HttpMethod, Nextable, RouteMatch } from "./types.d.d.cts";
|
|
2
|
-
export type Route<H> = {
|
|
3
|
-
fns: (H | Router<H extends FunctionLike ? H : never>)[];
|
|
4
|
-
isMiddleware: boolean;
|
|
5
|
-
method: HttpMethod | "";
|
|
6
|
-
} & ({
|
|
7
|
-
keys: string[] | false;
|
|
8
|
-
pattern: RegExp;
|
|
9
|
-
} | {
|
|
10
|
-
matchAll: true;
|
|
11
|
-
});
|
|
12
|
-
export declare class Router<H extends FunctionLike> {
|
|
13
|
-
base: string;
|
|
14
|
-
routes: Route<Nextable<H>>[];
|
|
15
|
-
static exec<FL extends FunctionLike>(fns: (Nextable<FL> | undefined)[], ...arguments_: Parameters<FL>): Promise<any>;
|
|
16
|
-
constructor(base?: string, routes?: Route<Nextable<H>>[]);
|
|
17
|
-
add(method: HttpMethod | "", route: Nextable<H> | RouteMatch, ...fns: Nextable<H>[]): this;
|
|
18
|
-
clone(base?: string): Router<H>;
|
|
19
|
-
find(method: HttpMethod, pathname: string): FindResult<H>;
|
|
20
|
-
use(base: Nextable<H> | RouteMatch | Router<H>, ...fns: (Nextable<H> | Router<H>)[]): this;
|
|
21
|
-
}
|
package/dist/router.d.mts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { FindResult, FunctionLike, HttpMethod, Nextable, RouteMatch } from "./types.d.d.mts";
|
|
2
|
-
export type Route<H> = {
|
|
3
|
-
fns: (H | Router<H extends FunctionLike ? H : never>)[];
|
|
4
|
-
isMiddleware: boolean;
|
|
5
|
-
method: HttpMethod | "";
|
|
6
|
-
} & ({
|
|
7
|
-
keys: string[] | false;
|
|
8
|
-
pattern: RegExp;
|
|
9
|
-
} | {
|
|
10
|
-
matchAll: true;
|
|
11
|
-
});
|
|
12
|
-
export declare class Router<H extends FunctionLike> {
|
|
13
|
-
base: string;
|
|
14
|
-
routes: Route<Nextable<H>>[];
|
|
15
|
-
static exec<FL extends FunctionLike>(fns: (Nextable<FL> | undefined)[], ...arguments_: Parameters<FL>): Promise<any>;
|
|
16
|
-
constructor(base?: string, routes?: Route<Nextable<H>>[]);
|
|
17
|
-
add(method: HttpMethod | "", route: Nextable<H> | RouteMatch, ...fns: Nextable<H>[]): this;
|
|
18
|
-
clone(base?: string): Router<H>;
|
|
19
|
-
find(method: HttpMethod, pathname: string): FindResult<H>;
|
|
20
|
-
use(base: Nextable<H> | RouteMatch | Router<H>, ...fns: (Nextable<H> | Router<H>)[]): this;
|
|
21
|
-
}
|
package/dist/router.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { FindResult, FunctionLike, HttpMethod, Nextable, RouteMatch } from "./types.d.d.ts";
|
|
2
|
-
export type Route<H> = {
|
|
3
|
-
fns: (H | Router<H extends FunctionLike ? H : never>)[];
|
|
4
|
-
isMiddleware: boolean;
|
|
5
|
-
method: HttpMethod | "";
|
|
6
|
-
} & ({
|
|
7
|
-
keys: string[] | false;
|
|
8
|
-
pattern: RegExp;
|
|
9
|
-
} | {
|
|
10
|
-
matchAll: true;
|
|
11
|
-
});
|
|
12
|
-
export declare class Router<H extends FunctionLike> {
|
|
13
|
-
base: string;
|
|
14
|
-
routes: Route<Nextable<H>>[];
|
|
15
|
-
static exec<FL extends FunctionLike>(fns: (Nextable<FL> | undefined)[], ...arguments_: Parameters<FL>): Promise<any>;
|
|
16
|
-
constructor(base?: string, routes?: Route<Nextable<H>>[]);
|
|
17
|
-
add(method: HttpMethod | "", route: Nextable<H> | RouteMatch, ...fns: Nextable<H>[]): this;
|
|
18
|
-
clone(base?: string): Router<H>;
|
|
19
|
-
find(method: HttpMethod, pathname: string): FindResult<H>;
|
|
20
|
-
use(base: Nextable<H> | RouteMatch | Router<H>, ...fns: (Nextable<H> | Router<H>)[]): this;
|
|
21
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { ServerResponse } from "node:http";
|
|
2
|
-
/**
|
|
3
|
-
* Send `JSON` object
|
|
4
|
-
* @param response response object
|
|
5
|
-
* @param statusCode
|
|
6
|
-
* @param jsonBody of data
|
|
7
|
-
*/
|
|
8
|
-
declare const sendJson: (response: ServerResponse, statusCode: number, jsonBody: unknown) => void;
|
|
9
|
-
export = sendJson;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { ServerResponse } from "node:http";
|
|
2
|
-
/**
|
|
3
|
-
* Send `JSON` object
|
|
4
|
-
* @param response response object
|
|
5
|
-
* @param statusCode
|
|
6
|
-
* @param jsonBody of data
|
|
7
|
-
*/
|
|
8
|
-
declare const sendJson: (response: ServerResponse, statusCode: number, jsonBody: unknown) => void;
|
|
9
|
-
export default sendJson;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { ServerResponse } from "node:http";
|
|
2
|
-
/**
|
|
3
|
-
* Send `JSON` object
|
|
4
|
-
* @param response response object
|
|
5
|
-
* @param statusCode
|
|
6
|
-
* @param jsonBody of data
|
|
7
|
-
*/
|
|
8
|
-
declare const sendJson: (response: ServerResponse, statusCode: number, jsonBody: unknown) => void;
|
|
9
|
-
export = sendJson;
|