@elementor/env 0.2.0 → 0.3.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.
- package/CHANGELOG.md +20 -0
- package/README.md +1 -2
- package/dist/index.d.mts +10 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/index.test.ts +2 -2
- package/src/index.ts +1 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# 0.3.0 (2023-09-11)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **env:** mark experimental functions [ED-12072] ([#115](https://github.com/elementor/elementor-packages/issues/115)) ([e5b1b4a](https://github.com/elementor/elementor-packages/commit/e5b1b4ab2c5c14fc77fc25d9fe86108756e26441))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# 0.2.0 (2023-05-09)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* **env:** support consuming environment variables in packages [ED-10161] ([#26](https://github.com/elementor/elementor-packages/issues/26)) ([dd811ff](https://github.com/elementor/elementor-packages/commit/dd811ff71e43bf998ee07c6e1f380a379874a7fd))
|
package/README.md
CHANGED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare function initEnv(env: Record<string, object>): void;
|
|
2
|
+
declare function __resetEnv(): void;
|
|
3
|
+
declare function parseEnv<TEnv extends object = object>(key: string, parseFn?: (envData: object) => TEnv): {
|
|
4
|
+
validateEnv: () => void;
|
|
5
|
+
env: TEnv;
|
|
6
|
+
};
|
|
7
|
+
declare class InvalidEnvError extends Error {
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { InvalidEnvError, __resetEnv, initEnv, parseEnv };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
declare function initEnv(env: Record<string, object>): void;
|
|
2
|
-
declare function
|
|
2
|
+
declare function __resetEnv(): void;
|
|
3
3
|
declare function parseEnv<TEnv extends object = object>(key: string, parseFn?: (envData: object) => TEnv): {
|
|
4
4
|
validateEnv: () => void;
|
|
5
5
|
env: TEnv;
|
|
@@ -7,4 +7,4 @@ declare function parseEnv<TEnv extends object = object>(key: string, parseFn?: (
|
|
|
7
7
|
declare class InvalidEnvError extends Error {
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
export { InvalidEnvError, initEnv, parseEnv
|
|
10
|
+
export { InvalidEnvError, __resetEnv, initEnv, parseEnv };
|
package/dist/index.js
CHANGED
|
@@ -21,16 +21,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
InvalidEnvError: () => InvalidEnvError,
|
|
24
|
+
__resetEnv: () => __resetEnv,
|
|
24
25
|
initEnv: () => initEnv,
|
|
25
|
-
parseEnv: () => parseEnv
|
|
26
|
-
resetEnv: () => resetEnv
|
|
26
|
+
parseEnv: () => parseEnv
|
|
27
27
|
});
|
|
28
28
|
module.exports = __toCommonJS(src_exports);
|
|
29
29
|
var globalEnv = null;
|
|
30
30
|
function initEnv(env) {
|
|
31
31
|
globalEnv = env;
|
|
32
32
|
}
|
|
33
|
-
function
|
|
33
|
+
function __resetEnv() {
|
|
34
34
|
globalEnv = null;
|
|
35
35
|
}
|
|
36
36
|
function parseEnv(key, parseFn = (rawSettings) => rawSettings) {
|
|
@@ -87,8 +87,8 @@ var InvalidEnvError = class extends Error {
|
|
|
87
87
|
// Annotate the CommonJS export names for ESM import in node:
|
|
88
88
|
0 && (module.exports = {
|
|
89
89
|
InvalidEnvError,
|
|
90
|
+
__resetEnv,
|
|
90
91
|
initEnv,
|
|
91
|
-
parseEnv
|
|
92
|
-
resetEnv
|
|
92
|
+
parseEnv
|
|
93
93
|
});
|
|
94
94
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["let globalEnv: Record<string, object> | null = null;\n\nexport function initEnv( env: Record<string, object> ) {\n\tglobalEnv = env;\n}\n\nexport function
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["let globalEnv: Record<string, object> | null = null;\n\nexport function initEnv( env: Record<string, object> ) {\n\tglobalEnv = env;\n}\n\nexport function __resetEnv() {\n\tglobalEnv = null;\n}\n\nexport function parseEnv<TEnv extends object = object>(\n\tkey: string,\n\tparseFn: ( envData: object ) => TEnv = ( rawSettings: object ) => rawSettings as TEnv,\n) {\n\tlet parsedEnv: TEnv = {} as TEnv;\n\tlet isParsed = false;\n\n\t// Proxy the exposed object so users will be able to access the properties\n\t// as if it was a plain object, while still having automatic parsing.\n\tconst proxiedEnv = new Proxy( parsedEnv, {\n\t\tget( target, property ) {\n\t\t\tif ( ! isParsed ) {\n\t\t\t\tparse();\n\t\t\t}\n\n\t\t\treturn parsedEnv[ property as keyof TEnv ];\n\t\t},\n\t\townKeys() {\n\t\t\tif ( ! isParsed ) {\n\t\t\t\tparse();\n\t\t\t}\n\n\t\t\treturn Reflect.ownKeys( parsedEnv );\n\t\t},\n\t\tgetOwnPropertyDescriptor() {\n\t\t\treturn {\n\t\t\t\tconfigurable: true,\n\t\t\t\tenumerable: true,\n\t\t\t};\n\t\t},\n\t} );\n\n\tconst parse = () => {\n\t\ttry {\n\t\t\tconst env = globalEnv?.[ key ];\n\n\t\t\tif ( ! env ) {\n\t\t\t\tthrow new InvalidEnvError( `Settings object not found` );\n\t\t\t}\n\n\t\t\tif ( typeof env !== 'object' ) {\n\t\t\t\tthrow new InvalidEnvError( `Expected settings to be \\`object\\`, but got \\`${ typeof env }\\`` );\n\t\t\t}\n\n\t\t\tparsedEnv = parseFn( env );\n\t\t} catch ( e ) {\n\t\t\t// If something in the validation goes wrong, we return an empty object instead of throwing,\n\t\t\t// so it'll fail in the component level rather than the module level.\n\t\t\tif ( e instanceof InvalidEnvError ) {\n\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\tconsole.warn( `${ key } - ${ e.message }` );\n\n\t\t\t\tparsedEnv = {} as TEnv;\n\t\t\t} else {\n\t\t\t\tthrow e;\n\t\t\t}\n\t\t} finally {\n\t\t\tisParsed = true;\n\t\t}\n\t};\n\n\treturn {\n\t\tvalidateEnv: parse,\n\t\tenv: proxiedEnv,\n\t};\n}\n\nexport class InvalidEnvError extends Error {}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAI,YAA2C;AAExC,SAAS,QAAS,KAA8B;AACtD,cAAY;AACb;AAEO,SAAS,aAAa;AAC5B,cAAY;AACb;AAEO,SAAS,SACf,KACA,UAAuC,CAAE,gBAAyB,aACjE;AACD,MAAI,YAAkB,CAAC;AACvB,MAAI,WAAW;AAIf,QAAM,aAAa,IAAI,MAAO,WAAW;AAAA,IACxC,IAAK,QAAQ,UAAW;AACvB,UAAK,CAAE,UAAW;AACjB,cAAM;AAAA,MACP;AAEA,aAAO,UAAW,QAAuB;AAAA,IAC1C;AAAA,IACA,UAAU;AACT,UAAK,CAAE,UAAW;AACjB,cAAM;AAAA,MACP;AAEA,aAAO,QAAQ,QAAS,SAAU;AAAA,IACnC;AAAA,IACA,2BAA2B;AAC1B,aAAO;AAAA,QACN,cAAc;AAAA,QACd,YAAY;AAAA,MACb;AAAA,IACD;AAAA,EACD,CAAE;AAEF,QAAM,QAAQ,MAAM;AACnB,QAAI;AACH,YAAM,MAAM,YAAa,GAAI;AAE7B,UAAK,CAAE,KAAM;AACZ,cAAM,IAAI,gBAAiB,2BAA4B;AAAA,MACxD;AAEA,UAAK,OAAO,QAAQ,UAAW;AAC9B,cAAM,IAAI,gBAAiB,iDAAkD,OAAO,GAAI,IAAK;AAAA,MAC9F;AAEA,kBAAY,QAAS,GAAI;AAAA,IAC1B,SAAU,GAAI;AAGb,UAAK,aAAa,iBAAkB;AAEnC,gBAAQ,KAAM,GAAI,GAAI,MAAO,EAAE,OAAQ,EAAG;AAE1C,oBAAY,CAAC;AAAA,MACd,OAAO;AACN,cAAM;AAAA,MACP;AAAA,IACD,UAAE;AACD,iBAAW;AAAA,IACZ;AAAA,EACD;AAEA,SAAO;AAAA,IACN,aAAa;AAAA,IACb,KAAK;AAAA,EACN;AACD;AAEO,IAAM,kBAAN,cAA8B,MAAM;AAAC;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@ var globalEnv = null;
|
|
|
3
3
|
function initEnv(env) {
|
|
4
4
|
globalEnv = env;
|
|
5
5
|
}
|
|
6
|
-
function
|
|
6
|
+
function __resetEnv() {
|
|
7
7
|
globalEnv = null;
|
|
8
8
|
}
|
|
9
9
|
function parseEnv(key, parseFn = (rawSettings) => rawSettings) {
|
|
@@ -59,8 +59,8 @@ var InvalidEnvError = class extends Error {
|
|
|
59
59
|
};
|
|
60
60
|
export {
|
|
61
61
|
InvalidEnvError,
|
|
62
|
+
__resetEnv,
|
|
62
63
|
initEnv,
|
|
63
|
-
parseEnv
|
|
64
|
-
resetEnv
|
|
64
|
+
parseEnv
|
|
65
65
|
};
|
|
66
66
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["let globalEnv: Record<string, object> | null = null;\n\nexport function initEnv( env: Record<string, object> ) {\n\tglobalEnv = env;\n}\n\nexport function
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["let globalEnv: Record<string, object> | null = null;\n\nexport function initEnv( env: Record<string, object> ) {\n\tglobalEnv = env;\n}\n\nexport function __resetEnv() {\n\tglobalEnv = null;\n}\n\nexport function parseEnv<TEnv extends object = object>(\n\tkey: string,\n\tparseFn: ( envData: object ) => TEnv = ( rawSettings: object ) => rawSettings as TEnv,\n) {\n\tlet parsedEnv: TEnv = {} as TEnv;\n\tlet isParsed = false;\n\n\t// Proxy the exposed object so users will be able to access the properties\n\t// as if it was a plain object, while still having automatic parsing.\n\tconst proxiedEnv = new Proxy( parsedEnv, {\n\t\tget( target, property ) {\n\t\t\tif ( ! isParsed ) {\n\t\t\t\tparse();\n\t\t\t}\n\n\t\t\treturn parsedEnv[ property as keyof TEnv ];\n\t\t},\n\t\townKeys() {\n\t\t\tif ( ! isParsed ) {\n\t\t\t\tparse();\n\t\t\t}\n\n\t\t\treturn Reflect.ownKeys( parsedEnv );\n\t\t},\n\t\tgetOwnPropertyDescriptor() {\n\t\t\treturn {\n\t\t\t\tconfigurable: true,\n\t\t\t\tenumerable: true,\n\t\t\t};\n\t\t},\n\t} );\n\n\tconst parse = () => {\n\t\ttry {\n\t\t\tconst env = globalEnv?.[ key ];\n\n\t\t\tif ( ! env ) {\n\t\t\t\tthrow new InvalidEnvError( `Settings object not found` );\n\t\t\t}\n\n\t\t\tif ( typeof env !== 'object' ) {\n\t\t\t\tthrow new InvalidEnvError( `Expected settings to be \\`object\\`, but got \\`${ typeof env }\\`` );\n\t\t\t}\n\n\t\t\tparsedEnv = parseFn( env );\n\t\t} catch ( e ) {\n\t\t\t// If something in the validation goes wrong, we return an empty object instead of throwing,\n\t\t\t// so it'll fail in the component level rather than the module level.\n\t\t\tif ( e instanceof InvalidEnvError ) {\n\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\tconsole.warn( `${ key } - ${ e.message }` );\n\n\t\t\t\tparsedEnv = {} as TEnv;\n\t\t\t} else {\n\t\t\t\tthrow e;\n\t\t\t}\n\t\t} finally {\n\t\t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,kBAAN,cAA8B,MAAM;AAAC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elementor/env",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "Elementor Team",
|
|
6
6
|
"homepage": "https://elementor.com/",
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"build": "tsup --config=../../tsup.build.ts",
|
|
32
32
|
"dev": "tsup --config=../../tsup.dev.ts"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "7dc3971d2dd061848d6547aa2caed8332e7b9167"
|
|
35
35
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { initEnv, InvalidEnvError, parseEnv,
|
|
1
|
+
import { initEnv, InvalidEnvError, parseEnv, __resetEnv } from '../index';
|
|
2
2
|
|
|
3
3
|
describe( '@elementor/env', () => {
|
|
4
4
|
it( 'should warn for non existing env key', () => {
|
|
5
5
|
// Arrange.
|
|
6
|
-
|
|
6
|
+
__resetEnv();
|
|
7
7
|
|
|
8
8
|
// Act.
|
|
9
9
|
const { env } = parseEnv( 'unknown-key' );
|