Last active
June 24, 2021 11:39
-
-
Save karol-majewski/7b8738d42921b833484325e1ec535a9a to your computer and use it in GitHub Desktop.
Using JavaScript Proxies with TypeScript
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
import LR from 'logrocket'; | |
/** | |
* Fails silently if `LogRocket#init` throws. | |
* | |
* @see https://stackoverflow.com/a/43823282/10325032 | |
*/ | |
export const LogRocket = new Proxy(LR, { | |
get(target, property: keyof typeof LR, receiver) { | |
if (property === 'init') { | |
return new Proxy(target[property], { | |
apply(applyTarget, thisArg, args) { | |
try { | |
return Reflect.apply(applyTarget, thisArg, args); | |
} catch (error: unknown) { | |
console.error(`Failed to call ${thisArg.constructor.name}.${property} with arguments ${args}`, error); | |
} | |
}, | |
}); | |
} | |
return Reflect.get(target, property, receiver); | |
}, | |
}); | |
LogRocket.init('<some-invalid-app-ID>'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A
revokable
version: