Skip to content

Commit

Permalink
feat(response): add render method
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Jan 15, 2025
1 parent 35d4e79 commit d46f6e0
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 17 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/http",
"version": "5.18.0",
"version": "5.20.0",
"description": "The Athenna Http server. Built on top of fastify.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -82,7 +82,7 @@
"@athenna/test": "^5.2.0",
"@athenna/tsconfig": "^5.0.0",
"@athenna/view": "^5.1.0",
"@athenna/vite": "^5.1.0",
"@athenna/vite": "^5.9.0",
"@fastify/cors": "^10.0.1",
"@fastify/helmet": "^13.0.0",
"@fastify/rate-limit": "^10.2.1",
Expand Down
77 changes: 71 additions & 6 deletions src/context/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@

import { View } from '@athenna/view'
import type { FastifyReply } from 'fastify'
import { Server } from '#src/facades/Server'
import { Module, Options } from '@athenna/common'
import type { SendOptions } from '@fastify/static'
import type { Request } from '#src/context/Request'
import type { FastifyHelmetOptions } from '@fastify/helmet'

const react = Module.safeImport('react') as any

export class Response {
/**
* The fastify response object.
Expand Down Expand Up @@ -108,6 +113,22 @@ export class Response {
return this.response.elapsedTime
}

/**
* Terminate the request sending an HTML content to be rendered.
*
* @example
* ```ts
* return response.html('<h1>Hello World!</h1>')
* ```
*/
public async html(html: string) {
await this.safeHeader('Content-Type', 'text/html; charset=utf-8').send(html)

this.response.body = html

return this
}

/**
* Terminate the request sending a view to be rendered.
*
Expand All @@ -116,19 +137,63 @@ export class Response {
* return response.view('welcome', { name: 'lenon' })
* ```
*/
public async view(view: string, data?: any): Promise<Response> {
public async view(view: string, data?: any) {
const content = await View.edge
.createRenderer()
.share({ request: this.request })
.render(view, data)

await this.safeHeader('Content-Type', 'text/html; charset=utf-8').send(
content
)
return this.html(content)
}

this.response.body = content
/**
* Terminate the request sending a React component to be rendered.
*
* @example
* ```ts
* return response.render('index')
* return response.render('index', {
* component: 'src/resources/app/app.tsx',
* viewData: {},
* beforeComponentRender: (component) => {
* return component.createApp()
* }
* })
* ```
*/
public async render(
view: string,
options?: {
component?: string
viewData?: any
beforeComponentRender?: (componentModule: any) => any
}
) {
if (!react) {
throw new Error('React is not installed, please run "npm i react".')
}

return this
options = Options.create(options, {
viewData: {},
component: Config.get('http.vite.ssrEntrypoint'),
beforeComponentRender: options.component
? null
: component => {
return component.createApp(this.request.baseUrl)
}
})

const vite = Server.getVitePlugin().getVite()
let component = await vite.ssrLoadModule(options.component)

if (options.beforeComponentRender) {
component = options.beforeComponentRender(component)
}

return this.view(view, {
element: react.renderToString(component),
...options.viewData
})
}

/**
Expand Down
33 changes: 30 additions & 3 deletions src/server/ServerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {
} from '#src/types'

import { Options } from '@athenna/common'
import type { FastifyVite } from '@athenna/vite'
import type { AddressInfo } from 'node:net'
import { FastifyHandler } from '#src/handlers/FastifyHandler'

Expand Down Expand Up @@ -193,9 +194,9 @@ export class ServerImpl {
}

/**
* Start vite server.
* Return the FastifyVite instance if it exists.
*/
public async viteReady() {
public getVitePlugin(): FastifyVite {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!this.fastify.vite) {
Expand All @@ -204,7 +205,33 @@ export class ServerImpl {

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.fastify.vite.ready()
return this.fastify.vite
}

/**
* Return ViteDevServer instance if it exists.
*/
public getViteDevServer() {
const vite = this.getVitePlugin()

if (!vite) {
return
}

return vite.getServer()
}

/**
* Start vite server.
*/
public async viteReady() {
const vite = this.getVitePlugin()

if (!vite) {
return
}

return vite.ready()
}

/**
Expand Down

0 comments on commit d46f6e0

Please sign in to comment.