Comparing version 1.2.4 to 1.2.5
@@ -20,3 +20,3 @@ import { Response } from "./Response"; | ||
withHandler(path: string, method: string, handler: HttpHandler): ResourceRoutingHttpHandler; | ||
asServer(server: Http4jsServer): Http4jsServer; | ||
asServer(server?: Http4jsServer): Http4jsServer; | ||
match(request: Request): Promise<Response>; | ||
@@ -23,0 +23,0 @@ private defaultNotFoundHandler; |
@@ -6,2 +6,3 @@ "use strict"; | ||
var Uri_1 = require("./Uri"); | ||
var NativeServer_1 = require("./NativeServer"); | ||
var ResourceRoutingHttpHandler = (function () { | ||
@@ -34,2 +35,3 @@ function ResourceRoutingHttpHandler(path, method, handler) { | ||
ResourceRoutingHttpHandler.prototype.asServer = function (server) { | ||
if (server === void 0) { server = new NativeServer_1.NativeServer(3000); } | ||
this.server = server; | ||
@@ -41,8 +43,10 @@ server.registerCatchAllHandler(this); | ||
var exactMatch = this.handlers.find(function (it) { | ||
return request.uri.exactMatch(it.path) && it.verb == request.method; | ||
return request.uri.exactMatch(it.path) && request.method.match(it.verb) != null; | ||
}); | ||
var fuzzyMatch = this.handlers.find(function (it) { | ||
return it.path == "/" | ||
? false | ||
: it.path.includes("{") && Uri_1.Uri.of(it.path).templateMatch(request.uri.path) && it.verb == request.method; | ||
if (it.path == "/") | ||
return false; | ||
return it.path.includes("{") | ||
&& Uri_1.Uri.of(it.path).templateMatch(request.uri.path) | ||
&& request.method.match(it.verb) != null; | ||
}); | ||
@@ -52,3 +56,5 @@ var matchedHandler = exactMatch || fuzzyMatch; | ||
var handler = matchedHandler.handler; | ||
var filtered = this.filters.reduce(function (acc, next) { return next(acc); }, handler); | ||
var filtered = this.filters.reduce(function (acc, next) { | ||
return next(acc); | ||
}, handler); | ||
request.pathParams = matchedHandler.path.includes("{") | ||
@@ -60,3 +66,5 @@ ? Uri_1.Uri.of(matchedHandler.path).extract(request.uri.path).matches | ||
else { | ||
var filtered = this.filters.reduce(function (acc, next) { return next(acc); }, this.defaultNotFoundHandler); | ||
var filtered = this.filters.reduce(function (acc, next) { | ||
return next(acc); | ||
}, this.defaultNotFoundHandler); | ||
return filtered(request); | ||
@@ -63,0 +71,0 @@ } |
@@ -20,4 +20,3 @@ export declare class Uri { | ||
withProtocol(protocol: string): Uri; | ||
private uriTemplateToExactPathParamCapturingRegex(); | ||
private uriTemplateToPathParamCapturingRegex(); | ||
private _uriTemplateToPathParamCapturingRegex(); | ||
} |
@@ -24,8 +24,6 @@ "use strict"; | ||
Uri.prototype.exactMatch = function (path) { | ||
var exec = this.uriTemplateToExactPathParamCapturingRegex().exec(path); | ||
return exec != null; | ||
return new RegExp("^" + path + "$").exec(this.template) != null; | ||
}; | ||
Uri.prototype.templateMatch = function (path) { | ||
var exec = this.uriTemplateToPathParamCapturingRegex().exec(path); | ||
return exec != null; | ||
return this._uriTemplateToPathParamCapturingRegex().exec(path) != null; | ||
}; | ||
@@ -37,3 +35,3 @@ Uri.prototype.extract = function (uri) { | ||
.map(function (it) { return it.replace("{", "").replace("}", ""); }); | ||
var pathParams = this.uriTemplateToPathParamCapturingRegex().exec(decodedUri); | ||
var pathParams = this._uriTemplateToPathParamCapturingRegex().exec(decodedUri); | ||
pathParamNames.map(function (name, i) { | ||
@@ -56,6 +54,3 @@ _this.matches[name] = pathParams[i + 1]; | ||
}; | ||
Uri.prototype.uriTemplateToExactPathParamCapturingRegex = function () { | ||
return new RegExp(("^" + this.template + "$").replace(pathParamMatchingRegex, pathParamCaptureTemplate)); | ||
}; | ||
Uri.prototype.uriTemplateToPathParamCapturingRegex = function () { | ||
Uri.prototype._uriTemplateToPathParamCapturingRegex = function () { | ||
return new RegExp(this.template.replace(pathParamMatchingRegex, pathParamCaptureTemplate)); | ||
@@ -62,0 +57,0 @@ }; |
@@ -6,2 +6,3 @@ ### Table of Contents | ||
- [In Memory Server](/http4js/In-memory) | ||
- [Proxy](/http4js/Proxy) | ||
- [Example App](https://github.com/TomShacham/http4js-eg) | ||
@@ -8,0 +9,0 @@ |
@@ -8,2 +8,3 @@ # http4js | ||
- [In Memory Server](/http4js/In-memory) | ||
- [Proxy](/http4js/Proxy) | ||
- [Example App](https://github.com/TomShacham/http4js-eg) | ||
@@ -10,0 +11,0 @@ |
@@ -6,2 +6,3 @@ ### Table of Contents | ||
- [In Memory Server](/http4js/In-memory) | ||
- [Proxy](/http4js/Proxy) | ||
- [Example App](https://github.com/TomShacham/http4js-eg) | ||
@@ -8,0 +9,0 @@ |
24
index.js
@@ -5,8 +5,30 @@ "use strict"; | ||
} | ||
exports.__esModule = true; | ||
var RoutingHttpHandler_1 = require("./src/main/core/RoutingHttpHandler"); | ||
var Client_1 = require("./src/main/core/Client"); | ||
var Response_1 = require("./src/main/core/Response"); | ||
var NativeServer_1 = require("./src/main/core/NativeServer"); | ||
__export(require("./dist/main/core/RoutingHttpHandler")); | ||
__export(require("./dist/main/core/Request")); | ||
__export(require("./dist/main/core/Response")); | ||
__export(require("./dist/main/core/Server")); | ||
__export(require("./dist/main/core/Client")); | ||
__export(require("./dist/main/core/Body")); | ||
__export(require("./dist/main/core/Uri")); | ||
var upstream = RoutingHttpHandler_1.routes(".*", ".*", function (req) { | ||
var response = new Response_1.Response(200, req.headers); | ||
console.log("*** UPSTREAM RESPONSE ***"); | ||
console.log(response); | ||
return Promise.resolve(response); | ||
}) | ||
.asServer(new NativeServer_1.NativeServer(3001)) | ||
.start(); | ||
var proxy = RoutingHttpHandler_1.routes(".*", ".*", function (req) { | ||
var rewrittenRequest = req.setUri("http://localhost:3001/") | ||
.setHeader("x-proxy", "header from proxy") | ||
.setHeader("host", req.uri.href); | ||
console.log("*** REWRITTEN REQUEST ***"); | ||
console.log(rewrittenRequest); | ||
return Client_1.HttpClient(rewrittenRequest); | ||
}) | ||
.asServer() | ||
.start(); |
25
index.ts
@@ -0,1 +1,6 @@ | ||
import {getTo, routes} from "./src/main/core/RoutingHttpHandler"; | ||
import {HttpClient} from "./src/main/core/Client"; | ||
import {Request} from "./src/main/core/Request"; | ||
import {Response} from "./src/main/core/Response"; | ||
import {NativeServer} from "./src/main/core/NativeServer"; | ||
export * from "./dist/main/core/RoutingHttpHandler"; | ||
@@ -8,1 +13,21 @@ export * from "./dist/main/core/Request"; | ||
export * from "./dist/main/core/Uri"; | ||
const upstream = routes(".*", ".*", (req: Request) => { | ||
let response = new Response(200, req.headers); | ||
console.log("*** UPSTREAM RESPONSE ***"); | ||
console.log(response); | ||
return Promise.resolve(response); | ||
}) | ||
.asServer(new NativeServer(3001)) | ||
.start(); | ||
const proxy = routes(".*", ".*", (req: Request) => { | ||
let rewrittenRequest = req.setUri("http://localhost:3001/") | ||
.setHeader("x-proxy", "header from proxy") | ||
.setHeader("host", req.uri.href); | ||
console.log("*** REWRITTEN REQUEST ***"); | ||
console.log(rewrittenRequest); | ||
return HttpClient(rewrittenRequest); | ||
}) | ||
.asServer() | ||
.start(); |
{ | ||
"name": "http4js", | ||
"version": "1.2.4", | ||
"version": "1.2.5", | ||
"description": "A lightweight HTTP toolkit", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -117,5 +117,5 @@ ## http4js | ||
- support koa backend | ||
- support regex in path like * | ||
- status enum | ||
- complete http4js-eg | ||
- document unit testing routing and fakes | ||
- document a proxy |
Sorry, the diff of this file is not supported yet
226913
62
1895