/* eslint-disable import/exports-last */ import {Instrumentation} from './Instrumentation'; interface InfoPointData { className?: string; methodName?: string; } /** * Annotate methods you wish to be reported as InfoPoint to eum-cloud. * * Consider the following critical method which you wish to be reported as InfoPoint to eum-cloud. * * ```ts * class MyClass { * public infoPointMethod(arg1, arg2, value) { * console.log("Executing infoPointMethod!"); * } * } * ``` * * ### Adding annotations to report info points automatically. * For convenience purpose, you can simply annotate your methods without having to modify the code * to manually report InfoPoints. * * ```js * class MyClass { * `@InfoPoint` * public infoPointMethod(arg1, arg2, value) { * console.log("Executing infoPointMethod!"); * } * } * ``` * * Your method will automatically report info points to the eum cloud. * * **Warning:** If your code gets minified, the info point will report useless class and function names. You can either * turn off minification for the annotated symbols, or you can manually specify the symbol names. * * To **manually specify** symbol names, pass a parameter to the `InfoPoint` decorator like this. * * ```js * class MyClass { * `@InfoPoint({className: 'MyClass', fnName: 'infoPointMethod'})` * public infoPointMethod(arg1, arg2, value) { * console.log("Executing infoPointMethod!"); * } * } * ``` * * Alternatively, you can **exclude classes and functions from minification** in the following way. * * - Install the npm package `metro-minify-terser` * - Add this to your `metro.config.js` configuration file: * * ```js * transformer: { * minifierPath: 'metro-minify-terser', * minifierConfig: { * // see: https://www.npmjs.com/package/terser * keep_fnames: true, * module: true * } * } * ``` * * @param {any | InfoPointData} target Depending on how you called InfoPoint, * either the method's class name or the parameter object you passed. * @param {string?} propertyKey If called without parameters, the method's name. * @param {PropertyDescriptor} descriptor Object containing info about the decorated method. * @see {@link Instrumentation.trackCall} */ export function InfoPoint( target: any, propertyKey: string, descriptor: PropertyDescriptor, ): void; export function InfoPoint( manualData?: InfoPointData, ): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void; // eslint-disable-next-line jsdoc/require-jsdoc export function InfoPoint( manualDataOrTarget?: any | InfoPointData, propertyKey?: string, descriptor?: PropertyDescriptor, ) { /* * Shorthand syntax: @InfoPoint * manualDataOrTarget = any */ if (propertyKey && descriptor) { return decorator(manualDataOrTarget, propertyKey, descriptor); } // Long-hand syntax: @InfoPoint(...manualData) const manualData = manualDataOrTarget as InfoPointData; const explicitClassName = manualData?.className; const explicitMethodName = manualData?.methodName; return decorator; function decorator( target: any, // We need to keep the exact signature, even if shadowing. // eslint-disable-next-line @typescript-eslint/no-shadow propertyKey: string, // eslint-disable-next-line @typescript-eslint/no-shadow descriptor: PropertyDescriptor, ) { const decoratedTarget = descriptor?.value; if (typeof decoratedTarget !== 'function') { throw new TypeError('InfoPoint can only be applied to a function.'); } // Override decorated function descriptor.value = function (...args: any[]) { const className = explicitClassName ?? guessClassName(target); const methodName = explicitMethodName ?? propertyKey; return Instrumentation.trackCall(className, methodName, args, () => decoratedTarget(...args), ); }; } } function guessClassName(target: Function | object): string { switch (typeof target) { // Class methods case 'function': { return target.name; } // Static methods case 'object': { return target.constructor.name; } } }