Skip to content

Commit

Permalink
add ctx mapped for hono to prev
Browse files Browse the repository at this point in the history
  • Loading branch information
barelyhuman committed May 19, 2023
1 parent 033dcdc commit 5f06de9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
2 changes: 0 additions & 2 deletions prev/core/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ export async function readRoutesFromDirectory({ cwd, outDir = 'dist' }) {

for (const httpMethod of allowedKeys) {
if (!mod[httpMethod]) continue

console.log({ routeFor })
defineRoute({
method: httpMethod,
url: routeFor,
Expand Down
60 changes: 51 additions & 9 deletions prev/kernel/hono.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,52 @@ import path from 'node:path'
import preactRenderToString from 'preact-render-to-string'
import { getRoutes } from '../core/router.js'

// TODO: create an abstraction over HONO to create a new CTX
// that's common and usable for most cases
function mapHonoCtxToPrev(ctx) {
return ctx
async function mapHonoCtxToPrev(ctx) {
let requestBody = await ctx.req.text()
let contentType = ctx.req.header('Content-Type')

if (contentType) {
contentType = contentType.toLowerCase()
if (contentType === 'application/json') {
requestBody = await ctx.req.json()
} else if (
contentType === 'multipart/form-data' ||
contentType === 'application/x-www-form-urlencoded'
) {
requestBody = await c.req.json()
}
}

return {
req: {
url: ctx.req.url,
method: ctx.req.method,
headers: ctx.req.headers,
credentials: ctx.req.credentials,
keepalive: ctx.req.keepalive,
mode: ctx.req.mode,
referrer: ctx.req.referrer,
refererPolicy: ctx.req.refererPolicy,
params: ctx.req.param(),
query: ctx.req.query(),
body: requestBody,
},
res: {
header: ctx.header,
body: ctx.body,
html: ctx.html,
json: ctx.json,
text: ctx.text,
status: ctx.status,
redirect: (url, { permanent = false, status } = {}) => {
if (status) {
return ctx.redirect(url, status)
}
return ctx.redirect(url, permanent ? 301 : 302)
},
},
rawCtx: ctx,
}
}

export async function kernel({
Expand All @@ -28,13 +70,13 @@ export async function kernel({
const details = routes[method][route]
if (method === 'get') {
app[method](details.url, async _ctx => {
const ctx = mapHonoCtxToPrev(_ctx)
const ctx = await mapHonoCtxToPrev(_ctx)
const result = await details.handler(ctx)
if (!result) return
if (result instanceof Response) return result

ctx.header('content-type', 'text/html')
return ctx.html(
ctx.res.header('content-type', 'text/html')
return ctx.res.html(
await renderer(result, plugRegister, {
isDev,
outDir: baseDir,
Expand All @@ -44,8 +86,8 @@ export async function kernel({
)
})
} else {
app[method](details.url, _ctx => {
const ctx = mapHonoCtxToPrev(_ctx)
app[method](details.url, async _ctx => {
const ctx = await mapHonoCtxToPrev(_ctx)
return details.handler(ctx)
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/+id.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Navigation from '@/components/navigation.js'
import BaseLayout from '@/layouts/BaseLayout'

export function get(ctx) {
const paramId = ctx.req.param('id')
const { id: paramId } = ctx.req.params
return (
<BaseLayout title={`Parameterised ${paramId}`}>
<Navigation />
Expand Down
4 changes: 2 additions & 2 deletions src/pages/posts/+postId.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const components = {
}

export async function get(ctx) {
const postName = ctx.req.param('postId')
const mod = await import(`../../content/${postName}.js?update=${Date.now()}`)
const { postId } = ctx.req.params
const mod = await import(`../../content/${postId}.js?update=${Date.now()}`)
const Component = mod.default
return (
<BaseLayout>
Expand Down

0 comments on commit 5f06de9

Please sign in to comment.