-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resource.ts
484 lines (441 loc) · 15.6 KB
/
Resource.ts
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import * as HalAdapter from 'traverson-hal';
import * as halfred from 'halfred';
import * as traverson from 'traverson';
import * as validator from 'json-schema-remote';
import * as isEqual from 'lodash.isequal';
import * as assert from 'assert';
import ListResource, { filterOptions } from './ListResource';
import { del, get, optionsToQuery, post, put } from '../helper';
const environmentSymbol = Symbol.for('environment');
const resourceSymbol = Symbol.for('resource');
const traversalSymbol = Symbol.for('traversal');
const resourcePropertiesSymbol = Symbol('resourceProperties');
const relationsSymbol = Symbol.for('relations');
traverson.registerMediaType(HalAdapter.mediaType, HalAdapter);
validator.setLoggingFunction(() => {
});
interface Resource {
[key: string]: any;
}
/**
* Generic resource class. Represents {@link https://tools.ietf.org/html/draft-kelly-json-hal-08
* HAL resources}.
*
* @class
* @access protected
*
* @prop {boolean} isDirty - Whether or not this Resource was modified
*/
class Resource {
/**
* Creates a new {@link Resource}.
*
* @access protected
*
* @param {object} resource resource loaded from the API.
* @param {environment} environment the environment this resource is associated to.
* @param {?object} traversal traversal from which traverson can continue.
*/
constructor(resource, environment = 'live', traversal, name?: string, schema?: any) {
this[environmentSymbol] = environment;
let r;
if (resource instanceof halfred['Resource']) {
r = resource.original();
} else {
r = resource;
}
this[resourceSymbol] = halfred.parse(JSON.parse(JSON.stringify(r)));
if (typeof this[environmentSymbol] !== 'string') {
throw new Error('environment must be a string');
}
if (traversal) {
this[traversalSymbol] = traversal;
} else {
this[traversalSymbol] = traverson.from(this.getLink('self').href).jsonHal();
}
Object.defineProperties(this, {
isDirty: {
enumerable: false,
get: () => {
try {
const original = this[resourceSymbol].original();
const current = this.toOriginal();
delete original._links;
delete current._links;
delete original._embedded;
delete current._embedded;
assert.deepEqual(current, original);
return false;
} catch (err) {
if (err.name && err.name.indexOf('AssertionError') !== -1) {
return true;
}
throw err;
}
},
},
});
this[relationsSymbol] = { dummy: {} };
this.countProperties();
}
/**
* Get all {@link https://tools.ietf.org/html/draft-kelly-json-hal-08#section-5 links} of this
* resource.
*
* @returns {object} object which has an array for each link.
*/
allLinks(): { [key: string]: Array<any> } {
return this[resourceSymbol].allLinks();
}
countProperties(): void {
this[resourcePropertiesSymbol] = Object.keys(this);
}
/**
* Create a new Resource. Note: Not all relations will support this.
*
* @example
* return accounts.create('client', {
* clientID: 'myClient',
* callbackURL: 'https://example.com/login',
* config: {
* tokenMethod: 'query',
* },
* })
* .then(client => show(client));
*
* @param {string} relation The shortened relation name
* @param {object} resource object representing the resource
* @returns {Promise<Resource>} the newly created Resource
*/
create(relation: string, resource: any): Promise<Resource> {
return Promise.resolve()
.then(() => {
if (!relation) {
throw new Error('relation must be defined');
}
if (!this[relationsSymbol][relation]) {
throw new Error(`unknown relation, use one of ${Object.keys(this[relationsSymbol]).join(', ')}`)
}
if (!this[relationsSymbol][relation].createRelation) {
throw new Error('Resource has no createRelation');
}
if (!resource) {
throw new Error('Cannot create resource with undefined object.');
}
return this.getLink(this[relationsSymbol][relation].createRelation);
})
.then(link => validator.validate(resource, `${link.profile}${this[relationsSymbol][relation].createTemplateModifier}`))
.then(() => this.newRequest().follow(this[relationsSymbol][relation].relation))
.then(request => {
if (this[relationsSymbol][relation].additionalTemplateParam) {
request.withTemplateParameters(optionsToQuery({
[this[relationsSymbol][relation].additionalTemplateParam]: this[this[relationsSymbol][relation].additionalTemplateParam],
}));
}
return post(this[environmentSymbol], request, resource)
})
.then(([c, traversal]) =>
new this[relationsSymbol][relation].ResourceClass(c, this[environmentSymbol], traversal));
}
/**
* Deletes this {@link Resource}.
*
* @returns {Promise<undefined>} Promise will resolve on success and reject otherwise.
*/
del(): Promise<void> {
return del(this[environmentSymbol], this.newRequest().follow('self'));
}
/**
* Loads the given {@link https://tools.ietf.org/html/draft-kelly-json-hal-08#section-5 link} and
* returns a {@link Resource} with the loaded result.
*
* @param {string} link the link name.
* @param {ResourceClass} ResourceClass override the default resource class ({@link Resource}).
* @param {string?} name the name of the embedded resources
* @param {object?} schema schema for {@link EntryResource}s
* @returns {Promise<Resource|ResourceClass>} the resource identified by the link.
*/
followLink(link: string, ResourceClass = Resource, name: string, schema: any): Promise<Resource> {
return get(this[environmentSymbol], this.newRequest().follow(link))
.then(([res, traversal]) => {
return new ResourceClass(res, this[environmentSymbol], traversal, name, schema);
});
}
/**
* Returns an object with selected properties of the {@link Resource}. Will return all properties
* when properties array is empty or undefined.
*
* @param {array<string>} properties array of properties to select.
* @returns {object} object containing selected properties.
*/
getAll(properties: Array<string>): any {
if (!properties || !Array.isArray(properties) || properties.length === 0) {
return Object.assign({}, this[resourceSymbol]);
}
const out = {};
properties.forEach((property) => {
out[property] = this.getProperty(property);
});
return out;
}
/**
* Get the first {@link https://tools.ietf.org/html/draft-kelly-json-hal-08#section-5 link} with
* the given name.
*
* @param {string} link the link name.
* @returns {object|null} the link with the given name or null.
*/
getLink(link: string): any {
return this[resourceSymbol].link(link);
}
/**
* Get all {@link https://tools.ietf.org/html/draft-kelly-json-hal-08#section-5 links} with
* the given name.
*
* @param {string} link the link name.
* @returns {Array<object>|null} the link with the given name or null.
*/
getLinks(link: string): Array<any> {
return this[resourceSymbol].linkArray(link);
}
/**
* Will return a single selected property identified by property.
*
* @param {string} property the selected property name.
* @returns {*} the property which was selected.
*/
getProperty(property: string): any {
if (!property) {
throw new Error('Property name cannot be undefined');
}
return this[resourceSymbol][property];
}
/**
* Checks if this {@link Resource} has at least one {@link
* https://tools.ietf.org/html/draft-kelly-json-hal-08#section-5
* link} with the given name.
*
* @param {string} link the link name.
* @returns {boolean} whether or not a link with the given name was found.
*/
hasLink(link) {
return this[resourceSymbol].link(link) !== null;
}
/**
* Creates a new {@link
* https://github.com/basti1302/traverson/blob/master/api.markdown#request-builder
* traverson request builder}
* which can be used for a new request to the API.
*
* @access private
*
* @returns {Object} traverson request builder instance.
*/
newRequest(): any {
if (typeof this[traversalSymbol].continue === 'function') {
return this[traversalSymbol].continue().newRequest();
}
return this[traversalSymbol].newRequest();
}
/**
* @private
*
* @typedef {function} ResourceClass
* @constructor
*/
/**
* Reset this {@link Resource} to its initial state. {@link Resource#isDirty} will be false
* afterwards.
*
* @returns {undefined}
*/
reset(): void {
this[resourceSymbol] = halfred.parse(this[resourceSymbol].original());
}
/**
* Reloads this {@link Resource}. Can be used when this resource was loaded from any {@link
* ListResource} from _embedded.
*
* @returns {Promise<Resource>} this resource
*/
resolve(): Promise<Resource> {
return get(
this[environmentSymbol],
this.newRequest().follow('self')
)
.then(([res, traversal]) => {
this[resourceSymbol] = halfred.parse(res);
this[traversalSymbol] = traversal;
return this;
});
}
/**
* Get a single {@link Resource} identified by resourceID.
*
* @example
* return accounts.resource('account', me.accountID)
* .then(account => show(account.email));
*
* @param {string} relation The shortened relation name
* @param {string} resourceID id of the Resource
* @returns {Promise<Resource>} resolves to the Resource which should be loaded
*/
resource(relation: string, resourceID, additionalTemplateParams: any = {}): Promise<Resource> {
return Promise.resolve()
.then(() => {
if (!relation) {
throw new Error('relation must be defined');
}
if (!this[relationsSymbol][relation]) {
throw new Error(`unknown relation, use one of ${Object.keys(this[relationsSymbol]).join(', ')}`)
}
if (!resourceID) {
throw new Error('resourceID must be defined');
}
return this.newRequest().follow(this[relationsSymbol][relation].relation)
})
.then((request) => {
const params = Object.assign({}, additionalTemplateParams, { [this[relationsSymbol][relation].id]: resourceID });
request.withTemplateParameters(params);
return get(this[environmentSymbol], request);
})
.then(([res, traversal]) =>
new this[relationsSymbol][relation].ResourceClass(res, this[environmentSymbol], traversal));
}
/**
* Load a {@link ListResource} of {@link Resource}s filtered by the values specified by the
* options parameter.
*
* @example
* return accounts.resourceList('account', {
* filter: {
* created: {
* from: new Date(new Date.getTime() - 600000).toISOString()),
* },
* },
* })
* .then(list => show(list))
*
* @param {string} relation The shortened relation name
* @param {filterOptions?} options the filter options
* @returns {Promise<ListResource>} resolves to resource list with applied filters
*/
resourceList(relation: string, options?: filterOptions | any, additionalTemplateParams: any = {}): Promise<ListResource> {
return Promise.resolve()
.then(() => {
if (!relation) {
throw new Error('relation must be defined');
}
if (!this[relationsSymbol][relation]) {
throw new Error(`unknown relation, use one of ${Object.keys(this[relationsSymbol]).join(', ')}`)
}
const id = this[relationsSymbol][relation].id;
if (
options && Object.keys(options).length === 1 && id in options
&& (typeof options[id] === 'string' || (!('any' in options[id] && !('all' in options[id]))))
) {
throw new Error('Providing only an id in ResourceList filter will result in single resource response.');
}
if (options && '_levels' in options) {
throw new Error('_levels on list resources not supported');
}
return this.newRequest().follow(this[relationsSymbol][relation].relation);
})
.then((request) => {
if (this[relationsSymbol][relation].additionalTemplateParam && !(this[relationsSymbol][relation].additionalTemplateParam in additionalTemplateParams)) {
additionalTemplateParams[this[relationsSymbol][relation].additionalTemplateParam] = this[this[relationsSymbol][relation].additionalTemplateParam]
}
const params = Object.assign({}, additionalTemplateParams, options);
request.withTemplateParameters(
optionsToQuery(params, this.getLink(this[relationsSymbol][relation].relation).href));
return get(this[environmentSymbol], request);
})
.then(([res, traversal]) =>
new this[relationsSymbol][relation].ListClass(res, this[environmentSymbol], traversal));
}
/**
* Saves this {@link Resource}.
*
* @param {boolean} safePut true when safe put functionality is required.
* @param {string?} overwriteSchemaUrl Other schema url to overwrite the one in
* `_link.self.profile`. Mainly for internal use.
* @returns {Promise<Resource>} Promise will resolve to the saved Resource. Will
* be the same object but with refreshed data.
*/
save(safePut: boolean = false, overwriteSchemaUrl?: string): Promise<Resource> {
return Promise.resolve()
.then(() => {
const out = this.toOriginal();
return validator.validate(out, overwriteSchemaUrl || this.getLink('self').profile)
.then(() => {
const request = this.newRequest().follow('self');
if (safePut) {
if (!('_modified' in out)) {
throw new Error('safe put without _modified date');
}
const date = new Date(out._modified);
request.addRequestOptions({
headers: {
'If-Modified-Since': date.toUTCString(),
}
});
}
return put(this[environmentSymbol], request, out)
})
.then(([res, traversal]) => {
this[resourceSymbol] = halfred.parse(res);
this[traversalSymbol] = traversal;
return this;
});
});
}
/**
* Will assign all properties in resource to this {@link Resource}.
*
* @param {object} resource object with properties to assign.
* @returns {Resource} this Resource for chainability
*/
setAll(resource: any): any {
if (!resource) {
throw new Error('Resource cannot be undefined.');
}
Object.assign(this[resourceSymbol], resource);
return this;
}
/**
* Set a new value to the property identified by property.
*
* @param {string} property the property to change.
* @param {any} value the value to assign.
* @returns {Resource} this Resource for chainability
*/
setProperty(property: string, value: any): any {
if (isEqual(value, this.getProperty(property))) {
return this;
}
this[resourceSymbol][property] = value;
return this;
}
toOriginal(): any {
const out = {};
const keys = Object.keys(this);
if (this[resourcePropertiesSymbol].length !== keys.length) {
throw new Error(`Additional properties found: ${keys.filter(k => !this[resourcePropertiesSymbol].includes(k)).join(', ')}`);
}
Object.keys(this[resourceSymbol].original()).forEach((key) => {
out[key] = this[resourceSymbol][key];
});
return out;
}
/**
* Validates this {@link Resource} against its schema (found in _links.self.profile)
*
* @returns {Promise<boolean>} Promise will resolve true when Resource is valid, rejects
* otherwise.
*/
validate(): Promise<boolean> {
return validator.validate(this.toOriginal(), this.getLink('self').profile)
.then(() => true);
}
}
export default Resource;