@h3ravel/shared 0.17.1 → 0.17.3
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/README.md +13 -1
- package/dist/index.cjs +167 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +89 -6
- package/dist/index.d.ts +89 -6
- package/dist/index.js +162 -6
- package/dist/index.js.map +1 -1
- package/package.json +12 -4
- package/tsconfig.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
<
|
|
1
|
+
<div align="center">
|
|
2
|
+
<a href="https://h3ravel.toneflix.net" target="_blank">
|
|
3
|
+
<img src="https://raw.githubusercontent.com/h3ravel/assets/refs/heads/main/logo-full.svg" width="200" alt="H3ravel Logo">
|
|
4
|
+
</a>
|
|
5
|
+
<h1 align="center"><a href="https://h3ravel.toneflix.net/arquebus">H3ravel Shared</a></h1>
|
|
2
6
|
|
|
3
7
|
[![Framework][ix]][lx]
|
|
4
8
|
[![Shared Package Version][i1]][l1]
|
|
5
9
|
[![Downloads][d1]][d1]
|
|
10
|
+
[![Tests][tei]][tel]
|
|
11
|
+
[![License][lini]][linl]
|
|
12
|
+
|
|
13
|
+
</div>
|
|
6
14
|
|
|
7
15
|
# About H3ravel/shared
|
|
8
16
|
|
|
@@ -29,3 +37,7 @@ The H3ravel framework is open-sourced software licensed under the [MIT license](
|
|
|
29
37
|
[i1]: https://img.shields.io/npm/v/%40h3ravel%2Fshared?style=flat-square&label=@h3ravel/shared&color=%230970ce
|
|
30
38
|
[l1]: https://www.npmjs.com/package/@h3ravel/shared
|
|
31
39
|
[d1]: https://img.shields.io/npm/dt/%40h3ravel%2Fshared?style=flat-square&label=Downloads&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40h3ravel%2Fshared
|
|
40
|
+
[linl]: https://github.com/h3ravel/framework/blob/main/LICENSE
|
|
41
|
+
[lini]: https://img.shields.io/github/license/h3ravel/framework
|
|
42
|
+
[tel]: https://github.com/h3ravel/framework/actions/workflows/test.yml
|
|
43
|
+
[tei]: https://github.com/h3ravel/framework/actions/workflows/test.yml/badge.svg
|
package/dist/index.cjs
CHANGED
|
@@ -29,12 +29,14 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
29
29
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
30
|
|
|
31
31
|
// src/index.ts
|
|
32
|
-
var
|
|
33
|
-
__export(
|
|
32
|
+
var index_exports = {};
|
|
33
|
+
__export(index_exports, {
|
|
34
|
+
EnvParser: () => EnvParser,
|
|
34
35
|
HttpContext: () => HttpContext,
|
|
36
|
+
Logger: () => Logger,
|
|
35
37
|
PathLoader: () => PathLoader
|
|
36
38
|
});
|
|
37
|
-
module.exports = __toCommonJS(
|
|
39
|
+
module.exports = __toCommonJS(index_exports);
|
|
38
40
|
|
|
39
41
|
// src/Contracts/IHttp.ts
|
|
40
42
|
var HttpContext = class _HttpContext {
|
|
@@ -59,6 +61,155 @@ var HttpContext = class _HttpContext {
|
|
|
59
61
|
}
|
|
60
62
|
};
|
|
61
63
|
|
|
64
|
+
// src/Utils/EnvParser.ts
|
|
65
|
+
var EnvParser = class {
|
|
66
|
+
static {
|
|
67
|
+
__name(this, "EnvParser");
|
|
68
|
+
}
|
|
69
|
+
static parse(initial) {
|
|
70
|
+
const parsed = {
|
|
71
|
+
...initial
|
|
72
|
+
};
|
|
73
|
+
for (const key in parsed) {
|
|
74
|
+
let value = parsed[key];
|
|
75
|
+
parsed[key] = this.parseValue(value);
|
|
76
|
+
}
|
|
77
|
+
return parsed;
|
|
78
|
+
}
|
|
79
|
+
static parseValue(value) {
|
|
80
|
+
if (value === null || value === void 0) return value;
|
|
81
|
+
if (value === "true") return true;
|
|
82
|
+
if (value === "false") return false;
|
|
83
|
+
if (!isNaN(value) && value.trim() !== "") {
|
|
84
|
+
return Number(value);
|
|
85
|
+
}
|
|
86
|
+
if (value === "null") return null;
|
|
87
|
+
if (value === "undefined") return void 0;
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// src/Utils/Logger.ts
|
|
93
|
+
var import_chalk = __toESM(require("chalk"), 1);
|
|
94
|
+
var Logger = class {
|
|
95
|
+
static {
|
|
96
|
+
__name(this, "Logger");
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Logs the message in two columns
|
|
100
|
+
* @param name
|
|
101
|
+
* @param value
|
|
102
|
+
* @returns
|
|
103
|
+
*/
|
|
104
|
+
static twoColumnLog(name, value) {
|
|
105
|
+
const regex = /\x1b\[\d+m/g;
|
|
106
|
+
const width = Math.min(process.stdout.columns, 100);
|
|
107
|
+
const dots = Math.max(width - name.replace(regex, "").length - value.replace(regex, "").length - 10, 0);
|
|
108
|
+
return console.log(name, import_chalk.default.gray(".".repeat(dots)), value);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Wraps text with chalk
|
|
112
|
+
*
|
|
113
|
+
* @param txt
|
|
114
|
+
* @param color
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
static textFormat(txt, color) {
|
|
118
|
+
return String(txt).split(":").map((e, i, a) => i == 0 && a.length > 1 ? color(" " + e + ": ") : e).join("");
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Logs a success message
|
|
122
|
+
*
|
|
123
|
+
* @param msg
|
|
124
|
+
* @param exit
|
|
125
|
+
*/
|
|
126
|
+
static success(msg, exit = false) {
|
|
127
|
+
console.log(import_chalk.default.green("\u2713"), this.textFormat(msg, import_chalk.default.bgGreen), "\n");
|
|
128
|
+
if (exit) process.exit(0);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Logs an informational message
|
|
132
|
+
*
|
|
133
|
+
* @param msg
|
|
134
|
+
* @param exit
|
|
135
|
+
*/
|
|
136
|
+
static info(msg, exit = false) {
|
|
137
|
+
console.log(import_chalk.default.blue("\u2139"), this.textFormat(msg, import_chalk.default.bgBlue), "\n");
|
|
138
|
+
if (exit) process.exit(0);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Logs an error message
|
|
142
|
+
*
|
|
143
|
+
* @param msg
|
|
144
|
+
* @param exit
|
|
145
|
+
*/
|
|
146
|
+
static error(msg, exit = true) {
|
|
147
|
+
if (msg instanceof Error) {
|
|
148
|
+
if (msg.message) {
|
|
149
|
+
console.error(import_chalk.default.red("\u2716"), this.textFormat("ERROR:" + msg.message, import_chalk.default.bgRed));
|
|
150
|
+
}
|
|
151
|
+
console.error(import_chalk.default.red(`${msg.detail ? `${msg.detail}
|
|
152
|
+
` : ""}${msg.stack}`), "\n");
|
|
153
|
+
} else {
|
|
154
|
+
console.error(import_chalk.default.red("\u2716"), this.textFormat(msg, import_chalk.default.bgRed), "\n");
|
|
155
|
+
}
|
|
156
|
+
if (exit) process.exit(1);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Logs a success message
|
|
160
|
+
*
|
|
161
|
+
* @param name
|
|
162
|
+
* @param value
|
|
163
|
+
* @param status
|
|
164
|
+
* @param exit
|
|
165
|
+
*/
|
|
166
|
+
static split(name, value, status, exit = false) {
|
|
167
|
+
status ??= "info";
|
|
168
|
+
const color = {
|
|
169
|
+
success: import_chalk.default.bgGreen,
|
|
170
|
+
info: import_chalk.default.bgBlue,
|
|
171
|
+
error: import_chalk.default.bgRed
|
|
172
|
+
};
|
|
173
|
+
const regex = /\x1b\[\d+m/g;
|
|
174
|
+
const width = Math.min(process.stdout.columns, 100);
|
|
175
|
+
const dots = Math.max(width - name.replace(regex, "").length - value.replace(regex, "").length - 10, 0);
|
|
176
|
+
console.log(this.textFormat(name, color[status]), import_chalk.default.gray(".".repeat(dots)), value);
|
|
177
|
+
if (exit) process.exit(0);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Terminates the process
|
|
181
|
+
*/
|
|
182
|
+
static quiet() {
|
|
183
|
+
process.exit(0);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Parse an array formated message and logs it
|
|
187
|
+
*
|
|
188
|
+
* @param config
|
|
189
|
+
* @param joiner
|
|
190
|
+
*/
|
|
191
|
+
static parse(config, joiner = " ") {
|
|
192
|
+
const string = config.map(([str, opt]) => {
|
|
193
|
+
return typeof import_chalk.default[opt] === "function" ? import_chalk.default[opt](str) : str;
|
|
194
|
+
}).join(joiner);
|
|
195
|
+
console.log(string);
|
|
196
|
+
}
|
|
197
|
+
static log(config, joiner) {
|
|
198
|
+
if (typeof config === "string") {
|
|
199
|
+
const conf = [
|
|
200
|
+
[
|
|
201
|
+
config,
|
|
202
|
+
joiner
|
|
203
|
+
]
|
|
204
|
+
];
|
|
205
|
+
return this.parse(conf);
|
|
206
|
+
} else if (config) {
|
|
207
|
+
return this.parse(config, joiner);
|
|
208
|
+
}
|
|
209
|
+
return this;
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
62
213
|
// src/Utils/PathLoader.ts
|
|
63
214
|
var import_path = __toESM(require("path"), 1);
|
|
64
215
|
var PathLoader = class {
|
|
@@ -72,24 +223,29 @@ var PathLoader = class {
|
|
|
72
223
|
routes: "/src/routes",
|
|
73
224
|
config: "/src/config",
|
|
74
225
|
public: "/public",
|
|
75
|
-
storage: "/storage"
|
|
226
|
+
storage: "/storage",
|
|
227
|
+
database: "/src/database"
|
|
76
228
|
};
|
|
77
229
|
/**
|
|
78
230
|
* Dynamically retrieves a path property from the class.
|
|
79
231
|
* Any property ending with "Path" is accessible automatically.
|
|
80
232
|
*
|
|
81
233
|
* @param name - The base name of the path property
|
|
82
|
-
* @param
|
|
234
|
+
* @param prefix - The base path to prefix to the path
|
|
83
235
|
* @returns
|
|
84
236
|
*/
|
|
85
|
-
getPath(name,
|
|
237
|
+
getPath(name, prefix) {
|
|
86
238
|
let path;
|
|
87
|
-
if (
|
|
88
|
-
path = import_path.default.join(
|
|
239
|
+
if (prefix && name !== "base") {
|
|
240
|
+
path = import_path.default.join(prefix, this.paths[name]);
|
|
89
241
|
} else {
|
|
90
242
|
path = this.paths[name];
|
|
91
243
|
}
|
|
92
|
-
|
|
244
|
+
path = path.replace("/src/", `/${process.env.SRC_PATH ?? "src"}/`.replace(/([^:]\/)\/+/g, "$1"));
|
|
245
|
+
if (name === "database" && process.env.SRC_PATH && !"/src/".includes(process.env.SRC_PATH)) {
|
|
246
|
+
return import_path.default.resolve(path.replace(process.env.SRC_PATH, ""));
|
|
247
|
+
}
|
|
248
|
+
return path;
|
|
93
249
|
}
|
|
94
250
|
/**
|
|
95
251
|
* Programatically set the paths.
|
|
@@ -107,7 +263,9 @@ var PathLoader = class {
|
|
|
107
263
|
};
|
|
108
264
|
// Annotate the CommonJS export names for ESM import in node:
|
|
109
265
|
0 && (module.exports = {
|
|
266
|
+
EnvParser,
|
|
110
267
|
HttpContext,
|
|
268
|
+
Logger,
|
|
111
269
|
PathLoader
|
|
112
270
|
});
|
|
113
271
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/Contracts/IHttp.ts","../src/Utils/PathLoader.ts"],"sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from './Contracts/BindingsContract';\nexport * from './Contracts/IApplication';\nexport * from './Contracts/IContainer';\nexport * from './Contracts/IHttp';\nexport * from './Contracts/IRequest';\nexport * from './Contracts/IResponse';\nexport * from './Contracts/IServiceProvider';\nexport * from './Contracts/ObjContract';\nexport * from './Utils/PathLoader';\n","import type { Middleware, MiddlewareOptions } from 'h3'\n\nimport { IApplication } from './IApplication'\nimport { IRequest } from './IRequest'\nimport { IResponse } from './IResponse'\n\nexport type RouterEnd = 'get' | 'delete' | 'put' | 'post' | 'patch' | 'apiResource' | 'group' | 'route';\n\n/**\n * Interface for the Router contract, defining methods for HTTP routing.\n */\nexport interface IRouter {\n /**\n * Registers a GET route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n get (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers a POST route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n post (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers a PUT route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n put (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers a DELETE route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n delete (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers an API resource with standard CRUD routes.\n * @param path - The base path for the resource.\n * @param controller - The controller class handling the resource.\n * @param middleware - Optional middleware array.\n */\n apiResource (\n path: string,\n controller: new (app: IApplication) => IController,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd | 'name'>;\n\n /**\n * Generates a URL for a named route.\n * @param name - The name of the route.\n * @param params - Optional parameters to replace in the route path.\n * @returns The generated URL or undefined if the route is not found.\n */\n route (name: string, params?: Record<string, string>): string | undefined;\n\n\n /**\n * Set the name of the current route\n * \n * @param name \n */\n name (name: string): this\n\n /**\n * Groups routes with shared prefix or middleware.\n * @param options - Configuration for prefix or middleware.\n * @param callback - Callback function defining grouped routes.\n */\n group (options: { prefix?: string; middleware?: EventHandler[] }, callback: () => this): this;\n\n /**\n * Registers middleware for a specific path.\n * @param path - The path to apply the middleware.\n * @param handler - The middleware handler.\n * @param opts - Optional middleware options.\n */\n middleware (path: string | IMiddleware[], handler: Middleware, opts?: MiddlewareOptions): this;\n}\n\n/**\n * Represents the HTTP context for a single request lifecycle.\n * Encapsulates the application instance, request, and response objects.\n */\nexport class HttpContext {\n constructor(\n public app: IApplication,\n public request: IRequest,\n public response: IResponse\n ) { }\n\n /**\n * Factory method to create a new HttpContext instance from a context object.\n * @param ctx - Object containing app, request, and response\n * @returns A new HttpContext instance\n */\n static init (ctx: { app: IApplication; request: IRequest; response: IResponse }): HttpContext {\n /**\n * Return a new instance\n */\n return new HttpContext(ctx.app, ctx.request, ctx.response);\n }\n}\n\n\n/**\n * Type for EventHandler, representing a function that handles an H3 event.\n */\nexport type EventHandler = (ctx: HttpContext) => any\nexport type RouteEventHandler = (...args: any[]) => any\n\n/**\n * Defines the contract for all controllers.\n * Any controller implementing this must define these methods.\n */\nexport interface IController {\n show (...ctx: any[]): any\n index (...ctx: any[]): any\n store (...ctx: any[]): any\n update (...ctx: any[]): any\n destroy (...ctx: any[]): any\n}\n\n/**\n * Defines the contract for all middlewares.\n * Any middleware implementing this must define these methods.\n */\nexport interface IMiddleware {\n handle (context: HttpContext, next: () => Promise<any>): Promise<any>\n} \n","import { IPathName } from '../Contracts/IApplication'\nimport nodepath from 'path'\n\nexport class PathLoader {\n private paths = {\n base: '',\n views: '/src/resources/views',\n assets: '/public/assets',\n routes: '/src/routes',\n config: '/src/config',\n public: '/public',\n storage: '/storage',\n }\n\n /**\n * Dynamically retrieves a path property from the class.\n * Any property ending with \"Path\" is accessible automatically.\n *\n * @param name - The base name of the path property\n * @param base - The base path to include to the path\n * @returns \n */\n getPath (name: IPathName, base?: string): string {\n let path: string;\n\n if (base && name !== 'base') {\n path = nodepath.join(base, this.paths[name])\n } else {\n path = this.paths[name]\n }\n return path.replace('/src/', `/${process.env.SRC_PATH ?? 'src'}/`.replace(/([^:]\\/)\\/+/g, \"$1\"))\n }\n\n /**\n * Programatically set the paths.\n *\n * @param name - The base name of the path property\n * @param path - The new path\n * @param base - The base path to include to the path\n */\n setPath (name: IPathName, path: string, base?: string) {\n if (base && name !== 'base') {\n this.paths[name] = nodepath.join(base, path)\n }\n\n this.paths[name] = path\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;ACoHO,IAAMA,cAAN,MAAMA,aAAAA;EAJb,OAIaA;;;;;;EACT,YACWC,KACAC,SACAC,UACT;SAHSF,MAAAA;SACAC,UAAAA;SACAC,WAAAA;EACP;;;;;;EAOJ,OAAOC,KAAMC,KAAiF;AAI1F,WAAO,IAAIL,aAAYK,IAAIJ,KAAKI,IAAIH,SAASG,IAAIF,QAAQ;EAC7D;AACJ;;;ACrIA,kBAAqB;AAEd,IAAMG,aAAN,MAAMA;EAFb,OAEaA;;;EACDC,QAAQ;IACZC,MAAM;IACNC,OAAO;IACPC,QAAQ;IACRC,QAAQ;IACRC,QAAQ;IACRC,QAAQ;IACRC,SAAS;EACb;;;;;;;;;EAUAC,QAASC,MAAiBR,MAAuB;AAC7C,QAAIS;AAEJ,QAAIT,QAAQQ,SAAS,QAAQ;AACzBC,aAAOC,YAAAA,QAASC,KAAKX,MAAM,KAAKD,MAAMS,IAAAA,CAAK;IAC/C,OAAO;AACHC,aAAO,KAAKV,MAAMS,IAAAA;IACtB;AACA,WAAOC,KAAKG,QAAQ,SAAS,IAAIC,QAAQC,IAAIC,YAAY,KAAA,IAASH,QAAQ,gBAAgB,IAAA,CAAA;EAC9F;;;;;;;;EASAI,QAASR,MAAiBC,MAAcT,MAAe;AACnD,QAAIA,QAAQQ,SAAS,QAAQ;AACzB,WAAKT,MAAMS,IAAAA,IAAQE,YAAAA,QAASC,KAAKX,MAAMS,IAAAA;IAC3C;AAEA,SAAKV,MAAMS,IAAAA,IAAQC;EACvB;AACJ;","names":["HttpContext","app","request","response","init","ctx","PathLoader","paths","base","views","assets","routes","config","public","storage","getPath","name","path","nodepath","join","replace","process","env","SRC_PATH","setPath"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/Contracts/IHttp.ts","../src/Utils/EnvParser.ts","../src/Utils/Logger.ts","../src/Utils/PathLoader.ts"],"sourcesContent":["export * from './Contracts/BindingsContract'\nexport * from './Contracts/IApplication'\nexport * from './Contracts/IContainer'\nexport * from './Contracts/IHttp'\nexport * from './Contracts/IRequest'\nexport * from './Contracts/IResponse'\nexport * from './Contracts/IServiceProvider'\nexport * from './Contracts/ObjContract'\nexport * from './Utils/EnvParser'\nexport * from './Utils/Logger'\nexport * from './Utils/PathLoader'\n","import type { Middleware, MiddlewareOptions } from 'h3'\n\nimport { IApplication } from './IApplication'\nimport { IRequest } from './IRequest'\nimport { IResponse } from './IResponse'\n\nexport type RouterEnd = 'get' | 'delete' | 'put' | 'post' | 'patch' | 'apiResource' | 'group' | 'route';\n\n/**\n * Interface for the Router contract, defining methods for HTTP routing.\n */\nexport interface IRouter {\n /**\n * Registers a GET route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n get (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers a POST route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n post (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers a PUT route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n put (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers a DELETE route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n delete (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers an API resource with standard CRUD routes.\n * @param path - The base path for the resource.\n * @param controller - The controller class handling the resource.\n * @param middleware - Optional middleware array.\n */\n apiResource (\n path: string,\n controller: new (app: IApplication) => IController,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd | 'name'>;\n\n /**\n * Generates a URL for a named route.\n * @param name - The name of the route.\n * @param params - Optional parameters to replace in the route path.\n * @returns The generated URL or undefined if the route is not found.\n */\n route (name: string, params?: Record<string, string>): string | undefined;\n\n\n /**\n * Set the name of the current route\n * \n * @param name \n */\n name (name: string): this\n\n /**\n * Groups routes with shared prefix or middleware.\n * @param options - Configuration for prefix or middleware.\n * @param callback - Callback function defining grouped routes.\n */\n group (options: { prefix?: string; middleware?: EventHandler[] }, callback: () => this): this;\n\n /**\n * Registers middleware for a specific path.\n * @param path - The path to apply the middleware.\n * @param handler - The middleware handler.\n * @param opts - Optional middleware options.\n */\n middleware (path: string | IMiddleware[], handler: Middleware, opts?: MiddlewareOptions): this;\n}\n\n/**\n * Represents the HTTP context for a single request lifecycle.\n * Encapsulates the application instance, request, and response objects.\n */\nexport class HttpContext {\n constructor(\n public app: IApplication,\n public request: IRequest,\n public response: IResponse\n ) { }\n\n /**\n * Factory method to create a new HttpContext instance from a context object.\n * @param ctx - Object containing app, request, and response\n * @returns A new HttpContext instance\n */\n static init (ctx: { app: IApplication; request: IRequest; response: IResponse }): HttpContext {\n /**\n * Return a new instance\n */\n return new HttpContext(ctx.app, ctx.request, ctx.response);\n }\n}\n\n\n/**\n * Type for EventHandler, representing a function that handles an H3 event.\n */\nexport type EventHandler = (ctx: HttpContext) => any\nexport type RouteEventHandler = (...args: any[]) => any\n\n/**\n * Defines the contract for all controllers.\n * Any controller implementing this must define these methods.\n */\nexport interface IController {\n show (...ctx: any[]): any\n index (...ctx: any[]): any\n store (...ctx: any[]): any\n update (...ctx: any[]): any\n destroy (...ctx: any[]): any\n}\n\n/**\n * Defines the contract for all middlewares.\n * Any middleware implementing this must define these methods.\n */\nexport interface IMiddleware {\n handle (context: HttpContext, next: () => Promise<any>): Promise<any>\n} \n","import { GenericWithNullableStringValues } from '../Contracts/ObjContract';\n\nexport class EnvParser {\n\n static parse (initial: GenericWithNullableStringValues) {\n const parsed = { ...initial }\n\n for (const key in parsed) {\n let value: any = parsed[key]\n parsed[key] = this.parseValue(value)\n }\n\n return parsed\n }\n\n static parseValue (value: any) {\n /**\n * Null/undefined stay untouched \n */\n if (value === null || value === undefined) return value;\n\n /**\n * Convert string \"true\"/\"false\" to boolean \n */\n if (value === 'true') return true;\n if (value === 'false') return false;\n\n /**\n * Convert string numbers to number \n */\n if (!isNaN(value) && value.trim() !== '') {\n return Number(value);\n }\n\n /**\n * Convert string \"null\" and \"undefined\"\n */\n if (value === 'null') return null;\n if (value === 'undefined') return undefined;\n\n /**\n * Otherwise return as-is (string)\n */\n return value;\n }\n}\n","import chalk, { type ChalkInstance } from 'chalk'\n\nexport class Logger {\n /**\n * Logs the message in two columns\n * @param name \n * @param value \n * @returns \n */\n static twoColumnLog (name: string, value: string) {\n // eslint-disable-next-line no-control-regex\n const regex = /\\x1b\\[\\d+m/g\n const width = Math.min(process.stdout.columns, 100)\n const dots = Math.max(width - name.replace(regex, '').length - value.replace(regex, '').length - 10, 0)\n return console.log(name, chalk.gray('.'.repeat(dots)), value)\n }\n\n /**\n * Wraps text with chalk\n * \n * @param txt \n * @param color \n * @returns \n */\n static textFormat (txt: any, color: (txt: string) => string) {\n return String(txt).split(':').map((e, i, a) => i == 0 && a.length > 1 ? color(' ' + e + ': ') : e).join('')\n }\n\n /**\n * Logs a success message\n * \n * @param msg \n * @param exit \n */\n static success (msg: any, exit = false) {\n console.log(chalk.green('✓'), this.textFormat(msg, chalk.bgGreen), '\\n')\n if (exit) process.exit(0)\n }\n\n /**\n * Logs an informational message\n * \n * @param msg \n * @param exit \n */\n static info (msg: any, exit = false) {\n console.log(chalk.blue('ℹ'), this.textFormat(msg, chalk.bgBlue), '\\n')\n if (exit) process.exit(0)\n }\n\n /**\n * Logs an error message\n * \n * @param msg \n * @param exit \n */\n static error (msg: string | string[] | Error & { detail?: string }, exit = true) {\n if (msg instanceof Error) {\n if (msg.message) {\n console.error(chalk.red('✖'), this.textFormat('ERROR:' + msg.message, chalk.bgRed))\n }\n console.error(chalk.red(`${msg.detail ? `${msg.detail}\\n` : ''}${msg.stack}`), '\\n')\n }\n else {\n console.error(chalk.red('✖'), this.textFormat(msg, chalk.bgRed), '\\n')\n }\n if (exit) process.exit(1)\n }\n\n /**\n * Logs a success message\n * \n * @param name \n * @param value \n * @param status \n * @param exit \n */\n static split (name: string, value: string, status?: 'success' | 'info' | 'error', exit = false) {\n status ??= 'info'\n const color = { success: chalk.bgGreen, info: chalk.bgBlue, error: chalk.bgRed }\n const regex = /\\x1b\\[\\d+m/g\n const width = Math.min(process.stdout.columns, 100)\n const dots = Math.max(width - name.replace(regex, '').length - value.replace(regex, '').length - 10, 0)\n\n console.log(this.textFormat(name, color[status]), chalk.gray('.'.repeat(dots)), value)\n if (exit) process.exit(0)\n }\n\n /**\n * Terminates the process\n */\n static quiet () {\n process.exit(0)\n }\n\n /**\n * Parse an array formated message and logs it\n * \n * @param config \n * @param joiner \n */\n static parse (config: [string, keyof ChalkInstance][], joiner = ' ') {\n const string = config.map(([str, opt]) => {\n return typeof chalk[opt] === 'function' ? (chalk as any)[opt](str) : str\n }).join(joiner)\n\n console.log(string)\n }\n\n /**\n * Ouput formater object or format the output\n * \n * @returns \n */\n static log (): typeof Logger\n static log (config: string, joiner: keyof ChalkInstance): void\n static log (config: [string, keyof ChalkInstance][], joiner?: string): void\n static log (config?: string | [string, keyof ChalkInstance][], joiner?: string): void | Logger {\n if (typeof config === 'string') {\n const conf = [[config, joiner]] as [string, keyof ChalkInstance][]\n return this.parse(conf)\n } else if (config) {\n return this.parse(config, joiner)\n }\n return this\n }\n}\n","import { IPathName } from '../Contracts/IApplication'\nimport nodepath from 'path'\n\nexport class PathLoader {\n private paths = {\n base: '',\n views: '/src/resources/views',\n assets: '/public/assets',\n routes: '/src/routes',\n config: '/src/config',\n public: '/public',\n storage: '/storage',\n database: '/src/database',\n }\n\n /**\n * Dynamically retrieves a path property from the class.\n * Any property ending with \"Path\" is accessible automatically.\n *\n * @param name - The base name of the path property\n * @param prefix - The base path to prefix to the path\n * @returns \n */\n getPath (name: IPathName, prefix?: string): string {\n let path: string;\n\n if (prefix && name !== 'base') {\n path = nodepath.join(prefix, this.paths[name])\n } else {\n path = this.paths[name]\n }\n\n path = path.replace('/src/', `/${process.env.SRC_PATH ?? 'src'}/`.replace(/([^:]\\/)\\/+/g, \"$1\"))\n\n if (name === 'database' && process.env.SRC_PATH && !'/src/'.includes(process.env.SRC_PATH)) {\n return nodepath.resolve(path.replace(process.env.SRC_PATH, ''))\n }\n\n return path\n }\n\n /**\n * Programatically set the paths.\n *\n * @param name - The base name of the path property\n * @param path - The new path\n * @param base - The base path to include to the path\n */\n setPath (name: IPathName, path: string, base?: string) {\n if (base && name !== 'base') {\n this.paths[name] = nodepath.join(base, path)\n }\n\n this.paths[name] = path\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;ACoHO,IAAMA,cAAN,MAAMA,aAAAA;EAJb,OAIaA;;;;;;EACT,YACWC,KACAC,SACAC,UACT;SAHSF,MAAAA;SACAC,UAAAA;SACAC,WAAAA;EACP;;;;;;EAOJ,OAAOC,KAAMC,KAAiF;AAI1F,WAAO,IAAIL,aAAYK,IAAIJ,KAAKI,IAAIH,SAASG,IAAIF,QAAQ;EAC7D;AACJ;;;ACpIO,IAAMG,YAAN,MAAMA;EAAb,OAAaA;;;EAET,O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E,aAAO,KAAKZ,MAAMU,IAAAA;IACtB;AAEAE,WAAOA,KAAKG,QAAQ,SAAS,IAAIC,QAAQC,IAAIC,YAAY,KAAA,IAASH,QAAQ,gBAAgB,IAAA,CAAA;AAE1F,QAAIL,SAAS,cAAcM,QAAQC,IAAIC,YAAY,CAAC,QAAQC,SAASH,QAAQC,IAAIC,QAAQ,GAAG;AACxF,aAAOL,YAAAA,QAASO,QAAQR,KAAKG,QAAQC,QAAQC,IAAIC,UAAU,EAAA,CAAA;IAC/D;AAEA,WAAON;EACX;;;;;;;;EASAS,QAASX,MAAiBE,MAAcX,MAAe;AACnD,QAAIA,QAAQS,SAAS,QAAQ;AACzB,WAAKV,MAAMU,IAAAA,IAAQG,YAAAA,QAASC,KAAKb,MAAMW,IAAAA;IAC3C;AAEA,SAAKZ,MAAMU,IAAAA,IAAQE;EACvB;AACJ;","names":["HttpContext","app","request","response","init","ctx","EnvParser","parse","initial","parsed","key","value","parseValue","undefined","isNaN","trim","Number","Logger","twoColumnLog","name","value","regex","width","Math","min","process","stdout","columns","dots","max","replace","length","console","log","chalk","gray","repeat","textFormat","txt","color","String","split","map","e","i","a","join","success","msg","exit","green","bgGreen","info","blue","bgBlue","error","Error","message","red","bgRed","detail","stack","status","quiet","parse","config","joiner","string","str","opt","conf","PathLoader","paths","base","views","assets","routes","config","public","storage","database","getPath","name","prefix","path","nodepath","join","replace","process","env","SRC_PATH","includes","resolve","setPath"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { H3Event, Middleware, MiddlewareOptions, H3, serve } from 'h3';
|
|
2
2
|
import { Edge } from 'edge.js';
|
|
3
3
|
import { TypedHeaders, ResponseHeaderMap } from 'fetchdts';
|
|
4
|
+
import { ChalkInstance } from 'chalk';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Adds a dot prefix to nested keys
|
|
@@ -30,6 +31,12 @@ type DotNestedKeys<T> = {
|
|
|
30
31
|
* Retrieves type at a given dot-path
|
|
31
32
|
*/
|
|
32
33
|
type DotNestedValue<T, Path extends string> = Path extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? DotNestedValue<T[Key], Rest> : never : Path extends keyof T ? T[Path] : never;
|
|
34
|
+
/**
|
|
35
|
+
* A generic object type that supports nullable string values
|
|
36
|
+
*/
|
|
37
|
+
interface GenericWithNullableStringValues {
|
|
38
|
+
[name: string]: string | undefined;
|
|
39
|
+
}
|
|
33
40
|
|
|
34
41
|
/**
|
|
35
42
|
* Interface for the Container contract, defining methods for dependency injection and service resolution.
|
|
@@ -75,15 +82,15 @@ interface IServiceProvider {
|
|
|
75
82
|
* Register bindings to the container.
|
|
76
83
|
* Runs before boot().
|
|
77
84
|
*/
|
|
78
|
-
register(): void | Promise<void>;
|
|
85
|
+
register(...app: unknown[]): void | Promise<void>;
|
|
79
86
|
/**
|
|
80
87
|
* Perform post-registration booting of services.
|
|
81
88
|
* Runs after all providers have been registered.
|
|
82
89
|
*/
|
|
83
|
-
boot?(): void | Promise<void>;
|
|
90
|
+
boot?(...app: unknown[]): void | Promise<void>;
|
|
84
91
|
}
|
|
85
92
|
|
|
86
|
-
type IPathName = 'views' | 'routes' | 'assets' | 'base' | 'public' | 'storage' | 'config';
|
|
93
|
+
type IPathName = 'views' | 'routes' | 'assets' | 'base' | 'public' | 'storage' | 'config' | 'database';
|
|
87
94
|
interface IApplication extends IContainer {
|
|
88
95
|
/**
|
|
89
96
|
* Registers configured service providers.
|
|
@@ -354,10 +361,10 @@ declare class PathLoader {
|
|
|
354
361
|
* Any property ending with "Path" is accessible automatically.
|
|
355
362
|
*
|
|
356
363
|
* @param name - The base name of the path property
|
|
357
|
-
* @param
|
|
364
|
+
* @param prefix - The base path to prefix to the path
|
|
358
365
|
* @returns
|
|
359
366
|
*/
|
|
360
|
-
getPath(name: IPathName,
|
|
367
|
+
getPath(name: IPathName, prefix?: string): string;
|
|
361
368
|
/**
|
|
362
369
|
* Programatically set the paths.
|
|
363
370
|
*
|
|
@@ -395,4 +402,80 @@ type Bindings = {
|
|
|
395
402
|
};
|
|
396
403
|
type UseKey = keyof RemoveIndexSignature<Bindings>;
|
|
397
404
|
|
|
398
|
-
|
|
405
|
+
declare class EnvParser {
|
|
406
|
+
static parse(initial: GenericWithNullableStringValues): {
|
|
407
|
+
[name: string]: string | undefined;
|
|
408
|
+
};
|
|
409
|
+
static parseValue(value: any): any;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
declare class Logger {
|
|
413
|
+
/**
|
|
414
|
+
* Logs the message in two columns
|
|
415
|
+
* @param name
|
|
416
|
+
* @param value
|
|
417
|
+
* @returns
|
|
418
|
+
*/
|
|
419
|
+
static twoColumnLog(name: string, value: string): void;
|
|
420
|
+
/**
|
|
421
|
+
* Wraps text with chalk
|
|
422
|
+
*
|
|
423
|
+
* @param txt
|
|
424
|
+
* @param color
|
|
425
|
+
* @returns
|
|
426
|
+
*/
|
|
427
|
+
static textFormat(txt: any, color: (txt: string) => string): string;
|
|
428
|
+
/**
|
|
429
|
+
* Logs a success message
|
|
430
|
+
*
|
|
431
|
+
* @param msg
|
|
432
|
+
* @param exit
|
|
433
|
+
*/
|
|
434
|
+
static success(msg: any, exit?: boolean): void;
|
|
435
|
+
/**
|
|
436
|
+
* Logs an informational message
|
|
437
|
+
*
|
|
438
|
+
* @param msg
|
|
439
|
+
* @param exit
|
|
440
|
+
*/
|
|
441
|
+
static info(msg: any, exit?: boolean): void;
|
|
442
|
+
/**
|
|
443
|
+
* Logs an error message
|
|
444
|
+
*
|
|
445
|
+
* @param msg
|
|
446
|
+
* @param exit
|
|
447
|
+
*/
|
|
448
|
+
static error(msg: string | string[] | Error & {
|
|
449
|
+
detail?: string;
|
|
450
|
+
}, exit?: boolean): void;
|
|
451
|
+
/**
|
|
452
|
+
* Logs a success message
|
|
453
|
+
*
|
|
454
|
+
* @param name
|
|
455
|
+
* @param value
|
|
456
|
+
* @param status
|
|
457
|
+
* @param exit
|
|
458
|
+
*/
|
|
459
|
+
static split(name: string, value: string, status?: 'success' | 'info' | 'error', exit?: boolean): void;
|
|
460
|
+
/**
|
|
461
|
+
* Terminates the process
|
|
462
|
+
*/
|
|
463
|
+
static quiet(): void;
|
|
464
|
+
/**
|
|
465
|
+
* Parse an array formated message and logs it
|
|
466
|
+
*
|
|
467
|
+
* @param config
|
|
468
|
+
* @param joiner
|
|
469
|
+
*/
|
|
470
|
+
static parse(config: [string, keyof ChalkInstance][], joiner?: string): void;
|
|
471
|
+
/**
|
|
472
|
+
* Ouput formater object or format the output
|
|
473
|
+
*
|
|
474
|
+
* @returns
|
|
475
|
+
*/
|
|
476
|
+
static log(): typeof Logger;
|
|
477
|
+
static log(config: string, joiner: keyof ChalkInstance): void;
|
|
478
|
+
static log(config: [string, keyof ChalkInstance][], joiner?: string): void;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
export { type Bindings, type DotFlatten, type DotNestedKeys, type DotNestedValue, EnvParser, type EventHandler, type GenericWithNullableStringValues, HttpContext, type IApplication, type IContainer, type IController, type IMiddleware, type IPathName, type IRequest, type IResponse, type IRouter, type IServiceProvider, Logger, PathLoader, type RouteEventHandler, type RouterEnd, type UseKey };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { H3Event, Middleware, MiddlewareOptions, H3, serve } from 'h3';
|
|
2
2
|
import { Edge } from 'edge.js';
|
|
3
3
|
import { TypedHeaders, ResponseHeaderMap } from 'fetchdts';
|
|
4
|
+
import { ChalkInstance } from 'chalk';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Adds a dot prefix to nested keys
|
|
@@ -30,6 +31,12 @@ type DotNestedKeys<T> = {
|
|
|
30
31
|
* Retrieves type at a given dot-path
|
|
31
32
|
*/
|
|
32
33
|
type DotNestedValue<T, Path extends string> = Path extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? DotNestedValue<T[Key], Rest> : never : Path extends keyof T ? T[Path] : never;
|
|
34
|
+
/**
|
|
35
|
+
* A generic object type that supports nullable string values
|
|
36
|
+
*/
|
|
37
|
+
interface GenericWithNullableStringValues {
|
|
38
|
+
[name: string]: string | undefined;
|
|
39
|
+
}
|
|
33
40
|
|
|
34
41
|
/**
|
|
35
42
|
* Interface for the Container contract, defining methods for dependency injection and service resolution.
|
|
@@ -75,15 +82,15 @@ interface IServiceProvider {
|
|
|
75
82
|
* Register bindings to the container.
|
|
76
83
|
* Runs before boot().
|
|
77
84
|
*/
|
|
78
|
-
register(): void | Promise<void>;
|
|
85
|
+
register(...app: unknown[]): void | Promise<void>;
|
|
79
86
|
/**
|
|
80
87
|
* Perform post-registration booting of services.
|
|
81
88
|
* Runs after all providers have been registered.
|
|
82
89
|
*/
|
|
83
|
-
boot?(): void | Promise<void>;
|
|
90
|
+
boot?(...app: unknown[]): void | Promise<void>;
|
|
84
91
|
}
|
|
85
92
|
|
|
86
|
-
type IPathName = 'views' | 'routes' | 'assets' | 'base' | 'public' | 'storage' | 'config';
|
|
93
|
+
type IPathName = 'views' | 'routes' | 'assets' | 'base' | 'public' | 'storage' | 'config' | 'database';
|
|
87
94
|
interface IApplication extends IContainer {
|
|
88
95
|
/**
|
|
89
96
|
* Registers configured service providers.
|
|
@@ -354,10 +361,10 @@ declare class PathLoader {
|
|
|
354
361
|
* Any property ending with "Path" is accessible automatically.
|
|
355
362
|
*
|
|
356
363
|
* @param name - The base name of the path property
|
|
357
|
-
* @param
|
|
364
|
+
* @param prefix - The base path to prefix to the path
|
|
358
365
|
* @returns
|
|
359
366
|
*/
|
|
360
|
-
getPath(name: IPathName,
|
|
367
|
+
getPath(name: IPathName, prefix?: string): string;
|
|
361
368
|
/**
|
|
362
369
|
* Programatically set the paths.
|
|
363
370
|
*
|
|
@@ -395,4 +402,80 @@ type Bindings = {
|
|
|
395
402
|
};
|
|
396
403
|
type UseKey = keyof RemoveIndexSignature<Bindings>;
|
|
397
404
|
|
|
398
|
-
|
|
405
|
+
declare class EnvParser {
|
|
406
|
+
static parse(initial: GenericWithNullableStringValues): {
|
|
407
|
+
[name: string]: string | undefined;
|
|
408
|
+
};
|
|
409
|
+
static parseValue(value: any): any;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
declare class Logger {
|
|
413
|
+
/**
|
|
414
|
+
* Logs the message in two columns
|
|
415
|
+
* @param name
|
|
416
|
+
* @param value
|
|
417
|
+
* @returns
|
|
418
|
+
*/
|
|
419
|
+
static twoColumnLog(name: string, value: string): void;
|
|
420
|
+
/**
|
|
421
|
+
* Wraps text with chalk
|
|
422
|
+
*
|
|
423
|
+
* @param txt
|
|
424
|
+
* @param color
|
|
425
|
+
* @returns
|
|
426
|
+
*/
|
|
427
|
+
static textFormat(txt: any, color: (txt: string) => string): string;
|
|
428
|
+
/**
|
|
429
|
+
* Logs a success message
|
|
430
|
+
*
|
|
431
|
+
* @param msg
|
|
432
|
+
* @param exit
|
|
433
|
+
*/
|
|
434
|
+
static success(msg: any, exit?: boolean): void;
|
|
435
|
+
/**
|
|
436
|
+
* Logs an informational message
|
|
437
|
+
*
|
|
438
|
+
* @param msg
|
|
439
|
+
* @param exit
|
|
440
|
+
*/
|
|
441
|
+
static info(msg: any, exit?: boolean): void;
|
|
442
|
+
/**
|
|
443
|
+
* Logs an error message
|
|
444
|
+
*
|
|
445
|
+
* @param msg
|
|
446
|
+
* @param exit
|
|
447
|
+
*/
|
|
448
|
+
static error(msg: string | string[] | Error & {
|
|
449
|
+
detail?: string;
|
|
450
|
+
}, exit?: boolean): void;
|
|
451
|
+
/**
|
|
452
|
+
* Logs a success message
|
|
453
|
+
*
|
|
454
|
+
* @param name
|
|
455
|
+
* @param value
|
|
456
|
+
* @param status
|
|
457
|
+
* @param exit
|
|
458
|
+
*/
|
|
459
|
+
static split(name: string, value: string, status?: 'success' | 'info' | 'error', exit?: boolean): void;
|
|
460
|
+
/**
|
|
461
|
+
* Terminates the process
|
|
462
|
+
*/
|
|
463
|
+
static quiet(): void;
|
|
464
|
+
/**
|
|
465
|
+
* Parse an array formated message and logs it
|
|
466
|
+
*
|
|
467
|
+
* @param config
|
|
468
|
+
* @param joiner
|
|
469
|
+
*/
|
|
470
|
+
static parse(config: [string, keyof ChalkInstance][], joiner?: string): void;
|
|
471
|
+
/**
|
|
472
|
+
* Ouput formater object or format the output
|
|
473
|
+
*
|
|
474
|
+
* @returns
|
|
475
|
+
*/
|
|
476
|
+
static log(): typeof Logger;
|
|
477
|
+
static log(config: string, joiner: keyof ChalkInstance): void;
|
|
478
|
+
static log(config: [string, keyof ChalkInstance][], joiner?: string): void;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
export { type Bindings, type DotFlatten, type DotNestedKeys, type DotNestedValue, EnvParser, type EventHandler, type GenericWithNullableStringValues, HttpContext, type IApplication, type IContainer, type IController, type IMiddleware, type IPathName, type IRequest, type IResponse, type IRouter, type IServiceProvider, Logger, PathLoader, type RouteEventHandler, type RouterEnd, type UseKey };
|
package/dist/index.js
CHANGED
|
@@ -24,6 +24,155 @@ var HttpContext = class _HttpContext {
|
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
+
// src/Utils/EnvParser.ts
|
|
28
|
+
var EnvParser = class {
|
|
29
|
+
static {
|
|
30
|
+
__name(this, "EnvParser");
|
|
31
|
+
}
|
|
32
|
+
static parse(initial) {
|
|
33
|
+
const parsed = {
|
|
34
|
+
...initial
|
|
35
|
+
};
|
|
36
|
+
for (const key in parsed) {
|
|
37
|
+
let value = parsed[key];
|
|
38
|
+
parsed[key] = this.parseValue(value);
|
|
39
|
+
}
|
|
40
|
+
return parsed;
|
|
41
|
+
}
|
|
42
|
+
static parseValue(value) {
|
|
43
|
+
if (value === null || value === void 0) return value;
|
|
44
|
+
if (value === "true") return true;
|
|
45
|
+
if (value === "false") return false;
|
|
46
|
+
if (!isNaN(value) && value.trim() !== "") {
|
|
47
|
+
return Number(value);
|
|
48
|
+
}
|
|
49
|
+
if (value === "null") return null;
|
|
50
|
+
if (value === "undefined") return void 0;
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// src/Utils/Logger.ts
|
|
56
|
+
import chalk from "chalk";
|
|
57
|
+
var Logger = class {
|
|
58
|
+
static {
|
|
59
|
+
__name(this, "Logger");
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Logs the message in two columns
|
|
63
|
+
* @param name
|
|
64
|
+
* @param value
|
|
65
|
+
* @returns
|
|
66
|
+
*/
|
|
67
|
+
static twoColumnLog(name, value) {
|
|
68
|
+
const regex = /\x1b\[\d+m/g;
|
|
69
|
+
const width = Math.min(process.stdout.columns, 100);
|
|
70
|
+
const dots = Math.max(width - name.replace(regex, "").length - value.replace(regex, "").length - 10, 0);
|
|
71
|
+
return console.log(name, chalk.gray(".".repeat(dots)), value);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Wraps text with chalk
|
|
75
|
+
*
|
|
76
|
+
* @param txt
|
|
77
|
+
* @param color
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
static textFormat(txt, color) {
|
|
81
|
+
return String(txt).split(":").map((e, i, a) => i == 0 && a.length > 1 ? color(" " + e + ": ") : e).join("");
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Logs a success message
|
|
85
|
+
*
|
|
86
|
+
* @param msg
|
|
87
|
+
* @param exit
|
|
88
|
+
*/
|
|
89
|
+
static success(msg, exit = false) {
|
|
90
|
+
console.log(chalk.green("\u2713"), this.textFormat(msg, chalk.bgGreen), "\n");
|
|
91
|
+
if (exit) process.exit(0);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Logs an informational message
|
|
95
|
+
*
|
|
96
|
+
* @param msg
|
|
97
|
+
* @param exit
|
|
98
|
+
*/
|
|
99
|
+
static info(msg, exit = false) {
|
|
100
|
+
console.log(chalk.blue("\u2139"), this.textFormat(msg, chalk.bgBlue), "\n");
|
|
101
|
+
if (exit) process.exit(0);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Logs an error message
|
|
105
|
+
*
|
|
106
|
+
* @param msg
|
|
107
|
+
* @param exit
|
|
108
|
+
*/
|
|
109
|
+
static error(msg, exit = true) {
|
|
110
|
+
if (msg instanceof Error) {
|
|
111
|
+
if (msg.message) {
|
|
112
|
+
console.error(chalk.red("\u2716"), this.textFormat("ERROR:" + msg.message, chalk.bgRed));
|
|
113
|
+
}
|
|
114
|
+
console.error(chalk.red(`${msg.detail ? `${msg.detail}
|
|
115
|
+
` : ""}${msg.stack}`), "\n");
|
|
116
|
+
} else {
|
|
117
|
+
console.error(chalk.red("\u2716"), this.textFormat(msg, chalk.bgRed), "\n");
|
|
118
|
+
}
|
|
119
|
+
if (exit) process.exit(1);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Logs a success message
|
|
123
|
+
*
|
|
124
|
+
* @param name
|
|
125
|
+
* @param value
|
|
126
|
+
* @param status
|
|
127
|
+
* @param exit
|
|
128
|
+
*/
|
|
129
|
+
static split(name, value, status, exit = false) {
|
|
130
|
+
status ??= "info";
|
|
131
|
+
const color = {
|
|
132
|
+
success: chalk.bgGreen,
|
|
133
|
+
info: chalk.bgBlue,
|
|
134
|
+
error: chalk.bgRed
|
|
135
|
+
};
|
|
136
|
+
const regex = /\x1b\[\d+m/g;
|
|
137
|
+
const width = Math.min(process.stdout.columns, 100);
|
|
138
|
+
const dots = Math.max(width - name.replace(regex, "").length - value.replace(regex, "").length - 10, 0);
|
|
139
|
+
console.log(this.textFormat(name, color[status]), chalk.gray(".".repeat(dots)), value);
|
|
140
|
+
if (exit) process.exit(0);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Terminates the process
|
|
144
|
+
*/
|
|
145
|
+
static quiet() {
|
|
146
|
+
process.exit(0);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Parse an array formated message and logs it
|
|
150
|
+
*
|
|
151
|
+
* @param config
|
|
152
|
+
* @param joiner
|
|
153
|
+
*/
|
|
154
|
+
static parse(config, joiner = " ") {
|
|
155
|
+
const string = config.map(([str, opt]) => {
|
|
156
|
+
return typeof chalk[opt] === "function" ? chalk[opt](str) : str;
|
|
157
|
+
}).join(joiner);
|
|
158
|
+
console.log(string);
|
|
159
|
+
}
|
|
160
|
+
static log(config, joiner) {
|
|
161
|
+
if (typeof config === "string") {
|
|
162
|
+
const conf = [
|
|
163
|
+
[
|
|
164
|
+
config,
|
|
165
|
+
joiner
|
|
166
|
+
]
|
|
167
|
+
];
|
|
168
|
+
return this.parse(conf);
|
|
169
|
+
} else if (config) {
|
|
170
|
+
return this.parse(config, joiner);
|
|
171
|
+
}
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
27
176
|
// src/Utils/PathLoader.ts
|
|
28
177
|
import nodepath from "path";
|
|
29
178
|
var PathLoader = class {
|
|
@@ -37,24 +186,29 @@ var PathLoader = class {
|
|
|
37
186
|
routes: "/src/routes",
|
|
38
187
|
config: "/src/config",
|
|
39
188
|
public: "/public",
|
|
40
|
-
storage: "/storage"
|
|
189
|
+
storage: "/storage",
|
|
190
|
+
database: "/src/database"
|
|
41
191
|
};
|
|
42
192
|
/**
|
|
43
193
|
* Dynamically retrieves a path property from the class.
|
|
44
194
|
* Any property ending with "Path" is accessible automatically.
|
|
45
195
|
*
|
|
46
196
|
* @param name - The base name of the path property
|
|
47
|
-
* @param
|
|
197
|
+
* @param prefix - The base path to prefix to the path
|
|
48
198
|
* @returns
|
|
49
199
|
*/
|
|
50
|
-
getPath(name,
|
|
200
|
+
getPath(name, prefix) {
|
|
51
201
|
let path;
|
|
52
|
-
if (
|
|
53
|
-
path = nodepath.join(
|
|
202
|
+
if (prefix && name !== "base") {
|
|
203
|
+
path = nodepath.join(prefix, this.paths[name]);
|
|
54
204
|
} else {
|
|
55
205
|
path = this.paths[name];
|
|
56
206
|
}
|
|
57
|
-
|
|
207
|
+
path = path.replace("/src/", `/${process.env.SRC_PATH ?? "src"}/`.replace(/([^:]\/)\/+/g, "$1"));
|
|
208
|
+
if (name === "database" && process.env.SRC_PATH && !"/src/".includes(process.env.SRC_PATH)) {
|
|
209
|
+
return nodepath.resolve(path.replace(process.env.SRC_PATH, ""));
|
|
210
|
+
}
|
|
211
|
+
return path;
|
|
58
212
|
}
|
|
59
213
|
/**
|
|
60
214
|
* Programatically set the paths.
|
|
@@ -71,7 +225,9 @@ var PathLoader = class {
|
|
|
71
225
|
}
|
|
72
226
|
};
|
|
73
227
|
export {
|
|
228
|
+
EnvParser,
|
|
74
229
|
HttpContext,
|
|
230
|
+
Logger,
|
|
75
231
|
PathLoader
|
|
76
232
|
};
|
|
77
233
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/Contracts/IHttp.ts","../src/Utils/PathLoader.ts"],"sourcesContent":["import type { Middleware, MiddlewareOptions } from 'h3'\n\nimport { IApplication } from './IApplication'\nimport { IRequest } from './IRequest'\nimport { IResponse } from './IResponse'\n\nexport type RouterEnd = 'get' | 'delete' | 'put' | 'post' | 'patch' | 'apiResource' | 'group' | 'route';\n\n/**\n * Interface for the Router contract, defining methods for HTTP routing.\n */\nexport interface IRouter {\n /**\n * Registers a GET route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n get (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers a POST route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n post (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers a PUT route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n put (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers a DELETE route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n delete (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers an API resource with standard CRUD routes.\n * @param path - The base path for the resource.\n * @param controller - The controller class handling the resource.\n * @param middleware - Optional middleware array.\n */\n apiResource (\n path: string,\n controller: new (app: IApplication) => IController,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd | 'name'>;\n\n /**\n * Generates a URL for a named route.\n * @param name - The name of the route.\n * @param params - Optional parameters to replace in the route path.\n * @returns The generated URL or undefined if the route is not found.\n */\n route (name: string, params?: Record<string, string>): string | undefined;\n\n\n /**\n * Set the name of the current route\n * \n * @param name \n */\n name (name: string): this\n\n /**\n * Groups routes with shared prefix or middleware.\n * @param options - Configuration for prefix or middleware.\n * @param callback - Callback function defining grouped routes.\n */\n group (options: { prefix?: string; middleware?: EventHandler[] }, callback: () => this): this;\n\n /**\n * Registers middleware for a specific path.\n * @param path - The path to apply the middleware.\n * @param handler - The middleware handler.\n * @param opts - Optional middleware options.\n */\n middleware (path: string | IMiddleware[], handler: Middleware, opts?: MiddlewareOptions): this;\n}\n\n/**\n * Represents the HTTP context for a single request lifecycle.\n * Encapsulates the application instance, request, and response objects.\n */\nexport class HttpContext {\n constructor(\n public app: IApplication,\n public request: IRequest,\n public response: IResponse\n ) { }\n\n /**\n * Factory method to create a new HttpContext instance from a context object.\n * @param ctx - Object containing app, request, and response\n * @returns A new HttpContext instance\n */\n static init (ctx: { app: IApplication; request: IRequest; response: IResponse }): HttpContext {\n /**\n * Return a new instance\n */\n return new HttpContext(ctx.app, ctx.request, ctx.response);\n }\n}\n\n\n/**\n * Type for EventHandler, representing a function that handles an H3 event.\n */\nexport type EventHandler = (ctx: HttpContext) => any\nexport type RouteEventHandler = (...args: any[]) => any\n\n/**\n * Defines the contract for all controllers.\n * Any controller implementing this must define these methods.\n */\nexport interface IController {\n show (...ctx: any[]): any\n index (...ctx: any[]): any\n store (...ctx: any[]): any\n update (...ctx: any[]): any\n destroy (...ctx: any[]): any\n}\n\n/**\n * Defines the contract for all middlewares.\n * Any middleware implementing this must define these methods.\n */\nexport interface IMiddleware {\n handle (context: HttpContext, next: () => Promise<any>): Promise<any>\n} \n","import { IPathName } from '../Contracts/IApplication'\nimport nodepath from 'path'\n\nexport class PathLoader {\n private paths = {\n base: '',\n views: '/src/resources/views',\n assets: '/public/assets',\n routes: '/src/routes',\n config: '/src/config',\n public: '/public',\n storage: '/storage',\n }\n\n /**\n * Dynamically retrieves a path property from the class.\n * Any property ending with \"Path\" is accessible automatically.\n *\n * @param name - The base name of the path property\n * @param base - The base path to include to the path\n * @returns \n */\n getPath (name: IPathName, base?: string): string {\n let path: string;\n\n if (base && name !== 'base') {\n path = nodepath.join(base, this.paths[name])\n } else {\n path = this.paths[name]\n }\n return path.replace('/src/', `/${process.env.SRC_PATH ?? 'src'}/`.replace(/([^:]\\/)\\/+/g, \"$1\"))\n }\n\n /**\n * Programatically set the paths.\n *\n * @param name - The base name of the path property\n * @param path - The new path\n * @param base - The base path to include to the path\n */\n setPath (name: IPathName, path: string, base?: string) {\n if (base && name !== 'base') {\n this.paths[name] = nodepath.join(base, path)\n }\n\n this.paths[name] = path\n }\n}\n"],"mappings":";;;;AAoHO,IAAMA,cAAN,MAAMA,aAAAA;EAJb,OAIaA;;;;;;EACT,YACWC,KACAC,SACAC,UACT;SAHSF,MAAAA;SACAC,UAAAA;SACAC,WAAAA;EACP;;;;;;EAOJ,OAAOC,KAAMC,KAAiF;AAI1F,WAAO,IAAIL,aAAYK,IAAIJ,KAAKI,IAAIH,SAASG,IAAIF,QAAQ;EAC7D;AACJ;;;ACrIA,OAAOG,cAAc;AAEd,IAAMC,aAAN,MAAMA;EAFb,OAEaA;;;EACDC,QAAQ;IACZC,MAAM;IACNC,OAAO;IACPC,QAAQ;IACRC,QAAQ;IACRC,QAAQ;IACRC,QAAQ;IACRC,SAAS;EACb;;;;;;;;;EAUAC,QAASC,MAAiBR,MAAuB;AAC7C,QAAIS;AAEJ,QAAIT,QAAQQ,SAAS,QAAQ;AACzBC,aAAOC,SAASC,KAAKX,MAAM,KAAKD,MAAMS,IAAAA,CAAK;IAC/C,OAAO;AACHC,aAAO,KAAKV,MAAMS,IAAAA;IACtB;AACA,WAAOC,KAAKG,QAAQ,SAAS,IAAIC,QAAQC,IAAIC,YAAY,KAAA,IAASH,QAAQ,gBAAgB,IAAA,CAAA;EAC9F;;;;;;;;EASAI,QAASR,MAAiBC,MAAcT,MAAe;AACnD,QAAIA,QAAQQ,SAAS,QAAQ;AACzB,WAAKT,MAAMS,IAAAA,IAAQE,SAASC,KAAKX,MAAMS,IAAAA;IAC3C;AAEA,SAAKV,MAAMS,IAAAA,IAAQC;EACvB;AACJ;","names":["HttpContext","app","request","response","init","ctx","nodepath","PathLoader","paths","base","views","assets","routes","config","public","storage","getPath","name","path","nodepath","join","replace","process","env","SRC_PATH","setPath"]}
|
|
1
|
+
{"version":3,"sources":["../src/Contracts/IHttp.ts","../src/Utils/EnvParser.ts","../src/Utils/Logger.ts","../src/Utils/PathLoader.ts"],"sourcesContent":["import type { Middleware, MiddlewareOptions } from 'h3'\n\nimport { IApplication } from './IApplication'\nimport { IRequest } from './IRequest'\nimport { IResponse } from './IResponse'\n\nexport type RouterEnd = 'get' | 'delete' | 'put' | 'post' | 'patch' | 'apiResource' | 'group' | 'route';\n\n/**\n * Interface for the Router contract, defining methods for HTTP routing.\n */\nexport interface IRouter {\n /**\n * Registers a GET route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n get (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers a POST route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n post (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers a PUT route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n put (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers a DELETE route.\n * @param path - The route path.\n * @param definition - The handler function or [controller class, method] array.\n * @param name - Optional route name.\n * @param middleware - Optional middleware array.\n */\n delete (\n path: string,\n definition: EventHandler | [(new (...args: any[]) => IController), methodName: string],\n name?: string,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd>;\n\n /**\n * Registers an API resource with standard CRUD routes.\n * @param path - The base path for the resource.\n * @param controller - The controller class handling the resource.\n * @param middleware - Optional middleware array.\n */\n apiResource (\n path: string,\n controller: new (app: IApplication) => IController,\n middleware?: IMiddleware[]\n ): Omit<this, RouterEnd | 'name'>;\n\n /**\n * Generates a URL for a named route.\n * @param name - The name of the route.\n * @param params - Optional parameters to replace in the route path.\n * @returns The generated URL or undefined if the route is not found.\n */\n route (name: string, params?: Record<string, string>): string | undefined;\n\n\n /**\n * Set the name of the current route\n * \n * @param name \n */\n name (name: string): this\n\n /**\n * Groups routes with shared prefix or middleware.\n * @param options - Configuration for prefix or middleware.\n * @param callback - Callback function defining grouped routes.\n */\n group (options: { prefix?: string; middleware?: EventHandler[] }, callback: () => this): this;\n\n /**\n * Registers middleware for a specific path.\n * @param path - The path to apply the middleware.\n * @param handler - The middleware handler.\n * @param opts - Optional middleware options.\n */\n middleware (path: string | IMiddleware[], handler: Middleware, opts?: MiddlewareOptions): this;\n}\n\n/**\n * Represents the HTTP context for a single request lifecycle.\n * Encapsulates the application instance, request, and response objects.\n */\nexport class HttpContext {\n constructor(\n public app: IApplication,\n public request: IRequest,\n public response: IResponse\n ) { }\n\n /**\n * Factory method to create a new HttpContext instance from a context object.\n * @param ctx - Object containing app, request, and response\n * @returns A new HttpContext instance\n */\n static init (ctx: { app: IApplication; request: IRequest; response: IResponse }): HttpContext {\n /**\n * Return a new instance\n */\n return new HttpContext(ctx.app, ctx.request, ctx.response);\n }\n}\n\n\n/**\n * Type for EventHandler, representing a function that handles an H3 event.\n */\nexport type EventHandler = (ctx: HttpContext) => any\nexport type RouteEventHandler = (...args: any[]) => any\n\n/**\n * Defines the contract for all controllers.\n * Any controller implementing this must define these methods.\n */\nexport interface IController {\n show (...ctx: any[]): any\n index (...ctx: any[]): any\n store (...ctx: any[]): any\n update (...ctx: any[]): any\n destroy (...ctx: any[]): any\n}\n\n/**\n * Defines the contract for all middlewares.\n * Any middleware implementing this must define these methods.\n */\nexport interface IMiddleware {\n handle (context: HttpContext, next: () => Promise<any>): Promise<any>\n} \n","import { GenericWithNullableStringValues } from '../Contracts/ObjContract';\n\nexport class EnvParser {\n\n static parse (initial: GenericWithNullableStringValues) {\n const parsed = { ...initial }\n\n for (const key in parsed) {\n let value: any = parsed[key]\n parsed[key] = this.parseValue(value)\n }\n\n return parsed\n }\n\n static parseValue (value: any) {\n /**\n * Null/undefined stay untouched \n */\n if (value === null || value === undefined) return value;\n\n /**\n * Convert string \"true\"/\"false\" to boolean \n */\n if (value === 'true') return true;\n if (value === 'false') return false;\n\n /**\n * Convert string numbers to number \n */\n if (!isNaN(value) && value.trim() !== '') {\n return Number(value);\n }\n\n /**\n * Convert string \"null\" and \"undefined\"\n */\n if (value === 'null') return null;\n if (value === 'undefined') return undefined;\n\n /**\n * Otherwise return as-is (string)\n */\n return value;\n }\n}\n","import chalk, { type ChalkInstance } from 'chalk'\n\nexport class Logger {\n /**\n * Logs the message in two columns\n * @param name \n * @param value \n * @returns \n */\n static twoColumnLog (name: string, value: string) {\n // eslint-disable-next-line no-control-regex\n const regex = /\\x1b\\[\\d+m/g\n const width = Math.min(process.stdout.columns, 100)\n const dots = Math.max(width - name.replace(regex, '').length - value.replace(regex, '').length - 10, 0)\n return console.log(name, chalk.gray('.'.repeat(dots)), value)\n }\n\n /**\n * Wraps text with chalk\n * \n * @param txt \n * @param color \n * @returns \n */\n static textFormat (txt: any, color: (txt: string) => string) {\n return String(txt).split(':').map((e, i, a) => i == 0 && a.length > 1 ? color(' ' + e + ': ') : e).join('')\n }\n\n /**\n * Logs a success message\n * \n * @param msg \n * @param exit \n */\n static success (msg: any, exit = false) {\n console.log(chalk.green('✓'), this.textFormat(msg, chalk.bgGreen), '\\n')\n if (exit) process.exit(0)\n }\n\n /**\n * Logs an informational message\n * \n * @param msg \n * @param exit \n */\n static info (msg: any, exit = false) {\n console.log(chalk.blue('ℹ'), this.textFormat(msg, chalk.bgBlue), '\\n')\n if (exit) process.exit(0)\n }\n\n /**\n * Logs an error message\n * \n * @param msg \n * @param exit \n */\n static error (msg: string | string[] | Error & { detail?: string }, exit = true) {\n if (msg instanceof Error) {\n if (msg.message) {\n console.error(chalk.red('✖'), this.textFormat('ERROR:' + msg.message, chalk.bgRed))\n }\n console.error(chalk.red(`${msg.detail ? `${msg.detail}\\n` : ''}${msg.stack}`), '\\n')\n }\n else {\n console.error(chalk.red('✖'), this.textFormat(msg, chalk.bgRed), '\\n')\n }\n if (exit) process.exit(1)\n }\n\n /**\n * Logs a success message\n * \n * @param name \n * @param value \n * @param status \n * @param exit \n */\n static split (name: string, value: string, status?: 'success' | 'info' | 'error', exit = false) {\n status ??= 'info'\n const color = { success: chalk.bgGreen, info: chalk.bgBlue, error: chalk.bgRed }\n const regex = /\\x1b\\[\\d+m/g\n const width = Math.min(process.stdout.columns, 100)\n const dots = Math.max(width - name.replace(regex, '').length - value.replace(regex, '').length - 10, 0)\n\n console.log(this.textFormat(name, color[status]), chalk.gray('.'.repeat(dots)), value)\n if (exit) process.exit(0)\n }\n\n /**\n * Terminates the process\n */\n static quiet () {\n process.exit(0)\n }\n\n /**\n * Parse an array formated message and logs it\n * \n * @param config \n * @param joiner \n */\n static parse (config: [string, keyof ChalkInstance][], joiner = ' ') {\n const string = config.map(([str, opt]) => {\n return typeof chalk[opt] === 'function' ? (chalk as any)[opt](str) : str\n }).join(joiner)\n\n console.log(string)\n }\n\n /**\n * Ouput formater object or format the output\n * \n * @returns \n */\n static log (): typeof Logger\n static log (config: string, joiner: keyof ChalkInstance): void\n static log (config: [string, keyof ChalkInstance][], joiner?: string): void\n static log (config?: string | [string, keyof ChalkInstance][], joiner?: string): void | Logger {\n if (typeof config === 'string') {\n const conf = [[config, joiner]] as [string, keyof ChalkInstance][]\n return this.parse(conf)\n } else if (config) {\n return this.parse(config, joiner)\n }\n return this\n }\n}\n","import { IPathName } from '../Contracts/IApplication'\nimport nodepath from 'path'\n\nexport class PathLoader {\n private paths = {\n base: '',\n views: '/src/resources/views',\n assets: '/public/assets',\n routes: '/src/routes',\n config: '/src/config',\n public: '/public',\n storage: '/storage',\n database: '/src/database',\n }\n\n /**\n * Dynamically retrieves a path property from the class.\n * Any property ending with \"Path\" is accessible automatically.\n *\n * @param name - The base name of the path property\n * @param prefix - The base path to prefix to the path\n * @returns \n */\n getPath (name: IPathName, prefix?: string): string {\n let path: string;\n\n if (prefix && name !== 'base') {\n path = nodepath.join(prefix, this.paths[name])\n } else {\n path = this.paths[name]\n }\n\n path = path.replace('/src/', `/${process.env.SRC_PATH ?? 'src'}/`.replace(/([^:]\\/)\\/+/g, \"$1\"))\n\n if (name === 'database' && process.env.SRC_PATH && !'/src/'.includes(process.env.SRC_PATH)) {\n return nodepath.resolve(path.replace(process.env.SRC_PATH, ''))\n }\n\n return path\n }\n\n /**\n * Programatically set the paths.\n *\n * @param name - The base name of the path property\n * @param path - The new path\n * @param base - The base path to include to the path\n */\n setPath (name: IPathName, path: string, base?: string) {\n if (base && name !== 'base') {\n this.paths[name] = nodepath.join(base, path)\n }\n\n this.paths[name] = path\n }\n}\n"],"mappings":";;;;AAoHO,IAAMA,cAAN,MAAMA,aAAAA;EAJb,OAIaA;;;;;;EACT,YACWC,KACAC,SACAC,UACT;SAHSF,MAAAA;SACAC,UAAAA;SACAC,WAAAA;EACP;;;;;;EAOJ,OAAOC,KAAMC,KAAiF;AAI1F,WAAO,IAAIL,aAAYK,IAAIJ,KAAKI,IAAIH,SAASG,IAAIF,QAAQ;EAC7D;AACJ;;;ACpIO,IAAMG,YAAN,MAAMA;EAAb,OAAaA;;;EAET,OAAOC,MAAOC,SAA0C;AACpD,UAAMC,SAAS;MAAE,GAAGD;IAAQ;AAE5B,eAAWE,OAAOD,QAAQ;AACtB,UAAIE,QAAaF,OAAOC,GAAAA;AACxBD,aAAOC,GAAAA,IAAO,KAAKE,WAAWD,KAAAA;IAClC;AAEA,WAAOF;EACX;EAEA,OAAOG,WAAYD,OAAY;AAI3B,QAAIA,UAAU,QAAQA,UAAUE,OAAW,QAAOF;AAKlD,QAAIA,UAAU,OAAQ,QAAO;AAC7B,QAAIA,UAAU,QAAS,QAAO;AAK9B,QAAI,CAACG,MAAMH,KAAAA,KAAUA,MAAMI,KAAI,MAAO,IAAI;AACtC,aAAOC,OAAOL,KAAAA;IAClB;AAKA,QAAIA,UAAU,OAAQ,QAAO;AAC7B,QAAIA,UAAU,YAAa,QAAOE;AAKlC,WAAOF;EACX;AACJ;;;AC7CA,OAAOM,WAAmC;AAEnC,IAAMC,SAAN,MAAMA;EAFb,OAEaA;;;;;;;;;EAOT,OAAOC,aAAcC,MAAcC,OAAe;AAE9C,UAAMC,QAAQ;AACd,UAAMC,QAAQC,KAAKC,IAAIC,QAAQC,OAAOC,SAAS,GAAA;AAC/C,UAAMC,OAAOL,KAAKM,IAAIP,QAAQ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ACX;AACJ;;;AC7HA,OAAOK,cAAc;AAEd,IAAMC,aAAN,MAAMA;EAFb,OAEaA;;;EACDC,QAAQ;IACZC,MAAM;IACNC,OAAO;IACPC,QAAQ;IACRC,QAAQ;IACRC,QAAQ;IACRC,QAAQ;IACRC,SAAS;IACTC,UAAU;EACd;;;;;;;;;EAUAC,QAASC,MAAiBC,QAAyB;AAC/C,QAAIC;AAEJ,QAAID,UAAUD,SAAS,QAAQ;AAC3BE,aAAOC,SAASC,KAAKH,QAAQ,KAAKX,MAAMU,IAAAA,CAAK;IACjD,OAAO;AACHE,aAAO,KAAKZ,MAAMU,IAAAA;IACtB;AAEAE,WAAOA,KAAKG,QAAQ,SAAS,IAAIC,QAAQC,IAAIC,YAAY,KAAA,IAASH,QAAQ,gBAAgB,IAAA,CAAA;AAE1F,QAAIL,SAAS,cAAcM,QAAQC,IAAIC,YAAY,CAAC,QAAQC,SAASH,QAAQC,IAAIC,QAAQ,GAAG;AACxF,aAAOL,SAASO,QAAQR,KAAKG,QAAQC,QAAQC,IAAIC,UAAU,EAAA,CAAA;IAC/D;AAEA,WAAON;EACX;;;;;;;;EASAS,QAASX,MAAiBE,MAAcX,MAAe;AACnD,QAAIA,QAAQS,SAAS,QAAQ;AACzB,WAAKV,MAAMU,IAAAA,IAAQG,SAASC,KAAKb,MAAMW,IAAAA;IAC3C;AAEA,SAAKZ,MAAMU,IAAAA,IAAQE;EACvB;AACJ;","names":["HttpContext","app","request","response","init","ctx","EnvParser","parse","initial","parsed","key","value","parseValue","undefined","isNaN","trim","Number","chalk","Logger","twoColumnLog","name","value","regex","width","Math","min","process","stdout","columns","dots","max","replace","length","console","log","chalk","gray","repeat","textFormat","txt","color","String","split","map","e","i","a","join","success","msg","exit","green","bgGreen","info","blue","bgBlue","error","Error","message","red","bgRed","detail","stack","status","quiet","parse","config","joiner","string","str","opt","conf","nodepath","PathLoader","paths","base","views","assets","routes","config","public","storage","database","getPath","name","prefix","path","nodepath","join","replace","process","env","SRC_PATH","includes","resolve","setPath"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@h3ravel/shared",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.3",
|
|
4
4
|
"description": "Shared Utilities.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -9,11 +9,17 @@
|
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"import": "./dist/index.js",
|
|
12
|
-
"require": "./dist/index.cjs"
|
|
13
|
-
"types": "./dist/index.d.ts"
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
14
13
|
},
|
|
15
14
|
"./tsconfig.json": "./tsconfig.json"
|
|
16
15
|
},
|
|
16
|
+
"typesVersions": {
|
|
17
|
+
"*": {
|
|
18
|
+
"*": [
|
|
19
|
+
"dist/index.d.ts"
|
|
20
|
+
]
|
|
21
|
+
}
|
|
22
|
+
},
|
|
17
23
|
"files": [
|
|
18
24
|
"dist",
|
|
19
25
|
"tsconfig.json"
|
|
@@ -39,6 +45,7 @@
|
|
|
39
45
|
],
|
|
40
46
|
"dependencies": {
|
|
41
47
|
"h3": "^2.0.0-beta.1",
|
|
48
|
+
"chalk": "^5.6.0",
|
|
42
49
|
"edge.js": "^6.3.0"
|
|
43
50
|
},
|
|
44
51
|
"devDependencies": {
|
|
@@ -52,6 +59,7 @@
|
|
|
52
59
|
"dev": "tsx watch src/index.ts",
|
|
53
60
|
"start": "node dist/index.js",
|
|
54
61
|
"lint": "eslint . --ext .ts",
|
|
55
|
-
"test": "jest --passWithNoTests"
|
|
62
|
+
"test": "jest --passWithNoTests",
|
|
63
|
+
"release:patch": "pnpm build && pnpm version patch && git add . && git commit -m \"version: bump shared package and publish\" && pnpm publish --tag latest"
|
|
56
64
|
}
|
|
57
65
|
}
|