Skip to content

Commit

Permalink
Allow sync return from fn(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Nov 22, 2019
1 parent 21d1abb commit 553dace
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function mount<
P extends object = object
>(
prefix: Path,
fn: (req: T & MountRequest, done: () => Promise<U>) => Promise<U>,
fn: (req: T & MountRequest, done: () => Promise<U>) => U | Promise<U>,
options?: Options
) {
const check = match<P>(prefix, {
Expand All @@ -37,7 +37,10 @@ export function mount<

log(`mount ${prefix}`);

return function(req: T & Partial<MountRequest<P>>, next: () => Promise<U>) {
return async function mountMiddleware(
req: T & Partial<MountRequest<P>>,
next: () => Promise<U>
) {
const url = getURL(req);
const match = check(url.pathname);
if (!match) return next();
Expand All @@ -63,7 +66,7 @@ export function mount<

debug(`enter ${prevUrl} -> ${req.url}`);

return fn(mountReq, function() {
return fn(mountReq, () => {
debug(`leave ${prevUrl} <- ${req.url}`);
req.url = prevUrl;
req[path] = prevMountPath;
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"compilerOptions": {
"target": "es2015",
"lib": ["es2015"],
"target": "es2018",
"rootDir": "src",
"outDir": "dist",
"module": "commonjs",
Expand Down

0 comments on commit 553dace

Please sign in to comment.