mirror of
https://github.com/strapi/strapi.git
synced 2025-08-31 20:33:03 +00:00
Fix wrong behavior of env helper with defautl values
Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
parent
da910b0b39
commit
dbda1cc254
@ -179,7 +179,7 @@ module.exports = ({ env }) => ({
|
|||||||
| `proxy.host` | Host name your proxy service uses for Strapi. | string | |
|
| `proxy.host` | Host name your proxy service uses for Strapi. | string | |
|
||||||
| `proxy.port` | Port that your proxy service accepts connections on. | integer | |
|
| `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` | 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` | Admin panel configuration | Object | |
|
||||||
| `admin.autoOpen` | Enable or disabled administration opening on start. | boolean | `true` |
|
| `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` |
|
| `admin.path` | Allow to change the URL to access the admin panel. | string | `/admin` |
|
||||||
|
@ -57,10 +57,14 @@ describe('Env helper', () => {
|
|||||||
expect(envHelper.bool('NOT_TRUE')).toEqual(false);
|
expect(envHelper.bool('NOT_TRUE')).toEqual(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Returns true when using "true" ', () => {
|
test('Returns true when using "true"', () => {
|
||||||
process.env.TRUE_VAR = 'true';
|
process.env.TRUE_VAR = 'true';
|
||||||
expect(envHelper.bool('TRUE_VAR')).toEqual(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', () => {
|
describe('env with json cast', () => {
|
||||||
|
@ -2,42 +2,58 @@
|
|||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
function env(key, defaultValue = undefined) {
|
function env(key, defaultValue) {
|
||||||
return _.has(process.env, key) ? process.env[key] : defaultValue;
|
return _.has(process.env, key) ? process.env[key] : defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const utils = {
|
const utils = {
|
||||||
int(...args) {
|
int(key, defaultValue) {
|
||||||
const value = env(...args);
|
if (!_.has(process.env, key)) {
|
||||||
return typeof value === 'undefined' ? value : parseInt(value, 10);
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const value = process.env[key];
|
||||||
|
return parseInt(value, 10);
|
||||||
},
|
},
|
||||||
|
|
||||||
bool(...args) {
|
float(key, defaultValue) {
|
||||||
const value = env(...args);
|
if (!_.has(process.env, key)) {
|
||||||
return typeof value === 'undefined' ? value : value === 'true';
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const value = process.env[key];
|
||||||
|
return parseFloat(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
float(...args) {
|
bool(key, defaultValue) {
|
||||||
const value = env(...args);
|
if (!_.has(process.env, key)) {
|
||||||
return typeof value === 'undefined' ? value : parseFloat(value);
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const value = process.env[key];
|
||||||
|
return value === 'true';
|
||||||
},
|
},
|
||||||
|
|
||||||
json(key, val) {
|
json(key, defaultValue) {
|
||||||
const value = env(key, val);
|
if (!_.has(process.env, key)) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const value = process.env[key];
|
||||||
try {
|
try {
|
||||||
return typeof value === 'undefined' ? value : JSON.parse(value);
|
return JSON.parse(value);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(`Invalid json environment variable ${key}: ${error.message}`);
|
throw new Error(`Invalid json environment variable ${key}: ${error.message}`);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
array(...args) {
|
array(key, defaultValue) {
|
||||||
let value = env(...args);
|
if (!_.has(process.env, key)) {
|
||||||
|
return defaultValue;
|
||||||
if (typeof value === 'undefined') {
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let value = process.env[key];
|
||||||
|
|
||||||
if (value.startsWith('[') && value.endsWith(']')) {
|
if (value.startsWith('[') && value.endsWith(']')) {
|
||||||
value = value.substring(1, value.length - 1);
|
value = value.substring(1, value.length - 1);
|
||||||
}
|
}
|
||||||
@ -47,9 +63,13 @@ const utils = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
date(...args) {
|
date(key, defaultValue) {
|
||||||
const value = env(...args);
|
if (!_.has(process.env, key)) {
|
||||||
return typeof value === 'undefined' ? value : new Date(value);
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const value = process.env[key];
|
||||||
|
return new Date(value);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user