Ghost/core/frontend/services/routing/registry.js
Naz Gargol df7e64fafa
Extracted frontend folder (#10780)
refs #10790

- Moved /core/apps into core/frontend
- Moved /core/server/helpers to /core/frontend/helpers along with /core/server/services/themes
- Changed helper location in overrides
- Moved /core/server/services/routing to /core/frontend/services
- Moved /core/server/services/url to /core/frontend/services
- Moved /core/server/data/meta to /core/frontend/meta
- Moved /core/server/services/rss to /core/frontend/services
- Moved /core/server/data/xml to /core/frontend/services
2019-06-19 11:30:28 +02:00

109 lines
2.6 KiB
JavaScript

const _ = require('lodash');
let routes = [];
let routers = {};
/**
* @description The router registry is helpful for debugging purposes and let's you search existing routes and routers.
*/
module.exports = {
/**
* @description Get's called if you register a url pattern in express.
* @param {String} routerName
* @param {String} route
*/
setRoute(routerName, route) {
routes.push({route: route, from: routerName});
},
/**
* @description Get's called if you register a router in express.
* @param {String} name
* @param {Express-Router} router
*/
setRouter(name, router) {
routers[name] = router;
},
/**
* @description Get all registered routes.
* @returns {Array}
*/
getAllRoutes() {
return _.cloneDeep(routes);
},
/**
* @description Get router by name.
* @param {String} name
* @returns {Express-Router}
*/
getRouter(name) {
return routers[name];
},
/**
*
*
* Hierarchy for primary rss url:
*
* - index collection (/)
* - if you only have one collection, we take this rss url
*/
/**
* @description Helper to figure out the primary rss url.
*
* If you either configure multiple collections or your collection does not live on "/", we need to know
* what your primary rss url is, otherwise /rss could be 404.
*
* More context: https://github.com/TryGhost/Team/issues/65#issuecomment-393622816
*
* @param {Object} options
* @returns {String}
*/
getRssUrl(options) {
let rssUrl = null;
const collectionIndexRouter = _.find(routers, {name: 'CollectionRouter', routerName: 'index'});
if (collectionIndexRouter) {
rssUrl = collectionIndexRouter.getRssUrl(options);
// CASE: is rss enabled?
if (rssUrl) {
return rssUrl;
}
}
const collectionRouters = _.filter(routers, {name: 'CollectionRouter'});
if (collectionRouters && collectionRouters.length === 1) {
rssUrl = collectionRouters[0].getRssUrl(options);
// CASE: is rss enabled?
if (rssUrl) {
return rssUrl;
}
}
return rssUrl;
},
/**
* @description Reset all routes.
*/
resetAllRoutes() {
routes = [];
},
/**
* @description Reset all routers.
*/
resetAllRouters() {
_.each(routers, (value) => {
value.reset();
});
routers = {};
}
};