1 |
|
2 | import Client from './client';
|
3 | import { State, HeadState } from './state';
|
4 | import { FollowPromiseOne, FollowPromiseMany } from './follow-promise';
|
5 | import { Link, LinkVariables } from './link';
|
6 | import { EventEmitter } from 'events';
|
7 | import { GetRequestOptions, PostRequestOptions, PatchRequestOptions, PutRequestOptions, HeadRequestOptions } from './types';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export declare class Resource<T = any> extends EventEmitter {
|
15 | |
16 |
|
17 |
|
18 | uri: string;
|
19 | |
20 |
|
21 |
|
22 | client: Client;
|
23 | private activeRefresh;
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 | constructor(client: Client, uri: string);
|
30 | /**
|
31 | * Gets the current state of the resource.
|
32 | *
|
33 | * This function will return a State object.
|
34 | */
|
35 | get(getOptions?: GetRequestOptions): Promise<State<T>>;
|
36 | /**
|
37 | * Does a HEAD request and returns a HeadState object.
|
38 | *
|
39 | * If there was a valid existing cache for a GET request, it will
|
40 | * still return that.
|
41 | */
|
42 | head(headOptions?: HeadRequestOptions): Promise<HeadState>;
|
43 | /**
|
44 | * Gets the current state of the resource, skipping
|
45 | * the cache.
|
46 | *
|
47 | * This function will return a State object.
|
48 | */
|
49 | refresh(getOptions?: GetRequestOptions): Promise<State<T>>;
|
50 | /**
|
51 | * Updates the server state with a PUT request
|
52 | */
|
53 | put(options: PutRequestOptions<T> | State): Promise<void>;
|
54 | /**
|
55 | * Deletes the resource
|
56 | */
|
57 | delete(): Promise<void>;
|
58 | /**
|
59 | * Sends a POST request to the resource.
|
60 | *
|
61 | * See the documentation for PostRequestOptions for more details.
|
62 | * This function is used for RPC-like endpoints and form submissions.
|
63 | *
|
64 | * This function will return the response as a State object.
|
65 | */
|
66 | post(options: PostRequestOptions): Promise<State>;
|
67 | /**
|
68 | * Sends a POST request, and follows to the next resource.
|
69 | *
|
70 | * If a server responds with a 201 Status code and a Location header,
|
71 | * it will automatically return the newly created resource.
|
72 | *
|
73 | * If the server responded with a 204 or 205, this function will return
|
74 | * `this`.
|
75 | */
|
76 | postFollow(options: PostRequestOptions): Promise<Resource>;
|
77 | /**
|
78 | * Sends a PATCH request to the resource.
|
79 | *
|
80 | * This function defaults to a application/json content-type header.
|
81 | *
|
82 | * If the server responds with 200 Status code this will return a State object
|
83 | */
|
84 | patch(options: PatchRequestOptions): Promise<void | State<T>>;
|
85 | /**
|
86 | * Follows a relationship, based on its reltype. For example, this might be
|
87 | * 'alternate', 'item', 'edit' or a custom url-based one.
|
88 | *
|
89 | * This function can also follow templated uris. You can specify uri
|
90 | * variables in the optional variables argument.
|
91 | */
|
92 | follow<TFollowedResource = any>(rel: string, variables?: LinkVariables): FollowPromiseOne<TFollowedResource>;
|
93 | /**
|
94 | * Follows a relationship based on its reltype. This function returns a
|
95 | * Promise that resolves to an array of Resource objects.
|
96 | *
|
97 | * If no resources were found, the array will be empty.
|
98 | */
|
99 | followAll<TFollowedResource = any>(rel: string): FollowPromiseMany<TFollowedResource>;
|
100 | /**
|
101 | * Resolves a new resource based on a relative uri.
|
102 | *
|
103 | * Use this function to manually get a Resource object via a uri. The uri
|
104 | * will be resolved based on the uri of the current resource.
|
105 | *
|
106 | * This function doesn't do any HTTP requests.
|
107 | */
|
108 | go<TGoResource = any>(uri: string): Resource<TGoResource>;
|
109 | /**
|
110 | * Does a HTTP request on the current resource URI
|
111 | */
|
112 | fetch(init?: RequestInit): Promise<Response>;
|
113 | /**
|
114 | * Does a HTTP request on the current resource URI.
|
115 | *
|
116 | * If the response was a 4XX or 5XX, this function will throw
|
117 | * an exception.
|
118 | */
|
119 | fetchOrThrow(init?: RequestInit): Promise<Response>;
|
120 | /**
|
121 | * Updates the state cache, and emits events.
|
122 | *
|
123 | * This will update the local state but *not* update the server
|
124 | */
|
125 | updateCache(state: State<T>): void;
|
126 | /**
|
127 | * Clears the state cache for this resource.
|
128 | */
|
129 | clearCache(): void;
|
130 | /**
|
131 | * Returns a Link object, by its REL.
|
132 | *
|
133 | * If the link does not exist, a LinkNotFound error will be thrown.
|
134 | *
|
135 | * @deprecated
|
136 | */
|
137 | link(rel: string): Promise<Link>;
|
138 | /**
|
139 | * Returns all links defined on this object.
|
140 | *
|
141 | * @deprecated
|
142 | */
|
143 | links(rel?: string): Promise<Link[]>;
|
144 | /**
|
145 | *
|
146 | * Returns true or false depending on if a link with the specified relation
|
147 | * type exists.
|
148 | *
|
149 | * @deprecated
|
150 | */
|
151 | hasLink(rel: string): Promise<boolean>;
|
152 | }
|
153 | export declare interface Resource<T = any> {
|
154 | |
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | on(event: 'update', listener: (state: State) => void): this;
|
165 | |
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | on(event: 'stale', listener: () => void): this;
|
174 | |
175 |
|
176 |
|
177 |
|
178 |
|
179 | on(event: 'delete', listener: () => void): this;
|
180 | |
181 |
|
182 |
|
183 |
|
184 | once(event: 'update', listener: (state: State) => void): this;
|
185 | |
186 |
|
187 |
|
188 |
|
189 | once(event: 'stale', listener: () => void): this;
|
190 | |
191 |
|
192 |
|
193 |
|
194 | once(event: 'delete', listener: () => void): this;
|
195 | |
196 |
|
197 |
|
198 | off(event: 'update', listener: (state: State) => void): this;
|
199 | |
200 |
|
201 |
|
202 | off(event: 'stale', listener: () => void): this;
|
203 | |
204 |
|
205 |
|
206 | off(event: 'delete', listener: () => void): this;
|
207 | |
208 |
|
209 |
|
210 | emit(event: 'update', state: State): boolean;
|
211 | |
212 |
|
213 |
|
214 | emit(event: 'stale'): boolean;
|
215 | |
216 |
|
217 |
|
218 | emit(event: 'delete'): boolean;
|
219 | }
|
220 | export default Resource;
|