amocrm-client
Version:
JS Library for AmoCRM
31 lines (25 loc) • 1.22 kB
text/typescript
import { IRequestOptions } from "../../../interfaces/common";
import { ICriteria, IResourceEntity, IResourceFactory, IResourcePagination } from "../../../interfaces/api";
import ResourcePagination from "../../ResourcePagination";
import { TFactoryConstructor } from "../../../types";
export interface IHasGetWithCriteria<T> {
get(criteria?: Partial<ICriteria>, options?: IRequestOptions): Promise<IResourcePagination<T>>;
}
export function hasGetByCriteria<T extends IResourceEntity<IResourceFactory<T>>>(Base: TFactoryConstructor<T>): TFactoryConstructor<T> {
return class HasGetWithCriteria extends Base implements IHasGetWithCriteria<T>, IResourceFactory<T> {
async get(criteria?: Partial<ICriteria>, options?: IRequestOptions) {
const url = this.getUrl();
const params = {
url,
criteria,
options,
factory: this,
embedded: this.getEmbeddedKey()
};
const pagination = new ResourcePagination<T>(this.getRequest(), params);
await pagination.fetch();
this.emit('get');
return pagination;
}
};
}