Skip to content

Commit

Permalink
Merge pull request #173 from sejori/routesFromDir
Browse files Browse the repository at this point in the history
feat: routeGen fcn in routesFromDir
  • Loading branch information
sejori authored May 30, 2023
2 parents e18be3e + 1ffc739 commit 403df1b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
24 changes: 9 additions & 15 deletions lib/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Middleware, Route } from "../types.ts"
import { Handler } from "../types.ts"
import { Route } from "../types.ts"

/**
* Merge source headers into base headers and return base
Expand All @@ -19,31 +18,26 @@ export const mergeHeaders = (base: Headers, source: Headers) => {
* Recursively create an array of routes with the static handler
* for all files/sub-directories in the provided directory.
* @param dirUrl: URL
* @param middleware: Middleware for each route added
* @param routes: init Route array
* @param routeGen: (path: string, url: URL) => Route
* @param _depth: used internally to correct paths, don't set
* @returns routes: Route[]
*/
export const routesFromDir = async (dirUrl: URL, handlerGen: (url: URL) => Handler, middleware?: Middleware | Middleware[], _depth = 1): Promise<Route[]> => {
export const routesFromDir = async (dirUrl: URL, routeGen: (path: `/${string}`, url: URL) => Route, _depth = 1): Promise<Route[]> => {
if (!(await Deno.stat(dirUrl)).isDirectory) throw new Error("URL does not point to directory.")
const routes: Route[] = []

for await (const file of Deno.readDir(dirUrl)) {
const fileUrl = new URL(`file://${dirUrl.pathname}/${file.name}`)
if (file.isDirectory) {
const subDirRoutes = await routesFromDir(fileUrl, handlerGen, middleware, _depth+1)
const subDirRoutes = await routesFromDir(fileUrl, routeGen, _depth+1)
routes.splice(0, 0, ...subDirRoutes)
} else {
const pieces = dirUrl.pathname.split("/")
let dirPath = ''
for (let i=1; i<_depth; i++) dirPath = `${pieces[pieces.length-i]}/${dirPath}`
const pieces = dirUrl.pathname.split("/").filter(Boolean)
let dirPath: `/${string}` = '/'
for (let i=1; i<=_depth; i++) dirPath = `/${pieces[pieces.length-i]}${dirPath}`

const filePath = `${dirPath}${file.name}`
routes.push({
path: `/${filePath}`,
middleware,
handler: handlerGen(fileUrl)
})
const filePath: `/${string}` = `${dirPath}${file.name}`
routes.push(routeGen(filePath, fileUrl))
}
}

Expand Down
11 changes: 9 additions & 2 deletions tests/utils/helpers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ Deno.test("UTIL: helpers", async (t) => {

await t.step("routesFromDir returns all file routes with supplied middleware and handler", async () => {
const server = new Server()
const request = new Request('https://localhost:7777/utils/helpers_test.ts')
const request = new Request('https://localhost:7777/tests/utils/helpers_test.ts')

let text = ''
const routes = await routesFromDir(new URL("../", import.meta.url), (url: URL) => staticHandler(url), () => { text = "I was set" })
const routes = await routesFromDir(
new URL("../", import.meta.url),
(path, url) => ({
path: path,
middleware: () => { text = "I was set" },
handler: staticHandler(url)
})
)

assert(routes.find(route => route.path.includes("handlers")))
assert(routes.find(route => route.path.includes("middleware")))
Expand Down

0 comments on commit 403df1b

Please sign in to comment.