Skip to content

Commit

Permalink
feat: routesFromDir helper
Browse files Browse the repository at this point in the history
  • Loading branch information
sejori committed May 24, 2023
1 parent 0201aee commit 13db410
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
14 changes: 7 additions & 7 deletions lib/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import { Server } from "../server.ts"
import { Middleware, Route } from "../types.ts"
import { staticHandler } from "../handlers/static.ts"
import { Handler } from "../types.ts"

/**
* Merge source headers into base headers and return base
Expand All @@ -22,28 +22,28 @@ export const mergeHeaders = (base: Headers, source: Headers) => {
* @param dirUrl: URL
* @param middleware: Middleware for each route added
* @param routes: init Route array
* @param depth: used internally to correct paths, don't set
* @param _depth: used internally to correct paths, don't set
* @returns routes: Route[]
*/
export const staticDir = async (dirUrl: URL, middleware?: Middleware | Middleware[], routes?: Route[], _depth = 1): Promise<Route[]> => {
export const routesFromDir = async (dirUrl: URL, handlerGen: (url: URL) => Handler, middleware?: Middleware | Middleware[], _depth = 1): Promise<Route[]> => {
if (!(await Deno.stat(dirUrl)).isDirectory) throw new Error("URL does not point to directory.")
if (!routes) routes = []
const routes: Route[] = []

for await (const file of Deno.readDir(dirUrl)) {
const fileUrl = new URL(`file://${dirUrl.pathname}/${file.name}`)
if (file.isDirectory) {
await staticDir(fileUrl, middleware, routes, _depth+1)
const subDirRoutes = await routesFromDir(fileUrl, handlerGen, middleware, _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 filePath = `${dirPath}${file.name}`
routes.push({
path: `/${filePath}`,
middleware,
handler: staticHandler(fileUrl)
handler: handlerGen(fileUrl)
})
}
}
Expand Down
7 changes: 4 additions & 3 deletions tests/utils/helpers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { assert } from "https://deno.land/std@0.174.0/testing/asserts.ts"
import { Server } from "../../lib/server.ts"
import {
mergeHeaders,
staticDir
routesFromDir
} from "../../lib/utils/helpers.ts"
import { staticHandler } from "../../mod.ts"

Deno.test("UTIL: helpers", async (t) => {
await t.step("mergeHeaders", () => {
Expand All @@ -17,12 +18,12 @@ Deno.test("UTIL: helpers", async (t) => {
assert(base.has("Content-Type") && base.has("Authorization"))
})

await t.step("staticDir returns all file routes with supplied middleware and static handler", async () => {
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')

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

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

0 comments on commit 13db410

Please sign in to comment.