|
| 1 | +import logger from '../logger'; |
| 2 | +import Deprecations from './Deprecations'; |
| 3 | + |
| 4 | +/** |
| 5 | + * The deprecator class. |
| 6 | + */ |
| 7 | +class Deprecator { |
| 8 | + /** |
| 9 | + * Scans the Parse Server for deprecated options. |
| 10 | + * This needs to be called before setting option defaults, otherwise it |
| 11 | + * becomes indistinguishable whether an option has been set manually or |
| 12 | + * by default. |
| 13 | + * @param {any} options The Parse Server options. |
| 14 | + */ |
| 15 | + static scanParseServerOptions(options) { |
| 16 | + // Scan for deprecations |
| 17 | + for (const deprecation of Deprecator._getDeprecations()) { |
| 18 | + // Get deprecation properties |
| 19 | + const optionKey = deprecation.optionKey; |
| 20 | + const changeNewDefault = deprecation.changeNewDefault; |
| 21 | + |
| 22 | + // If default will change, only throw a warning if option is not set |
| 23 | + if (changeNewDefault != null && options[optionKey] == null) { |
| 24 | + Deprecator._log({ optionKey, changeNewDefault }); |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Returns the deprecation definitions. |
| 31 | + * @returns {Array<Object>} The deprecations. |
| 32 | + */ |
| 33 | + static _getDeprecations() { |
| 34 | + return Deprecations; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Logs a deprecation warning for a Parse Server option. |
| 39 | + * @param {String} optionKey The option key incl. its path, e.g. `security.enableCheck`. |
| 40 | + * @param {String} envKey The environment key, e.g. `PARSE_SERVER_SECURITY`. |
| 41 | + * @param {String} changeNewKey Set the new key name if the current key will be replaced, |
| 42 | + * or set to an empty string if the current key will be removed without replacement. |
| 43 | + * @param {String} changeNewDefault Set the new default value if the key's default value |
| 44 | + * will change in a future version. |
| 45 | + * @param {String} [solution] The instruction to resolve this deprecation warning. This |
| 46 | + * message must not include the warning that the parameter is deprecated, that is |
| 47 | + * automatically added to the message. It should only contain the instruction on how |
| 48 | + * to resolve this warning. |
| 49 | + */ |
| 50 | + static _log({ optionKey, envKey, changeNewKey, changeNewDefault, solution }) { |
| 51 | + const type = optionKey ? 'option' : 'environment key'; |
| 52 | + const key = optionKey ? optionKey : envKey; |
| 53 | + const keyAction = |
| 54 | + changeNewKey == null |
| 55 | + ? undefined |
| 56 | + : changeNewKey.length > 0 |
| 57 | + ? `renamed to '${changeNewKey}'` |
| 58 | + : `removed`; |
| 59 | + |
| 60 | + // Compose message |
| 61 | + let output = `DeprecationWarning: The Parse Server ${type} '${key}' `; |
| 62 | + output += changeNewKey ? `is deprecated and will be ${keyAction} in a future version.` : ''; |
| 63 | + output += changeNewDefault |
| 64 | + ? `default will change to '${changeNewDefault}' in a future version.` |
| 65 | + : ''; |
| 66 | + output += solution ? ` ${solution}` : ''; |
| 67 | + logger.warn(output); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +module.exports = Deprecator; |
0 commit comments