import { Item, ItemOptions } from './item'; import { Pagination } from './pagination'; /** * Item cache. * Dictates the implementation of the item cache. */ export interface ItemCache { has(key: string): boolean; get(key: string): Promise; put(key: string, item: Promise): Promise; remove(key: string): void; } /** * Common interfaces. */ export interface ParsedURI { resource: typeof Item; id: string | number | null; uri: string; } export interface URLSearchParams { [key: string]: any; } export interface RequestOptions { method?: 'OPTIONS' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'GET' | 'DELETE' | 'JSONP'; params?: URLSearchParams | QueryParams | null; body?: any; cache?: boolean; paginate?: boolean; } export interface QueryParams { page?: number; perPage?: number; where?: any; sort?: any; } export interface FetchExtras { pagination?: Pagination; origin?: string[]; } export declare type FetchOptions = RequestOptions & FetchExtras; export interface PotionResponse { headers: any; body: any; } export interface PotionOptions { host?: string; prefix?: string; cache?: ItemCache; } export interface PotionResources { [key: string]: typeof Item; } /** * This class contains the main logic for interacting with the Flask Potion backend. * Note that this class does not contain the logic for making the HTTP requests, * it is up to the child class to implement the logic for that through the `request` method. * Furthermore, the child class also needs to provide the Promise class/fn as this class is set to use the native Promise only available from ES6. * * @example * class Potion extends PotionBase { * protected request(uri, options?: RequestOptions): Promise { * // Here we need to implement the actual HTTP request * }; * } */ export declare abstract class PotionBase { readonly resources: PotionResources; readonly cache: ItemCache; host: string; readonly prefix: string; private readonly Promise; private requests; constructor({host, prefix, cache}?: PotionOptions); /** * Register a resource. * @param uri - Path on which the resource is registered. * @param resource * @param options - Set the property options for any instance of the resource (setting a property to readonly for instance). */ register(uri: string, resource: typeof Item, options?: ItemOptions): typeof Item; /** * Register a resource. * @param uri - Path on which the resource is registered. * @param options - Set the property options for any instance of the resource (setting a property to readonly for instance). * * @example * @potion.registerAs('/user') * class User extends Item {} */ registerAs(uri: string, options?: ItemOptions): ClassDecorator; /** * Make a HTTP request. * @param uri * @param options * @returns An object with {body, headers} where {body} can be anything and {headers} is an object with the response headers from the HTTP request. */ protected abstract request(uri: string, options?: RequestOptions): Promise; fetch(uri: string, requestOptions?: RequestOptions, extras?: FetchExtras): Promise | any>; private resolve(uri, options); private serialize(options); private deserialize({headers, body}, uri, options); private fromPotionJSON(json, origin); private parsePotionJSONProperties(json, origin); private parseURI({$ref, $uri, $type, $id}); }