refactor: do some refactoring (#104)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent d7fe0b0 · Browse files · 5 files·+27 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+27 added−18 removed
- other:+27 added−18 removed
Provenance
- Commit
7efda2e- Tree
bd4c389- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
d7fe0b0
5 files changed+27 added−18 removed
modified:src/middleware/body-parse/body-parse.ts+2 added-1 removed
123456789101112131314151617186 unmodified linesimport type { Context } from '../../context'
export const bodyParse = () => { return async (ctx: Context, next: Function) => { const contentType = ctx.req.headers.get('Content-Type') || ''
if (contentType.includes('application/json')) { ctx.req.parsedBody = await ctx.req.json() } else if (contentType.includes('application/text')) { ctx.req.parsedBody = await ctx.req.text() } else if (contentType.includes('text/html')) { ctx.req.parsedBody = await ctx.req.text() } else if (contentType.includes('form')) { const form: { [key: string]: string | File } = {} const form: Record<string, string | File> = {} const data = [...(await ctx.req.formData())].reduce((acc, cur) => { acc[cur[0]] = cur[1] return acc }, form)6 unmodified linesmodified:src/middleware/mustache/mustache.ts+12 added-6 removed
2 unmodified lines34567789101112131314151617181718192021222 unmodified lines25262728292930313233343536373839403741424344454611 unmodified lines5859606162636465667 unmodified lines2 unmodified lines
const EXTENSION = '.mustache'const DEFAULT_DOCUMENT = 'index.mustache'
type Partials = { [file: string]: string }type Partials = Record<string, string>
interface Mustache { render: (content: string, params: object, partials: Partials) => string}
type Options = {type Init = { root: string}
export const mustache = (opt: Options = { root: '' }) => { const { root } = optexport const mustache = (init: Init = { root: '' }) => { const { root } = init
return async (c: Context, next: Function) => { let Mustache: Mustache try {2 unmodified lines throw new Error('If you want to use Mustache Middleware, install "mustache" package first.') }
c.render = async (filename, params = {}, options?) => { const path = getKVFilePath({ filename: `${filename}${EXTENSION}`, root: root, defaultDocument: DEFAULT_DOCUMENT }) const path = getKVFilePath({ filename: `${filename}${EXTENSION}`, root: root, defaultDocument: DEFAULT_DOCUMENT, })
const buffer = await getContentFromKVAsset(path) if (!buffer) { throw new Error(`Template "${path}" is not found or blank.`) } const content = bufferToString(buffer)
const partialArgs: { [name: string]: string } = {} const partialArgs: Record<string, string> = {}
if (options) { const partials = options as Partials for (const key of Object.keys(partials)) { const partialPath = getKVFilePath({11 unmodified lines
const output = Mustache.render(content, params, partialArgs) return c.html(output) }
await next() }}
7 unmodified linesmodified:src/context.ts+5 added-2 removed
1 import { isAbsoluteURL } from './utils/url'2 import { getStatusText } from './utils/http-status'3 4-type Headers = { [key: string]: string }4+type Headers = Record<string, string>5 type Data = string | ArrayBuffer | ReadableStream6 7 export interface Env {}8 7 unmodified lines16 private _statusText: string17 18 render: (template: string, params?: object, options?: object) => Promise<Response>19 20- constructor(req: Request<RequestParamKeyType>, opts?: { res: Response; env: Env; event: FetchEvent }) {20+ constructor(21+ req: Request<RequestParamKeyType>,22+ opts?: { res: Response; env: Env; event: FetchEvent }23+ ) {24 this.req = req25 if (opts) {26 this.res = opts.res27 this.env = opts.env98 unmodified linesmodified:src/hono.ts+2 added-3 removed
1 import type { Result } from './node'2-import { Node } from './node'2+import { Node, METHOD_NAME_OF_ALL } from './node'3 import { compose } from './compose'4 import { getPathFromURL } from './utils/url'5 import { Context } from './context'6 import type { Env } from './context'7 8-const METHOD_NAME_OF_ALL = 'ALL'9-8 declare global {9 interface Request<ParamKeyType = string> {10 param: (key: ParamKeyType) => string11 query: (key: string) => string12 header: (name: string) => string13+ // TODO: do not use `any`14 parsedBody: any15 }16 }17 238 unmodified linesmodified:src/node.ts+6 added-6 removed
1 import { splitPath, getPattern } from './utils/url'2 3-const METHOD_NAME_OF_ALL = 'ALL'3+export const METHOD_NAME_OF_ALL = 'ALL'4 5 export class Result<T> {6 handler: T7- params: { [key: string]: string }8- constructor(handler: T, params: { [key: string]: string }) {7+ params: Record<string, string>8+ constructor(handler: T, params: Record<string, string>) {9 this.handler = handler10 this.params = params11 }12 }2 unmodified lines15 return null16 }17 18 export class Node<T> {19- method: { [key: string]: T }19+ method: Record<string, T>20 handler: T21- children: { [key: string]: Node<T> }21+ children: Record<string, Node<T>>22 middlewares: []23 24- constructor(method?: string, handler?: T, children?: { [key: string]: Node<T> }) {24+ constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {25 this.children = children || {}26 this.method = {}27 if (method && handler) {28 this.method[method] = handler98 unmodified lines