@h3ravel/config 0.1.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/LICENSE +21 -0
- package/dist/index.cjs +126 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +43 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +89 -0
- package/dist/index.js.map +1 -0
- package/package.json +34 -0
- package/src/ConfigRepository.ts +51 -0
- package/src/Contracts/.gitkeep +0 -0
- package/src/EnvLoader.ts +16 -0
- package/src/Helpers.ts +1 -0
- package/src/Providers/ConfigServiceProvider.ts +46 -0
- package/src/index.ts +8 -0
- package/tests/.gitkeep +0 -0
- package/tsconfig.json +8 -0
- package/vite.config.ts +9 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 h3ravel
|
|
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/dist/index.cjs
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var index_exports = {};
|
|
33
|
+
__export(index_exports, {
|
|
34
|
+
ConfigRepository: () => ConfigRepository,
|
|
35
|
+
ConfigServiceProvider: () => ConfigServiceProvider,
|
|
36
|
+
EnvLoader: () => EnvLoader
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
|
|
40
|
+
// src/ConfigRepository.ts
|
|
41
|
+
var import_support = require("@h3ravel/support");
|
|
42
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
43
|
+
var import_promises = require("fs/promises");
|
|
44
|
+
var ConfigRepository = class {
|
|
45
|
+
static {
|
|
46
|
+
__name(this, "ConfigRepository");
|
|
47
|
+
}
|
|
48
|
+
app;
|
|
49
|
+
loaded = false;
|
|
50
|
+
configs = {};
|
|
51
|
+
constructor(app) {
|
|
52
|
+
this.app = app;
|
|
53
|
+
}
|
|
54
|
+
get(key, def) {
|
|
55
|
+
return (0, import_support.safeDot)(this.configs, key) ?? def;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Modify the defined configurations
|
|
59
|
+
*/
|
|
60
|
+
set(key, value) {
|
|
61
|
+
(0, import_support.setNested)(this.configs, key, value);
|
|
62
|
+
}
|
|
63
|
+
async load() {
|
|
64
|
+
if (!this.loaded) {
|
|
65
|
+
const configPath = this.app.getPath("config");
|
|
66
|
+
const files = await (0, import_promises.readdir)(configPath);
|
|
67
|
+
for (let i = 0; i < files.length; i++) {
|
|
68
|
+
const configModule = await import(import_node_path.default.join(configPath, files[i]));
|
|
69
|
+
const name = files[i].replaceAll(/.ts|js/g, "");
|
|
70
|
+
if (typeof configModule.default === "function") {
|
|
71
|
+
this.configs[name] = configModule.default(this.app);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
this.loaded = true;
|
|
75
|
+
}
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// src/EnvLoader.ts
|
|
81
|
+
var import_support2 = require("@h3ravel/support");
|
|
82
|
+
var EnvLoader = class {
|
|
83
|
+
static {
|
|
84
|
+
__name(this, "EnvLoader");
|
|
85
|
+
}
|
|
86
|
+
_app;
|
|
87
|
+
constructor(_app) {
|
|
88
|
+
this._app = _app;
|
|
89
|
+
}
|
|
90
|
+
get(key, def) {
|
|
91
|
+
return (0, import_support2.safeDot)(process.env, key) ?? def;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// src/Providers/ConfigServiceProvider.ts
|
|
96
|
+
var import_core = require("@h3ravel/core");
|
|
97
|
+
var import_dotenv = require("dotenv");
|
|
98
|
+
var ConfigServiceProvider = class extends import_core.ServiceProvider {
|
|
99
|
+
static {
|
|
100
|
+
__name(this, "ConfigServiceProvider");
|
|
101
|
+
}
|
|
102
|
+
async register() {
|
|
103
|
+
(0, import_dotenv.config)();
|
|
104
|
+
this.app.singleton("env", () => {
|
|
105
|
+
return new EnvLoader(this.app).get;
|
|
106
|
+
});
|
|
107
|
+
const repo = new ConfigRepository(this.app);
|
|
108
|
+
await repo.load();
|
|
109
|
+
this.app.singleton("config", () => {
|
|
110
|
+
return {
|
|
111
|
+
get: /* @__PURE__ */ __name((key, def) => repo.get(key, def), "get"),
|
|
112
|
+
set: repo.set
|
|
113
|
+
};
|
|
114
|
+
});
|
|
115
|
+
this.app.make("http.app").use((e) => {
|
|
116
|
+
repo.set("app.url", e.url.origin);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
121
|
+
0 && (module.exports = {
|
|
122
|
+
ConfigRepository,
|
|
123
|
+
ConfigServiceProvider,
|
|
124
|
+
EnvLoader
|
|
125
|
+
});
|
|
126
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/ConfigRepository.ts","../src/EnvLoader.ts","../src/Providers/ConfigServiceProvider.ts"],"sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from './ConfigRepository';\nexport * from './EnvLoader';\nexport * from './Helpers';\nexport * from './Providers/ConfigServiceProvider';\n","import { DotNestedKeys, DotNestedValue, safeDot, setNested } from '@h3ravel/support'\n\nimport { Application } from \"@h3ravel/core\";\nimport path from 'node:path'\nimport { readdir } from 'node:fs/promises'\n\nexport class ConfigRepository {\n private loaded: boolean = false\n private configs: Record<string, Record<string, any>> = {}\n\n constructor(private app: Application) { }\n\n // get<X extends Record<string, any>> (): X\n // get<X extends Record<string, any>, T extends Extract<keyof X, string>> (key: T): X[T]\n\n /**\n * Get the defined configurations\n */\n get<X extends Record<string, any>> (): X\n get<X extends Record<string, any>, K extends DotNestedKeys<X>> (key: K, def?: any): DotNestedValue<X, K>\n get<X extends Record<string, any>, K extends DotNestedKeys<X>> (key?: K, def?: any): any {\n return safeDot(this.configs, key) ?? def\n }\n\n /**\n * Modify the defined configurations\n */\n set<T extends string> (key: T, value: any): void {\n setNested(this.configs, key, value)\n }\n\n async load () {\n if (!this.loaded) {\n const configPath = this.app.getPath('config')\n\n const files = await readdir(configPath);\n\n for (let i = 0; i < files.length; i++) {\n const configModule = await import(path.join(configPath, files[i]))\n const name = files[i].replaceAll(/.ts|js/g, '')\n if (typeof configModule.default === 'function') {\n this.configs[name] = configModule.default(this.app)\n }\n }\n\n this.loaded = true\n }\n\n return this\n }\n}\n","import { DotNestedKeys, DotNestedValue, safeDot } from '@h3ravel/support'\n\nimport { Application } from \"@h3ravel/core\";\n\nexport class EnvLoader {\n constructor(private _app: Application) { }\n\n /**\n * Get the defined environment vars\n */\n get<X extends NodeJS.ProcessEnv> (): X\n get<X extends NodeJS.ProcessEnv, K extends DotNestedKeys<X>> (key: K, def?: any): DotNestedValue<X, K>\n get<X extends NodeJS.ProcessEnv, K extends DotNestedKeys<X>> (key?: K, def?: any): any {\n return safeDot(process.env, key) ?? def\n }\n}\n","import { Bindings, ServiceProvider } from '@h3ravel/core'\nimport { ConfigRepository, EnvLoader } from '..'\n\nimport { config as loadEnv } from 'dotenv'\n\n/**\n * Loads configuration and environment files.\n * \n * Load .env and merge with config files.\n * Bind ConfigRepository to the container.\n * \n * Auto-Registered\n */\nexport class ConfigServiceProvider extends ServiceProvider {\n async register () {\n\n loadEnv()\n\n /**\n * Create singleton to load env\n */\n this.app.singleton('env', () => {\n return new EnvLoader(this.app).get\n })\n\n /**\n * Initialize the configuration through the repository\n */\n const repo = new ConfigRepository(this.app)\n await repo.load()\n\n /**\n * Create singleton to load configurations\n */\n this.app.singleton('config', () => {\n return {\n get: (key, def) => repo.get(key as any, def),\n set: repo.set\n } as Bindings['config']\n })\n\n this.app.make('http.app').use(e => {\n repo.set('app.url', e.url.origin)\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACAA,qBAAkE;AAGlE,uBAAiB;AACjB,sBAAwB;AAEjB,IAAMA,mBAAN,MAAMA;EANb,OAMaA;;;;EACDC,SAAkB;EAClBC,UAA+C,CAAC;EAExD,YAAoBC,KAAkB;SAAlBA,MAAAA;EAAoB;EAUxCC,IAAgEC,KAASC,KAAgB;AACrF,eAAOC,wBAAQ,KAAKL,SAASG,GAAAA,KAAQC;EACzC;;;;EAKAE,IAAuBH,KAAQI,OAAkB;AAC7CC,kCAAU,KAAKR,SAASG,KAAKI,KAAAA;EACjC;EAEA,MAAME,OAAQ;AACV,QAAI,CAAC,KAAKV,QAAQ;AACd,YAAMW,aAAa,KAAKT,IAAIU,QAAQ,QAAA;AAEpC,YAAMC,QAAQ,UAAMC,yBAAQH,UAAAA;AAE5B,eAASI,IAAI,GAAGA,IAAIF,MAAMG,QAAQD,KAAK;AACnC,cAAME,eAAe,MAAM,OAAOC,iBAAAA,QAAKC,KAAKR,YAAYE,MAAME,CAAAA,CAAE;AAChE,cAAMK,OAAOP,MAAME,CAAAA,EAAGM,WAAW,WAAW,EAAA;AAC5C,YAAI,OAAOJ,aAAaK,YAAY,YAAY;AAC5C,eAAKrB,QAAQmB,IAAAA,IAAQH,aAAaK,QAAQ,KAAKpB,GAAG;QACtD;MACJ;AAEA,WAAKF,SAAS;IAClB;AAEA,WAAO;EACX;AACJ;;;AClDA,IAAAuB,kBAAuD;AAIhD,IAAMC,YAAN,MAAMA;EAJb,OAIaA;;;;EACT,YAAoBC,MAAmB;SAAnBA,OAAAA;EAAqB;EAOzCC,IAA8DC,KAASC,KAAgB;AACnF,eAAOC,yBAAQC,QAAQC,KAAKJ,GAAAA,KAAQC;EACxC;AACJ;;;ACfA,kBAA0C;AAG1C,oBAAkC;AAU3B,IAAMI,wBAAN,cAAoCC,4BAAAA;EAb3C,OAa2CA;;;EACvC,MAAMC,WAAY;AAEdC,sBAAAA,QAAAA;AAKA,SAAKC,IAAIC,UAAU,OAAO,MAAA;AACtB,aAAO,IAAIC,UAAU,KAAKF,GAAG,EAAEG;IACnC,CAAA;AAKA,UAAMC,OAAO,IAAIC,iBAAiB,KAAKL,GAAG;AAC1C,UAAMI,KAAKE,KAAI;AAKf,SAAKN,IAAIC,UAAU,UAAU,MAAA;AACzB,aAAO;QACHE,KAAK,wBAACI,KAAKC,QAAQJ,KAAKD,IAAII,KAAYC,GAAAA,GAAnC;QACLC,KAAKL,KAAKK;MACd;IACJ,CAAA;AAEA,SAAKT,IAAIU,KAAK,UAAA,EAAYC,IAAIC,CAAAA,MAAAA;AAC1BR,WAAKK,IAAI,WAAWG,EAAEC,IAAIC,MAAM;IACpC,CAAA;EACJ;AACJ;","names":["ConfigRepository","loaded","configs","app","get","key","def","safeDot","set","value","setNested","load","configPath","getPath","files","readdir","i","length","configModule","path","join","name","replaceAll","default","import_support","EnvLoader","_app","get","key","def","safeDot","process","env","ConfigServiceProvider","ServiceProvider","register","loadEnv","app","singleton","EnvLoader","get","repo","ConfigRepository","load","key","def","set","make","use","e","url","origin"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { DotNestedKeys, DotNestedValue } from '@h3ravel/support';
|
|
2
|
+
import { Application, ServiceProvider } from '@h3ravel/core';
|
|
3
|
+
|
|
4
|
+
declare class ConfigRepository {
|
|
5
|
+
private app;
|
|
6
|
+
private loaded;
|
|
7
|
+
private configs;
|
|
8
|
+
constructor(app: Application);
|
|
9
|
+
/**
|
|
10
|
+
* Get the defined configurations
|
|
11
|
+
*/
|
|
12
|
+
get<X extends Record<string, any>>(): X;
|
|
13
|
+
get<X extends Record<string, any>, K extends DotNestedKeys<X>>(key: K, def?: any): DotNestedValue<X, K>;
|
|
14
|
+
/**
|
|
15
|
+
* Modify the defined configurations
|
|
16
|
+
*/
|
|
17
|
+
set<T extends string>(key: T, value: any): void;
|
|
18
|
+
load(): Promise<this>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare class EnvLoader {
|
|
22
|
+
private _app;
|
|
23
|
+
constructor(_app: Application);
|
|
24
|
+
/**
|
|
25
|
+
* Get the defined environment vars
|
|
26
|
+
*/
|
|
27
|
+
get<X extends NodeJS.ProcessEnv>(): X;
|
|
28
|
+
get<X extends NodeJS.ProcessEnv, K extends DotNestedKeys<X>>(key: K, def?: any): DotNestedValue<X, K>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Loads configuration and environment files.
|
|
33
|
+
*
|
|
34
|
+
* Load .env and merge with config files.
|
|
35
|
+
* Bind ConfigRepository to the container.
|
|
36
|
+
*
|
|
37
|
+
* Auto-Registered
|
|
38
|
+
*/
|
|
39
|
+
declare class ConfigServiceProvider extends ServiceProvider {
|
|
40
|
+
register(): Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { ConfigRepository, ConfigServiceProvider, EnvLoader };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { DotNestedKeys, DotNestedValue } from '@h3ravel/support';
|
|
2
|
+
import { Application, ServiceProvider } from '@h3ravel/core';
|
|
3
|
+
|
|
4
|
+
declare class ConfigRepository {
|
|
5
|
+
private app;
|
|
6
|
+
private loaded;
|
|
7
|
+
private configs;
|
|
8
|
+
constructor(app: Application);
|
|
9
|
+
/**
|
|
10
|
+
* Get the defined configurations
|
|
11
|
+
*/
|
|
12
|
+
get<X extends Record<string, any>>(): X;
|
|
13
|
+
get<X extends Record<string, any>, K extends DotNestedKeys<X>>(key: K, def?: any): DotNestedValue<X, K>;
|
|
14
|
+
/**
|
|
15
|
+
* Modify the defined configurations
|
|
16
|
+
*/
|
|
17
|
+
set<T extends string>(key: T, value: any): void;
|
|
18
|
+
load(): Promise<this>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare class EnvLoader {
|
|
22
|
+
private _app;
|
|
23
|
+
constructor(_app: Application);
|
|
24
|
+
/**
|
|
25
|
+
* Get the defined environment vars
|
|
26
|
+
*/
|
|
27
|
+
get<X extends NodeJS.ProcessEnv>(): X;
|
|
28
|
+
get<X extends NodeJS.ProcessEnv, K extends DotNestedKeys<X>>(key: K, def?: any): DotNestedValue<X, K>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Loads configuration and environment files.
|
|
33
|
+
*
|
|
34
|
+
* Load .env and merge with config files.
|
|
35
|
+
* Bind ConfigRepository to the container.
|
|
36
|
+
*
|
|
37
|
+
* Auto-Registered
|
|
38
|
+
*/
|
|
39
|
+
declare class ConfigServiceProvider extends ServiceProvider {
|
|
40
|
+
register(): Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { ConfigRepository, ConfigServiceProvider, EnvLoader };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/ConfigRepository.ts
|
|
5
|
+
import { safeDot, setNested } from "@h3ravel/support";
|
|
6
|
+
import path from "path";
|
|
7
|
+
import { readdir } from "fs/promises";
|
|
8
|
+
var ConfigRepository = class {
|
|
9
|
+
static {
|
|
10
|
+
__name(this, "ConfigRepository");
|
|
11
|
+
}
|
|
12
|
+
app;
|
|
13
|
+
loaded = false;
|
|
14
|
+
configs = {};
|
|
15
|
+
constructor(app) {
|
|
16
|
+
this.app = app;
|
|
17
|
+
}
|
|
18
|
+
get(key, def) {
|
|
19
|
+
return safeDot(this.configs, key) ?? def;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Modify the defined configurations
|
|
23
|
+
*/
|
|
24
|
+
set(key, value) {
|
|
25
|
+
setNested(this.configs, key, value);
|
|
26
|
+
}
|
|
27
|
+
async load() {
|
|
28
|
+
if (!this.loaded) {
|
|
29
|
+
const configPath = this.app.getPath("config");
|
|
30
|
+
const files = await readdir(configPath);
|
|
31
|
+
for (let i = 0; i < files.length; i++) {
|
|
32
|
+
const configModule = await import(path.join(configPath, files[i]));
|
|
33
|
+
const name = files[i].replaceAll(/.ts|js/g, "");
|
|
34
|
+
if (typeof configModule.default === "function") {
|
|
35
|
+
this.configs[name] = configModule.default(this.app);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
this.loaded = true;
|
|
39
|
+
}
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/EnvLoader.ts
|
|
45
|
+
import { safeDot as safeDot2 } from "@h3ravel/support";
|
|
46
|
+
var EnvLoader = class {
|
|
47
|
+
static {
|
|
48
|
+
__name(this, "EnvLoader");
|
|
49
|
+
}
|
|
50
|
+
_app;
|
|
51
|
+
constructor(_app) {
|
|
52
|
+
this._app = _app;
|
|
53
|
+
}
|
|
54
|
+
get(key, def) {
|
|
55
|
+
return safeDot2(process.env, key) ?? def;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/Providers/ConfigServiceProvider.ts
|
|
60
|
+
import { ServiceProvider } from "@h3ravel/core";
|
|
61
|
+
import { config as loadEnv } from "dotenv";
|
|
62
|
+
var ConfigServiceProvider = class extends ServiceProvider {
|
|
63
|
+
static {
|
|
64
|
+
__name(this, "ConfigServiceProvider");
|
|
65
|
+
}
|
|
66
|
+
async register() {
|
|
67
|
+
loadEnv();
|
|
68
|
+
this.app.singleton("env", () => {
|
|
69
|
+
return new EnvLoader(this.app).get;
|
|
70
|
+
});
|
|
71
|
+
const repo = new ConfigRepository(this.app);
|
|
72
|
+
await repo.load();
|
|
73
|
+
this.app.singleton("config", () => {
|
|
74
|
+
return {
|
|
75
|
+
get: /* @__PURE__ */ __name((key, def) => repo.get(key, def), "get"),
|
|
76
|
+
set: repo.set
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
this.app.make("http.app").use((e) => {
|
|
80
|
+
repo.set("app.url", e.url.origin);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
export {
|
|
85
|
+
ConfigRepository,
|
|
86
|
+
ConfigServiceProvider,
|
|
87
|
+
EnvLoader
|
|
88
|
+
};
|
|
89
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/ConfigRepository.ts","../src/EnvLoader.ts","../src/Providers/ConfigServiceProvider.ts"],"sourcesContent":["import { DotNestedKeys, DotNestedValue, safeDot, setNested } from '@h3ravel/support'\n\nimport { Application } from \"@h3ravel/core\";\nimport path from 'node:path'\nimport { readdir } from 'node:fs/promises'\n\nexport class ConfigRepository {\n private loaded: boolean = false\n private configs: Record<string, Record<string, any>> = {}\n\n constructor(private app: Application) { }\n\n // get<X extends Record<string, any>> (): X\n // get<X extends Record<string, any>, T extends Extract<keyof X, string>> (key: T): X[T]\n\n /**\n * Get the defined configurations\n */\n get<X extends Record<string, any>> (): X\n get<X extends Record<string, any>, K extends DotNestedKeys<X>> (key: K, def?: any): DotNestedValue<X, K>\n get<X extends Record<string, any>, K extends DotNestedKeys<X>> (key?: K, def?: any): any {\n return safeDot(this.configs, key) ?? def\n }\n\n /**\n * Modify the defined configurations\n */\n set<T extends string> (key: T, value: any): void {\n setNested(this.configs, key, value)\n }\n\n async load () {\n if (!this.loaded) {\n const configPath = this.app.getPath('config')\n\n const files = await readdir(configPath);\n\n for (let i = 0; i < files.length; i++) {\n const configModule = await import(path.join(configPath, files[i]))\n const name = files[i].replaceAll(/.ts|js/g, '')\n if (typeof configModule.default === 'function') {\n this.configs[name] = configModule.default(this.app)\n }\n }\n\n this.loaded = true\n }\n\n return this\n }\n}\n","import { DotNestedKeys, DotNestedValue, safeDot } from '@h3ravel/support'\n\nimport { Application } from \"@h3ravel/core\";\n\nexport class EnvLoader {\n constructor(private _app: Application) { }\n\n /**\n * Get the defined environment vars\n */\n get<X extends NodeJS.ProcessEnv> (): X\n get<X extends NodeJS.ProcessEnv, K extends DotNestedKeys<X>> (key: K, def?: any): DotNestedValue<X, K>\n get<X extends NodeJS.ProcessEnv, K extends DotNestedKeys<X>> (key?: K, def?: any): any {\n return safeDot(process.env, key) ?? def\n }\n}\n","import { Bindings, ServiceProvider } from '@h3ravel/core'\nimport { ConfigRepository, EnvLoader } from '..'\n\nimport { config as loadEnv } from 'dotenv'\n\n/**\n * Loads configuration and environment files.\n * \n * Load .env and merge with config files.\n * Bind ConfigRepository to the container.\n * \n * Auto-Registered\n */\nexport class ConfigServiceProvider extends ServiceProvider {\n async register () {\n\n loadEnv()\n\n /**\n * Create singleton to load env\n */\n this.app.singleton('env', () => {\n return new EnvLoader(this.app).get\n })\n\n /**\n * Initialize the configuration through the repository\n */\n const repo = new ConfigRepository(this.app)\n await repo.load()\n\n /**\n * Create singleton to load configurations\n */\n this.app.singleton('config', () => {\n return {\n get: (key, def) => repo.get(key as any, def),\n set: repo.set\n } as Bindings['config']\n })\n\n this.app.make('http.app').use(e => {\n repo.set('app.url', e.url.origin)\n })\n }\n}\n"],"mappings":";;;;AAAA,SAAwCA,SAASC,iBAAiB;AAGlE,OAAOC,UAAU;AACjB,SAASC,eAAe;AAEjB,IAAMC,mBAAN,MAAMA;EANb,OAMaA;;;;EACDC,SAAkB;EAClBC,UAA+C,CAAC;EAExD,YAAoBC,KAAkB;SAAlBA,MAAAA;EAAoB;EAUxCC,IAAgEC,KAASC,KAAgB;AACrF,WAAOC,QAAQ,KAAKL,SAASG,GAAAA,KAAQC;EACzC;;;;EAKAE,IAAuBH,KAAQI,OAAkB;AAC7CC,cAAU,KAAKR,SAASG,KAAKI,KAAAA;EACjC;EAEA,MAAME,OAAQ;AACV,QAAI,CAAC,KAAKV,QAAQ;AACd,YAAMW,aAAa,KAAKT,IAAIU,QAAQ,QAAA;AAEpC,YAAMC,QAAQ,MAAMC,QAAQH,UAAAA;AAE5B,eAASI,IAAI,GAAGA,IAAIF,MAAMG,QAAQD,KAAK;AACnC,cAAME,eAAe,MAAM,OAAOC,KAAKC,KAAKR,YAAYE,MAAME,CAAAA,CAAE;AAChE,cAAMK,OAAOP,MAAME,CAAAA,EAAGM,WAAW,WAAW,EAAA;AAC5C,YAAI,OAAOJ,aAAaK,YAAY,YAAY;AAC5C,eAAKrB,QAAQmB,IAAAA,IAAQH,aAAaK,QAAQ,KAAKpB,GAAG;QACtD;MACJ;AAEA,WAAKF,SAAS;IAClB;AAEA,WAAO;EACX;AACJ;;;AClDA,SAAwCuB,WAAAA,gBAAe;AAIhD,IAAMC,YAAN,MAAMA;EAJb,OAIaA;;;;EACT,YAAoBC,MAAmB;SAAnBA,OAAAA;EAAqB;EAOzCC,IAA8DC,KAASC,KAAgB;AACnF,WAAOC,SAAQC,QAAQC,KAAKJ,GAAAA,KAAQC;EACxC;AACJ;;;ACfA,SAAmBI,uBAAuB;AAG1C,SAASC,UAAUC,eAAe;AAU3B,IAAMC,wBAAN,cAAoCC,gBAAAA;EAb3C,OAa2CA;;;EACvC,MAAMC,WAAY;AAEdC,YAAAA;AAKA,SAAKC,IAAIC,UAAU,OAAO,MAAA;AACtB,aAAO,IAAIC,UAAU,KAAKF,GAAG,EAAEG;IACnC,CAAA;AAKA,UAAMC,OAAO,IAAIC,iBAAiB,KAAKL,GAAG;AAC1C,UAAMI,KAAKE,KAAI;AAKf,SAAKN,IAAIC,UAAU,UAAU,MAAA;AACzB,aAAO;QACHE,KAAK,wBAACI,KAAKC,QAAQJ,KAAKD,IAAII,KAAYC,GAAAA,GAAnC;QACLC,KAAKL,KAAKK;MACd;IACJ,CAAA;AAEA,SAAKT,IAAIU,KAAK,UAAA,EAAYC,IAAIC,CAAAA,MAAAA;AAC1BR,WAAKK,IAAI,WAAWG,EAAEC,IAAIC,MAAM;IACpC,CAAA;EACJ;AACJ;","names":["safeDot","setNested","path","readdir","ConfigRepository","loaded","configs","app","get","key","def","safeDot","set","value","setNested","load","configPath","getPath","files","readdir","i","length","configModule","path","join","name","replaceAll","default","safeDot","EnvLoader","_app","get","key","def","safeDot","process","env","ServiceProvider","config","loadEnv","ConfigServiceProvider","ServiceProvider","register","loadEnv","app","singleton","EnvLoader","get","repo","ConfigRepository","load","key","def","set","make","use","e","url","origin"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@h3ravel/config",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Environment/config loading and management system for H3ravel.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"dotenv": "^17.2.1",
|
|
19
|
+
"h3": "^2.0.0-beta.1",
|
|
20
|
+
"@h3ravel/core": "0.1.0",
|
|
21
|
+
"@h3ravel/support": "0.1.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"typescript": "^5.4.0"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"barrel": "barrelsby --directory src --delete --singleQuotes",
|
|
28
|
+
"build": "tsup",
|
|
29
|
+
"dev": "tsx watch src/index.ts",
|
|
30
|
+
"start": "node dist/index.js",
|
|
31
|
+
"lint": "eslint . --ext .ts",
|
|
32
|
+
"test": "vitest"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { DotNestedKeys, DotNestedValue, safeDot, setNested } from '@h3ravel/support'
|
|
2
|
+
|
|
3
|
+
import { Application } from "@h3ravel/core";
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { readdir } from 'node:fs/promises'
|
|
6
|
+
|
|
7
|
+
export class ConfigRepository {
|
|
8
|
+
private loaded: boolean = false
|
|
9
|
+
private configs: Record<string, Record<string, any>> = {}
|
|
10
|
+
|
|
11
|
+
constructor(private app: Application) { }
|
|
12
|
+
|
|
13
|
+
// get<X extends Record<string, any>> (): X
|
|
14
|
+
// get<X extends Record<string, any>, T extends Extract<keyof X, string>> (key: T): X[T]
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get the defined configurations
|
|
18
|
+
*/
|
|
19
|
+
get<X extends Record<string, any>> (): X
|
|
20
|
+
get<X extends Record<string, any>, K extends DotNestedKeys<X>> (key: K, def?: any): DotNestedValue<X, K>
|
|
21
|
+
get<X extends Record<string, any>, K extends DotNestedKeys<X>> (key?: K, def?: any): any {
|
|
22
|
+
return safeDot(this.configs, key) ?? def
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Modify the defined configurations
|
|
27
|
+
*/
|
|
28
|
+
set<T extends string> (key: T, value: any): void {
|
|
29
|
+
setNested(this.configs, key, value)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async load () {
|
|
33
|
+
if (!this.loaded) {
|
|
34
|
+
const configPath = this.app.getPath('config')
|
|
35
|
+
|
|
36
|
+
const files = await readdir(configPath);
|
|
37
|
+
|
|
38
|
+
for (let i = 0; i < files.length; i++) {
|
|
39
|
+
const configModule = await import(path.join(configPath, files[i]))
|
|
40
|
+
const name = files[i].replaceAll(/.ts|js/g, '')
|
|
41
|
+
if (typeof configModule.default === 'function') {
|
|
42
|
+
this.configs[name] = configModule.default(this.app)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.loaded = true
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return this
|
|
50
|
+
}
|
|
51
|
+
}
|
|
File without changes
|
package/src/EnvLoader.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DotNestedKeys, DotNestedValue, safeDot } from '@h3ravel/support'
|
|
2
|
+
|
|
3
|
+
import { Application } from "@h3ravel/core";
|
|
4
|
+
|
|
5
|
+
export class EnvLoader {
|
|
6
|
+
constructor(private _app: Application) { }
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Get the defined environment vars
|
|
10
|
+
*/
|
|
11
|
+
get<X extends NodeJS.ProcessEnv> (): X
|
|
12
|
+
get<X extends NodeJS.ProcessEnv, K extends DotNestedKeys<X>> (key: K, def?: any): DotNestedValue<X, K>
|
|
13
|
+
get<X extends NodeJS.ProcessEnv, K extends DotNestedKeys<X>> (key?: K, def?: any): any {
|
|
14
|
+
return safeDot(process.env, key) ?? def
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/Helpers.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default class { }
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Bindings, ServiceProvider } from '@h3ravel/core'
|
|
2
|
+
import { ConfigRepository, EnvLoader } from '..'
|
|
3
|
+
|
|
4
|
+
import { config as loadEnv } from 'dotenv'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Loads configuration and environment files.
|
|
8
|
+
*
|
|
9
|
+
* Load .env and merge with config files.
|
|
10
|
+
* Bind ConfigRepository to the container.
|
|
11
|
+
*
|
|
12
|
+
* Auto-Registered
|
|
13
|
+
*/
|
|
14
|
+
export class ConfigServiceProvider extends ServiceProvider {
|
|
15
|
+
async register () {
|
|
16
|
+
|
|
17
|
+
loadEnv()
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create singleton to load env
|
|
21
|
+
*/
|
|
22
|
+
this.app.singleton('env', () => {
|
|
23
|
+
return new EnvLoader(this.app).get
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Initialize the configuration through the repository
|
|
28
|
+
*/
|
|
29
|
+
const repo = new ConfigRepository(this.app)
|
|
30
|
+
await repo.load()
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Create singleton to load configurations
|
|
34
|
+
*/
|
|
35
|
+
this.app.singleton('config', () => {
|
|
36
|
+
return {
|
|
37
|
+
get: (key, def) => repo.get(key as any, def),
|
|
38
|
+
set: repo.set
|
|
39
|
+
} as Bindings['config']
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
this.app.make('http.app').use(e => {
|
|
43
|
+
repo.set('app.url', e.url.origin)
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/index.ts
ADDED
package/tests/.gitkeep
ADDED
|
File without changes
|
package/tsconfig.json
ADDED