@adonisjs/env 3.0.9 → 4.0.0-0

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,127 +0,0 @@
1
- declare module '@ioc:Adonis/Core/Env' {
2
- /**
3
- * An interface meant to be extended in the user land that holds
4
- * types for environment variables extracted using env.validate
5
- * method.
6
- */
7
- export interface EnvTypes {
8
- }
9
- /**
10
- * The shape of the validate fn
11
- */
12
- export type ValidateFn<T extends unknown> = (key: string, value?: string) => T;
13
- /**
14
- * A standard set of options accepted by the schema validation
15
- * functions
16
- */
17
- export type SchemaFnOptions = {
18
- message?: string;
19
- };
20
- export type StringFnUrlOptions = SchemaFnOptions & {
21
- format: 'url';
22
- /**
23
- * Whether the URL must have a valid TLD in their domain.
24
- * Defaults to `true`.
25
- */
26
- tld?: boolean;
27
- /**
28
- * Whether the URL must start with a valid protocol.
29
- * Defaults to `true`.
30
- */
31
- protocol?: boolean;
32
- };
33
- /**
34
- * Options accepted by the string schema function
35
- */
36
- export type StringFnOptions = (SchemaFnOptions & {
37
- format?: 'host' | 'email';
38
- }) | StringFnUrlOptions;
39
- /**
40
- * Shape of the number validator
41
- */
42
- export interface NumberType {
43
- (options?: SchemaFnOptions): ValidateFn<number>;
44
- optional: (options?: SchemaFnOptions) => ValidateFn<number | undefined>;
45
- }
46
- /**
47
- * Shape of the string validator
48
- */
49
- export interface StringType {
50
- (options?: StringFnOptions): ValidateFn<string>;
51
- optional: (options?: StringFnOptions) => ValidateFn<string | undefined>;
52
- }
53
- /**
54
- * Shape of the boolean validator
55
- */
56
- export interface BooleanType {
57
- (options?: SchemaFnOptions): ValidateFn<boolean>;
58
- optional: (options?: SchemaFnOptions) => ValidateFn<boolean | undefined>;
59
- }
60
- /**
61
- * Shape of the enum validator
62
- */
63
- export interface EnumType {
64
- <K extends any>(choices: readonly K[], options?: SchemaFnOptions): ValidateFn<K>;
65
- optional: <K extends any>(choices: readonly K[], options?: SchemaFnOptions) => ValidateFn<K | undefined>;
66
- }
67
- /**
68
- * Available schema functions. The end user can also define their
69
- * own
70
- */
71
- export interface EnvSchema {
72
- /**
73
- * Number function to enforce value to be a strict number
74
- */
75
- number: NumberType;
76
- /**
77
- * String function to enforce value to be a string. Optionally
78
- * the format can also be defined
79
- */
80
- string: StringType;
81
- /**
82
- * Boolean function to enforce value to be a boolean value.
83
- */
84
- boolean: BooleanType;
85
- /**
86
- * Boolean function to enforce value to be one of the defined values.
87
- */
88
- enum: EnumType;
89
- }
90
- /**
91
- * Env contract
92
- */
93
- export interface EnvContract {
94
- /**
95
- * Get value for a given environment variable
96
- */
97
- get<K extends keyof EnvTypes>(key: K): EnvTypes[K];
98
- get<K extends keyof EnvTypes>(key: K, defaultValue: Exclude<EnvTypes[K], undefined>): Exclude<EnvTypes[K], undefined>;
99
- get(key: string, defaultValue?: any): any;
100
- /**
101
- * Update/set value for a given environment variable. Ideally one should
102
- * avoid updating values during runtime
103
- */
104
- set<K extends keyof EnvTypes>(key: K, value: EnvTypes[K]): void;
105
- set(key: string, value: any): void;
106
- /**
107
- * Validate environment variables
108
- */
109
- rules<T extends {
110
- [key: string]: ValidateFn<unknown>;
111
- }>(values: T): {
112
- [K in keyof T]: ReturnType<T[K]>;
113
- };
114
- /**
115
- * Processes environment variables and performs the registered
116
- * validations
117
- */
118
- process(): void;
119
- /**
120
- * Reference to the schema object for defining validation
121
- * rules
122
- */
123
- schema: EnvSchema;
124
- }
125
- const Env: EnvContract;
126
- export default Env;
127
- }
@@ -1,8 +0,0 @@
1
- /*
2
- * @adonisjs/env
3
- *
4
- * (c) Harminder Virk <virk@adonisjs.com>
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
@@ -1,59 +0,0 @@
1
- /// <reference path="../../adonis-typings/env.d.ts" />
2
- import { DotenvParseOutput } from 'dotenv';
3
- import { EnvContract, ValidateFn } from '@ioc:Adonis/Core/Env';
4
- /**
5
- * The ENV module enables the use of environment variables by parsing dotfiles syntax
6
- * and updates the `process.env` object in Node.js.
7
- *
8
- * AdonisJs automatically reads and passes the contents of `.env` file to this class.
9
- */
10
- export declare class Env implements EnvContract {
11
- private valuesToProcess;
12
- /**
13
- * A boolean to know if the values have been processed or
14
- * not
15
- */
16
- private hasProcessedValues;
17
- /**
18
- * A cache of env values
19
- */
20
- private envCache;
21
- /**
22
- * The schema to be used for validating and casting environment
23
- * variables
24
- */
25
- private validationSchema;
26
- constructor(valuesToProcess: {
27
- values: DotenvParseOutput;
28
- overwriteExisting: boolean;
29
- }[]);
30
- /**
31
- * Reference to the underlying schema
32
- */
33
- schema: import("@ioc:Adonis/Core/Env").EnvSchema;
34
- /**
35
- * Process parsed env variables. The values will be validated
36
- * against the validation schema
37
- */
38
- process(): void;
39
- /**
40
- * Register the validation schema
41
- */
42
- rules(schema: {
43
- [key: string]: ValidateFn<unknown>;
44
- }): any;
45
- /**
46
- * Returns the environment variable value. First the cached
47
- * values are preferred. When missing, the value from
48
- * "process.env" is used
49
- */
50
- get(key: string, defaultValue?: any): any;
51
- /**
52
- * Set key-value pair. The value will be validated using
53
- * the validation rule if exists.
54
- *
55
- * The original value is also updated on the `process.env`
56
- * object
57
- */
58
- set(key: string, value: any): void;
59
- }
@@ -1,140 +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.Env = void 0;
12
- const Schema_1 = require("../Schema");
13
- /**
14
- * The ENV module enables the use of environment variables by parsing dotfiles syntax
15
- * and updates the `process.env` object in Node.js.
16
- *
17
- * AdonisJs automatically reads and passes the contents of `.env` file to this class.
18
- */
19
- class Env {
20
- constructor(valuesToProcess) {
21
- this.valuesToProcess = valuesToProcess;
22
- /**
23
- * A boolean to know if the values have been processed or
24
- * not
25
- */
26
- this.hasProcessedValues = false;
27
- /**
28
- * A cache of env values
29
- */
30
- this.envCache = {};
31
- /**
32
- * The schema to be used for validating and casting environment
33
- * variables
34
- */
35
- this.validationSchema = {};
36
- /**
37
- * Reference to the underlying schema
38
- */
39
- this.schema = Schema_1.schema;
40
- }
41
- /**
42
- * Process parsed env variables. The values will be validated
43
- * against the validation schema
44
- */
45
- process() {
46
- /**
47
- * Avoid re-processing the same values over and over
48
- * again
49
- */
50
- if (this.hasProcessedValues) {
51
- return;
52
- }
53
- this.hasProcessedValues = true;
54
- /**
55
- * Loop over the parsed object and set the value
56
- * on the process.env and the local cache.
57
- *
58
- * At this stage we process the values like a regular env parser with
59
- * no validation taking place
60
- */
61
- this.valuesToProcess.forEach(({ values, overwriteExisting }) => {
62
- Object.keys(values).forEach((key) => {
63
- /**
64
- * Use existing value when it already exists in process.env object
65
- * and [this.overwriteExisting] is set to false
66
- */
67
- const existingValue = process.env[key];
68
- if (existingValue && !overwriteExisting) {
69
- this.envCache[key] = existingValue;
70
- return;
71
- }
72
- /**
73
- * Otherwise set the value on "process.env"
74
- */
75
- this.envCache[key] = values[key];
76
- process.env[key] = values[key];
77
- });
78
- });
79
- /**
80
- * Release parsed values, since we don't need it anymore
81
- */
82
- this.valuesToProcess = [];
83
- /**
84
- * Perform validations by reading the environment variables
85
- */
86
- Object.keys(this.validationSchema).forEach((key) => {
87
- this.envCache[key] = this.validationSchema[key](key, this.get(key));
88
- });
89
- }
90
- /**
91
- * Register the validation schema
92
- */
93
- rules(schema) {
94
- this.validationSchema = schema;
95
- return {};
96
- }
97
- /**
98
- * Returns the environment variable value. First the cached
99
- * values are preferred. When missing, the value from
100
- * "process.env" is used
101
- */
102
- get(key, defaultValue) {
103
- /**
104
- * Return cached value
105
- */
106
- if (this.envCache[key] !== undefined) {
107
- return this.envCache[key];
108
- }
109
- /**
110
- * Get value from process.env and update the cache
111
- */
112
- const envValue = process.env[key];
113
- if (envValue) {
114
- this.envCache[key] = envValue;
115
- return envValue;
116
- }
117
- /**
118
- * Return default value when unable to lookup any other value
119
- */
120
- return defaultValue;
121
- }
122
- /**
123
- * Set key-value pair. The value will be validated using
124
- * the validation rule if exists.
125
- *
126
- * The original value is also updated on the `process.env`
127
- * object
128
- */
129
- set(key, value) {
130
- const validationFn = this.validationSchema[key];
131
- if (validationFn) {
132
- this.envCache[key] = validationFn(key, value);
133
- }
134
- else {
135
- this.envCache[key] = value;
136
- }
137
- process.env[key] = value;
138
- }
139
- }
140
- exports.Env = Env;
@@ -1,54 +0,0 @@
1
- /**
2
- * Env parser parses the environment variables from a string formatted
3
- * as a key-value pair seperated using an `=`. For example:
4
- *
5
- * ```
6
- * PORT=3333
7
- * HOST=127.0.0.1
8
- * ```
9
- *
10
- * The variables can reference other environment variables as well using `$`.
11
- * For example:
12
- *
13
- * ```
14
- * PORT=3333
15
- * REDIS_PORT=$PORT
16
- * ```
17
- *
18
- * The variables using characters other than letters can use wrap variables inside
19
- * a curly brace.
20
- *
21
- * ```
22
- * APP-PORT=3333
23
- * REDIS_PORT=${APP-PORT}
24
- * ```
25
- */
26
- export declare class EnvParser {
27
- private preferExistingEnvVariables;
28
- constructor(preferExistingEnvVariables?: boolean);
29
- /**
30
- * Returns value for a given key from the environment variables. Also
31
- * the current parsed object is used to pull the reference.
32
- */
33
- private getValue;
34
- /**
35
- * Interpolating the token wrapped inside the mustache
36
- * braces.
37
- */
38
- private interpolateMustache;
39
- /**
40
- * Interpolating the variable reference starting with a
41
- * `$`. We only capture numbers,letter and underscore.
42
- * For other characters, one can use the mustache
43
- * braces.
44
- */
45
- private interpolateVariable;
46
- /**
47
- * Interpolates the referenced values
48
- */
49
- private interpolate;
50
- /**
51
- * Parse the env string to an object of environment variables.
52
- */
53
- parse(envString: string): {};
54
- }
@@ -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
- };