import { Fetcher, FetchMiddleware } from './http/fetcher'; import Resource from './resource'; import { State, StateFactory } from './state'; import { LinkVariables } from './link'; import { FollowPromiseOne } from './follow-promise'; import { StateCache } from './cache'; export default class Client { /** * All relative urls will by default use the bookmarkUri to * expand. It should usually be the starting point of your * API */ bookmarkUri: string; /** * Supported content types * * Each content-type has a 'factory' that turns a HTTP response * into a State object. * * The last value in the array is the 'q=' value, used in Accept * headers. Higher means higher priority. */ contentTypeMap: { [mimeType: string]: [StateFactory, string]; }; /** * The cache for 'State' objects */ cache: StateCache; /** * The cache for 'Resource' objects. Each unique uri should * only ever get 1 associated resource. */ resources: Map; /** * Fetcher is a utility object that handles fetch() requests * and middlewares. */ fetcher: Fetcher; constructor(bookmarkUri: string); /** * Follows a relationship, based on its reltype. For example, this might be * 'alternate', 'item', 'edit' or a custom url-based one. * * This function can also follow templated uris. You can specify uri * variables in the optional variables argument. */ follow(rel: string, variables?: LinkVariables): FollowPromiseOne; /** * Returns a resource by its uri. * * This function doesn't do any HTTP requests. The uri is optional. If it's * not specified, it will return the bookmark resource. * * If a relative uri is passed, it will be resolved based on the bookmark * uri. * * @example * const res = ketting.go('https://example.org/); * @example * const res = ketting.go('/users/1'); * @example * const res = ketting.go(); // bookmark */ go(uri?: string): Resource; /** * Adds a fetch middleware, which will be executed for * each fetch() call. * * If 'origin' is specified, fetch middlewares can be executed * only if the host/origin matches. */ use(middleware: FetchMiddleware, origin?: string): void; /** * Clears the entire state cache */ clearCache(): void; /** * Transforms a fetch Response to a State object. */ getStateForResponse(uri: string, response: Response): Promise; /** * Caches a State object * * This function will also emit 'update' events to resources, and store all * embedded states. */ cacheState(state: State): void; private acceptHeader; private cacheExpireHandler; }