1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.FollowPromiseMany = exports.FollowPromiseOne = void 0;
|
4 | const link_1 = require("./link");
|
5 | const uri_1 = require("./util/uri");
|
6 | const uri_template_1 = require("./util/uri-template");
|
7 |
|
8 |
|
9 |
|
10 | class FollowPromise {
|
11 | constructor() {
|
12 | this.prefetchEnabled = false;
|
13 | this.preferPushEnabled = false;
|
14 | this.preferTranscludeEnabled = false;
|
15 | this.useHeadEnabled = false;
|
16 | }
|
17 | preFetch() {
|
18 | this.prefetchEnabled = true;
|
19 | return this;
|
20 | }
|
21 | preferPush() {
|
22 | this.preferPushEnabled = true;
|
23 | return this;
|
24 | }
|
25 | preferTransclude() {
|
26 | this.preferTranscludeEnabled = true;
|
27 | return this;
|
28 | }
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | useHead() {
|
36 | this.useHeadEnabled = true;
|
37 | return this;
|
38 | }
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | class FollowPromiseOne extends FollowPromise {
|
56 | constructor(resource, rel, variables) {
|
57 | super();
|
58 | this.resource = resource;
|
59 | this.rel = rel;
|
60 | this.variables = variables;
|
61 | }
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | then(onfulfilled, onrejected) {
|
69 | return this.fetchLinkedResource().then(onfulfilled, onrejected);
|
70 | }
|
71 | |
72 |
|
73 |
|
74 | catch(onrejected) {
|
75 | return this.fetchLinkedResource().then(undefined, onrejected);
|
76 | }
|
77 | |
78 |
|
79 |
|
80 | finally(onfinally) {
|
81 | return this.then(() => onfinally(), () => onfinally());
|
82 | }
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | follow(rel, variables) {
|
91 | return new FollowPromiseOne(this.fetchLinkedResource(), rel, variables);
|
92 | }
|
93 | |
94 |
|
95 |
|
96 |
|
97 |
|
98 | followAll(rel) {
|
99 | return new FollowPromiseMany(this.fetchLinkedResource(), rel);
|
100 | }
|
101 | |
102 |
|
103 |
|
104 |
|
105 | async fetchLinkedResource() {
|
106 | const resource = await this.resource;
|
107 | const headers = {};
|
108 | if (this.preferPushEnabled) {
|
109 | headers['Prefer-Push'] = this.rel;
|
110 | }
|
111 | if (!this.useHeadEnabled && this.preferTranscludeEnabled) {
|
112 | headers.Prefer = 'transclude=' + this.rel;
|
113 | }
|
114 | let state;
|
115 | if (this.useHeadEnabled) {
|
116 | state = await resource.head({ headers });
|
117 | }
|
118 | else {
|
119 | state = await resource.get({
|
120 | headers
|
121 | });
|
122 | }
|
123 | const link = state.links.get(this.rel);
|
124 | if (!link)
|
125 | throw new link_1.LinkNotFound(`Link with rel ${this.rel} on ${state.uri} not found`);
|
126 | let href;
|
127 | if (link.templated && this.variables) {
|
128 | href = uri_template_1.expand(link, this.variables);
|
129 | }
|
130 | else {
|
131 | href = uri_1.resolve(link);
|
132 | }
|
133 | const newResource = resource.go(href);
|
134 | if (this.prefetchEnabled) {
|
135 | newResource.get().catch(err => {
|
136 |
|
137 | console.warn('Error while prefetching linked resource', err);
|
138 | });
|
139 | }
|
140 | return newResource;
|
141 | }
|
142 | }
|
143 | exports.FollowPromiseOne = FollowPromiseOne;
|
144 |
|
145 |
|
146 | class FollowPromiseMany extends FollowPromise {
|
147 | constructor(resource, rel) {
|
148 | super();
|
149 | this.resource = resource;
|
150 | this.rel = rel;
|
151 | }
|
152 | |
153 |
|
154 |
|
155 | then(onfulfilled, onrejected) {
|
156 | return this.fetchLinkedResources().then(onfulfilled, onrejected);
|
157 | }
|
158 | |
159 |
|
160 |
|
161 | catch(onrejected) {
|
162 | return this.fetchLinkedResources().then(undefined, onrejected);
|
163 | }
|
164 | |
165 |
|
166 |
|
167 | finally(onfinally) {
|
168 | return this.then(() => onfinally(), () => onfinally());
|
169 | }
|
170 | |
171 |
|
172 |
|
173 |
|
174 | async fetchLinkedResources() {
|
175 | const resource = await this.resource;
|
176 | const headers = {};
|
177 | if (this.preferPushEnabled) {
|
178 | headers['Prefer-Push'] = this.rel;
|
179 | }
|
180 | if (!this.useHeadEnabled && this.preferTranscludeEnabled) {
|
181 | headers.Prefer = 'transclude=' + this.rel;
|
182 | }
|
183 | let state;
|
184 | if (this.useHeadEnabled) {
|
185 | state = await resource.head({ headers });
|
186 | }
|
187 | else {
|
188 | state = await resource.get({
|
189 | headers
|
190 | });
|
191 | }
|
192 | const links = state.links.getMany(this.rel);
|
193 | let href;
|
194 | const result = [];
|
195 | for (const link of links) {
|
196 | href = uri_1.resolve(link);
|
197 | const newResource = resource.go(href);
|
198 | result.push(newResource);
|
199 | if (this.prefetchEnabled) {
|
200 | newResource.get().catch(err => {
|
201 |
|
202 | console.warn('Error while prefetching linked resource', err);
|
203 | });
|
204 | }
|
205 | }
|
206 | return result;
|
207 | }
|
208 | }
|
209 | exports.FollowPromiseMany = FollowPromiseMany;
|
210 |
|
\ | No newline at end of file |