Skip to content

Commit

Permalink
chore: merge
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Feb 13, 2024
2 parents 7555dbf + 238ce8a commit 98159b8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 54 deletions.
8 changes: 3 additions & 5 deletions examples/_dev/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/** @jsx jsx */
/** @jsxImportSource hono/jsx */
/** @jsxFrag */

import { Button, Farc, TextInput } from 'farc'
import { app as todoApp } from './todos'

const app = new Farc()
const app = new Farc({
// basePath: '/api'
})

app.frame('/', (context) => {
const { buttonValue, inputText, status } = context
Expand Down
4 changes: 0 additions & 4 deletions examples/_dev/src/todos.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/** @jsx jsx */
/** @jsxImportSource hono/jsx */
/** @jsxFrag */

import { Button, Farc, TextInput } from 'farc'

type State = {
Expand Down
109 changes: 67 additions & 42 deletions src/farc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ import { parseIntents } from './utils/parseIntents.js'
import { parsePath } from './utils/parsePath.js'
import { requestToContext } from './utils/requestToContext.js'
import { serializeJson } from './utils/serializeJson.js'
import { toBaseUrl } from './utils/toBaseUrl.js'

export type FarcConstructorParameters<
state = undefined,
env extends Env = Env,
> = HonoOptions<env> & {
basePath extends string = '/',
> = {
basePath?: basePath | string | undefined
honoOptions?: HonoOptions<env> | undefined
initialState?: state | undefined
}

Expand All @@ -48,14 +50,25 @@ export class Farc<
env extends Env = Env,
schema extends Schema = {},
basePath extends string = '/',
> extends Hono<env, schema, basePath> {
> {
#initialState: state = undefined as state

hono: Hono<env, schema, basePath>
fetch: Hono<env, schema, basePath>['fetch']
get: Hono<env, schema, basePath>['get']
post: Hono<env, schema, basePath>['post']

constructor({
basePath,
honoOptions,
initialState,
...options
}: FarcConstructorParameters<state, env> = {}) {
super(options)
}: FarcConstructorParameters<state, env, basePath> = {}) {
this.hono = new Hono<env, schema, basePath>(honoOptions)
if (basePath) this.hono = this.hono.basePath(basePath)
this.fetch = this.hono.fetch.bind(this.hono)
this.get = this.hono.get.bind(this.hono)
this.post = this.hono.post.bind(this.hono)

if (initialState) this.#initialState = initialState
}

Expand All @@ -67,8 +80,12 @@ export class Farc<
) => FrameHandlerReturnType | Promise<FrameHandlerReturnType>,
) {
// Frame Route (implements GET & POST).
this.use(path, async (c) => {
this.hono.use(parsePath(path), async (c) => {
const query = c.req.query()

const url = new URL(c.req.url)
const baseUrl = `${url.origin}${url.pathname}`

const previousContext = query.previousContext
? deserializeJson<PreviousFrameContext<path, state>>(
query.previousContext,
Expand Down Expand Up @@ -127,9 +144,7 @@ export class Farc<
<meta
property="fc:frame:post_url"
content={`${
action
? toBaseUrl(c.req.url) + parsePath(action || '')
: context.url
action ? baseUrl + parsePath(action || '') : context.url
}?${postSearch}`}
/>
{parsedIntents}
Expand Down Expand Up @@ -160,7 +175,7 @@ export class Farc<
})

// OG Image Route
this.get(`${parsePath(path)}/image`, async (c) => {
this.hono.get(`${parsePath(path)}/image`, async (c) => {
const query = c.req.query()
const previousContext = query.previousContext
? deserializeJson<PreviousFrameContext<path, state>>(
Expand All @@ -178,43 +193,44 @@ export class Farc<
})

// Frame Dev Routes
this.use(`${parsePath(path)}/dev`, (c, next) =>
jsxRenderer((props) => {
const { children } = props
const path = new URL(c.req.url).pathname.replace('/dev', '')
return (
<html lang="en">
<head>
<title>𝑭𝒂𝒓𝒄 {path || '/'}</title>
<Style />
{/* TODO: Vendor into project */}
<script
defer
src="https://cdn.jsdelivr.net/npm/@alpinejs/focus@3.x.x/dist/cdn.min.js"
/>
<script
defer
src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"
/>
<script
src="https://unpkg.com/htmx.org@1.9.10"
integrity="sha384-D1Kt99CQMDuVetoL1lrYwg5t+9QdHe7NLX/SoJYkXDFfX37iInKRy5xLSi8nO7UC"
crossorigin="anonymous"
/>
</head>
<body>{children}</body>
</html>
)
})(c, next),
)
this.hono
.use(`${parsePath(path)}/dev`, (c, next) =>
jsxRenderer((props) => {
const { children } = props
const path = new URL(c.req.url).pathname.replace('/dev', '')
return (
<html lang="en">
<head>
<title>𝑭𝒂𝒓𝒄 {path || '/'}</title>
<Style />
{/* TODO: Vendor into project */}
<script
defer
src="https://cdn.jsdelivr.net/npm/@alpinejs/focus@3.x.x/dist/cdn.min.js"
/>
<script
defer
src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"
/>
<script
src="https://unpkg.com/htmx.org@1.9.10"
integrity="sha384-D1Kt99CQMDuVetoL1lrYwg5t+9QdHe7NLX/SoJYkXDFfX37iInKRy5xLSi8nO7UC"
crossorigin="anonymous"
/>
</head>
<body>{children}</body>
</html>
)
})(c, next),
)
.get(async (c) => {
const baseUrl = c.req.url.replace('/dev', '')
const response = await fetch(baseUrl)
const text = await response.text()

const frame = htmlToFrame(text)
const state = htmlToState(text)
const routes = getRoutes(baseUrl, inspectRoutes(this))
const routes = getRoutes(baseUrl, inspectRoutes(this.hono))

return c.render(<Dev {...{ baseUrl, frame, routes, state }} />)
})
Expand Down Expand Up @@ -334,11 +350,20 @@ export class Farc<
// TODO: handle redirects
const frame = htmlToFrame(text)
const state = htmlToState(text)
const routes = getRoutes(baseUrl, inspectRoutes(this))
const routes = getRoutes(baseUrl, inspectRoutes(this.hono))

return c.render(
<Preview {...{ baseUrl, error, frame, routes, state }} />,
)
})
}

route<
subPath extends string,
subEnv extends Env,
subSchema extends Schema,
subBasePath extends string,
>(path: subPath, farc: Farc<any, subEnv, subSchema, subBasePath>) {
return this.hono.route(path, farc.hono)
}
}
3 changes: 0 additions & 3 deletions src/utils/toBaseUrl.ts

This file was deleted.

0 comments on commit 98159b8

Please sign in to comment.