Fix wrong behavior of env helper with defautl values

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-04-27 17:52:16 +02:00
parent da910b0b39
commit dbda1cc254
3 changed files with 47 additions and 23 deletions

View File

@ -179,7 +179,7 @@ module.exports = ({ env }) => ({
| `proxy.host` | Host name your proxy service uses for Strapi. | string | |
| `proxy.port` | Port that your proxy service accepts connections on. | integer | |
| `cron` | Cron configuration (powered by [`node-schedule`](https://github.com/node-schedule/node-schedule)) | Object | |
| `cron.enabled` | Enable or disable CRON tasks to schedule jobs at specific dates. | boolean | false |
| `cron.enabled` | Enable or disable CRON tasks to schedule jobs at specific dates. | boolean | `false` |
| `admin` | Admin panel configuration | Object | |
| `admin.autoOpen` | Enable or disabled administration opening on start. | boolean | `true` |
| `admin.path` | Allow to change the URL to access the admin panel. | string | `/admin` |

View File

@ -57,10 +57,14 @@ describe('Env helper', () => {
expect(envHelper.bool('NOT_TRUE')).toEqual(false);
});
test('Returns true when using "true" ', () => {
test('Returns true when using "true"', () => {
process.env.TRUE_VAR = 'true';
expect(envHelper.bool('TRUE_VAR')).toEqual(true);
});
test('Returns true when using boolean true default Value', () => {
expect(envHelper.bool('TRUE_VAR', true)).toEqual(true);
});
});
describe('env with json cast', () => {

View File

@ -2,42 +2,58 @@
const _ = require('lodash');
function env(key, defaultValue = undefined) {
function env(key, defaultValue) {
return _.has(process.env, key) ? process.env[key] : defaultValue;
}
const utils = {
int(...args) {
const value = env(...args);
return typeof value === 'undefined' ? value : parseInt(value, 10);
int(key, defaultValue) {
if (!_.has(process.env, key)) {
return defaultValue;
}
const value = process.env[key];
return parseInt(value, 10);
},
bool(...args) {
const value = env(...args);
return typeof value === 'undefined' ? value : value === 'true';
float(key, defaultValue) {
if (!_.has(process.env, key)) {
return defaultValue;
}
const value = process.env[key];
return parseFloat(value);
},
float(...args) {
const value = env(...args);
return typeof value === 'undefined' ? value : parseFloat(value);
bool(key, defaultValue) {
if (!_.has(process.env, key)) {
return defaultValue;
}
const value = process.env[key];
return value === 'true';
},
json(key, val) {
const value = env(key, val);
json(key, defaultValue) {
if (!_.has(process.env, key)) {
return defaultValue;
}
const value = process.env[key];
try {
return typeof value === 'undefined' ? value : JSON.parse(value);
return JSON.parse(value);
} catch (error) {
throw new Error(`Invalid json environment variable ${key}: ${error.message}`);
}
},
array(...args) {
let value = env(...args);
if (typeof value === 'undefined') {
return value;
array(key, defaultValue) {
if (!_.has(process.env, key)) {
return defaultValue;
}
let value = process.env[key];
if (value.startsWith('[') && value.endsWith(']')) {
value = value.substring(1, value.length - 1);
}
@ -47,9 +63,13 @@ const utils = {
});
},
date(...args) {
const value = env(...args);
return typeof value === 'undefined' ? value : new Date(value);
date(key, defaultValue) {
if (!_.has(process.env, key)) {
return defaultValue;
}
const value = process.env[key];
return new Date(value);
},
};