From 660382c80e92a7bd973dd4ff619d5e8e2074d624 Mon Sep 17 00:00:00 2001 From: berlysia Date: Thu, 22 Feb 2024 13:35:27 +0900 Subject: [PATCH] feat: add createApp without default values (#85) --- package.json | 7 +++++ src/server/base.ts | 1 + src/server/index.ts | 2 +- src/server/server.ts | 57 +++++++++++++------------------------ src/server/with-defaults.ts | 38 +++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 39 deletions(-) create mode 100644 src/server/base.ts create mode 100644 src/server/with-defaults.ts diff --git a/package.json b/package.json index 5c7a780..071b225 100644 --- a/package.json +++ b/package.json @@ -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" @@ -69,6 +73,9 @@ "server": [ "./dist/server" ], + "server/base": [ + "./dist/server/base" + ], "client": [ "./dist/client" ], diff --git a/src/server/base.ts b/src/server/base.ts new file mode 100644 index 0000000..84ccb8a --- /dev/null +++ b/src/server/base.ts @@ -0,0 +1 @@ +export { createApp } from './server.js' diff --git a/src/server/index.ts b/src/server/index.ts index 693a5f6..eaa95f9 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -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' diff --git a/src/server/server.ts b/src/server/server.ts index 6048d42..6329db4 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -32,66 +32,47 @@ type MiddlewareFile = { default: MiddlewareHandler[] } type InitFunction = (app: Hono) => void -export type ServerOptions = { - ROUTES?: Record - RENDERER?: Record - NOT_FOUND?: Record - ERROR?: Record - MIDDLEWARE?: Record - root?: string +type BaseServerOptions = { + ROUTES: Record + RENDERER: Record + NOT_FOUND: Record + ERROR: Record + MIDDLEWARE: Record + root: string app?: Hono init?: InitFunction } -export const createApp = (options?: ServerOptions): Hono => { - const root = options?.root ?? '/app/routes' +export type ServerOptions = Partial> + +export const createApp = (options: BaseServerOptions): Hono => { + 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('/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('/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('/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('/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('/app/routes/**/[!_]*.(ts|tsx|mdx)', { - eager: true, - }) - - const routesMap = sortDirectoriesByDepth(groupByDirectory(ROUTES_FILE)) + const ROUTES_FILE = options.ROUTES + const routesMap = sortDirectoriesByDepth(groupByDirectory(ROUTES_FILE)) const getPaths = (currentDirectory: string, fileList: Record) => { let paths = fileList[currentDirectory] ?? [] diff --git a/src/server/with-defaults.ts b/src/server/with-defaults.ts new file mode 100644 index 0000000..dc8be11 --- /dev/null +++ b/src/server/with-defaults.ts @@ -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 = (options?: ServerOptions) => { + 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) +}