UNPKG

7.11 kBTypeScriptView Raw
1/// <reference types="node" />
2import Client from './client';
3import { State, HeadState } from './state';
4import { FollowPromiseOne, FollowPromiseMany } from './follow-promise';
5import { Link, LinkVariables } from './link';
6import { EventEmitter } from 'events';
7import { GetRequestOptions, PostRequestOptions, PatchRequestOptions, PutRequestOptions, HeadRequestOptions } from './types';
8/**
9 * A 'resource' represents an endpoint on a server.
10 *
11 * A resource has a uri, methods that correspond to HTTP methods,
12 * and events to subscribe to state changes.
13 */
14export declare class Resource<T = any> extends EventEmitter {
15 /**
16 * URI of the current resource
17 */
18 uri: string;
19 /**
20 * Reference to the Client that created the resource
21 */
22 client: Client;
23 private activeRefresh;
24 /**
25 * Create the resource.
26 *
27 * This is usually done by the Client.
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}
153export declare interface Resource<T = any> {
154 /**
155 * Subscribe to the 'update' event.
156 *
157 * This event will get triggered whenever a new State is received
158 * from the server, either through a GET request or if it was
159 * transcluded.
160 *
161 * It will also trigger when calling 'PUT' with a full state object,
162 * and when updateCache() was used.
163 */
164 on(event: 'update', listener: (state: State) => void): this;
165 /**
166 * Subscribe to the 'stale' event.
167 *
168 * This event will get triggered whenever an unsafe method was
169 * used, such as POST, PUT, PATCH, etc.
170 *
171 * When any of these methods are used, the local cache is stale.
172 */
173 on(event: 'stale', listener: () => void): this;
174 /**
175 * Subscribe to the 'delete' event.
176 *
177 * This event gets triggered when the `DELETE` http method is used.
178 */
179 on(event: 'delete', listener: () => void): this;
180 /**
181 * Subscribe to the 'update' event and unsubscribe after it was
182 * emitted the first time.
183 */
184 once(event: 'update', listener: (state: State) => void): this;
185 /**
186 * Subscribe to the 'stale' event and unsubscribe after it was
187 * emitted the first time.
188 */
189 once(event: 'stale', listener: () => void): this;
190 /**
191 * Subscribe to the 'delete' event and unsubscribe after it was
192 * emitted the first time.
193 */
194 once(event: 'delete', listener: () => void): this;
195 /**
196 * Unsubscribe from the 'update' event
197 */
198 off(event: 'update', listener: (state: State) => void): this;
199 /**
200 * Unsubscribe from the 'stale' event
201 */
202 off(event: 'stale', listener: () => void): this;
203 /**
204 * Unsubscribe from the 'delete' event
205 */
206 off(event: 'delete', listener: () => void): this;
207 /**
208 * Emit an 'update' event.
209 */
210 emit(event: 'update', state: State): boolean;
211 /**
212 * Emit a 'stale' event.
213 */
214 emit(event: 'stale'): boolean;
215 /**
216 * Emit a 'delete' event.
217 */
218 emit(event: 'delete'): boolean;
219}
220export default Resource;