UNPKG

5.18 kBTypeScriptView Raw
1import FollowablePromise from './followable-promise';
2import Ketting from './ketting';
3import Link from './link';
4import Representation from './representor/base';
5/**
6 * A 'resource' represents an endpoint on the server.
7 *
8 * The endpoint has a uri, you might for example be able to GET its
9 * presentation.
10 *
11 * A resource may also have a list of links on them, pointing to other
12 * resources.
13 *
14 * @param {Client} client
15 * @param {string} uri
16 * @constructor
17 */
18export default class Resource<T = any> {
19 /**
20 * Reference to the main Client
21 */
22 client: Ketting;
23 /**
24 * The current representation, or body of the resource.
25 */
26 repr: Representation | null;
27 /**
28 * The uri of the resource
29 */
30 uri: string;
31 /**
32 * A default mimetype for the resource.
33 *
34 * This mimetype is used for PUT and POST requests by default.
35 * The mimetype is sniffed in a few different ways.
36 *
37 * If a GET request is done, and the GET request had a mimetype it will
38 * be used to set this value.
39 *
40 * It's also possible for resources to get a mimetype through a link.
41 *
42 * If the mimetype was "null" when doing the request, the chosen mimetype
43 * will come from the first item in Client.resourceTypes
44 */
45 contentType: string | null;
46 private inFlightRefresh;
47 /**
48 * A list of rels that should be added to a Prefer-Push header.
49 */
50 private preferPushRels;
51 constructor(client: Ketting, uri: string, contentType?: string);
52 /**
53 * Fetches the resource representation.
54 * Returns a promise that resolves to a parsed json object.
55 */
56 get(): Promise<T>;
57 /**
58 * Updates the resource representation with a new JSON object.
59 */
60 put(body: T): Promise<void>;
61 /**
62 * Updates the resource representation with a new JSON object.
63 */
64 delete(): Promise<void>;
65 /**
66 * Sends a POST request to the resource.
67 *
68 * This function assumes that POST is used to create new resources, and
69 * that the response will be a 201 Created along with a Location header that
70 * identifies the new resource location.
71 *
72 * This function returns a Promise that resolves into the newly created
73 * Resource.
74 *
75 * If no Location header was given, it will resolve still, but with an empty
76 * value.
77 */
78 post(body: object): Promise<Resource | null>;
79 /**
80 * Sends a PATCH request to the resource.
81 *
82 * This function defaults to a application/json content-type header.
83 */
84 patch(body: object): Promise<void>;
85 /**
86 * Refreshes the representation for this resource.
87 *
88 * This function will return the a parsed JSON object, like the get
89 * function does.
90 *
91 * @return {object}
92 */
93 refresh(): Promise<T>;
94 /**
95 * Returns the links for this resource, as a promise.
96 *
97 * The rel argument is optional. If it's given, we will only return links
98 * from that relationship type.
99 */
100 links(rel?: string): Promise<Link[]>;
101 /**
102 * Follows a relationship, based on its reltype. For example, this might be
103 * 'alternate', 'item', 'edit' or a custom url-based one.
104 *
105 * This function can also follow templated uris. You can specify uri
106 * variables in the optional variables argument.
107 */
108 follow(rel: string, variables?: object): FollowablePromise;
109 /**
110 * Follows a relationship based on its reltype. This function returns a
111 * Promise that resolves to an array of Resource objects.
112 *
113 * If no resources were found, the array will be empty.
114 */
115 followAll(rel: string): Promise<Resource[]>;
116 /**
117 * Resolves a new resource based on a relative uri.
118 *
119 * Use this function to manually get a Resource object via a uri. The uri
120 * will be resolved based on the uri of the current resource.
121 *
122 * This function doesn't do any HTTP requests.
123 */
124 go(uri: string): Resource;
125 /**
126 * Returns the representation for the object.
127 * If it wasn't fetched yet, this function does the fetch as well.
128 *
129 * Usually you will want to use the `get()` method instead, unless you need
130 * the full object.
131 */
132 representation(): Promise<Representation>;
133 clearCache(): void;
134 /**
135 * Does an arbitrary HTTP request on the resource using the Fetch API.
136 *
137 * The method signature is the same as the MDN fetch object. However, it's
138 * possible in this case to not specify a URI or specify a relative URI.
139 *
140 * When doing the actual request, any relative uri will be resolved to the
141 * uri of the current resource.
142 *
143 * @see https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
144 */
145 fetch(input: Request | string | RequestInit, init?: RequestInit): Promise<Response>;
146 /**
147 * Does a HTTP request and throws an exception if the server emitted
148 * a HTTP error.
149 *
150 * @see https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
151 */
152 fetchAndThrow(input: Request | string | RequestInit, init?: RequestInit): Promise<Response>;
153}