chain-simple
Version:
Main purpose of this package is - provide simple way to build chain between any item methods
52 lines (51 loc) • 1.62 kB
TypeScript
type TFn = (...args: any) => any;
type TReplaceReturnType<T extends TFn, TNewReturnType> = (...args: Parameters<T>) => TNewReturnType;
export type TChainable<T extends Record<string, TFn>> = {
[K in keyof T]: TReplaceReturnType<T[K], ReturnType<T[K]> & TChainable<T>>;
};
type TConfig = {
getEntity?: string;
extendOnly?: boolean;
extendProxed?: (propName: any) => {
[k: string]: any;
} | ((item: any) => {
[k: string]: any;
});
getEntityPropList?: string[] | {
[k: string]: any;
};
};
/**
* @example
* const {chainProps} = require('chain-simple');
* const obj = {
* async method1() {
* return Promise.resolve(1).then(value => {
* console.log('method1', value);
* return value;
* });
* },
* async method2() {
* return Promise.resolve(2).then(value => {
* console.log('method2', value);
* return value;
* });
* },
* async method3() {
* return Promise.resolve(3).then(value => {
* console.log('method3', value);
* return value;
* });
* },
* };
* const chainableObj = chainProps(obj);
* obj.method1().method3().then((val) => console.log(val))
*
*
* @param {!object} item
* @param {{getEntity: string}} [config] config to describe how to get original not project object
* @returns {object} object with chainable properties
*/
declare function chainProps(item: any, config?: TConfig): any;
declare function makeConstructorInstancePropertiesChainable(constructorFunction: any, config?: TConfig): any;
export { chainProps, makeConstructorInstancePropertiesChainable };