-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow to explicitly set state using generics
- Loading branch information
1 parent
92987b2
commit dbabaa9
Showing
20 changed files
with
460 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"frames.js": patch | ||
--- | ||
|
||
fix: allow to explicitly set state using generics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { ExecutionContext, Request as CfRequest, ExportedHandlerFetchHandler } from '@cloudflare/workers-types'; | ||
import { createFrames, types } from '.'; | ||
|
||
const framesWithoutState = createFrames(); | ||
framesWithoutState(async (ctx) => { | ||
ctx.initialState satisfies types.JsonValue | undefined; | ||
ctx.state satisfies types.JsonValue | undefined; | ||
|
||
return { | ||
image: 'http://test.png', | ||
}; | ||
}) satisfies ExportedHandlerFetchHandler; | ||
|
||
const framesWithInferredState = createFrames({ | ||
initialState: { test: true }, | ||
}); | ||
|
||
framesWithInferredState(async (ctx) => { | ||
ctx.state satisfies { test: boolean; }; | ||
|
||
return { | ||
image: 'http://test.png', | ||
}; | ||
}) satisfies ExportedHandlerFetchHandler; | ||
|
||
const framesWithExplicitState = createFrames<{ test: boolean }>({}); | ||
framesWithExplicitState(async (ctx) => { | ||
ctx.state satisfies { test: boolean }; | ||
ctx satisfies { initialState?: {test: boolean}; message?: any, pressedButton?: any }; | ||
ctx satisfies { cf: { env: unknown; ctx: ExecutionContext; req: CfRequest }} | ||
|
||
return { | ||
image: 'http://test.png', | ||
}; | ||
}) satisfies ExportedHandlerFetchHandler; | ||
|
||
const framesWithExplicitStateAndEnv = createFrames<{ test: boolean }, { secret: string }>({}); | ||
framesWithExplicitStateAndEnv(async (ctx) => { | ||
ctx.state satisfies { test: boolean }; | ||
ctx satisfies { initialState?: { test: boolean }; message?: any, pressedButton?: any; request: Request; }; | ||
ctx satisfies { cf: { env: { secret: string }; ctx: ExecutionContext; req: CfRequest }} | ||
|
||
return { | ||
image: 'http://test.png', | ||
}; | ||
}) satisfies ExportedHandlerFetchHandler<{ secret: string }>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { createFrames, types } from '.'; | ||
|
||
type Handler = (req: Request) => Promise<Response>; | ||
|
||
const framesWithoutState = createFrames(); | ||
framesWithoutState(async (ctx) => { | ||
ctx.initialState satisfies types.JsonValue | undefined; | ||
ctx.state satisfies types.JsonValue | undefined; | ||
|
||
return { | ||
image: 'http://test.png', | ||
}; | ||
}) satisfies Handler; | ||
|
||
const framesWithInferredState = createFrames({ | ||
initialState: { test: true }, | ||
}); | ||
|
||
framesWithInferredState(async (ctx) => { | ||
ctx.state satisfies { test: boolean }; | ||
|
||
return { | ||
image: 'http://test.png', | ||
}; | ||
}) satisfies Handler; | ||
|
||
const framesWithExplicitState = createFrames<{ test: boolean }>({}); | ||
framesWithExplicitState(async (ctx) => { | ||
ctx.state satisfies { test: boolean }; | ||
ctx satisfies { initialState?: {test: boolean}; message?: any, pressedButton?: any }; | ||
|
||
return { | ||
image: 'http://test.png', | ||
}; | ||
}) satisfies Handler; | ||
|
||
const framesWithExplicitStateAndEnv = createFrames<{ test: boolean }>({}); | ||
framesWithExplicitStateAndEnv(async (ctx) => { | ||
ctx.state satisfies { test: boolean }; | ||
ctx satisfies { initialState?: { test: boolean }; message?: any, pressedButton?: any; request: Request; }; | ||
|
||
|
||
return { | ||
image: 'http://test.png', | ||
}; | ||
}) satisfies Handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Handler } from 'express'; | ||
import { createFrames, types } from '.'; | ||
|
||
const framesWithoutState = createFrames(); | ||
framesWithoutState(async ctx => { | ||
ctx.initialState satisfies types.JsonValue | undefined; | ||
ctx.state satisfies types.JsonValue | undefined; | ||
|
||
return { | ||
image: 'http://test.png' | ||
}; | ||
}) satisfies Handler; | ||
|
||
const framesWithInferredState = createFrames({ | ||
initialState: { | ||
test: true | ||
} | ||
}); | ||
framesWithInferredState(async ctx => { | ||
ctx.initialState satisfies { test: boolean; }; | ||
ctx.state satisfies { | ||
test: boolean; | ||
}; | ||
|
||
return { | ||
image: 'http://test.png' | ||
}; | ||
}) satisfies Handler; | ||
|
||
const framesWithExplicitState = createFrames<{ | ||
test: boolean; | ||
}>({}); | ||
framesWithExplicitState(async ctx => { | ||
ctx.state satisfies { | ||
test: boolean; | ||
}; | ||
ctx.initialState satisfies { | ||
test: boolean; | ||
}; | ||
ctx satisfies { | ||
message?: any; | ||
pressedButton?: any; | ||
request: Request; | ||
} | ||
|
||
return { | ||
image: 'http://test.png' | ||
}; | ||
}) satisfies Handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.