From a49bbfb9d61a9cf22421b01d30515ec4ea50867f Mon Sep 17 00:00:00 2001 From: Rish Date: Tue, 2 Oct 2018 16:21:44 +0530 Subject: [PATCH] Updated method `getApiPath` to take options as param Refs #9936 - Updated method to take single options param with version and admin instead of separate values - Updated urlFor method to use the updated syntax - Updated parent-app to use updated syntax --- core/server/services/url/utils.js | 16 ++++++++++------ core/server/web/parent-app.js | 6 +++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/core/server/services/url/utils.js b/core/server/services/url/utils.js index ae4687b26e..238cc1cf5c 100644 --- a/core/server/services/url/utils.js +++ b/core/server/services/url/utils.js @@ -11,13 +11,16 @@ const moment = require('moment-timezone'), /** * Returns API path combining base path and path for specific version asked or deprecated by default - * @param {string} version version for which to get the path(stable, actice, deprecated: content, admin), defaults to deprecated:content + * @param {Object} options {version} for which to get the path(stable, actice, deprecated), + * {type} admin|content: defaults to {version: deprecated, type: content} * @return {string} API Path for version */ -function getApiPath(version = 'deprecated', admin = false) { +function getApiPath(options) { const apiVersions = config.get('api:versions'); - let versionType = apiVersions[version] || apiVersions.deprecated; - let versionPath = admin ? versionType.admin : versionType.content; + let version = options.version || 'deprecated'; + let type = options.type || 'content'; + let versionData = apiVersions[version]; + let versionPath = versionData[type]; return `${BASE_API_PATH}${versionPath}/`; } @@ -313,7 +316,7 @@ function urlFor(context, data, absolute) { } } else if (context === 'api') { urlPath = getAdminUrl() || getBlogUrl(); - let apiPath = getApiPath('deprecated'); + let apiPath = getApiPath({version: 'deprecated', type: 'content'}); // CASE: with or without protocol? If your blog url (or admin url) is configured to http, it's still possible that e.g. nginx allows both https+http. // So it depends how you serve your blog. The main focus here is to avoid cors problems. // @TODO: rename cors @@ -324,7 +327,8 @@ function urlFor(context, data, absolute) { } if (data && data.version) { - apiPath = getApiPath(data.version, data.admin); + let versionType = data.admin ? 'admin' : 'content'; + apiPath = getApiPath({version: data.version, type: versionType}); } if (absolute) { diff --git a/core/server/web/parent-app.js b/core/server/web/parent-app.js index 7159798b21..426538b21f 100644 --- a/core/server/web/parent-app.js +++ b/core/server/web/parent-app.js @@ -39,9 +39,9 @@ module.exports = function setupParentApp(options = {}) { // API // @TODO: finish refactoring the API app // @TODO: decide what to do with these paths - config defaults? config overrides? - parentApp.use(urlUtils.getApiPath('deprecated'), require('./api/v0.1/app')()); - parentApp.use(urlUtils.getApiPath('active'), require('./api/v2/content/app')()); - parentApp.use(urlUtils.getApiPath('active', true), require('./api/v2/admin/app')()); + parentApp.use(urlUtils.getApiPath({version: 'deprecated', type: 'content'}), require('./api/v0.1/app')()); + parentApp.use(urlUtils.getApiPath({version: 'active', type: 'content'}), require('./api/v2/content/app')()); + parentApp.use(urlUtils.getApiPath({version: 'active', type: 'admin'}), require('./api/v2/admin/app')()); // ADMIN parentApp.use('/ghost', require('./admin')());