Skip to content

Commit

Permalink
feat: initialState to be derived from Context
Browse files Browse the repository at this point in the history
Useful with path parameters.
  • Loading branch information
dalechyn committed Jul 24, 2024
1 parent 084c866 commit 74b323d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/frog-base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export type FrogConstructorParameters<
* }
* ```
*/
initialState?: _state | undefined
initialState?: ((c: Context<env>) => _state) | _state | undefined
/**
* Origin URL of the server instance.
*
Expand Down Expand Up @@ -264,7 +264,7 @@ export class FrogBase<
> {
// Note: not using native `private` fields to avoid tslib being injected
// into bundled code.
_initialState: env['State'] = undefined as env['State']
_initialState: ((c: Context<env>) => _state) | _state | undefined = undefined
/** Path for assets. */
assetsPath: string
/** Base path of the server instance. */
Expand Down Expand Up @@ -585,7 +585,10 @@ export class FrogBase<
secret: this.secret,
verify,
}),
initialState: this._initialState,
initialState:
typeof this._initialState === 'function'
? (this._initialState as any)(c)
: this._initialState,
origin,
})

Expand Down Expand Up @@ -989,7 +992,10 @@ export class FrogBase<
context: await requestBodyToImageContext(c, {
secret: this.secret,
}),
initialState: this._initialState,
initialState:
typeof this._initialState === 'function'
? (this._initialState as any)(c)
: this._initialState,
})

const response = await handler(context)
Expand Down
2 changes: 1 addition & 1 deletion src/frog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class Frog<
//
_state = env['State'],
> extends FrogBase<env, schema, basePath, _state> {
constructor(params: FrogConstructorParameters) {
constructor(params: FrogConstructorParameters<env, basePath, _state>) {
super(params as any)

const frame = this.frame
Expand Down

0 comments on commit 74b323d

Please sign in to comment.