Skip to content

Commit 5b74eea

Browse files
authored
fix(cloudflare): Account for static fields in wrapper type (#16303)
fixes #16247 By adjusting the generic, we'll make sure that we don't erase static fields with the `instrumentDurableObjectWithSentry` function. See an example below: ```js class MyDurableObjectBase extends DurableObject { public static readonly VERSION = '1.0.0'; } const MyDurableObject = instrumentDurableObjectWithSentry( () => ({ dsn: 'https://example.com/sentry', tracesSampleRate: 1.0, }), MyDurableObjectBase, ); console.log(MyDurableObject.VERSION); // This will now work correctly ``` By moving the `DurableObjectClass` into it's own generic (`new (state: DurableObjectState, env: E) => T`), which we named `C`, it helps preserve the exact constructor type of the input class, including all its static properties and methods. This was previously being lost by not aligning the `DurableObjectClass` with the function return value.
1 parent 33e965d commit 5b74eea

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

packages/cloudflare/src/durableobject.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ function wrapMethodWithSentry<T extends (...args: any[]) => any>(
133133
* );
134134
* ```
135135
*/
136-
export function instrumentDurableObjectWithSentry<E, T extends DurableObject<E>>(
137-
optionsCallback: (env: E) => CloudflareOptions,
138-
DurableObjectClass: new (state: DurableObjectState, env: E) => T,
139-
): new (state: DurableObjectState, env: E) => T {
136+
export function instrumentDurableObjectWithSentry<
137+
E,
138+
T extends DurableObject<E>,
139+
C extends new (state: DurableObjectState, env: E) => T,
140+
>(optionsCallback: (env: E) => CloudflareOptions, DurableObjectClass: C): C {
140141
return new Proxy(DurableObjectClass, {
141142
construct(target, [context, env]) {
142143
setAsyncLocalStorageAsyncContextStrategy();

0 commit comments

Comments
 (0)