Updated local config helpers bind pattern

- The new @tryghost/config-url-helpers has a pattern of exposing bindAll()
- Changed the local (non url) helpers to have the same pattern for consistency
- Also fixed types as best I can
This commit is contained in:
Hannah Wolfe 2021-06-18 20:34:14 +01:00
parent 6aafb735b1
commit 8cf411e524
No known key found for this signature in database
GPG Key ID: 9F8C7532D0A6BA55
2 changed files with 18 additions and 7 deletions

View File

@ -1,5 +1,10 @@
const path = require('path');
/**
* @callback isPrivacyDisabledFn
* @param {string} privacyFlag - the flag to be looked up
* @returns {boolean}
*/
const isPrivacyDisabled = function isPrivacyDisabled(privacyFlag) {
if (!this.get('privacy')) {
return false;
@ -19,7 +24,9 @@ const isPrivacyDisabled = function isPrivacyDisabled(privacyFlag) {
};
/**
* we can later support setting folder names via custom config values
* @callback getContentPathFn
* @param {string} type - the type of context you want the path for
* @returns {string}
*/
const getContentPath = function getContentPath(type) {
switch (type) {
@ -43,7 +50,12 @@ const getContentPath = function getContentPath(type) {
}
};
module.exports = {
isPrivacyDisabled,
getContentPath
/**
* @typedef ConfigHelpers
* @property {isPrivacyDisabledFn} isPrivacyDisabled
* @property {getContentPathFn} getContentPath
*/
module.exports.bindAll = (nconf) => {
nconf.isPrivacyDisabled = isPrivacyDisabled.bind(nconf);
nconf.getContentPath = getContentPath.bind(nconf);
};

View File

@ -11,7 +11,7 @@ const env = process.env.NODE_ENV || 'development';
/**
* @param {object} options
* @returns {Nconf.Provider & urlHelpers.BoundHelpers}
* @returns {Nconf.Provider & urlHelpers.BoundHelpers & helpers.ConfigHelpers}
*/
function loadNconf(options) {
debug('config start');
@ -50,8 +50,7 @@ function loadNconf(options) {
nconf.checkUrlProtocol = localUtils.checkUrlProtocol.bind(nconf);
// Expose dynamic utility methods
nconf.isPrivacyDisabled = helpers.isPrivacyDisabled.bind(nconf);
nconf.getContentPath = helpers.getContentPath.bind(nconf);
helpers.bindAll(nconf);
urlHelpers.bindAll(nconf);
// ## Sanitization