1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | module.exports = Request;
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function Request(path, client, delim) {
|
19 | if (!(this instanceof Request)) return new Request(path, client);
|
20 |
|
21 |
|
22 | this.client = client;
|
23 | if (!this.client) throw new Error('hyper-path requires a client to be passed as the second argument');
|
24 |
|
25 | this.delim = delim || '.';
|
26 | this.parse(path);
|
27 |
|
28 | this._listeners = {};
|
29 | this._scope = {};
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | Request.prototype.scope = function(scope) {
|
40 | this._scope = this.wrappedScope ? [scope] : scope;
|
41 | if (this._fn) this.get();
|
42 | return this;
|
43 | };
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | Request.prototype.on = function(fn) {
|
53 | this._fn = fn;
|
54 | this.get();
|
55 | return this;
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | Request.prototype.get =
|
65 | Request.prototype.refresh = function(fn) {
|
66 | var self = this;
|
67 | var scope = self._scope;
|
68 | fn = fn || self._fn;
|
69 |
|
70 |
|
71 | this.off();
|
72 |
|
73 | if (!self.isRoot) return self.traverse(scope, {}, 0, self.path, {}, true, fn);
|
74 |
|
75 | return this._listeners['.'] = self.client.root(function(err, body, links) {
|
76 | if (err) return fn(err);
|
77 | links = links || {};
|
78 | return self.traverse(body || scope, links, 1, self.path, body, true, fn);
|
79 | });
|
80 | };
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | Request.prototype.parse = function(str) {
|
98 | var path = this.path = Array.isArray(str) ? str.slice() : str.split(this.delim);
|
99 | this.index = path[0];
|
100 | if (path.length === 1) {
|
101 | this.wrappedScope = true;
|
102 | path.unshift(0);
|
103 | }
|
104 | this.isRoot = this.index === '';
|
105 | this.target = path[path.length - 1];
|
106 | };
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | Request.prototype.off = function() {
|
115 | for (var i = 0, listener; i < this._listeners; i++) {
|
116 | listener = this._listener[i];
|
117 | if (listener) listener();
|
118 | }
|
119 | return this;
|
120 | };
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | Request.prototype.traverse = function(parent, links, i, path, parentDocument, normalize, cb) {
|
136 | var self = this;
|
137 |
|
138 |
|
139 | if (i >= path.length) return cb(null, normalize ? normalizeTarget(parent) : parent);
|
140 |
|
141 | var key = path[i];
|
142 | var value = get(key, parent, links);
|
143 |
|
144 | // we couldn't find the property
|
145 | if (!isDefined(value)) return self.handleUndefined(key, parent, links, i, path, parentDocument, normalize, cb);
|
146 |
|
147 | var next = i + 1;
|
148 | var nextProp = path[next];
|
149 | var href = value.href;
|
150 |
|
151 | // we don't have a link to use or it's set locally on the object
|
152 | if (!href || value.hasOwnProperty(nextProp)) return self.traverse(value, links, next, path, parentDocument, normalize, cb);
|
153 |
|
154 | // it's a local pointer
|
155 | if (href.charAt(0) === '#') return self.fetchJsonPath(parentDocument, links, href.slice(1), next, path, normalize, cb);
|
156 |
|
157 | // fetch the resource
|
158 | return self.fetchResource(href, next, path, normalize, cb);
|
159 | }
|
160 |
|
161 | /**
|
162 | * Handle an undefined value
|
163 | *
|
164 | * @param {String} key
|
165 | * @param {Object|Array} parent
|
166 | * @param {Object} links
|
167 | * @param {Integer} i
|
168 | * @param {Array} path
|
169 | * @param {Object} parentDocument
|
170 | * @param {Boolean} normalize
|
171 | * @param {Function} cb
|
172 | */
|
173 |
|
174 | Request.prototype.handleUndefined = function(key, parent, links, i, path, parentDocument, normalize, cb) {
|
175 |
|
176 | var collection = normalizeTarget(parent);
|
177 | if (collection && collection.hasOwnProperty(key)) return this.traverse(collection, links, i, path, parentDocument, normalize, cb);
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | var value;
|
184 | if (this.wrappedScope) value = parent[key];
|
185 | if (typeof value === 'function') value = void 0;
|
186 | return cb(null, value);
|
187 | };
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | Request.prototype.fetchResource = function(href, i, path, normalize, cb) {
|
200 | var self = this;
|
201 | var orig = href;
|
202 | var parts = orig.split('#');
|
203 | href = parts[0];
|
204 |
|
205 | var listener = self._listeners[orig];
|
206 | var res = self._listeners[orig] = self.client.get(href, function(err, body, links) {
|
207 | if (err) return cb(err);
|
208 | if (!body && !links) return cb(null);
|
209 | links = links || {};
|
210 |
|
211 |
|
212 | if (!body.href) body.href = href;
|
213 |
|
214 | if (parts.length === 1) return self.traverse(body, links, i, path, body, normalize, cb);
|
215 | return self.fetchJsonPath(body, links, parts[1], i, path, normalize, cb);
|
216 | });
|
217 |
|
218 |
|
219 | if (listener) listener();
|
220 |
|
221 | return res;
|
222 | };
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | Request.prototype.fetchJsonPath = function(parentDocument, links, href, i, path, normalize, cb) {
|
237 | var self = this;
|
238 | var pointer = href.split('/');
|
239 |
|
240 | if (pointer[0] === '') pointer.shift();
|
241 |
|
242 | return self.traverse(parentDocument, links, 0, pointer, parentDocument, false, function(err, val) {
|
243 | if (err) return cb(err);
|
244 | if (typeof val === 'object' && !val.href) val.href = parentDocument.href + '#' + href;
|
245 | return self.traverse(val, links, i, path, parentDocument, normalize, cb);
|
246 | });
|
247 | };
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | function get(key, parent, fallback) {
|
256 | if (!parent) return undefined;
|
257 | if (parent.hasOwnProperty(key)) return parent[key];
|
258 | if (typeof parent.get === 'function') return parent.get(key);
|
259 | if (fallback && fallback.hasOwnProperty(key)) return {href: fallback[key]};
|
260 | return void 0;
|
261 | }
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | function normalizeTarget(target) {
|
270 | if (typeof target !== 'object') return target;
|
271 | var href = target.href;
|
272 | target = target.collection || target.data || target;
|
273 | target.href = href;
|
274 | return target;
|
275 | }
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | function isDefined(value) {
|
284 | return typeof value !== 'undefined' && value !== null;
|
285 | }
|