Skip to content
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

add a cf field to the getBindingsProxy result #4926

Merged
merged 10 commits into from
Feb 7, 2024
16 changes: 16 additions & 0 deletions .changeset/strong-otters-hope.md
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` filed to the `getBindingsProxy` result that people can use to mock the production
dario-piotrowicz marked this conversation as resolved.
Show resolved Hide resolved
`cf` (`IncomingRequestCfProperties`) object.

Example:

```ts
const { cf } = await getBindingsProxy();

console.log(`country = ${cf.country} ; colo = ${cf.colo}`); // logs 'country = GB ; colo = LHR'
dario-piotrowicz marked this conversation as resolved.
Show resolved Hide resolved
```
38 changes: 38 additions & 0 deletions fixtures/get-bindings-proxy/tests/get-bindings-proxy.cf.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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);
} finally {
await dispose();
}
});
});
6 changes: 6 additions & 0 deletions packages/wrangler/src/api/integrations/bindings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export type BindingsProxy<Bindings = Record<string, unknown>> = {
* Object containing the various proxies
*/
bindings: Bindings;
/**
* Mock of the context object that Workers received in their request handler, all the object's methods are no-op
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
cf: Record<string, any>;
Copy link
Member Author

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 IncomingRequestCfProperties here but I've been having issues with the @microsoft/api-extractor package (it seems not to cope with workers-types for 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-extractor problem for this PR if that's preferred 🙂

Copy link
Contributor

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 with IncomingRequestCfProperties? 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.

/**
* Mock of the context object that Workers received in their request handler, all the object's methods are no-op
*/
Expand Down Expand Up @@ -93,6 +98,7 @@ export async function getBindingsProxy<Bindings = Record<string, unknown>>(
...vars,
...bindings,
},
cf: Object.freeze(await mf.getCf()),
dario-piotrowicz marked this conversation as resolved.
Show resolved Hide resolved
ctx: new ExecutionContext(),
caches: new CacheStorage(),
dispose: () => mf.dispose(),
Expand Down
Loading