Skip to content

Commit

Permalink
fix: default export handler detection
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaaas committed May 28, 2023
1 parent 93af59c commit 8e8a300
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ExpressLike, Options } from "./types"
import config from "./config"

import { generateRoutes, walkTree } from "./lib"
import { getHandlers, getMethodKey, isCjs } from "./utils"
import { getHandlers, getMethodKey } from "./utils"

const CJS_MAIN_FILENAME =
typeof require !== "undefined" && require.main?.filename
Expand All @@ -14,8 +14,6 @@ const PROJECT_DIRECTORY = CJS_MAIN_FILENAME
? path.dirname(CJS_MAIN_FILENAME)
: process.cwd()

const IS_CJS = isCjs()

/**
* Attach routes to an Express app or router instance
*
Expand Down Expand Up @@ -53,10 +51,18 @@ const createRouter = async <T extends ExpressLike = ExpressLike>(
}

// wildcard default export route matching
if (IS_CJS && typeof exports.default.default !== "undefined") {
app.all.apply(app, [url, ...getHandlers(exports.default.default)])
} else if (!IS_CJS && typeof exports.default !== "undefined") {
app.all.apply(app, [url, ...getHandlers(exports.default)])
if (typeof exports.default !== "undefined") {
if (
typeof exports.default === "function" ||
Array.isArray(exports.default)
) {
app.all.apply(app, [url, ...getHandlers(exports.default)])
} else if (
typeof exports.default === "object" &&
typeof exports.default.default !== "undefined"
) {
app.all.apply(app, [url, ...getHandlers(exports.default.default)])
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Route } from "./types"

import config from "./config"

export const isCjs = () => typeof module !== "undefined" && module?.exports
export const isCjs = () => typeof module !== "undefined" && !!module?.exports

/**
* @param parsedFile
Expand Down

0 comments on commit 8e8a300

Please sign in to comment.