-
Notifications
You must be signed in to change notification settings - Fork 1.4k
add a cf field to the getBindingsProxy result
#4926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f743084
add a `cf` field to the `getBindingsProxy` result
dario-piotrowicz a2497b8
deepFreeze cf object
dario-piotrowicz 6bb6eee
Apply suggestions from code review
dario-piotrowicz 091db66
use IncomingRequestCfProperties type of cf
dario-piotrowicz 20037dc
don't add workers-types to tsconfig types
dario-piotrowicz 2784cd2
run prettify
dario-piotrowicz fdc5b2f
remove workers-types optional peer dependency
dario-piotrowicz 97afdf5
Revert "remove workers-types optional peer dependency"
dario-piotrowicz 9ab96ed
make sure workers-types doesn't get bundled in d.ts file
dario-piotrowicz fb1421b
add new `CfProperties` type argument
dario-piotrowicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,16 @@ | ||
| --- | ||
| "wrangler": minor | ||
| --- | ||
|
|
||
| feature: add a `cf` field to the `getBindingsProxy` result | ||
|
|
||
| Add a new `cf` field to the `getBindingsProxy` result that people can use to mock the production | ||
| `cf` (`IncomingRequestCfProperties`) object. | ||
|
|
||
| Example: | ||
|
|
||
| ```ts | ||
| const { cf } = await getBindingsProxy(); | ||
|
|
||
| console.log(`country = ${cf.country}; colo = ${cf.colo}`); // logs 'country = GB ; colo = LHR' | ||
| ``` |
43 changes: 43 additions & 0 deletions
43
fixtures/get-bindings-proxy/tests/get-bindings-proxy.cf.test.ts
This file contains hidden or 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,43 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { getBindingsProxy } from "./shared"; | ||
|
|
||
| describe("getBindingsProxy - cf", () => { | ||
| it("should provide mock data", async () => { | ||
| const { cf, dispose } = await getBindingsProxy(); | ||
| try { | ||
| expect(cf).toMatchObject({ | ||
| colo: "DFW", | ||
| city: "Austin", | ||
| regionCode: "TX", | ||
| }); | ||
| } finally { | ||
| await dispose(); | ||
| } | ||
| }); | ||
|
|
||
| it("should match the production runtime cf object", async () => { | ||
| const { cf, dispose } = await getBindingsProxy(); | ||
| try { | ||
| expect(cf.constructor.name).toBe("Object"); | ||
|
|
||
| expect(() => { | ||
| cf.city = "test city"; | ||
| }).toThrowError( | ||
| "Cannot assign to read only property 'city' of object '#<Object>'" | ||
| ); | ||
| expect(cf.city).not.toBe("test city"); | ||
|
|
||
| expect(() => { | ||
| cf.newField = "test new field"; | ||
| }).toThrowError("Cannot add property newField, object is not extensible"); | ||
| expect("newField" in cf).toBe(false); | ||
|
|
||
| expect(cf.botManagement).toMatchObject({ | ||
| score: 99, | ||
| }); | ||
| expect(Object.isFrozen(cf.botManagement)).toBe(true); | ||
| } finally { | ||
| await dispose(); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PS: I wanted to use
IncomingRequestCfPropertieshere but I've been having issues with the@microsoft/api-extractorpackage (it seems not to cope withworkers-typesfor some reason 😕)I would go with this simple type for now and look into improving the types later on (also as I want to get proper types for #4792 anyways)
But I don't mind spending more time investigating the
api-extractorproblem for this PR if that's preferred 🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about we make this
Record<string, string>initially and later replace it withIncomingRequestCfProperties? That way you'll be widening the type which is a non-breaking change.If you go with
any, you'll be forced to narrow the type in the future which is breaking.