Skip to content

Commit

Permalink
feat: initialize initial state in places where state is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
dalechyn committed Jul 26, 2024
1 parent 41218bd commit ef55cff
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 43 deletions.
25 changes: 12 additions & 13 deletions src/frog-base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -593,19 +593,16 @@ export class FrogBase<
// @ts-ignore - private
this.initialBasePath,
)

const { context, getState } = getFrameContext<env, string>({
const { context, getState } = await getFrameContext<env, string>({
context: await requestBodyToContext(c, {
hub:
this.hub ||
(this.hubApiUrl ? { apiUrl: this.hubApiUrl } : undefined),
secret: this.secret,
verify,
}),
initialState:
typeof this._initialState === 'function'
? await (this._initialState as any)(c)
: this._initialState,
contextHono: c,
initialState: this._initialState,
origin,
})

Expand Down Expand Up @@ -1005,14 +1002,12 @@ export class FrogBase<
const origin = this.origin ?? url.origin
const assetsUrl = origin + parsePath(this.assetsPath)

const { context } = getImageContext<env, string>({
const { context } = await getImageContext<env, string>({
context: await requestBodyToImageContext(c, {
secret: this.secret,
}),
initialState:
typeof this._initialState === 'function'
? await (this._initialState as any)(c)
: this._initialState,
contextHono: c,
initialState: this._initialState,
})

const response = await handler(context)
Expand Down Expand Up @@ -1120,14 +1115,16 @@ export class FrogBase<
const { verify = this.verify } = options

this.hono.post(parseHonoPath(path), ...middlewares, async (c) => {
const { context } = getTransactionContext<env, string, {}, _state>({
const { context } = await getTransactionContext<env, string, {}, _state>({
context: await requestBodyToContext(c, {
hub:
this.hub ||
(this.hubApiUrl ? { apiUrl: this.hubApiUrl } : undefined),
secret: this.secret,
verify,
}),
contextHono: c,
initialState: this._initialState,
req: c.req,
})
const response = await handler(context)
Expand Down Expand Up @@ -1155,14 +1152,16 @@ export class FrogBase<
const { verify = this.verify } = options

this.hono.post(parseHonoPath(path), ...middlewares, async (c) => {
const { context } = getSignatureContext<env, string, {}, _state>({
const { context } = await getSignatureContext<env, string, {}, _state>({
context: await requestBodyToContext(c, {
hub:
this.hub ||
(this.hubApiUrl ? { apiUrl: this.hubApiUrl } : undefined),
secret: this.secret,
verify,
}),
contextHono: c,
initialState: this._initialState,
req: c.req,
})
const response = await handler(context)
Expand Down
26 changes: 17 additions & 9 deletions src/utils/getFrameContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Input } from 'hono'
import type { Input, Context as Context_Hono } from 'hono'
import type { Context, FrameContext } from '../types/context.js'
import type { Env } from '../types/env.js'
import { getIntentState } from './getIntentState.js'
Expand All @@ -12,7 +12,11 @@ type GetFrameContextParameters<
_state = env['State'],
> = {
context: Context<env, path, input>
initialState?: _state
contextHono: Context_Hono<env, path, input>
initialState?:
| ((c: Context<env>) => _state | Promise<_state>)
| _state
| undefined
origin: string
}

Expand All @@ -22,12 +26,12 @@ type GetFrameContextReturnType<
input extends Input = {},
//
_state = env['State'],
> = {
> = Promise<{
context: FrameContext<env, path, input>
getState: () => _state
}
}>

export function getFrameContext<
export async function getFrameContext<
env extends Env,
path extends string,
input extends Input = {},
Expand All @@ -36,7 +40,7 @@ export function getFrameContext<
>(
parameters: GetFrameContextParameters<env, path, input, _state>,
): GetFrameContextReturnType<env, path, input, _state> {
const { context, origin } = parameters
const { context, contextHono, origin } = parameters
const { env, frameData, initialPath, previousButtonValues, req, verified } =
context || {}

Expand All @@ -55,9 +59,13 @@ export function getFrameContext<
// initial URL.
const url = parsePath(reset ? `${origin}${initialPath}` : context.url)

let previousState = (() => {
if (context.status === 'initial') return parameters.initialState
return context?.previousState || parameters.initialState
let previousState = await (async () => {
if (context.status === 'initial') {
if (typeof parameters.initialState === 'function')
return await (parameters.initialState as any)(contextHono)
return parameters.initialState
}
return context?.previousState
})()

function deriveState(
Expand Down
23 changes: 16 additions & 7 deletions src/utils/getImageContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Input } from 'hono'
import type { Input, Context as Context_Hono } from 'hono'
import type { Context, ImageContext } from '../types/context.js'
import type { Env } from '../types/env.js'

Expand All @@ -13,7 +13,11 @@ type GetImageContextParameters<
Context<env, path, input, _state>,
'frameData' | 'verified' | 'status' | 'initialPath'
>
initialState?: _state
contextHono: Context_Hono<env, path, input>
initialState?:
| ((c: Context<env>) => _state | Promise<_state>)
| _state
| undefined
}

type GetImageContextReturnType<
Expand All @@ -22,11 +26,11 @@ type GetImageContextReturnType<
input extends Input = {},
//
_state = env['State'],
> = {
> = Promise<{
context: ImageContext<env, path, input, _state>
}
}>

export function getImageContext<
export async function getImageContext<
env extends Env,
path extends string,
input extends Input = {},
Expand All @@ -35,13 +39,18 @@ export function getImageContext<
>(
parameters: GetImageContextParameters<env, path, input, _state>,
): GetImageContextReturnType<env, path, input, _state> {
const { context, initialState } = parameters
const { context, contextHono, initialState } = parameters
const { env, previousState, req } = context || {}

return {
context: {
env,
previousState: previousState ?? (initialState as _state),
previousState: await (async () => {
if (previousState) return previousState
if (typeof initialState === 'function')
return await (initialState as any)(contextHono)
return initialState
})(),
req,
res: (data) => ({ data, format: 'image', status: 'success' }),
var: context.var,
Expand Down
27 changes: 20 additions & 7 deletions src/utils/getSignatureContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { HonoRequest, Input } from 'hono'
import type { HonoRequest, Input, Context as Context_Hono } from 'hono'
import type { Context, SignatureContext } from '../types/context.js'
import type { Env } from '../types/env.js'
import type { SignatureResponse } from '../types/signature.js'
Expand All @@ -12,6 +12,11 @@ type GetSignatureContextParameters<
_state = env['State'],
> = {
context: Context<env, path, input, _state>
contextHono: Context_Hono<env, path, input>
initialState:
| ((c: Context_Hono<env>) => _state | Promise<_state>)
| _state
| undefined
req: HonoRequest
}

Expand All @@ -21,11 +26,11 @@ type GetSignatureContextReturnType<
input extends Input = {},
//
_state = env['State'],
> = {
> = Promise<{
context: SignatureContext<env, path, input, _state>
}
}>

export function getSignatureContext<
export async function getSignatureContext<
env extends Env,
path extends string,
input extends Input,
Expand All @@ -34,19 +39,27 @@ export function getSignatureContext<
>(
parameters: GetSignatureContextParameters<env, path, input, _state>,
): GetSignatureContextReturnType<env, path, input, _state> {
const { context } = parameters
const { context, contextHono } = parameters
const {
env,
frameData,
initialPath,
previousButtonValues,
previousState,
req,
status,
verified,
url,
} = context || {}

const previousState = await (async () => {
if (context.status === 'initial') {
if (typeof parameters.initialState === 'function')
return await (parameters.initialState as any)(contextHono)
return parameters.initialState
}
return context?.previousState
})()

const { buttonValue, inputText } = getIntentState({
buttonValues: previousButtonValues || [],
frameData,
Expand All @@ -67,7 +80,7 @@ export function getSignatureContext<
initialPath,
inputText,
previousButtonValues,
previousState,
previousState: previousState as any,
req,
res(parameters) {
const { chainId, method, params } = parameters
Expand Down
27 changes: 20 additions & 7 deletions src/utils/getTransactionContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { HonoRequest, Input } from 'hono'
import type { HonoRequest, Input, Context as Context_Hono } from 'hono'
import {
type Abi,
AbiFunctionNotFoundError,
Expand All @@ -20,6 +20,11 @@ type GetTransactionContextParameters<
_state = env['State'],
> = {
context: Context<env, path, input, _state>
contextHono: Context_Hono<env, path, input>
initialState:
| ((c: Context_Hono<env>) => _state | Promise<_state>)
| _state
| undefined
req: HonoRequest
}

Expand All @@ -29,11 +34,11 @@ type GetTransactionContextReturnType<
input extends Input = {},
//
_state = env['State'],
> = {
> = Promise<{
context: TransactionContext<env, path, input, _state>
}
}>

export function getTransactionContext<
export async function getTransactionContext<
env extends Env,
path extends string,
input extends Input,
Expand All @@ -42,19 +47,27 @@ export function getTransactionContext<
>(
parameters: GetTransactionContextParameters<env, path, input, _state>,
): GetTransactionContextReturnType<env, path, input, _state> {
const { context } = parameters
const { context, contextHono } = parameters
const {
env,
frameData,
initialPath,
previousButtonValues,
previousState,
req,
status,
verified,
url,
} = context || {}

const previousState = await (async () => {
if (context.status === 'initial') {
if (typeof parameters.initialState === 'function')
return await (parameters.initialState as any)(contextHono)
return parameters.initialState
}
return context?.previousState
})()

const { buttonValue, inputText } = getIntentState({
buttonValues: previousButtonValues || [],
frameData,
Expand Down Expand Up @@ -112,7 +125,7 @@ export function getTransactionContext<
initialPath,
inputText,
previousButtonValues,
previousState,
previousState: previousState as any,
req,
res(parameters) {
const { attribution, chainId, method, params } = parameters
Expand Down

0 comments on commit ef55cff

Please sign in to comment.