feat(jsx-renderer): `docType` option (#1621)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat(jsx-renderer): `docType` option * denoify * refactor Co-authored-by: Taku Amano <taku@taaas.jp> * denoify ---------
authored by Yusuke Wada · committed by GitHub · parent 3ead9a7 · Browse files · 3 files·+97 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Taku Amano <taku@taaas.jp>
Diff stats
3 files+97 added−20 removed
- tests:+49 added−0 removed
- other:+48 added−20 removed
Provenance
- Commit
517a6c4- Tree
86c9e1b- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
3ead9a7
3 files changed+97 added−20 removed
modified:deno_dist/middleware/jsx-renderer/index.ts+24 added-10 removed
8 unmodified lines9101112131415161718192021222324252614151617181920212728293031323334353637382539404128424344454610 unmodified lines8 unmodified linestype PropsForRenderer = [...Required<Parameters<Renderer>>] extends [unknown, infer Props] ? Props : unknown
type RendererOptions = { docType?: boolean | string}
const createRenderer = (c: Context, component?: FC<PropsForRenderer>, options?: RendererOptions) => (children: JSXNode, props: PropsForRenderer) => { const docType = typeof options?.docType === 'string' ? options.docType : options?.docType === true ? '<!DOCTYPE html>' : ''
(c: Context, component?: FC<PropsForRenderer>) => (children: JSXNode, props: PropsForRenderer) => /* eslint-disable @typescript-eslint/no-explicit-any */ c.html( jsx( RequestContext.Provider, { value: c }, (component ? component({ children, ...(props || {}) }) : children) as any ) as any return c.html( (docType + /* eslint-disable @typescript-eslint/no-explicit-any */ jsx( RequestContext.Provider, { value: c }, (component ? component({ children, ...(props || {}) }) : children) as any )) as any ) }
export const jsxRenderer = (component?: FC<PropsForRenderer>): MiddlewareHandler => (component?: FC<PropsForRenderer>, options?: RendererOptions): MiddlewareHandler => (c, next) => { /* eslint-disable @typescript-eslint/no-explicit-any */ c.setRenderer(createRenderer(c, component) as any) c.setRenderer(createRenderer(c, component, options) as any) return next() }
export const useRequestContext = <10 unmodified linesmodified:src/middleware/jsx-renderer/index.test.tsx+49 added
54 unmodified lines55565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011140 unmodified lines54 unmodified lines expect(res.status).toBe(200) expect(await res.text()).toBe('<h1>http://localhost/</h1>') })
it('Should return a default doctype', async () => { const app = new Hono() app.use( '*', jsxRenderer( ({ children }) => { return ( <html> <body>{children}</body> </html> ) }, { docType: true } ) ) app.get('/', (c) => c.render(<h1>Hello</h1>, { title: 'Title' })) const res = await app.request('/') expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe('<!DOCTYPE html><html><body><h1>Hello</h1></body></html>') })
it('Should return a custom doctype', async () => { const app = new Hono() app.use( '*', jsxRenderer( ({ children }) => { return ( <html> <body>{children}</body> </html> ) }, { docType: '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">', } ) ) app.get('/', (c) => c.render(<h1>Hello</h1>, { title: 'Title' })) const res = await app.request('/') expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe( '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><body><h1>Hello</h1></body></html>' ) })
it('Env', async () => { type JSXRendererEnv = { Variables: { foo: string40 unmodified linesmodified:src/middleware/jsx-renderer/index.ts+24 added-10 removed
8 unmodified lines9 type PropsForRenderer = [...Required<Parameters<Renderer>>] extends [unknown, infer Props]10 ? Props11 : unknown12 13+type RendererOptions = {14+ docType?: boolean | string15+}16+17 const createRenderer =18+ (c: Context, component?: FC<PropsForRenderer>, options?: RendererOptions) =>19+ (children: JSXNode, props: PropsForRenderer) => {20+ const docType =21+ typeof options?.docType === 'string'22+ ? options.docType23+ : options?.docType === true24+ ? '<!DOCTYPE html>'25+ : ''26+14- (c: Context, component?: FC<PropsForRenderer>) => (children: JSXNode, props: PropsForRenderer) =>15- /* eslint-disable @typescript-eslint/no-explicit-any */16- c.html(17- jsx(18- RequestContext.Provider,19- { value: c },20- (component ? component({ children, ...(props || {}) }) : children) as any21- ) as any27+ return c.html(28+ (docType +29+ /* eslint-disable @typescript-eslint/no-explicit-any */30+ jsx(31+ RequestContext.Provider,32+ { value: c },33+ (component ? component({ children, ...(props || {}) }) : children) as any34+ )) as any35 )36+ }37 38 export const jsxRenderer =25- (component?: FC<PropsForRenderer>): MiddlewareHandler =>39+ (component?: FC<PropsForRenderer>, options?: RendererOptions): MiddlewareHandler =>40 (c, next) => {41 /* eslint-disable @typescript-eslint/no-explicit-any */28- c.setRenderer(createRenderer(c, component) as any)42+ c.setRenderer(createRenderer(c, component, options) as any)43 return next()44 }45 46 export const useRequestContext = <10 unmodified lines