feat(jsx-renderer): pass the context as 2nd arg (#2459)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat(jsx-renderer): pass the context as 2nd arg * denoify
authored by Yusuke Wada · committed by GitHub · parent 84bcbe8 · Browse files · 3 files·+53 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
3 files+53 added−18 removed
- tests:+23 added−0 removed
- other:+30 added−18 removed
Provenance
- Commit
c99f696- Tree
03e1f7a- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
84bcbe8
3 files changed+53 added−18 removed
modified:deno_dist/middleware/jsx-renderer/index.ts+15 added-9 removed
3 unmodified lines45678910111213141516171819202122232425262717181920212228293031322 unmodified lines3536373833394041424321 unmodified lines65666768636970717273747570767778798013 unmodified lines3 unmodified linesimport { jsx, createContext, useContext, Fragment } from '../../jsx/index.ts'import type { FC, PropsWithChildren, JSXNode } from '../../jsx/index.ts'import { renderToReadableStream } from '../../jsx/streaming.ts'import type { Env, Input, MiddlewareHandler } from '../../types.ts'import type { HtmlEscapedString } from '../../utils/html.ts'
export const RequestContext = createContext<Context | null>(null)
type RendererOptions = { docType?: boolean | string stream?: boolean | Record<string, string>}
type Component = ( props: PropsForRenderer & { Layout: FC }, c: Context) => HtmlEscapedString | Promise<HtmlEscapedString>
type ComponentWithChildren = ( props: PropsWithChildren<PropsForRenderer & { Layout: FC }>, c: Context) => HtmlEscapedString | Promise<HtmlEscapedString>
const createRenderer = ( c: Context, Layout: FC, component?: FC<PropsForRenderer & { Layout: FC }>, options?: RendererOptions ) => (c: Context, Layout: FC, component?: Component, options?: RendererOptions) => (children: JSXNode, props: PropsForRenderer) => { const docType = typeof options?.docType === 'string' ? options.docType2 unmodified lines : '<!DOCTYPE html>'
const currentLayout = component ? jsx( component, (props: any) => component(props, c), { ...{ Layout, ...(props as any) }, }, children as any21 unmodified lines } }
export const jsxRenderer = ( component?: FC<PropsWithChildren<PropsForRenderer & { Layout: FC }>>, component?: ComponentWithChildren, options?: RendererOptions): MiddlewareHandler => function jsxRenderer(c, next) { const Layout = (c.getLayout() ?? Fragment) as FC if (component) { c.setLayout((props) => { return component({ ...props, Layout }) return component({ ...props, Layout }, c) }) } c.setRenderer(createRenderer(c, Layout, component, options) as any) return next()13 unmodified linesmodified:src/middleware/jsx-renderer/index.test.tsx+23 added
54 unmodified lines55 '<!DOCTYPE html><div class="nested"><h1>http://localhost/nested</h1></div>'56 )57 })58 59+ it('Should get the context object as a 2nd arg', async () => {60+ const app = new Hono()61+ app.use(62+ jsxRenderer(63+ ({ children }, c) => {64+ return (65+ <div>66+ {children} at {c.req.path}67+ </div>68+ )69+ },70+ { docType: false }71+ )72+ )73+ app.get('/hi', (c) => {74+ return c.render('hi', { title: 'hi' })75+ })76+77+ const res = await app.request('/hi')78+ expect(res.status).toBe(200)79+ expect(await res.text()).toBe('<div>hi at /hi</div>')80+ })81+82 it('nested layout with Layout', async () => {83 const app = new Hono()84 app.use(85 '*',373 unmodified linesmodified:src/middleware/jsx-renderer/index.ts+15 added-9 removed
3 unmodified lines45678910111213141516171819202122232425262717181920212228293031322 unmodified lines3536373833394041424321 unmodified lines65666768636970717273747570767778798013 unmodified lines3 unmodified linesimport { jsx, createContext, useContext, Fragment } from '../../jsx'import type { FC, PropsWithChildren, JSXNode } from '../../jsx'import { renderToReadableStream } from '../../jsx/streaming'import type { Env, Input, MiddlewareHandler } from '../../types'import type { HtmlEscapedString } from '../../utils/html'
export const RequestContext = createContext<Context | null>(null)
type RendererOptions = { docType?: boolean | string stream?: boolean | Record<string, string>}
type Component = ( props: PropsForRenderer & { Layout: FC }, c: Context) => HtmlEscapedString | Promise<HtmlEscapedString>
type ComponentWithChildren = ( props: PropsWithChildren<PropsForRenderer & { Layout: FC }>, c: Context) => HtmlEscapedString | Promise<HtmlEscapedString>
const createRenderer = ( c: Context, Layout: FC, component?: FC<PropsForRenderer & { Layout: FC }>, options?: RendererOptions ) => (c: Context, Layout: FC, component?: Component, options?: RendererOptions) => (children: JSXNode, props: PropsForRenderer) => { const docType = typeof options?.docType === 'string' ? options.docType2 unmodified lines : '<!DOCTYPE html>'
const currentLayout = component ? jsx( component, (props: any) => component(props, c), { ...{ Layout, ...(props as any) }, }, children as any21 unmodified lines } }
export const jsxRenderer = ( component?: FC<PropsWithChildren<PropsForRenderer & { Layout: FC }>>, component?: ComponentWithChildren, options?: RendererOptions): MiddlewareHandler => function jsxRenderer(c, next) { const Layout = (c.getLayout() ?? Fragment) as FC if (component) { c.setLayout((props) => { return component({ ...props, Layout }) return component({ ...props, Layout }, c) }) } c.setRenderer(createRenderer(c, Layout, component, options) as any) return next()13 unmodified lines