Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add createApp without default routes #85

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"types": "./dist/server/index.d.ts",
"import": "./dist/server/index.js"
},
"./server/base": {
"types": "./dist/server/base.d.ts",
"import": "./dist/server/base.js"
},
"./client": {
"types": "./dist/client/index.d.ts",
"import": "./dist/client/index.js"
Expand Down Expand Up @@ -69,6 +73,9 @@
"server": [
"./dist/server"
],
"server/base": [
"./dist/server/base"
],
"client": [
"./dist/client"
],
Expand Down
1 change: 1 addition & 0 deletions src/server/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { createApp } from './server.js'
2 changes: 1 addition & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { createApp } from './server.js'
export { createApp } from './with-defaults.js'
export type { ServerOptions } from './server.js'
export * from './components/index.js'
57 changes: 19 additions & 38 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,66 +32,47 @@ type MiddlewareFile = { default: MiddlewareHandler[] }

type InitFunction<E extends Env = Env> = (app: Hono<E>) => void

export type ServerOptions<E extends Env = Env> = {
ROUTES?: Record<string, RouteFile>
RENDERER?: Record<string, RendererFile>
NOT_FOUND?: Record<string, NotFoundFile>
ERROR?: Record<string, ErrorFile>
MIDDLEWARE?: Record<string, MiddlewareFile>
root?: string
type BaseServerOptions<E extends Env = Env> = {
ROUTES: Record<string, RouteFile | AppFile>
RENDERER: Record<string, RendererFile>
NOT_FOUND: Record<string, NotFoundFile>
ERROR: Record<string, ErrorFile>
MIDDLEWARE: Record<string, MiddlewareFile>
root: string
app?: Hono<E>
init?: InitFunction<E>
}

export const createApp = <E extends Env>(options?: ServerOptions<E>): Hono<E> => {
const root = options?.root ?? '/app/routes'
export type ServerOptions<E extends Env = Env> = Partial<BaseServerOptions<E>>

export const createApp = <E extends Env>(options: BaseServerOptions<E>): Hono<E> => {
const root = options.root
const rootRegExp = new RegExp(`^${root}`)
const app = options?.app ?? new Hono()
const app = options.app ?? new Hono()

if (options?.init) {
if (options.init) {
options.init(app)
}

// Not Found
const NOT_FOUND_FILE =
options?.NOT_FOUND ??
import.meta.glob<NotFoundFile>('/app/routes/**/_404.(ts|tsx)', {
eager: true,
})
const NOT_FOUND_FILE = options.NOT_FOUND
const notFoundMap = groupByDirectory(NOT_FOUND_FILE)

// Error
const ERROR_FILE =
options?.ERROR ??
import.meta.glob<ErrorFile>('/app/routes/**/_error.(ts|tsx)', {
eager: true,
})
const ERROR_FILE = options.ERROR
const errorMap = groupByDirectory(ERROR_FILE)

// Renderer
const RENDERER_FILE =
options?.RENDERER ??
import.meta.glob<RendererFile>('/app/routes/**/_renderer.tsx', {
eager: true,
})
const RENDERER_FILE = options.RENDERER
const rendererList = listByDirectory(RENDERER_FILE)

// Middleware
const MIDDLEWARE_FILE =
options?.MIDDLEWARE ??
import.meta.glob<MiddlewareFile>('/app/routes/**/_middleware.(ts|tsx)', {
eager: true,
})
const MIDDLEWARE_FILE = options.MIDDLEWARE
const middlewareList = listByDirectory(MIDDLEWARE_FILE)

// Routes
const ROUTES_FILE =
options?.ROUTES ??
import.meta.glob<RouteFile | AppFile>('/app/routes/**/[!_]*.(ts|tsx|mdx)', {
eager: true,
})

const routesMap = sortDirectoriesByDepth(groupByDirectory(ROUTES_FILE))
const ROUTES_FILE = options.ROUTES
const routesMap = sortDirectoriesByDepth(groupByDirectory<RouteFile | AppFile>(ROUTES_FILE))

const getPaths = (currentDirectory: string, fileList: Record<string, string[]>) => {
let paths = fileList[currentDirectory] ?? []
Expand Down
38 changes: 38 additions & 0 deletions src/server/with-defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { Env } from 'hono'
import { createApp as baseCreateApp } from './server.js'
import type { ServerOptions } from './server.js'

export const createApp = <E extends Env>(options?: ServerOptions<E>) => {
const newOptions = {
root: options?.root ?? '/app/routes',
app: options?.app,
init: options?.init,
NOT_FOUND:
options?.NOT_FOUND ??
import.meta.glob('/app/routes/**/_404.(ts|tsx)', {
eager: true,
}),
ERROR:
options?.ERROR ??
import.meta.glob('/app/routes/**/_error.(ts|tsx)', {
eager: true,
}),
RENDERER:
options?.RENDERER ??
import.meta.glob('/app/routes/**/_renderer.tsx', {
eager: true,
}),
MIDDLEWARE:
options?.MIDDLEWARE ??
import.meta.glob('/app/routes/**/_middleware.(ts|tsx)', {
eager: true,
}),
ROUTES:
options?.ROUTES ??
import.meta.glob('/app/routes/**/[!_]*.(ts|tsx|mdx)', {
eager: true,
}),
}

return baseCreateApp(newOptions)
}