@arabesque/middleware-http-router 1.0.0-alpha.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.
- package/LICENSE +20 -0
- package/README.md +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +18 -0
- package/dist/lib/http-method.d.ts +1 -0
- package/dist/lib/http-method.js +2 -0
- package/dist/lib/route.d.ts +6 -0
- package/dist/lib/route.js +29 -0
- package/dist/lib/router.d.ts +4 -0
- package/dist/lib/router.js +12 -0
- package/package.json +44 -0
package/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2021 Eric MORAND
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# HTTP Router Middleware for [Arabesque](https://www.npmjs.com/package/@arabesque/core)
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
exports.__esModule = true;
|
14
|
+
exports.createRouter = exports.createRoute = void 0;
|
15
|
+
var route_1 = require("./lib/route");
|
16
|
+
__createBinding(exports, route_1, "createRoute");
|
17
|
+
var router_1 = require("./lib/router");
|
18
|
+
__createBinding(exports, router_1, "createRouter");
|
@@ -0,0 +1 @@
|
|
1
|
+
export type HTTPMethod = 'CONNECT ' | 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS ' | 'PATCH' | 'POST' | 'PUT' | 'TRACE';
|
@@ -0,0 +1,6 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
import type { Context } from "@arabesque/listener-http";
|
3
|
+
import type { Middleware } from "@arabesque/core";
|
4
|
+
import type { HTTPMethod } from "./http-method";
|
5
|
+
export type RouteHandler<Context, Parameters extends Record<string, any>> = (context: Context, parameters: Parameters) => Promise<void>;
|
6
|
+
export declare const createRoute: <C extends Context<import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>>, P extends Record<string, string>>(method: HTTPMethod, specification: string, handler: RouteHandler<C, P>) => Middleware<C>;
|
@@ -0,0 +1,29 @@
|
|
1
|
+
"use strict";
|
2
|
+
exports.__esModule = true;
|
3
|
+
exports.createRoute = void 0;
|
4
|
+
var url_1 = require("url");
|
5
|
+
var RouteParser = require("route-parser");
|
6
|
+
var http_1 = require("http");
|
7
|
+
var isAnHTTPMethod = function (candidate) {
|
8
|
+
return http_1.METHODS.includes(candidate);
|
9
|
+
};
|
10
|
+
var createRoute = function (method, specification, handler) {
|
11
|
+
return function (context, next) {
|
12
|
+
var message = context.message;
|
13
|
+
if (message.url && context.message.method && isAnHTTPMethod(context.message.method) && (context.message.method === method)) {
|
14
|
+
var pathname = (0, url_1.parse)(message.url).pathname;
|
15
|
+
if (pathname !== null) {
|
16
|
+
var routeParser = new RouteParser(specification);
|
17
|
+
var parameters = routeParser.match(pathname);
|
18
|
+
if (parameters !== false) {
|
19
|
+
return handler(context, parameters)
|
20
|
+
.then(function () {
|
21
|
+
return next(context);
|
22
|
+
});
|
23
|
+
}
|
24
|
+
}
|
25
|
+
}
|
26
|
+
return Promise.resolve(context);
|
27
|
+
};
|
28
|
+
};
|
29
|
+
exports.createRoute = createRoute;
|
@@ -0,0 +1,4 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
import type { Middleware } from "@arabesque/core";
|
3
|
+
import type { Context } from "@arabesque/listener-http";
|
4
|
+
export declare const createRouter: <C extends Context<import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>>>(...routes: Middleware<C>[]) => Middleware<C>;
|
@@ -0,0 +1,12 @@
|
|
1
|
+
"use strict";
|
2
|
+
exports.__esModule = true;
|
3
|
+
exports.createRouter = void 0;
|
4
|
+
var logic_middlewares_1 = require("@arabesque/logic-middlewares");
|
5
|
+
var createRouter = function () {
|
6
|
+
var routes = [];
|
7
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
8
|
+
routes[_i] = arguments[_i];
|
9
|
+
}
|
10
|
+
return logic_middlewares_1.createORMiddleware.apply(void 0, routes);
|
11
|
+
};
|
12
|
+
exports.createRouter = createRouter;
|
package/package.json
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
{
|
2
|
+
"name": "@arabesque/middleware-http-router",
|
3
|
+
"description": "",
|
4
|
+
"main": "dist/index.js",
|
5
|
+
"types": "dist/index.d.ts",
|
6
|
+
"files": [
|
7
|
+
"dist",
|
8
|
+
"README.md",
|
9
|
+
"LICENSE"
|
10
|
+
],
|
11
|
+
"scripts": {
|
12
|
+
"prebuild": "rimraf dist",
|
13
|
+
"prebuild:test": "rimraf dist",
|
14
|
+
"prepack": "npm run build",
|
15
|
+
"pretest": "npm run build:test",
|
16
|
+
"build": "tsc --project src --declaration true --outDir dist",
|
17
|
+
"build:test": "tsc --project test --outDir dist",
|
18
|
+
"cover": "nyc --include 'dist/src/**' --exclude-after-remap=false --all=true --branches=100 --functions=100 --lines=100 --statements=100 --reporter html --reporter text npm t",
|
19
|
+
"test": "exit 0"
|
20
|
+
},
|
21
|
+
"keywords": [
|
22
|
+
"arabesque",
|
23
|
+
"middleware",
|
24
|
+
"router",
|
25
|
+
"http"
|
26
|
+
],
|
27
|
+
"author": "Eric MORAND <eric.morand@gmail.com>",
|
28
|
+
"license": "MIT License",
|
29
|
+
"devDependencies": {
|
30
|
+
"@arabesque/core": "^0.3.0",
|
31
|
+
"@arabesque/listener-http": "^0.1.1",
|
32
|
+
"@types/node": "^16.18.3",
|
33
|
+
"@types/route-parser": "^0.1.4",
|
34
|
+
"nyc": "^15.1.0",
|
35
|
+
"rimraf": "^3.0.2",
|
36
|
+
"tape": "^5.6.1",
|
37
|
+
"typescript": "^4.9.3"
|
38
|
+
},
|
39
|
+
"dependencies": {
|
40
|
+
"@arabesque/logic-middlewares": "^0.2.0",
|
41
|
+
"route-parser": "^0.0.5"
|
42
|
+
},
|
43
|
+
"version": "1.0.0-alpha.0"
|
44
|
+
}
|