Skip to content

Commit

Permalink
Adopt servie-url for better cache re-use
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed May 19, 2019
1 parent 0f70f25 commit 341214c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
},
"dependencies": {
"debug": "^4.0.1",
"path-to-regexp": "^3.0.0"
"path-to-regexp": "^3.0.0",
"servie-url": "^1.0.0"
}
}
16 changes: 6 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import debug = require("debug");
import pathToRegexp = require("path-to-regexp");
import { Request, Response } from "servie";
import { format, parse } from "url";
import { getURL } from "servie-url";

const log = debug("servie-mount");

Expand Down Expand Up @@ -31,20 +31,16 @@ export function mount<T extends Request, U extends Response>(
log(`mount ${prefix} -> ${re}`);

return function(req: T & Partial<MountRequest>, next: () => Promise<U>) {
const Url = parse(req.url);
if (!Url.pathname) return next();

const match = re.exec(Url.pathname);
const url = getURL(req);
const match = re.exec(url.pathname);
if (!match) return next();

const prevUrl = req.url;
const prevMountPath = req[mountPath];

req.url = format({
...Url,
path: undefined,
pathname: Url.pathname.substr(match[0].length) || "/"
});
req.url = `${
prevUrl.startsWith(url.protocol) ? url.origin : ""
}${url.pathname.substr(match[0].length) || "/"}${url.search}${url.hash}`;

// Set mounted parameters on request.
const mountReq = Object.assign(req, {
Expand Down

0 comments on commit 341214c

Please sign in to comment.