2017-11-16 16:03:24 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash'),
|
2017-12-11 21:14:05 +03:00
|
|
|
localUtils = require('./utils'),
|
2017-11-16 16:03:24 +03:00
|
|
|
prefetchDefaults = {
|
|
|
|
context: {
|
|
|
|
internal: true
|
|
|
|
},
|
|
|
|
limit: 'all'
|
|
|
|
};
|
|
|
|
|
|
|
|
class Resource {
|
|
|
|
constructor(config) {
|
|
|
|
this.name = config.name;
|
|
|
|
this.api = config.api;
|
|
|
|
this.prefetchOptions = config.prefetchOptions || {};
|
|
|
|
this.urlLookup = config.urlLookup || config.name;
|
|
|
|
this.events = config.events;
|
|
|
|
this.items = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchAll() {
|
|
|
|
const options = _.defaults(this.prefetchOptions, prefetchDefaults);
|
|
|
|
|
2017-12-11 21:14:05 +03:00
|
|
|
return require('../../api')[this.api]
|
2017-11-16 16:03:24 +03:00
|
|
|
.browse(options)
|
|
|
|
.then((resp) => {
|
|
|
|
this.items = resp[this.api];
|
|
|
|
return this.items;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
toUrl(item) {
|
|
|
|
const data = {
|
|
|
|
[this.urlLookup]: item
|
|
|
|
};
|
2017-12-11 21:14:05 +03:00
|
|
|
return localUtils.urlFor(this.urlLookup, data);
|
2017-11-16 16:03:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
toData(item) {
|
|
|
|
return {
|
|
|
|
slug: item.slug,
|
|
|
|
resource: {
|
|
|
|
type: this.name,
|
|
|
|
id: item.id
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Resource;
|