@ucdjs/env 0.1.1-beta.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.
- package/LICENSE +21 -0
- package/README.md +24 -0
- package/dist/index.d.mts +61 -0
- package/dist/index.mjs +70 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-PRESENT Lucas Nørgård
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @ucdjs/env
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
[![codecov][codecov-src]][codecov-href]
|
|
6
|
+
|
|
7
|
+
Environment configuration for UCDJS projects.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @ucdjs/env
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## 📄 License
|
|
16
|
+
|
|
17
|
+
Published under [MIT License](./LICENSE).
|
|
18
|
+
|
|
19
|
+
[npm-version-src]: https://img.shields.io/npm/v/@ucdjs/env?style=flat&colorA=18181B&colorB=4169E1
|
|
20
|
+
[npm-version-href]: https://npmjs.com/package/@ucdjs/env
|
|
21
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/@ucdjs/env?style=flat&colorA=18181B&colorB=4169E1
|
|
22
|
+
[npm-downloads-href]: https://npmjs.com/package/@ucdjs/env
|
|
23
|
+
[codecov-src]: https://img.shields.io/codecov/c/gh/ucdjs/ucd?style=flat&colorA=18181B&colorB=4169E1
|
|
24
|
+
[codecov-href]: https://codecov.io/gh/ucdjs/ucd
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
//#region src/constants.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* HTTP header name for specifying the type of UCD statistics.
|
|
4
|
+
*
|
|
5
|
+
* This constant defines the header key used to indicate the type of
|
|
6
|
+
* statistics being transmitted or requested in UCD-related HTTP communications.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { UCD_STAT_TYPE_HEADER, UCD_STAT_SIZE_HEADER } from "@ucdjs/env";
|
|
11
|
+
*
|
|
12
|
+
* headers[UCD_STAT_TYPE_HEADER] = "file";
|
|
13
|
+
* headers[UCD_STAT_SIZE_HEADER] = "1024";
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
declare const UCD_STAT_TYPE_HEADER = "X-UCD-Stat-Type";
|
|
17
|
+
declare const UCD_STAT_SIZE_HEADER = "X-UCD-Stat-Size";
|
|
18
|
+
declare const UCD_STAT_CHILDREN_HEADER = "X-UCD-Stat-Children";
|
|
19
|
+
declare const UCD_STAT_CHILDREN_FILES_HEADER = "X-UCD-Stat-Children-Files";
|
|
20
|
+
declare const UCD_STAT_CHILDREN_DIRS_HEADER = "X-UCD-Stat-Children-Dirs";
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/env-constants.d.ts
|
|
23
|
+
/**
|
|
24
|
+
* Base URL for the UCDJS API
|
|
25
|
+
* @default "https://api.ucdjs.dev"
|
|
26
|
+
*/
|
|
27
|
+
declare const UCDJS_API_BASE_URL: string;
|
|
28
|
+
/**
|
|
29
|
+
* Base URL for the UCDJS Store API
|
|
30
|
+
* @default "https://ucd-store.ucdjs.dev"
|
|
31
|
+
*/
|
|
32
|
+
declare const UCDJS_STORE_BASE_URL: string;
|
|
33
|
+
declare const DEFAULT_USER_AGENT = "ucdjs.dev (https://ucdjs.dev)";
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/required-env.d.ts
|
|
36
|
+
/**
|
|
37
|
+
* Validates that required environment variables are present and returns a typed environment object.
|
|
38
|
+
*
|
|
39
|
+
* This function ensures that specified environment variables exist in the provided environment object.
|
|
40
|
+
* If any required keys are missing, it throws an error. Otherwise, it returns the environment object
|
|
41
|
+
* with proper typing where required keys are guaranteed to be present.
|
|
42
|
+
*
|
|
43
|
+
* @template {object} Env - The type of the environment object
|
|
44
|
+
* @template {keyof Env} Keys - The keys from Env that are required to be present
|
|
45
|
+
* @param {Env} env - The environment object to validate
|
|
46
|
+
* @param {Keys[]} requiredKeys - Array of keys that must be present in the environment object
|
|
47
|
+
* @returns {Required<Pick<Env, Keys>> & Partial<Omit<Env, Keys>>} The environment object with required keys typed as non-nullable and optional keys as partial
|
|
48
|
+
* @throws {Error} When any of the required environment variables are missing
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* import { requiredEnv } from "@ucdjs/env";
|
|
53
|
+
* const env = { DATABASE_URL: "...", API_KEY: "...", OPTIONAL_VAR: undefined };
|
|
54
|
+
* const validatedEnv = requiredEnv(env, ["DATABASE_URL", "API_KEY"]);
|
|
55
|
+
* // validatedEnv.DATABASE_URL and validatedEnv.API_KEY are guaranteed to be present
|
|
56
|
+
* // validatedEnv.OPTIONAL_VAR remains optional
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
declare function requiredEnv<Env extends object, Keys extends keyof Env>(env: Env, requiredKeys: Keys[]): Required<Pick<Env, Keys>> & Partial<Omit<Env, Keys>>;
|
|
60
|
+
//#endregion
|
|
61
|
+
export { DEFAULT_USER_AGENT, UCDJS_API_BASE_URL, UCDJS_STORE_BASE_URL, UCD_STAT_CHILDREN_DIRS_HEADER, UCD_STAT_CHILDREN_FILES_HEADER, UCD_STAT_CHILDREN_HEADER, UCD_STAT_SIZE_HEADER, UCD_STAT_TYPE_HEADER, requiredEnv };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { getOwnProperty } from "@luxass/utils";
|
|
2
|
+
|
|
3
|
+
//#region src/constants.ts
|
|
4
|
+
/**
|
|
5
|
+
* HTTP header name for specifying the type of UCD statistics.
|
|
6
|
+
*
|
|
7
|
+
* This constant defines the header key used to indicate the type of
|
|
8
|
+
* statistics being transmitted or requested in UCD-related HTTP communications.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { UCD_STAT_TYPE_HEADER, UCD_STAT_SIZE_HEADER } from "@ucdjs/env";
|
|
13
|
+
*
|
|
14
|
+
* headers[UCD_STAT_TYPE_HEADER] = "file";
|
|
15
|
+
* headers[UCD_STAT_SIZE_HEADER] = "1024";
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
const UCD_STAT_TYPE_HEADER = "X-UCD-Stat-Type";
|
|
19
|
+
const UCD_STAT_SIZE_HEADER = "X-UCD-Stat-Size";
|
|
20
|
+
const UCD_STAT_CHILDREN_HEADER = "X-UCD-Stat-Children";
|
|
21
|
+
const UCD_STAT_CHILDREN_FILES_HEADER = "X-UCD-Stat-Children-Files";
|
|
22
|
+
const UCD_STAT_CHILDREN_DIRS_HEADER = "X-UCD-Stat-Children-Dirs";
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/env-constants.ts
|
|
26
|
+
const env = typeof process === "undefined" ? {} : process.env;
|
|
27
|
+
/**
|
|
28
|
+
* Base URL for the UCDJS API
|
|
29
|
+
* @default "https://api.ucdjs.dev"
|
|
30
|
+
*/
|
|
31
|
+
const UCDJS_API_BASE_URL = env.UCDJS_API_BASE_URL || "https://api.ucdjs.dev";
|
|
32
|
+
/**
|
|
33
|
+
* Base URL for the UCDJS Store API
|
|
34
|
+
* @default "https://ucd-store.ucdjs.dev"
|
|
35
|
+
*/
|
|
36
|
+
const UCDJS_STORE_BASE_URL = env.UCDJS_STORE_BASE_URL || "https://ucd-store.ucdjs.dev";
|
|
37
|
+
const DEFAULT_USER_AGENT = "ucdjs.dev (https://ucdjs.dev)";
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/required-env.ts
|
|
41
|
+
/**
|
|
42
|
+
* Validates that required environment variables are present and returns a typed environment object.
|
|
43
|
+
*
|
|
44
|
+
* This function ensures that specified environment variables exist in the provided environment object.
|
|
45
|
+
* If any required keys are missing, it throws an error. Otherwise, it returns the environment object
|
|
46
|
+
* with proper typing where required keys are guaranteed to be present.
|
|
47
|
+
*
|
|
48
|
+
* @template {object} Env - The type of the environment object
|
|
49
|
+
* @template {keyof Env} Keys - The keys from Env that are required to be present
|
|
50
|
+
* @param {Env} env - The environment object to validate
|
|
51
|
+
* @param {Keys[]} requiredKeys - Array of keys that must be present in the environment object
|
|
52
|
+
* @returns {Required<Pick<Env, Keys>> & Partial<Omit<Env, Keys>>} The environment object with required keys typed as non-nullable and optional keys as partial
|
|
53
|
+
* @throws {Error} When any of the required environment variables are missing
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* import { requiredEnv } from "@ucdjs/env";
|
|
58
|
+
* const env = { DATABASE_URL: "...", API_KEY: "...", OPTIONAL_VAR: undefined };
|
|
59
|
+
* const validatedEnv = requiredEnv(env, ["DATABASE_URL", "API_KEY"]);
|
|
60
|
+
* // validatedEnv.DATABASE_URL and validatedEnv.API_KEY are guaranteed to be present
|
|
61
|
+
* // validatedEnv.OPTIONAL_VAR remains optional
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
function requiredEnv(env, requiredKeys) {
|
|
65
|
+
for (const key of requiredKeys) if (getOwnProperty(env, key) == null) throw new Error(`Missing required env var: ${key}`);
|
|
66
|
+
return env;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
export { DEFAULT_USER_AGENT, UCDJS_API_BASE_URL, UCDJS_STORE_BASE_URL, UCD_STAT_CHILDREN_DIRS_HEADER, UCD_STAT_CHILDREN_FILES_HEADER, UCD_STAT_CHILDREN_HEADER, UCD_STAT_SIZE_HEADER, UCD_STAT_TYPE_HEADER, requiredEnv };
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ucdjs/env",
|
|
3
|
+
"version": "0.1.1-beta.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Lucas Nørgård",
|
|
7
|
+
"email": "lucasnrgaard@gmail.com",
|
|
8
|
+
"url": "https://luxass.dev"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"homepage": "https://github.com/ucdjs/ucd",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/ucdjs/ucd.git",
|
|
15
|
+
"directory": "packages/env"
|
|
16
|
+
},
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/ucdjs/ucd/issues"
|
|
19
|
+
},
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./dist/index.mjs",
|
|
22
|
+
"./package.json": "./package.json"
|
|
23
|
+
},
|
|
24
|
+
"types": "./dist/index.d.mts",
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22.18"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@luxass/utils": "2.7.3"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@luxass/eslint-config": "7.2.0",
|
|
36
|
+
"eslint": "10.0.0",
|
|
37
|
+
"publint": "0.3.17",
|
|
38
|
+
"tsdown": "0.20.3",
|
|
39
|
+
"typescript": "5.9.3",
|
|
40
|
+
"@ucdjs-tooling/tsconfig": "1.0.0",
|
|
41
|
+
"@ucdjs-tooling/tsdown-config": "1.0.0"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsdown --tsconfig=./tsconfig.build.json",
|
|
48
|
+
"dev": "tsdown --watch",
|
|
49
|
+
"clean": "git clean -xdf dist node_modules",
|
|
50
|
+
"lint": "eslint .",
|
|
51
|
+
"typecheck": "tsc --noEmit -p tsconfig.build.json"
|
|
52
|
+
}
|
|
53
|
+
}
|