Created
December 30, 2022 21:03
-
-
Save drumnickydrum/84adc36252fe2a9bf996b6d6c45dc43d to your computer and use it in GitHub Desktop.
[TS: SDK Proxy] Wrap a script-added SDK so it is usable in server and client contexts #typescript #browser #sdk #script
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
declare global { | |
interface Window { | |
someSDK: SomeSDKType | undefined; | |
} | |
} | |
/** | |
* Wrap a script-added SDK so it is usable in server and client contexts. | |
* This allows it to be freely used without errors related to undefined `window` | |
*/ | |
export const someSDK = new Proxy({} as SomeSDKType, { | |
get<K extends keyof SomeSDKType>(_: unknown, key: K) { | |
if (!window) { | |
return noop; | |
} | |
return window.someSDK[key]; | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment