This repository has been archived by the owner on Mar 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.js
184 lines (152 loc) · 4.84 KB
/
resource.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
module.exports = Resource;
var util = require('util'),
pointer = require('./pointer');
var Flow = require('./flow'),
Q = require('./qlient');
function NOOP () {}
function isObject (obj) {
return (typeof obj === 'object' && obj !== null);
}
function travasal (obj, tokens) {
if (tokens.length === 0) {
return obj;
}
var token = tokens[0];
if (obj instanceof Resource) {
return travasal(obj.value[token], tokens.slice(1));
} else if (isObject(obj)) {
return travasal(obj[token], tokens.slice(1));
} else {
throw new TypeError('premitive value `' + obj + '`(' + typeof obj + ') has no property: ' + tokens.toString());
}
}
function Resource (type, obj, children) {
Flow.call(this);
this.type = type;
this.types = children.map(function (typeDefinition) {
var Type = typeDefinition.new(this);
this[typeDefinition.name.toUpperCamelCase()] = Type;
return { key: typeDefinition.name, value: Type };
}.bind(this)).toObject();
this.subscribe(function () {
this.type.resources[this.value[this.type.idField]] = this;
}.bind(this));
this.set(obj);
}
util.inherits(Resource, Flow);
Object.defineProperties(Resource.prototype, {
path: { get: function () {
if (this.id.length === 0) {
throw new Error('id is not set');
}
return this.id + '/';
} },
id: { get: function () {
return (isObject(this.value) && this.value.hasOwnProperty(this.type.idField)) ?
this.value[this.type.idField] : '';
} }
});
Resource.prototype.create = function (query) {
return this.type.request('POST', '', query, {}, this).then(this.set.bind(this));
};
Resource.prototype.$create = function (query, cb) {
this.$create(query).then(cb || NOOP);
return this;
};
Resource.prototype.resolve = function (resolutions) {
resolutions = resolutions || [''];
function resolve (res) {
if (res instanceof Resource) {
return res.fetch();
} else if (Array.isArray(res)) {
return Q.Promise.all(res.map(function (res) { return resolve(res, []); }));
}
return res;
}
return resolutions.map(pointer.decode).map(function (tokens) {
return function () {
return resolve(travasal({'': this}, tokens));
}.bind(this);
}.bind(this)).reduce(function (p, n) {
return p.then(n);
}, Q.Promise.resolve()).then(function () {
return this;
}.bind(this));
};
Resource.prototype.$resolve = function (resolutions, cb) {
this.resolve(resolutions).then(cb || NOOP);
return this;
};
Resource.prototype.fetch = function (query) {
return this.request('GET', null, query).then(this.set.bind(this));
};
Resource.prototype.$fetch = function (query, cb) {
this.fetch(query).then(cb || NOOP);
return this;
};
Resource.prototype.update = function (query) {
return this.request('PUT', null, query, null, this).then(this.set.bind(this));
};
Resource.prototype.$update = function (query, cb) {
this.update(query).then(cb || NOOP);
return this;
};
Resource.prototype.remove = function (query) {
return this.request('DELETE', null, query).then(this.set.bind(this));
};
Resource.prototype.$remove = function (query, cb) {
this.remove(query).then(cb || NOOP);
return this;
};
Resource.prototype.request = function (method, pathname, query, headers, body) {
pathname = pathname || '';
return this.type.request(method, this.path + pathname, query, headers, body);
};
Resource.prototype.set = function (jsonObj) {
var that = this;
this.value = (function resolve (jsonObj) {
if (Array.isArray(jsonObj)) {
return jsonObj.map(resolve);
} else if (isObject(jsonObj)) {
if (jsonObj.hasOwnProperty('$ref')) {
return that.q.resolveReference(jsonObj.$ref);
} else {
return Object.keys(jsonObj).map(function (key) {
return { key: key, value: resolve(jsonObj[key]) };
}).toObject();
}
} else {
return jsonObj;
}
})(jsonObj);
return this;
};
Resource.prototype.toObject = function (resolutions) {
resolutions = resolutions || [''];
function copy (res) {
if (res instanceof Resource) {
return Object.keys(res.value).map(function (key) {
return { key: key, value: res.value[key] };
}).filter(function (kv) {
return !(kv.value instanceof Resource);
}).map(function (kv) {
return { key: kv.key, value: copy(kv.value) };
}).toObject();
} else if (Array.isArray(res)) {
return res.map(copy);
} else {
return res;
}
}
return this.resolve(resolutions).then(function () {
return resolutions.map(pointer.decode).reduce(function (o, tokens) {
return pointer.path(o, tokens).set(copy(travasal({'': this}, tokens)));
}.bind(this), {});
}.bind(this)).catch(function (err) {
console.warn(err.stack);
});
};
Resource.prototype.$toObject = function (resolutions, cb) {
this.toObject(resolutions).then(cb || NOOP);
return this;
};