@adonisjs/env 3.0.9 → 4.0.0-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,172 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/env
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- var __importDefault = (this && this.__importDefault) || function (mod) {
11
- return (mod && mod.__esModule) ? mod : { "default": mod };
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.EnvParser = void 0;
15
- const dotenv_1 = __importDefault(require("dotenv"));
16
- /**
17
- * Env parser parses the environment variables from a string formatted
18
- * as a key-value pair seperated using an `=`. For example:
19
- *
20
- * ```
21
- * PORT=3333
22
- * HOST=127.0.0.1
23
- * ```
24
- *
25
- * The variables can reference other environment variables as well using `$`.
26
- * For example:
27
- *
28
- * ```
29
- * PORT=3333
30
- * REDIS_PORT=$PORT
31
- * ```
32
- *
33
- * The variables using characters other than letters can use wrap variables inside
34
- * a curly brace.
35
- *
36
- * ```
37
- * APP-PORT=3333
38
- * REDIS_PORT=${APP-PORT}
39
- * ```
40
- */
41
- class EnvParser {
42
- constructor(preferExistingEnvVariables = true) {
43
- this.preferExistingEnvVariables = preferExistingEnvVariables;
44
- }
45
- /**
46
- * Returns value for a given key from the environment variables. Also
47
- * the current parsed object is used to pull the reference.
48
- */
49
- getValue(key, parsed) {
50
- /**
51
- * When existing env variables are preferred, then we lookup the
52
- * value inside `process.env` first.
53
- */
54
- if (this.preferExistingEnvVariables) {
55
- if (process.env[key]) {
56
- return process.env[key];
57
- }
58
- if (parsed[key]) {
59
- return this.interpolate(parsed[key], parsed);
60
- }
61
- return '';
62
- }
63
- /**
64
- * Otherwise we lookup the value inside the parsed object
65
- * first
66
- */
67
- if (parsed[key]) {
68
- return this.interpolate(parsed[key], parsed);
69
- }
70
- if (process.env[key]) {
71
- return process.env[key];
72
- }
73
- return '';
74
- }
75
- /**
76
- * Interpolating the token wrapped inside the mustache
77
- * braces.
78
- */
79
- interpolateMustache(token, parsed) {
80
- /**
81
- * Finding the closing brace. If closing brace is missing, we
82
- * consider the block as a normal string
83
- */
84
- const closingBrace = token.indexOf('}');
85
- if (closingBrace === -1) {
86
- return token;
87
- }
88
- /**
89
- * Then we pull everything until the closing brace, except
90
- * the opening brace and trim off all white spaces.
91
- */
92
- const varReference = token.slice(1, closingBrace).trim();
93
- /**
94
- * Getting the value of the reference inside the braces
95
- */
96
- return `${this.getValue(varReference, parsed)}${token.slice(closingBrace + 1)}`;
97
- }
98
- /**
99
- * Interpolating the variable reference starting with a
100
- * `$`. We only capture numbers,letter and underscore.
101
- * For other characters, one can use the mustache
102
- * braces.
103
- */
104
- interpolateVariable(token, parsed) {
105
- return token.replace(/[a-zA-Z0-9_]+/, (key) => {
106
- return this.getValue(key, parsed);
107
- });
108
- }
109
- /**
110
- * Interpolates the referenced values
111
- */
112
- interpolate(value, parsed) {
113
- const tokens = value.split('$');
114
- let newValue = '';
115
- let skipNextToken = true;
116
- tokens.forEach((token) => {
117
- /**
118
- * If the value is an escaped sequence, then we replace it
119
- * with a `$` and then skip the next token.
120
- */
121
- if (token === '\\') {
122
- newValue += '$';
123
- skipNextToken = true;
124
- return;
125
- }
126
- /**
127
- * Use the value as it is when "skipNextToken" is set to true.
128
- */
129
- if (skipNextToken) {
130
- /**
131
- * Replace the ending escape sequence with a $
132
- */
133
- newValue += token.replace(/\\$/, '$');
134
- /**
135
- * and then skip the next token if it ends with escape sequence
136
- */
137
- if (token.endsWith('\\')) {
138
- return;
139
- }
140
- }
141
- else {
142
- /**
143
- * Handle mustache block
144
- */
145
- if (token.startsWith('{')) {
146
- newValue += this.interpolateMustache(token, parsed);
147
- return;
148
- }
149
- /**
150
- * Process all words as variable
151
- */
152
- newValue += this.interpolateVariable(token, parsed);
153
- }
154
- /**
155
- * Process next token
156
- */
157
- skipNextToken = false;
158
- });
159
- return newValue;
160
- }
161
- /**
162
- * Parse the env string to an object of environment variables.
163
- */
164
- parse(envString) {
165
- const envCollection = dotenv_1.default.parse(envString.trim());
166
- return Object.keys(envCollection).reduce((result, key) => {
167
- result[key] = this.interpolate(envCollection[key], envCollection);
168
- return result;
169
- }, {});
170
- }
171
- }
172
- exports.EnvParser = EnvParser;
@@ -1,10 +0,0 @@
1
- import { SchemaFnOptions } from '@ioc:Adonis/Core/Env';
2
- /**
3
- * Enforces the value to be of type boolean. Also casts
4
- * string representation of a boolean to a boolean
5
- * type
6
- */
7
- export declare function boolean(options?: SchemaFnOptions): (key: string, value?: string | undefined) => boolean;
8
- export declare namespace boolean {
9
- var optional: (options?: SchemaFnOptions | undefined) => (key: string, value?: string | undefined) => boolean | undefined;
10
- }
@@ -1,49 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/env
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.boolean = void 0;
12
- const utils_1 = require("@poppinss/utils");
13
- const helpers_1 = require("./helpers");
14
- /**
15
- * Casts a string value to a boolean
16
- */
17
- function castToBoolean(key, value, message) {
18
- if (helpers_1.BOOLEAN_POSITIVES.includes(value)) {
19
- return true;
20
- }
21
- if (helpers_1.BOOLEAN_NEGATIVES.includes(value)) {
22
- return false;
23
- }
24
- throw new utils_1.Exception(message ||
25
- `Value for environment variable "${key}" must be a boolean, instead received "${value}"`, 500, 'E_INVALID_ENV_VALUE');
26
- }
27
- /**
28
- * Enforces the value to be of type boolean. Also casts
29
- * string representation of a boolean to a boolean
30
- * type
31
- */
32
- function boolean(options) {
33
- return function validate(key, value) {
34
- (0, helpers_1.ensureValue)(key, value, options?.message);
35
- return castToBoolean(key, value, options?.message);
36
- };
37
- }
38
- exports.boolean = boolean;
39
- /**
40
- * Same as boolean, but allows undefined values as well.
41
- */
42
- boolean.optional = function optionalBoolean(options) {
43
- return function validate(key, value) {
44
- if (!value) {
45
- return undefined;
46
- }
47
- return castToBoolean(key, value, options?.message);
48
- };
49
- };
@@ -1,12 +0,0 @@
1
- /**
2
- * Following values are considered as "true"
3
- */
4
- export declare const BOOLEAN_POSITIVES: (string | number | boolean)[];
5
- /**
6
- * Following values are considered as "false"
7
- */
8
- export declare const BOOLEAN_NEGATIVES: (string | number | boolean)[];
9
- /**
10
- * Ensures the value to exist
11
- */
12
- export declare function ensureValue(key: string, value?: string, message?: string): asserts value is string;
@@ -1,29 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/env
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.ensureValue = exports.BOOLEAN_NEGATIVES = exports.BOOLEAN_POSITIVES = void 0;
12
- const utils_1 = require("@poppinss/utils");
13
- /**
14
- * Following values are considered as "true"
15
- */
16
- exports.BOOLEAN_POSITIVES = ['1', 1, 'true', true];
17
- /**
18
- * Following values are considered as "false"
19
- */
20
- exports.BOOLEAN_NEGATIVES = ['0', 0, 'false', false];
21
- /**
22
- * Ensures the value to exist
23
- */
24
- function ensureValue(key, value, message) {
25
- if (!value) {
26
- throw new utils_1.Exception(message || `Missing environment variable "${key}"`, 500, 'E_MISSING_ENV_VALUE');
27
- }
28
- }
29
- exports.ensureValue = ensureValue;
@@ -1,2 +0,0 @@
1
- import { EnvSchema } from '@ioc:Adonis/Core/Env';
2
- export declare const schema: EnvSchema;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/env
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.schema = void 0;
12
- const number_1 = require("./number");
13
- const string_1 = require("./string");
14
- const boolean_1 = require("./boolean");
15
- const oneOf_1 = require("./oneOf");
16
- exports.schema = {
17
- number: number_1.number,
18
- string: string_1.string,
19
- boolean: boolean_1.boolean,
20
- enum: oneOf_1.oneOf,
21
- };
@@ -1,13 +0,0 @@
1
- import { SchemaFnOptions } from '@ioc:Adonis/Core/Env';
2
- /**
3
- * Casts the string to a number and ensures it is no NaN
4
- */
5
- export declare function castToNumber(key: string, value: string, message?: string): number;
6
- /**
7
- * Enforces the value to be of valid number type and the
8
- * value will also be casted to a number
9
- */
10
- export declare function number(options?: SchemaFnOptions): (key: string, value?: string | undefined) => number;
11
- export declare namespace number {
12
- var optional: (options?: SchemaFnOptions | undefined) => (key: string, value?: string | undefined) => number | undefined;
13
- }
@@ -1,48 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/env
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.number = exports.castToNumber = void 0;
12
- const utils_1 = require("@poppinss/utils");
13
- const helpers_1 = require("./helpers");
14
- /**
15
- * Casts the string to a number and ensures it is no NaN
16
- */
17
- function castToNumber(key, value, message) {
18
- const castedValue = Number(value);
19
- if (isNaN(castedValue)) {
20
- throw new utils_1.Exception(message ||
21
- `Value for environment variable "${key}" must be numeric, instead received "${value}"`, 500, 'E_INVALID_ENV_VALUE');
22
- }
23
- return castedValue;
24
- }
25
- exports.castToNumber = castToNumber;
26
- /**
27
- * Enforces the value to be of valid number type and the
28
- * value will also be casted to a number
29
- */
30
- function number(options) {
31
- return function validate(key, value) {
32
- (0, helpers_1.ensureValue)(key, value, options?.message);
33
- return castToNumber(key, value, options?.message);
34
- };
35
- }
36
- exports.number = number;
37
- /**
38
- * Similar to the number rule, but also allows optional
39
- * values
40
- */
41
- number.optional = function optionalNumber(options) {
42
- return function validate(key, value) {
43
- if (!value) {
44
- return undefined;
45
- }
46
- return castToNumber(key, value, options?.message);
47
- };
48
- };
@@ -1,8 +0,0 @@
1
- import { SchemaFnOptions } from '@ioc:Adonis/Core/Env';
2
- /**
3
- * Enforces value to be one of the defined choices
4
- */
5
- export declare function oneOf(choices: any[], options?: SchemaFnOptions): (key: string, value?: string | undefined) => any;
6
- export declare namespace oneOf {
7
- var optional: (choices: any[], options?: SchemaFnOptions | undefined) => (key: string, value?: string | undefined) => any;
8
- }
@@ -1,69 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/env
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.oneOf = void 0;
12
- const utils_1 = require("@poppinss/utils");
13
- const helpers_1 = require("./helpers");
14
- /**
15
- * Validates the number to be present in the user defined choices.
16
- *
17
- * The incoming value will be casted as follows:
18
- *
19
- * - "0", 0, "false", false will be casted to false
20
- * - "1", 1, "true", true will be casted to true
21
- * - string representation of a number will be casted to a number
22
- */
23
- function ensureOneOf(choices, key, value, message) {
24
- if (helpers_1.BOOLEAN_NEGATIVES.includes(value)) {
25
- value = false;
26
- }
27
- else if (helpers_1.BOOLEAN_POSITIVES.includes(value)) {
28
- value = true;
29
- }
30
- else {
31
- const toNumber = Number(value);
32
- if (!isNaN(toNumber)) {
33
- value = toNumber;
34
- }
35
- }
36
- /**
37
- * If choices includes the value, then return the casted
38
- * value
39
- */
40
- if (choices.includes(value)) {
41
- return value;
42
- }
43
- /**
44
- * Otherwise raise exception
45
- */
46
- throw new utils_1.Exception(message ||
47
- `Value for environment variable "${key}" must be one of "${choices.join(',')}", instead received "${value}"`, 500, 'E_INVALID_ENV_VALUE');
48
- }
49
- /**
50
- * Enforces value to be one of the defined choices
51
- */
52
- function oneOf(choices, options) {
53
- return function validate(key, value) {
54
- (0, helpers_1.ensureValue)(key, value, options?.message);
55
- return ensureOneOf(choices, key, value, options?.message);
56
- };
57
- }
58
- exports.oneOf = oneOf;
59
- /**
60
- * Similar to oneOf, but also allows optional properties
61
- */
62
- oneOf.optional = function optionalBoolean(choices, options) {
63
- return function validate(key, value) {
64
- if (!value) {
65
- return undefined;
66
- }
67
- return ensureOneOf(choices, key, value, options?.message);
68
- };
69
- };
@@ -1,8 +0,0 @@
1
- import { StringFnOptions } from '@ioc:Adonis/Core/Env';
2
- /**
3
- * Enforces the value to exist and be of type string
4
- */
5
- export declare function string(options?: StringFnOptions): (key: string, value?: string | undefined) => string;
6
- export declare namespace string {
7
- var optional: (options?: StringFnOptions | undefined) => (key: string, value?: string | undefined) => string | undefined;
8
- }
@@ -1,66 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/env
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.string = void 0;
12
- const utils_1 = require("@poppinss/utils");
13
- const helpers_1 = require("./helpers");
14
- /**
15
- * Formats against which a string can be optionally validated. We
16
- * lazy load the dependencies required for validating formats
17
- */
18
- const formats = {
19
- email: (key, value, options) => {
20
- if (!require('validator/lib/isEmail')(value)) {
21
- throw new utils_1.Exception(options.message ||
22
- `Value for environment variable "${key}" must be a valid email, instead received "${value}"`, 500, 'E_INVALID_ENV_VALUE');
23
- }
24
- },
25
- host: (key, value, options) => {
26
- if (!require('validator/lib/isFQDN')(value, { require_tld: false }) &&
27
- !require('validator/lib/isIP')(value)) {
28
- throw new utils_1.Exception(options.message ||
29
- `Value for environment variable "${key}" must be a valid (domain or ip), instead received "${value}"`, 500, 'E_INVALID_ENV_VALUE');
30
- }
31
- },
32
- url: (key, value, options) => {
33
- const { tld = true, protocol = true } = options;
34
- if (!require('validator/lib/isURL')(value, { require_tld: tld, require_protocol: protocol })) {
35
- throw new utils_1.Exception(options.message ||
36
- `Value for environment variable "${key}" must be a valid URL, instead received "${value}"`, 500, 'E_INVALID_ENV_VALUE');
37
- }
38
- },
39
- };
40
- /**
41
- * Enforces the value to exist and be of type string
42
- */
43
- function string(options) {
44
- return function validate(key, value) {
45
- (0, helpers_1.ensureValue)(key, value, options?.message);
46
- if (options?.format) {
47
- formats[options.format](key, value, options);
48
- }
49
- return value;
50
- };
51
- }
52
- exports.string = string;
53
- /**
54
- * Same as the string rule, but allows non-existing values too
55
- */
56
- string.optional = function optionalString(options) {
57
- return function validate(key, value) {
58
- if (!value) {
59
- return undefined;
60
- }
61
- if (options?.format) {
62
- formats[options.format](key, value, options);
63
- }
64
- return value;
65
- };
66
- };