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, 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, 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 | Request.prototype.traverse = function(parent, links, i, path, cb) {
|
132 | var request = this;
|
133 |
|
134 |
|
135 | if (i >= path.length) return cb(null, normalizeTarget(parent));
|
136 |
|
137 | var key = path[i];
|
138 | var value = get(key, parent, links);
|
139 |
|
140 | // We couldn't find the property
|
141 | if (!isDefined(value)) {
|
142 | var collection = parent.collection || parent.data;
|
143 | if (collection && collection.hasOwnProperty(key)) return request.traverse(collection, links, i, path, cb);
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | if (this.wrappedScope) value = parent[key];
|
149 | if (typeof value === 'function') value = void 0;
|
150 | return cb(null, value);
|
151 | }
|
152 |
|
153 | var next = i + 1;
|
154 | var nextProp = path[next];
|
155 |
|
156 |
|
157 | if (!value.href || value.hasOwnProperty(nextProp)) return request.traverse(value, links, next, path, cb);
|
158 |
|
159 |
|
160 | if (nextProp === 'href') return cb(null, value);
|
161 |
|
162 |
|
163 | var href = value.href;
|
164 |
|
165 | var listener = request._listeners[href];
|
166 | var res = request._listeners[href] = request.client.get(href, function(err, body, links) {
|
167 | if (err) return cb(err);
|
168 | if (!body && !links) return cb(null);
|
169 | links = links || {};
|
170 |
|
171 |
|
172 | if (!body.href) body.href = href;
|
173 |
|
174 | var pointer = href.split('#')[1];
|
175 | if (!pointer) return request.traverse(body, links, i + 1, path, cb);
|
176 |
|
177 | pointer = pointer.split('/');
|
178 | if (pointer[0] === '') pointer.shift();
|
179 |
|
180 | return request.traverse(body, links, 0, pointer, function(err, val) {
|
181 | if (err) return cb(err);
|
182 | return request.traverse(val, links, i + 1, path, cb);
|
183 | });
|
184 | });
|
185 |
|
186 |
|
187 | if (listener) listener();
|
188 |
|
189 | return res;
|
190 | }
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | function get(key, parent, fallback) {
|
199 | if (!parent) return undefined;
|
200 | if (parent.hasOwnProperty(key)) return parent[key];
|
201 | if (typeof parent.get === 'function') return parent.get(key);
|
202 | if (fallback.hasOwnProperty(key)) return {href: fallback[key]};
|
203 | return void 0;
|
204 | }
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | function normalizeTarget(target) {
|
213 | if (typeof target !== 'object') return target;
|
214 | var href = target.href;
|
215 | target = target.collection || target.data || target;
|
216 | target.href = href;
|
217 | return target;
|
218 | }
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | function isDefined(value) {
|
227 | return typeof value !== 'undefined' && value !== null;
|
228 | }
|