@h3ravel/shared 0.12.1 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @h3ravel/shared
2
2
 
3
+ ## 0.14.0
4
+
5
+ ### Minor Changes
6
+
7
+ - feat: add app to HttpContext interface
8
+
9
+ ## 0.13.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 6e249fe: feat: bind view as a self contained method to render views
14
+
3
15
  ## 0.12.1
4
16
 
5
17
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -1,8 +1,15 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
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
+ };
6
13
  var __copyProps = (to, from, except, desc) => {
7
14
  if (from && typeof from === "object" || typeof from === "function") {
8
15
  for (let key of __getOwnPropNames(from))
@@ -11,9 +18,68 @@ var __copyProps = (to, from, except, desc) => {
11
18
  }
12
19
  return to;
13
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
+ ));
14
29
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
30
 
16
31
  // src/index.ts
17
32
  var src_exports = {};
33
+ __export(src_exports, {
34
+ PathLoader: () => PathLoader
35
+ });
18
36
  module.exports = __toCommonJS(src_exports);
37
+
38
+ // src/Utils/PathLoader.ts
39
+ var import_path = __toESM(require("path"), 1);
40
+ var PathLoader = class {
41
+ static {
42
+ __name(this, "PathLoader");
43
+ }
44
+ paths = {
45
+ base: "",
46
+ views: "/src/resources/views",
47
+ assets: "/public/assets",
48
+ routes: "/src/routes",
49
+ config: "/src/config",
50
+ public: "/public",
51
+ storage: "/storage"
52
+ };
53
+ /**
54
+ * Dynamically retrieves a path property from the class.
55
+ * Any property ending with "Path" is accessible automatically.
56
+ *
57
+ * @param name - The base name of the path property
58
+ * @param base - The base path to include to the path
59
+ * @returns
60
+ */
61
+ getPath(name, base) {
62
+ if (base && name !== "base") {
63
+ return import_path.default.join(base, this.paths[name]);
64
+ }
65
+ return this.paths[name];
66
+ }
67
+ /**
68
+ * Programatically set the paths.
69
+ *
70
+ * @param name - The base name of the path property
71
+ * @param path - The new path
72
+ * @param base - The base path to include to the path
73
+ */
74
+ setPath(name, path, base) {
75
+ if (base && name !== "base") {
76
+ this.paths[name] = import_path.default.join(base, path);
77
+ }
78
+ this.paths[name] = path;
79
+ }
80
+ };
81
+ // Annotate the CommonJS export names for ESM import in node:
82
+ 0 && (module.exports = {
83
+ PathLoader
84
+ });
19
85
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from './Contracts/IApplication';\nexport * from './Contracts/IHttp';\nexport * from './Contracts/IRequest';\nexport * from './Contracts/IResponse';\nexport * from './Contracts/IServiceProvider';\nexport * from './Contracts/ObjContract';\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;;","names":[]}
1
+ {"version":3,"sources":["../src/index.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 { 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 if (base && name !== 'base') {\n return nodepath.join(base, this.paths[name])\n }\n\n return this.paths[name]\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;;;;;;;ACCA,kBAAqB;AAEd,IAAMA,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,QAAIA,QAAQQ,SAAS,QAAQ;AACzB,aAAOC,YAAAA,QAASC,KAAKV,MAAM,KAAKD,MAAMS,IAAAA,CAAK;IAC/C;AAEA,WAAO,KAAKT,MAAMS,IAAAA;EACtB;;;;;;;;EASAG,QAASH,MAAiBI,MAAcZ,MAAe;AACnD,QAAIA,QAAQQ,SAAS,QAAQ;AACzB,WAAKT,MAAMS,IAAAA,IAAQC,YAAAA,QAASC,KAAKV,MAAMY,IAAAA;IAC3C;AAEA,SAAKb,MAAMS,IAAAA,IAAQI;EACvB;AACJ;","names":["PathLoader","paths","base","views","assets","routes","config","public","storage","getPath","name","nodepath","join","setPath","path"]}
package/dist/index.d.cts CHANGED
@@ -1,6 +1,67 @@
1
- import { H3Event, Middleware, MiddlewareOptions } from 'h3';
1
+ import { H3Event, Middleware, MiddlewareOptions, H3, serve } from 'h3';
2
+ import { Edge } from 'edge.js';
2
3
  import { TypedHeaders, ResponseHeaderMap } from 'fetchdts';
3
4
 
5
+ /**
6
+ * Adds a dot prefix to nested keys
7
+ */
8
+ type DotPrefix<T extends string, U extends string> = T extends '' ? U : `${T}.${U}`;
9
+ /**
10
+ * Converts a union of objects into a single merged object
11
+ */
12
+ type MergeUnion<T> = (T extends any ? (k: T) => void : never) extends (k: infer I) => void ? {
13
+ [K in keyof I]: I[K];
14
+ } : never;
15
+ /**
16
+ * Flattens nested objects into dotted keys
17
+ */
18
+ type DotFlatten<T, Prefix extends string = ''> = MergeUnion<{
19
+ [K in keyof T & string]: T[K] extends Record<string, any> ? DotFlatten<T[K], DotPrefix<Prefix, K>> : {
20
+ [P in DotPrefix<Prefix, K>]: T[K];
21
+ };
22
+ }[keyof T & string]>;
23
+ /**
24
+ * Builds "nested.key" paths for autocompletion
25
+ */
26
+ type DotNestedKeys<T> = {
27
+ [K in keyof T & string]: T[K] extends object ? `${K}` | `${K}.${DotNestedKeys<T[K]>}` : `${K}`;
28
+ }[keyof T & string];
29
+ /**
30
+ * Retrieves type at a given dot-path
31
+ */
32
+ 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;
33
+
34
+ /**
35
+ * Interface for the Container contract, defining methods for dependency injection and service resolution.
36
+ */
37
+ interface IContainer {
38
+ /**
39
+ * Binds a transient service to the container.
40
+ * @param key - The key or constructor for the service.
41
+ * @param factory - The factory function to create the service instance.
42
+ */
43
+ bind<T>(key: new (...args: any[]) => T, factory: () => T): void;
44
+ bind<T extends UseKey>(key: T, factory: () => Bindings[T]): void;
45
+ /**
46
+ * Binds a singleton service to the container.
47
+ * @param key - The key or constructor for the service.
48
+ * @param factory - The factory function to create the singleton instance.
49
+ */
50
+ singleton<T extends UseKey>(key: T | (new (...args: any[]) => Bindings[T]), factory: () => Bindings[T]): void;
51
+ /**
52
+ * Resolves a service from the container.
53
+ * @param key - The key or constructor for the service.
54
+ * @returns The resolved service instance.
55
+ */
56
+ make<T extends UseKey>(key: T | (new (...args: any[]) => Bindings[T])): Bindings[T];
57
+ /**
58
+ * Checks if a service is registered in the container.
59
+ * @param key - The key to check.
60
+ * @returns True if the service is registered, false otherwise.
61
+ */
62
+ has(key: UseKey): boolean;
63
+ }
64
+
4
65
  interface IServiceProvider {
5
66
  /**
6
67
  * Sort order
@@ -23,7 +84,7 @@ interface IServiceProvider {
23
84
  }
24
85
 
25
86
  type IPathName = 'views' | 'routes' | 'assets' | 'base' | 'public' | 'storage' | 'config';
26
- interface IApplication {
87
+ interface IApplication extends IContainer {
27
88
  /**
28
89
  * Registers configured service providers.
29
90
  */
@@ -69,35 +130,6 @@ interface IApplication {
69
130
  getVersion(key: 'app' | 'ts'): string | undefined;
70
131
  }
71
132
 
72
- /**
73
- * Adds a dot prefix to nested keys
74
- */
75
- type DotPrefix<T extends string, U extends string> = T extends '' ? U : `${T}.${U}`;
76
- /**
77
- * Converts a union of objects into a single merged object
78
- */
79
- type MergeUnion<T> = (T extends any ? (k: T) => void : never) extends (k: infer I) => void ? {
80
- [K in keyof I]: I[K];
81
- } : never;
82
- /**
83
- * Flattens nested objects into dotted keys
84
- */
85
- type DotFlatten<T, Prefix extends string = ''> = MergeUnion<{
86
- [K in keyof T & string]: T[K] extends Record<string, any> ? DotFlatten<T[K], DotPrefix<Prefix, K>> : {
87
- [P in DotPrefix<Prefix, K>]: T[K];
88
- };
89
- }[keyof T & string]>;
90
- /**
91
- * Builds "nested.key" paths for autocompletion
92
- */
93
- type DotNestedKeys<T> = {
94
- [K in keyof T & string]: T[K] extends object ? `${K}` | `${K}.${DotNestedKeys<T[K]>}` : `${K}`;
95
- }[keyof T & string];
96
- /**
97
- * Retrieves type at a given dot-path
98
- */
99
- 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;
100
-
101
133
  /**
102
134
  * Interface for the Request contract, defining methods for handling HTTP request data.
103
135
  */
@@ -272,6 +304,7 @@ interface IRouter {
272
304
  middleware(path: string | IMiddleware[], handler: Middleware, opts?: MiddlewareOptions): this;
273
305
  }
274
306
  interface HttpContext {
307
+ app: IApplication;
275
308
  request: IRequest;
276
309
  response: IResponse;
277
310
  }
@@ -298,4 +331,51 @@ interface IMiddleware {
298
331
  handle(context: HttpContext, next: () => Promise<any>): Promise<any>;
299
332
  }
300
333
 
301
- export type { DotFlatten, DotNestedKeys, DotNestedValue, EventHandler, HttpContext, IApplication, IController, IMiddleware, IPathName, IRequest, IResponse, IRouter, IServiceProvider, RouterEnd };
334
+ declare class PathLoader {
335
+ private paths;
336
+ /**
337
+ * Dynamically retrieves a path property from the class.
338
+ * Any property ending with "Path" is accessible automatically.
339
+ *
340
+ * @param name - The base name of the path property
341
+ * @param base - The base path to include to the path
342
+ * @returns
343
+ */
344
+ getPath(name: IPathName, base?: string): string;
345
+ /**
346
+ * Programatically set the paths.
347
+ *
348
+ * @param name - The base name of the path property
349
+ * @param path - The new path
350
+ * @param base - The base path to include to the path
351
+ */
352
+ setPath(name: IPathName, path: string, base?: string): void;
353
+ }
354
+
355
+ type RemoveIndexSignature<T> = {
356
+ [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
357
+ };
358
+ type Bindings = {
359
+ [key: string]: any;
360
+ env(): NodeJS.ProcessEnv;
361
+ env<T extends string>(key: T, def?: any): any;
362
+ view(templatePath: string, state?: Record<string, any>): Promise<string>;
363
+ edge: Edge;
364
+ asset(key: string, def?: string): string;
365
+ router: IRouter;
366
+ config: {
367
+ get<X extends Record<string, any>>(): X;
368
+ get<X extends Record<string, any>, T extends Extract<keyof X, string>>(key: T, def?: any): X[T];
369
+ set<T extends string>(key: T, value: any): void;
370
+ load?(): any;
371
+ };
372
+ 'http.app': H3;
373
+ 'path.base': string;
374
+ 'app.paths': PathLoader;
375
+ 'http.serve': typeof serve;
376
+ 'http.request': IRequest;
377
+ 'http.response': IResponse;
378
+ };
379
+ type UseKey = keyof RemoveIndexSignature<Bindings>;
380
+
381
+ export { type Bindings, type DotFlatten, type DotNestedKeys, type DotNestedValue, type EventHandler, type HttpContext, type IApplication, type IContainer, type IController, type IMiddleware, type IPathName, type IRequest, type IResponse, type IRouter, type IServiceProvider, PathLoader, type RouterEnd, type UseKey };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,67 @@
1
- import { H3Event, Middleware, MiddlewareOptions } from 'h3';
1
+ import { H3Event, Middleware, MiddlewareOptions, H3, serve } from 'h3';
2
+ import { Edge } from 'edge.js';
2
3
  import { TypedHeaders, ResponseHeaderMap } from 'fetchdts';
3
4
 
5
+ /**
6
+ * Adds a dot prefix to nested keys
7
+ */
8
+ type DotPrefix<T extends string, U extends string> = T extends '' ? U : `${T}.${U}`;
9
+ /**
10
+ * Converts a union of objects into a single merged object
11
+ */
12
+ type MergeUnion<T> = (T extends any ? (k: T) => void : never) extends (k: infer I) => void ? {
13
+ [K in keyof I]: I[K];
14
+ } : never;
15
+ /**
16
+ * Flattens nested objects into dotted keys
17
+ */
18
+ type DotFlatten<T, Prefix extends string = ''> = MergeUnion<{
19
+ [K in keyof T & string]: T[K] extends Record<string, any> ? DotFlatten<T[K], DotPrefix<Prefix, K>> : {
20
+ [P in DotPrefix<Prefix, K>]: T[K];
21
+ };
22
+ }[keyof T & string]>;
23
+ /**
24
+ * Builds "nested.key" paths for autocompletion
25
+ */
26
+ type DotNestedKeys<T> = {
27
+ [K in keyof T & string]: T[K] extends object ? `${K}` | `${K}.${DotNestedKeys<T[K]>}` : `${K}`;
28
+ }[keyof T & string];
29
+ /**
30
+ * Retrieves type at a given dot-path
31
+ */
32
+ 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;
33
+
34
+ /**
35
+ * Interface for the Container contract, defining methods for dependency injection and service resolution.
36
+ */
37
+ interface IContainer {
38
+ /**
39
+ * Binds a transient service to the container.
40
+ * @param key - The key or constructor for the service.
41
+ * @param factory - The factory function to create the service instance.
42
+ */
43
+ bind<T>(key: new (...args: any[]) => T, factory: () => T): void;
44
+ bind<T extends UseKey>(key: T, factory: () => Bindings[T]): void;
45
+ /**
46
+ * Binds a singleton service to the container.
47
+ * @param key - The key or constructor for the service.
48
+ * @param factory - The factory function to create the singleton instance.
49
+ */
50
+ singleton<T extends UseKey>(key: T | (new (...args: any[]) => Bindings[T]), factory: () => Bindings[T]): void;
51
+ /**
52
+ * Resolves a service from the container.
53
+ * @param key - The key or constructor for the service.
54
+ * @returns The resolved service instance.
55
+ */
56
+ make<T extends UseKey>(key: T | (new (...args: any[]) => Bindings[T])): Bindings[T];
57
+ /**
58
+ * Checks if a service is registered in the container.
59
+ * @param key - The key to check.
60
+ * @returns True if the service is registered, false otherwise.
61
+ */
62
+ has(key: UseKey): boolean;
63
+ }
64
+
4
65
  interface IServiceProvider {
5
66
  /**
6
67
  * Sort order
@@ -23,7 +84,7 @@ interface IServiceProvider {
23
84
  }
24
85
 
25
86
  type IPathName = 'views' | 'routes' | 'assets' | 'base' | 'public' | 'storage' | 'config';
26
- interface IApplication {
87
+ interface IApplication extends IContainer {
27
88
  /**
28
89
  * Registers configured service providers.
29
90
  */
@@ -69,35 +130,6 @@ interface IApplication {
69
130
  getVersion(key: 'app' | 'ts'): string | undefined;
70
131
  }
71
132
 
72
- /**
73
- * Adds a dot prefix to nested keys
74
- */
75
- type DotPrefix<T extends string, U extends string> = T extends '' ? U : `${T}.${U}`;
76
- /**
77
- * Converts a union of objects into a single merged object
78
- */
79
- type MergeUnion<T> = (T extends any ? (k: T) => void : never) extends (k: infer I) => void ? {
80
- [K in keyof I]: I[K];
81
- } : never;
82
- /**
83
- * Flattens nested objects into dotted keys
84
- */
85
- type DotFlatten<T, Prefix extends string = ''> = MergeUnion<{
86
- [K in keyof T & string]: T[K] extends Record<string, any> ? DotFlatten<T[K], DotPrefix<Prefix, K>> : {
87
- [P in DotPrefix<Prefix, K>]: T[K];
88
- };
89
- }[keyof T & string]>;
90
- /**
91
- * Builds "nested.key" paths for autocompletion
92
- */
93
- type DotNestedKeys<T> = {
94
- [K in keyof T & string]: T[K] extends object ? `${K}` | `${K}.${DotNestedKeys<T[K]>}` : `${K}`;
95
- }[keyof T & string];
96
- /**
97
- * Retrieves type at a given dot-path
98
- */
99
- 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;
100
-
101
133
  /**
102
134
  * Interface for the Request contract, defining methods for handling HTTP request data.
103
135
  */
@@ -272,6 +304,7 @@ interface IRouter {
272
304
  middleware(path: string | IMiddleware[], handler: Middleware, opts?: MiddlewareOptions): this;
273
305
  }
274
306
  interface HttpContext {
307
+ app: IApplication;
275
308
  request: IRequest;
276
309
  response: IResponse;
277
310
  }
@@ -298,4 +331,51 @@ interface IMiddleware {
298
331
  handle(context: HttpContext, next: () => Promise<any>): Promise<any>;
299
332
  }
300
333
 
301
- export type { DotFlatten, DotNestedKeys, DotNestedValue, EventHandler, HttpContext, IApplication, IController, IMiddleware, IPathName, IRequest, IResponse, IRouter, IServiceProvider, RouterEnd };
334
+ declare class PathLoader {
335
+ private paths;
336
+ /**
337
+ * Dynamically retrieves a path property from the class.
338
+ * Any property ending with "Path" is accessible automatically.
339
+ *
340
+ * @param name - The base name of the path property
341
+ * @param base - The base path to include to the path
342
+ * @returns
343
+ */
344
+ getPath(name: IPathName, base?: string): string;
345
+ /**
346
+ * Programatically set the paths.
347
+ *
348
+ * @param name - The base name of the path property
349
+ * @param path - The new path
350
+ * @param base - The base path to include to the path
351
+ */
352
+ setPath(name: IPathName, path: string, base?: string): void;
353
+ }
354
+
355
+ type RemoveIndexSignature<T> = {
356
+ [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
357
+ };
358
+ type Bindings = {
359
+ [key: string]: any;
360
+ env(): NodeJS.ProcessEnv;
361
+ env<T extends string>(key: T, def?: any): any;
362
+ view(templatePath: string, state?: Record<string, any>): Promise<string>;
363
+ edge: Edge;
364
+ asset(key: string, def?: string): string;
365
+ router: IRouter;
366
+ config: {
367
+ get<X extends Record<string, any>>(): X;
368
+ get<X extends Record<string, any>, T extends Extract<keyof X, string>>(key: T, def?: any): X[T];
369
+ set<T extends string>(key: T, value: any): void;
370
+ load?(): any;
371
+ };
372
+ 'http.app': H3;
373
+ 'path.base': string;
374
+ 'app.paths': PathLoader;
375
+ 'http.serve': typeof serve;
376
+ 'http.request': IRequest;
377
+ 'http.response': IResponse;
378
+ };
379
+ type UseKey = keyof RemoveIndexSignature<Bindings>;
380
+
381
+ export { type Bindings, type DotFlatten, type DotNestedKeys, type DotNestedValue, type EventHandler, type HttpContext, type IApplication, type IContainer, type IController, type IMiddleware, type IPathName, type IRequest, type IResponse, type IRouter, type IServiceProvider, PathLoader, type RouterEnd, type UseKey };
package/dist/index.js CHANGED
@@ -1 +1,50 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/Utils/PathLoader.ts
5
+ import nodepath from "path";
6
+ var PathLoader = class {
7
+ static {
8
+ __name(this, "PathLoader");
9
+ }
10
+ paths = {
11
+ base: "",
12
+ views: "/src/resources/views",
13
+ assets: "/public/assets",
14
+ routes: "/src/routes",
15
+ config: "/src/config",
16
+ public: "/public",
17
+ storage: "/storage"
18
+ };
19
+ /**
20
+ * Dynamically retrieves a path property from the class.
21
+ * Any property ending with "Path" is accessible automatically.
22
+ *
23
+ * @param name - The base name of the path property
24
+ * @param base - The base path to include to the path
25
+ * @returns
26
+ */
27
+ getPath(name, base) {
28
+ if (base && name !== "base") {
29
+ return nodepath.join(base, this.paths[name]);
30
+ }
31
+ return this.paths[name];
32
+ }
33
+ /**
34
+ * Programatically set the paths.
35
+ *
36
+ * @param name - The base name of the path property
37
+ * @param path - The new path
38
+ * @param base - The base path to include to the path
39
+ */
40
+ setPath(name, path, base) {
41
+ if (base && name !== "base") {
42
+ this.paths[name] = nodepath.join(base, path);
43
+ }
44
+ this.paths[name] = path;
45
+ }
46
+ };
47
+ export {
48
+ PathLoader
49
+ };
1
50
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"sources":["../src/Utils/PathLoader.ts"],"sourcesContent":["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 if (base && name !== 'base') {\n return nodepath.join(base, this.paths[name])\n }\n\n return this.paths[name]\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":";;;;AACA,OAAOA,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,QAAIA,QAAQQ,SAAS,QAAQ;AACzB,aAAOC,SAASC,KAAKV,MAAM,KAAKD,MAAMS,IAAAA,CAAK;IAC/C;AAEA,WAAO,KAAKT,MAAMS,IAAAA;EACtB;;;;;;;;EASAG,QAASH,MAAiBI,MAAcZ,MAAe;AACnD,QAAIA,QAAQQ,SAAS,QAAQ;AACzB,WAAKT,MAAMS,IAAAA,IAAQC,SAASC,KAAKV,MAAMY,IAAAA;IAC3C;AAEA,SAAKb,MAAMS,IAAAA,IAAQI;EACvB;AACJ;","names":["nodepath","PathLoader","paths","base","views","assets","routes","config","public","storage","getPath","name","nodepath","join","setPath","path"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h3ravel/shared",
3
- "version": "0.12.1",
3
+ "version": "0.14.0",
4
4
  "description": "Shared Utilities.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -19,7 +19,8 @@
19
19
  "laravel"
20
20
  ],
21
21
  "dependencies": {
22
- "h3": "^2.0.0-beta.1"
22
+ "h3": "^2.0.0-beta.1",
23
+ "edge.js": "^6.3.0"
23
24
  },
24
25
  "devDependencies": {
25
26
  "fetchdts": "^0.1.6",
@@ -0,0 +1,42 @@
1
+ // import { DotNestedKeys, DotNestedValue } from "@h3ravel/support";
2
+ import type { H3, serve } from 'h3'
3
+
4
+ import type { Edge } from 'edge.js'
5
+ import { IRequest } from './IRequest'
6
+ import { IResponse } from './IResponse'
7
+ import { IRouter } from './IHttp'
8
+ import { PathLoader } from '../Utils/PathLoader'
9
+
10
+ type RemoveIndexSignature<T> = {
11
+ [K in keyof T as string extends K
12
+ ? never
13
+ : number extends K
14
+ ? never
15
+ : K]: T[K]
16
+ }
17
+
18
+ export type Bindings = {
19
+ [key: string]: any;
20
+ env (): NodeJS.ProcessEnv
21
+ env<T extends string> (key: T, def?: any): any
22
+ view (templatePath: string, state?: Record<string, any>): Promise<string>
23
+ edge: Edge;
24
+ asset (key: string, def?: string): string
25
+ router: IRouter
26
+ config: {
27
+ // get<X extends Record<string, any>> (): X
28
+ // get<X extends Record<string, any>, K extends DotNestedKeys<X>> (key: K, def?: any): DotNestedValue<X, K>
29
+ get<X extends Record<string, any>> (): X
30
+ get<X extends Record<string, any>, T extends Extract<keyof X, string>> (key: T, def?: any): X[T]
31
+ set<T extends string> (key: T, value: any): void
32
+ load?(): any
33
+ }
34
+ 'http.app': H3
35
+ 'path.base': string
36
+ 'app.paths': PathLoader
37
+ 'http.serve': typeof serve
38
+ 'http.request': IRequest
39
+ 'http.response': IResponse
40
+ }
41
+
42
+ export type UseKey = keyof RemoveIndexSignature<Bindings>
@@ -1,8 +1,9 @@
1
+ import { IContainer } from './IContainer';
1
2
  import { IServiceProvider } from './IServiceProvider'
2
3
 
3
4
  export type IPathName = 'views' | 'routes' | 'assets' | 'base' | 'public' | 'storage' | 'config'
4
5
 
5
- export interface IApplication {
6
+ export interface IApplication extends IContainer {
6
7
  /**
7
8
  * Registers configured service providers.
8
9
  */
@@ -0,0 +1,38 @@
1
+ import type { Bindings, UseKey } from './BindingsContract';
2
+
3
+ /**
4
+ * Interface for the Container contract, defining methods for dependency injection and service resolution.
5
+ */
6
+ export interface IContainer {
7
+ /**
8
+ * Binds a transient service to the container.
9
+ * @param key - The key or constructor for the service.
10
+ * @param factory - The factory function to create the service instance.
11
+ */
12
+ bind<T> (key: new (...args: any[]) => T, factory: () => T): void;
13
+ bind<T extends UseKey> (key: T, factory: () => Bindings[T]): void;
14
+
15
+ /**
16
+ * Binds a singleton service to the container.
17
+ * @param key - The key or constructor for the service.
18
+ * @param factory - The factory function to create the singleton instance.
19
+ */
20
+ singleton<T extends UseKey> (
21
+ key: T | (new (...args: any[]) => Bindings[T]),
22
+ factory: () => Bindings[T]
23
+ ): void;
24
+
25
+ /**
26
+ * Resolves a service from the container.
27
+ * @param key - The key or constructor for the service.
28
+ * @returns The resolved service instance.
29
+ */
30
+ make<T extends UseKey> (key: T | (new (...args: any[]) => Bindings[T])): Bindings[T];
31
+
32
+ /**
33
+ * Checks if a service is registered in the container.
34
+ * @param key - The key to check.
35
+ * @returns True if the service is registered, false otherwise.
36
+ */
37
+ has (key: UseKey): boolean;
38
+ }
@@ -111,6 +111,7 @@ export interface IRouter {
111
111
  }
112
112
 
113
113
  export interface HttpContext {
114
+ app: IApplication
114
115
  request: IRequest
115
116
  response: IResponse
116
117
  }
@@ -0,0 +1,45 @@
1
+ import { IPathName } from '../Contracts/IApplication'
2
+ import nodepath from 'path'
3
+
4
+ export class PathLoader {
5
+ private paths = {
6
+ base: '',
7
+ views: '/src/resources/views',
8
+ assets: '/public/assets',
9
+ routes: '/src/routes',
10
+ config: '/src/config',
11
+ public: '/public',
12
+ storage: '/storage',
13
+ }
14
+
15
+ /**
16
+ * Dynamically retrieves a path property from the class.
17
+ * Any property ending with "Path" is accessible automatically.
18
+ *
19
+ * @param name - The base name of the path property
20
+ * @param base - The base path to include to the path
21
+ * @returns
22
+ */
23
+ getPath (name: IPathName, base?: string): string {
24
+ if (base && name !== 'base') {
25
+ return nodepath.join(base, this.paths[name])
26
+ }
27
+
28
+ return this.paths[name]
29
+ }
30
+
31
+ /**
32
+ * Programatically set the paths.
33
+ *
34
+ * @param name - The base name of the path property
35
+ * @param path - The new path
36
+ * @param base - The base path to include to the path
37
+ */
38
+ setPath (name: IPathName, path: string, base?: string) {
39
+ if (base && name !== 'base') {
40
+ this.paths[name] = nodepath.join(base, path)
41
+ }
42
+
43
+ this.paths[name] = path
44
+ }
45
+ }
package/src/index.ts CHANGED
@@ -2,9 +2,12 @@
2
2
  * @file Automatically generated by barrelsby.
3
3
  */
4
4
 
5
+ export * from './Contracts/BindingsContract';
5
6
  export * from './Contracts/IApplication';
7
+ export * from './Contracts/IContainer';
6
8
  export * from './Contracts/IHttp';
7
9
  export * from './Contracts/IRequest';
8
10
  export * from './Contracts/IResponse';
9
11
  export * from './Contracts/IServiceProvider';
10
12
  export * from './Contracts/ObjContract';
13
+ export * from './Utils/PathLoader';