Skip to content

Commit

Permalink
feat: attach
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Feb 11, 2024
1 parent 801e869 commit df2dfac
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ coverage
_lib
node_modules
tsconfig.*.tsbuildinfo
tsconfig.tsbuildinfo
tsconfig.tsbuildinfo
.vercel
3 changes: 3 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"suspicious": {
"noExplicitAny": "off",
"noRedeclare": "off"
},
"style": {
"noNonNullAssertion": "off"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion example/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ app.frame('/falsy-intents', () => {
}
})

app.route('/todos', todoApp)
app.attach(todoApp)

const server = Bun.serve(app)
console.log(`𝑭𝒂𝒓𝒄 ▶︎ http://localhost:${server.port}/dev`)
1 change: 1 addition & 0 deletions example/src/todos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const app = new Farc<State>({
index: -1,
todos: [],
},
route: '/todos',
})

app.frame('/', ({ buttonValue, deriveState, inputText }) => {
Expand Down
41 changes: 36 additions & 5 deletions src/farc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import { ImageResponse } from 'hono-og'
import { inspectRoutes } from 'hono/dev'
import type { HonoOptions } from 'hono/hono-base'
import { jsxRenderer } from 'hono/jsx-renderer'
import { type Env, type Schema } from 'hono/types'
import {
type Env,
type MergePath,
type MergeSchemaPath,
type Schema,
} from 'hono/types'

import { App, DevStyles, Preview } from './dev/components.js'
import { getFrameRoutes, htmlToFrame, htmlToState } from './dev/utils.js'
Expand All @@ -32,7 +37,10 @@ import { toBaseUrl } from './utils/toBaseUrl.js'
export type FarcConstructorParameters<
state = undefined,
env extends Env = Env,
> = HonoOptions<env> & { initialState?: state | undefined }
> = HonoOptions<env> & {
initialState?: state | undefined
route?: string | undefined
}

export type FrameHandlerReturnType = {
action?: string | undefined
Expand All @@ -48,10 +56,31 @@ export class Farc<
basePath extends string = '/',
> extends Hono<env, schema, basePath> {
#initialState: state = undefined as state
subPath: string | undefined

constructor({ initialState }: FarcConstructorParameters<state, env> = {}) {
super()
constructor({
initialState,
route,
...options
}: FarcConstructorParameters<state, env> = {}) {
super(options)
if (initialState) this.#initialState = initialState
if (route) this.subPath = route
}

attach<
subEnv extends Env,
subSchema extends Schema,
subBasePath extends string,
>(
app: Farc<any, subEnv, subSchema, subBasePath>,
): Farc<
any,
env,
MergeSchemaPath<subSchema, MergePath<basePath, subBasePath>> & schema,
basePath
> {
return this.route(app.subPath!, app as any) as any
}

frame<path extends string>(
Expand Down Expand Up @@ -123,7 +152,9 @@ export class Farc<
property="fc:frame:post_url"
content={`${
action
? toBaseUrl(c.req.url) + parsePath(action || '')
? toBaseUrl(c.req.url) +
parsePath(this.subPath || '') +
parsePath(action || '')
: context.url
}?${postSearch}`}
/>
Expand Down

0 comments on commit df2dfac

Please sign in to comment.