UNPKG

2.96 kBTypeScriptView Raw
1import { Fetcher, FetchMiddleware } from './http/fetcher';
2import Resource from './resource';
3import { State, StateFactory } from './state';
4import { LinkVariables } from './link';
5import { FollowPromiseOne } from './follow-promise';
6import { StateCache } from './cache';
7export default class Client {
8 /**
9 * All relative urls will by default use the bookmarkUri to
10 * expand. It should usually be the starting point of your
11 * API
12 */
13 bookmarkUri: string;
14 /**
15 * Supported content types
16 *
17 * Each content-type has a 'factory' that turns a HTTP response
18 * into a State object.
19 *
20 * The last value in the array is the 'q=' value, used in Accept
21 * headers. Higher means higher priority.
22 */
23 contentTypeMap: {
24 [mimeType: string]: [StateFactory<any>, string];
25 };
26 /**
27 * The cache for 'State' objects
28 */
29 cache: StateCache;
30 /**
31 * The cache for 'Resource' objects. Each unique uri should
32 * only ever get 1 associated resource.
33 */
34 resources: Map<string, Resource>;
35 /**
36 * Fetcher is a utility object that handles fetch() requests
37 * and middlewares.
38 */
39 fetcher: Fetcher;
40 constructor(bookmarkUri: string);
41 /**
42 * Follows a relationship, based on its reltype. For example, this might be
43 * 'alternate', 'item', 'edit' or a custom url-based one.
44 *
45 * This function can also follow templated uris. You can specify uri
46 * variables in the optional variables argument.
47 */
48 follow<TFollowedResource = any>(rel: string, variables?: LinkVariables): FollowPromiseOne<TFollowedResource>;
49 /**
50 * Returns a resource by its uri.
51 *
52 * This function doesn't do any HTTP requests. The uri is optional. If it's
53 * not specified, it will return the bookmark resource.
54 *
55 * If a relative uri is passed, it will be resolved based on the bookmark
56 * uri.
57 *
58 * @example
59 * const res = ketting.go('https://example.org/);
60 * @example
61 * const res = ketting.go<Author>('/users/1');
62 * @example
63 * const res = ketting.go(); // bookmark
64 */
65 go<TResource = any>(uri?: string): Resource<TResource>;
66 /**
67 * Adds a fetch middleware, which will be executed for
68 * each fetch() call.
69 *
70 * If 'origin' is specified, fetch middlewares can be executed
71 * only if the host/origin matches.
72 */
73 use(middleware: FetchMiddleware, origin?: string): void;
74 /**
75 * Clears the entire state cache
76 */
77 clearCache(): void;
78 /**
79 * Transforms a fetch Response to a State object.
80 */
81 getStateForResponse(uri: string, response: Response): Promise<State>;
82 /**
83 * Caches a State object
84 *
85 * This function will also emit 'update' events to resources, and store all
86 * embedded states.
87 */
88 cacheState(state: State): void;
89 private acceptHeader;
90 private cacheExpireHandler;
91}
92
\No newline at end of file