Ghost/core/server/services/url/Resource.js
kirrg001 4265afe580 Moved utils/url.js to UrlService
refs #9178

- we have to take care that we don't end up in circular dependencies
  - e.g. API requires UrlService and UrlService needs to require the API (for requesting data)
- update the references
- we would like to get rid of the utils folder, this is/was the most complicated change
2017-12-11 20:05:33 +01:00

52 lines
1.1 KiB
JavaScript

'use strict';
const _ = require('lodash'),
localUtils = require('./utils'),
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);
return require('../../api')[this.api]
.browse(options)
.then((resp) => {
this.items = resp[this.api];
return this.items;
});
}
toUrl(item) {
const data = {
[this.urlLookup]: item
};
return localUtils.urlFor(this.urlLookup, data);
}
toData(item) {
return {
slug: item.slug,
resource: {
type: this.name,
id: item.id
}
};
}
}
module.exports = Resource;