Ghost/core/server/services/url/Resource.js
Hannah Wolfe 5e5b90ac29
Added Url Service to track all URLs in the system (#9247)
refs #9192

- Introduces a url service that can be initialised
- Added a concept of Resources and resource config.json that contains details about the resources in the system that we may want to make customisable 
- Note that individual resources know how to create their own Urls... this is important for later
- Url Service loads all of the resources, and stores their URLs
- The UrlService binds to all events, so that when a resource changes its url and related data can be updated if needed
- There is a temporary config guard so that this can be turned off easily
2017-11-16 13:03:24 +00:00

53 lines
1.1 KiB
JavaScript

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