UNPKG

@nymphjs/client

Version:

Nymph.js - Client

113 lines (112 loc) 6.13 kB
import Entity, { type EntityInstanceType } from './Entity.js'; import type { EntityConstructor, EntityInterface, EntityJson, ServerCallResponse, ServerCallStaticResponse } from './Entity.types.js'; import EntityWeakCache from './EntityWeakCache.js'; import type { AbortableAsyncIterator } from './HttpRequester.js'; import type { EventType, NymphOptions, Options, RequestCallback, ResponseCallback, Selector } from './Nymph.types.js'; import type PubSub from './PubSub.js'; export default class Nymph { /** * And optional PubSub client instance. */ pubsub: PubSub | undefined; /** * A simple map of names to Entity classes. */ private entityClasses; /** * The entity class for this instance of Nymph. */ Entity: typeof Entity; private requestCallbacks; private responseCallbacks; private restUrl; private weakCache; /** * Headers that will be sent with every request. * * These are used by Tilmeld for authentication. */ headers: { [k: string]: string; }; /** * The entity cache. */ cache: EntityWeakCache; constructor(NymphOptions: NymphOptions); /** * Add your class to this instance. * * This will create a class that extends your class within this instance of * Nymph and return it. You can then use this class's constructor and methods, * which will use this instance of Nymph. * * Because this creates a subclass, don't use the class returned from * `getEntityClass` to check with `instanceof`. Instead, use the base class * that you passed into this method. */ addEntityClass<T extends EntityConstructor>(entityClass: T): T; getEntityClass<T extends EntityConstructor>(className: T): T; getEntityClass(className: string): EntityConstructor; newUID(name: string): Promise<number>; setUID(name: string, value: number): Promise<any>; getUID(name: string): Promise<number>; deleteUID(name: string): Promise<any>; saveEntity(entity: EntityInterface): Promise<EntityInterface>; saveEntities(entities: EntityInterface[]): Promise<boolean | EntityInterface[]>; patchEntity(entity: EntityInterface): Promise<EntityInterface>; patchEntities(entities: EntityInterface[]): Promise<boolean | EntityInterface[]>; private requestWithMethod; getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T> & { return: 'count'; }, ...selectors: Selector[]): Promise<number>; getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T> & { return: 'guid'; }, ...selectors: Selector[]): Promise<string | null>; getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T>, ...selectors: Selector[]): Promise<EntityInstanceType<T> | null>; getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T> & { return: 'count'; }, guid: string): Promise<number>; getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T> & { return: 'guid'; }, guid: string): Promise<string | null>; getEntity<T extends EntityConstructor = EntityConstructor>(options: Options<T>, guid: string): Promise<EntityInstanceType<T> | null>; getEntityData<T extends EntityConstructor = EntityConstructor>(options: Options<T> & { return: 'count'; }, ...selectors: Selector[]): Promise<number>; getEntityData<T extends EntityConstructor = EntityConstructor>(options: Options<T> & { return: 'guid'; }, ...selectors: Selector[]): Promise<string | null>; getEntityData<T extends EntityConstructor = EntityConstructor>(options: Options<T>, ...selectors: Selector[]): Promise<EntityJson<T> | null>; getEntityData<T extends EntityConstructor = EntityConstructor>(options: Options<T> & { return: 'count'; }, guid: string): Promise<number>; getEntityData<T extends EntityConstructor = EntityConstructor>(options: Options<T> & { return: 'guid'; }, guid: string): Promise<string | null>; getEntityData<T extends EntityConstructor = EntityConstructor>(options: Options<T>, guid: string): Promise<EntityJson<T> | null>; getEntities<T extends EntityConstructor = EntityConstructor>(options: Options<T> & { return: 'count'; }, ...selectors: Selector[]): Promise<number>; getEntities<T extends EntityConstructor = EntityConstructor>(options: Options<T> & { return: 'guid'; }, ...selectors: Selector[]): Promise<string[]>; getEntities<T extends EntityConstructor = EntityConstructor>(options: Options<T>, ...selectors: Selector[]): Promise<EntityInstanceType<T>[]>; initEntity<T extends EntityConstructor = EntityConstructor>(entityJSON: EntityJson<T>): EntityInstanceType<T>; getEntityFromCache<T extends EntityConstructor = EntityConstructor>(EntityClass: EntityConstructor, guid: string): EntityInstanceType<T> | null; setEntityToCache(EntityClass: EntityConstructor, entity: EntityInterface): void; initEntitiesFromData<T extends any>(item: T): T; deleteEntity(entity: EntityInterface | EntityInterface[], _plural?: boolean): Promise<any>; deleteEntities(entities: EntityInterface[]): Promise<any>; serverCall(entity: EntityInterface, method: string, params: any[], stateless?: boolean): Promise<ServerCallResponse>; serverCallStatic(className: string, method: string, params: any[]): Promise<ServerCallStaticResponse>; serverCallStaticIterator(className: string, method: string, params: any[]): Promise<AbortableAsyncIterator<ServerCallStaticResponse>>; on<T extends EventType>(event: T, callback: T extends 'request' ? RequestCallback : T extends 'response' ? ResponseCallback : never): () => boolean; off<T extends EventType>(event: T, callback: T extends 'request' ? RequestCallback : T extends 'response' ? ResponseCallback : never): boolean; } export declare class ClassNotAvailableError extends Error { constructor(message: string); } export declare class InvalidRequestError extends Error { constructor(message: string); }