@maicol07/coloquent
Version:
Library for retrieving model objects from a JSON-API, with a fluent syntax inspired by Laravel Eloquent.
187 lines (186 loc) • 7.89 kB
TypeScript
import { Builder } from "./Builder";
import { Resource } from "./Resource";
import { PluralResponse } from "./response/PluralResponse";
import { SingularResponse } from "./response/SingularResponse";
import { PaginationStrategy } from "./PaginationStrategy";
import { SaveResponse } from "./response/SaveResponse";
import { ToManyRelation } from "./relation/ToManyRelation";
import { ToOneRelation } from "./relation/ToOneRelation";
import { HttpClient } from "./httpclient/HttpClient";
export interface Model {
constructor: typeof Model;
}
export declare abstract class Model {
/**
* The page size
*/
protected static pageSize: number;
/**
* The pagination strategy
*/
protected static paginationStrategy: PaginationStrategy;
/**
* The number query parameter name. By default: 'page[number]'
*/
protected static paginationPageNumberParamName: string;
/**
* The size query parameter name. By default: 'page[size]'
*/
protected static paginationPageSizeParamName: string;
/**
* The offset query parameter name. By default: 'page[offset]'
*/
protected static paginationOffsetParamName: string;
/**
* The limit query parameter name. By default: 'page[limit]'
*/
protected static paginationLimitParName: string;
private id;
private readonly relations;
private readonly attributes;
/**
* The model endpoint base URL, e.g 'http://localhost:3000/api/v1'.
*/
protected static jsonApiBaseUrl: string | undefined;
private static _effectiveJsonApiBaseUrl;
/**
* The JSON-API type, choose plural, lowercase alphabetic only, e.g. 'artists'.
* Required property. If not set, Colu
*/
protected static jsonApiType: string | undefined;
private static _effectiveJsonApiType;
/**
* The endpoint. Optional. If not set, the {@link Model.jsonApiType}
* prepended with a slash (e.g. "/cars") will be used.
*/
protected static endpoint: string | undefined;
/**
* @type {HttpClient} The HTTP client used to perform request for this model.
* If not set, {@link AxiosHttpClient} will be used.
*/
protected static httpClient: HttpClient | undefined;
private static _effectiveHttpClient;
protected static readOnlyAttributes: string[];
protected static dates: {
[]: string;
};
private static dateFormatter;
/**
* Get a {@link Builder} instance from a {@link Model} instance
* so you can query without having a static reference to your specific {@link Model}
* class.
*/
query(): Builder<this, PluralResponse<this>>;
/**
* Get a {@link Builder} instance from a static {@link Model}
* so you can start querying
*/
static query<M extends Model>(): Builder<M, PluralResponse<M>>;
static get<M extends typeof Model & {
new (): Model;
}>(this: M, page?: number): Promise<PluralResponse<InstanceType<M>>>;
static first<M extends typeof Model & {
new (): Model;
}>(this: M): Promise<SingularResponse<InstanceType<M>>>;
static find<M extends typeof Model & {
new (): Model;
}>(this: M, id: string | number): Promise<SingularResponse<InstanceType<M>>>;
static with<M extends typeof Model & {
new (): Model;
}>(this: M, attribute: any): Builder<InstanceType<M>, PluralResponse<InstanceType<M>>>;
static limit<M extends typeof Model & {
new (): Model;
}>(this: M, limit: number): Builder<InstanceType<M>, PluralResponse<InstanceType<M>>>;
static where<M extends typeof Model & {
new (): Model;
}>(this: M, attribute: string, value: string): Builder<InstanceType<M>, PluralResponse<InstanceType<M>>>;
static orderBy<M extends typeof Model & {
new (): Model;
}>(this: M, attribute: string, direction?: string): Builder<InstanceType<M>, PluralResponse<InstanceType<M>>>;
static option<M extends typeof Model & {
new (): Model;
}>(this: M, queryParameter: string, value: string): Builder<InstanceType<M>, PluralResponse<InstanceType<M>>>;
private serialize;
private serializeRelatedModel;
private serializeToOneRelation;
private serializeToManyRelation;
save(): Promise<SaveResponse<this>>;
create(): Promise<SaveResponse<this>>;
delete(): Promise<void>;
/**
* @return A {@link Promise} resolving to:
*
* * the representation of this {@link Model} instance in the API if this {@link Model} has an ID and this ID can
* be found in the API too
* * `undefined` if this {@link Model} instance has no ID
* * `null` if there _is_ an ID, but a {@link Model} with this ID cannot be found in the backend
*/
fresh(): Promise<this | null | undefined>;
getRelations(): {
[]: any;
};
getRelationsKeys(parentRelationName?: string): Array<string>;
/**
* The base URL that is used to call the API
*/
static get effectiveJsonApiBaseUrl(): string;
static get effectiveJsonApiType(): string;
private static get effectiveEndpoint();
static getJsonApiUrl(): string;
/**
* The {@link HttpClient} that is used by Coloquent. Is equal to {@link httpClient}
* property unless that one was left undefined, in which case it is an instance
* of {@link AxiosHttpClient}. This is a read-only property.
*/
static get effectiveHttpClient(): HttpClient;
/**
* @deprecated Use the static method with the same name instead
*/
getJsonApiType(): string;
/**
* @deprecated Use the static property {@link jsonApiBaseUrl} or
* {@link effectiveJsonApiBaseUrl}
*/
getJsonApiBaseUrl(): string;
/**
* @deprecated Use the static {@link httpClient} to get the one that is
* configured, and {@link effectiveHttpClient} to get the one that is
*/
getHttpClient(): HttpClient;
populateFromResource(resource: Resource): void;
/**
* @deprecated Access the static {@link pageSize} property directly
*/
static getPageSize(): number;
static getPaginationStrategy(): PaginationStrategy;
static getPaginationPageNumberParamName(): string;
static getPaginationPageSizeParamName(): string;
static getPaginationOffsetParamName(): string;
static getPaginationLimitParamName(): string;
protected getRelation(relationName: string): any;
setRelation(relationName: string, value: any): void;
getAttributes(): {
[]: any;
};
protected getAttribute(attributeName: string): any;
protected getAttributeAsDate(attributeName: string): any;
private isDateAttribute;
protected setAttribute(attributeName: string, value: any): void;
/**
* We use a single instance of DateFormatter, which is stored as a static property on Model, to minimize the number
* of times we need to instantiate the DateFormatter class. By using this getter a DateFormatter is instantiated
* only when it is used at least once.
*
* @returns DateFormatter
*/
private static getDateFormatter;
getApiId(): string | undefined;
setApiId(id: string | undefined): void;
protected hasMany<R extends Model>(relatedType: typeof Model): ToManyRelation<R, this>;
protected hasMany<R extends Model>(relatedType: typeof Model, relationName: string): ToManyRelation<R, this>;
protected hasOne<R extends Model>(relatedType: typeof Model): ToOneRelation<R, this>;
protected hasOne<R extends Model>(relatedType: typeof Model, relationName: string): ToOneRelation<R, this>;
private get hasId();
private getConstructor;
private getConstructorOf;
}