UNPKG

4.5 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Base interface for both FollowOne and FollowAll
5 */
6class Follower {
7 preFetch() {
8 this.prefetchEnabled = true;
9 return this;
10 }
11}
12/**
13 * The Follower class is what's being returned from follow() functions.
14 *
15 * It's 'PromiseLike', which means you can treat it like a Promise, and it
16 * can be awaited. When used as a Promise, it resolves to the Resource object
17 * that was followed.
18 *
19 * In addition to being a Promise<Resource> stand-in, it also exposes other
20 * functions, namely:
21 *
22 * * `follow()` to allow a user to chain several follow() functions to do
23 * several 'hops' all at once.
24 * * `followAll()`, allowing a user to call `followAll()` at the end of a
25 * chain.
26 */
27class FollowerOne extends Follower {
28 constructor(resource, rel, variables) {
29 super();
30 this.resource = resource;
31 this.rel = rel;
32 this.variables = variables;
33 }
34 /**
35 * This 'then' function behaves like a Promise then() function.
36 *
37 * This method signature is pretty crazy, but trust that it's pretty much
38 * like any then() method on a promise.
39 */
40 then(onfulfilled, onrejected) {
41 return this.fetchLinkedResource().then(onfulfilled, onrejected);
42 }
43 /**
44 * This 'then' function behaves like a Promise then() function.
45 */
46 catch(onrejected) {
47 return this.fetchLinkedResource().then(undefined, onrejected);
48 }
49 /**
50 * Follow another link immediately after following this link.
51 *
52 * This allows you to follow several hops of links in one go.
53 *
54 * For example: resource.follow('foo').follow('bar');
55 */
56 follow(rel, variables) {
57 return new FollowerOne(this.fetchLinkedResource(), rel, variables);
58 }
59 /**
60 * Follows a set of links immediately after following this link.
61 *
62 * For example: resource.follow('foo').followAll('item');
63 */
64 async followAll(rel) {
65 return new FollowerMany(this.fetchLinkedResource(), rel);
66 }
67 /**
68 * This function does the actual fetching, to obtained the url
69 * of the linked resource. It returns the Resource object.
70 */
71 async fetchLinkedResource() {
72 const resource = await this.resource;
73 const link = await resource.link(this.rel);
74 let href;
75 if (link.templated && this.variables) {
76 href = link.expand(this.variables);
77 }
78 else {
79 href = link.resolve();
80 }
81 const newResource = resource.go(href);
82 if (link.type) {
83 newResource.contentType = link.type;
84 }
85 if (this.prefetchEnabled) {
86 newResource.get().catch(err => {
87 // tslint:disable-next-line no-console
88 console.warn('Error while prefetching linked resource', err);
89 });
90 }
91 return newResource;
92 }
93}
94exports.FollowerOne = FollowerOne;
95/**
96 */
97class FollowerMany extends Follower {
98 constructor(resource, rel) {
99 super();
100 this.resource = resource;
101 this.rel = rel;
102 }
103 /**
104 * This 'then' function behaves like a Promise then() function.
105 */
106 then(onfulfilled, onrejected) {
107 return this.fetchLinkedResources().then(onfulfilled, onrejected);
108 }
109 /**
110 * This 'then' function behaves like a Promise then() function.
111 */
112 catch(onrejected) {
113 return this.fetchLinkedResources().then(undefined, onrejected);
114 }
115 /**
116 * This function does the actual fetching, to obtained the url
117 * of the linked resource. It returns the Resource object.
118 */
119 async fetchLinkedResources() {
120 const resource = await this.resource;
121 const links = await resource.links(this.rel);
122 let href;
123 const result = [];
124 for (const link of links) {
125 href = link.resolve();
126 const newResource = resource.go(href);
127 if (link.type) {
128 newResource.contentType = link.type;
129 }
130 result.push(newResource);
131 if (this.prefetchEnabled) {
132 newResource.get().catch(err => {
133 // tslint:disable-next-line no-console
134 console.warn('Error while prefetching linked resource', err);
135 });
136 }
137 }
138 return result;
139 }
140}
141exports.FollowerMany = FollowerMany;
142//# sourceMappingURL=follower.js.map
\No newline at end of file