ketting
Version:
Opinionated HATEOAS / Rest client.
121 lines (120 loc) • 4.09 kB
TypeScript
import { Fetcher, FetchMiddleware } from './http/fetcher.js';
import Resource from './resource.js';
import { State, StateFactory } from './state/index.js';
import { Link, LinkVariables } from './link.js';
import { FollowPromiseOne } from './follow-promise.js';
import { StateCache } from './cache/index.js';
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<any>, 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<string, Resource>;
/**
* 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<TFollowedResource = any>(rel: string, variables?: LinkVariables): FollowPromiseOne<TFollowedResource>;
/**
* 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<Author>('/users/1');
* @example
* const res = ketting.go(); // bookmark
*/
go<TResource = any>(uri?: string | Link): Resource<TResource>;
/**
* 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;
/**
* Caches a State object
*
* This function will also emit 'update' events to resources, and store all
* embedded states.
*/
cacheState(state: State): void;
/**
* cacheDependencies contains all cache relationships between
* resources.
*
* This lets a user (for example) let a resource automatically
* expire, if another one expires.
*
* A server can populate this list using the `inv-by' link.
*
* @deprecated This property will go private in a future release.
*/
cacheDependencies: Map<string, Set<string>>;
/**
* Adds a cache dependency between two resources.
*
* If the 'target' resource ever expires, it will cause 'dependentUri' to
* also expire.
*
* Both argument MUST be absolute urls.
*/
addCacheDependency(targetUri: string, dependentUri: string): void;
/**
* Helper function for clearing the cache for a resource.
*
* This function will also emit the 'stale' event for resources that have
* subscribers, and handle any dependent resource caches.
*
* If any resources are specified in deletedUris, those will not
* receive 'stale' events, but 'delete' events instead.
*/
clearResourceCache(staleUris: string[], deletedUris: string[]): void;
/**
* Transforms a fetch Response to a State object.
*/
getStateForResponse(uri: string, response: Response): Promise<State>;
}