@magicbell/core
Version:
Official MagicBell API wrapper
22 lines (18 loc) • 629 B
text/typescript
import get from 'lodash/get.js';
/**
* Decorator factory to unwrap the arguments of the method decorated with this.
*
* @example
* @unwrap('notification')
* set(json = {}) { Object.assign(this, json) }
*/
export default function unwrap(wrapKey: string) {
return function (target: any, propertyKey: string, descriptor?: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
const [json, ...otherArgs] = args;
const unwrappedJson = get(json, wrapKey, json);
return originalMethod.apply(this, [unwrappedJson, ...otherArgs]);
};
};
}