@h3ravel/core 1.16.2 → 1.18.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/dist/index.cjs CHANGED
@@ -24,12 +24,12 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
24
24
  require("reflect-metadata");
25
25
  let __h3ravel_shared = require("@h3ravel/shared");
26
26
  __h3ravel_shared = __toESM(__h3ravel_shared);
27
+ let __h3ravel_support = require("@h3ravel/support");
28
+ __h3ravel_support = __toESM(__h3ravel_support);
27
29
  let fast_glob = require("fast-glob");
28
30
  fast_glob = __toESM(fast_glob);
29
31
  let node_path = require("node:path");
30
32
  node_path = __toESM(node_path);
31
- let __h3ravel_support = require("@h3ravel/support");
32
- __h3ravel_support = __toESM(__h3ravel_support);
33
33
  let detect_port = require("detect-port");
34
34
  detect_port = __toESM(detect_port);
35
35
  let dotenv = require("dotenv");
@@ -40,6 +40,8 @@ let node_fs_promises = require("node:fs/promises");
40
40
  node_fs_promises = __toESM(node_fs_promises);
41
41
  let semver = require("semver");
42
42
  semver = __toESM(semver);
43
+ let __h3ravel_http = require("@h3ravel/http");
44
+ __h3ravel_http = __toESM(__h3ravel_http);
43
45
 
44
46
  //#region src/Container.ts
45
47
  var Container = class {
@@ -111,7 +113,8 @@ var ContainerResolver = class ContainerResolver {
111
113
  /**
112
114
  * Resolve the bound dependencies
113
115
  */
114
- const args = params.filter((e) => ContainerResolver.isClass(e)).map((type) => {
116
+ const args = params.filter((e) => ContainerResolver.isClass(e) || e instanceof Application).map((type) => {
117
+ if (type instanceof Application) return type;
115
118
  return this.app.make(type);
116
119
  });
117
120
  return new Promise((resolve) => {
@@ -293,13 +296,15 @@ var ProviderRegistry = class {
293
296
  "node_modules/h3ravel-*/package.json"
294
297
  ]);
295
298
  const providers = [];
296
- for (const manifestPath of manifests) {
297
- const pkg = await this.getManifest(node_path.default.resolve(manifestPath));
298
- if (pkg.h3ravel?.providers) providers.push(...await Promise.all(pkg.h3ravel.providers.map(async (name) => (await import(node_path.default.resolve(node_path.default.dirname(manifestPath), "dist/index.js")))[name])));
299
- }
300
- if (autoRegister) for (const provider of providers) {
301
- const key = this.getKey(provider);
302
- this.providers.set(key, provider);
299
+ if (autoRegister) {
300
+ for (const manifestPath of manifests) {
301
+ const pkg = await this.getManifest(node_path.default.resolve(manifestPath));
302
+ if (pkg.h3ravel?.providers) providers.push(...await Promise.all(pkg.h3ravel.providers.map(async (name) => (await import(node_path.default.resolve(node_path.default.dirname(manifestPath), "dist/index.js")))[name])));
303
+ }
304
+ for (const provider of providers) {
305
+ const key = this.getKey(provider);
306
+ this.providers.set(key, provider);
307
+ }
303
308
  }
304
309
  return providers;
305
310
  }
@@ -459,13 +464,13 @@ var Application = class Application extends Container {
459
464
  ProviderRegistry.setSortable(false);
460
465
  ProviderRegistry.setFiltered(this.filteredProviders);
461
466
  ProviderRegistry.registerMany(providers);
462
- if (autoRegister) await ProviderRegistry.discoverProviders();
467
+ if (autoRegister) await ProviderRegistry.discoverProviders(autoRegister);
463
468
  ProviderRegistry.doSort();
464
- for (const ProviderClass of ProviderRegistry.all()) {
465
- if (!ProviderClass) continue;
469
+ ProviderRegistry.all().forEach(async (ProviderClass) => {
470
+ if (!ProviderClass) return;
466
471
  const provider = new ProviderClass(this);
467
472
  await this.register(provider);
468
- }
473
+ });
469
474
  }
470
475
  /**
471
476
  * Register service providers
@@ -513,7 +518,7 @@ var Application = class Application extends Container {
513
518
  /**
514
519
  * If debug is enabled, let's show the loaded service provider info
515
520
  */
516
- if (process.env.APP_DEBUG === "true" && process.env.EXTENDED_DEBUG !== "false" && !this.providers.some((e) => e.runsInConsole)) ProviderRegistry.log(this.providers);
521
+ if ((process.env.APP_DEBUG === "true" && process.env.EXTENDED_DEBUG !== "false" || Number(process.env.VERBOSE) > 1) && !this.providers.some((e) => e.runsInConsole)) ProviderRegistry.log(this.providers);
517
522
  for (const provider of this.providers) if (provider.boot) if (Container.hasAnyDecorator(provider.boot))
518
523
  /**
519
524
  * If the service provider is decorated use the IoC container
@@ -528,17 +533,10 @@ var Application = class Application extends Container {
528
533
  this.booted = true;
529
534
  return this;
530
535
  }
531
- /**
532
- * Fire up the developement server using the user provided arguments
533
- *
534
- * Port will be auto assigned if provided one is not available
535
- *
536
- * @param h3App The current H3 app instance
537
- * @param preferedPort If provided, this will overide the port set in the evironment
538
- */
539
- async fire(h3App, preferedPort) {
536
+ async fire(h3App, preferredPort) {
537
+ if (!h3App) throw new __h3ravel_support.InvalidArgumentException("No valid H3 app instance was provided.");
540
538
  const serve = this.make("http.serve");
541
- const port = preferedPort ?? env("PORT", 3e3);
539
+ const port = preferredPort ?? env("PORT", 3e3);
542
540
  const tries = env("RETRIES", 1);
543
541
  const hostname = env("HOSTNAME", "localhost");
544
542
  try {
@@ -561,6 +559,7 @@ var Application = class Application extends Container {
561
559
  [e.stack, "red"]
562
560
  ], "\n");
563
561
  }
562
+ return this;
564
563
  }
565
564
  /**
566
565
  * Get the base path of the app
@@ -608,215 +607,6 @@ var Application = class Application extends Container {
608
607
  }
609
608
  };
610
609
 
611
- //#endregion
612
- //#region src/Console/ConsoleCommand.ts
613
- var ConsoleCommand = class {
614
- constructor(app, kernel) {
615
- this.app = app;
616
- this.kernel = kernel;
617
- }
618
- /**
619
- * The underlying commander instance.
620
- *
621
- * @var Command
622
- */
623
- program;
624
- /**
625
- * The name and signature of the console command.
626
- *
627
- * @var string
628
- */
629
- signature;
630
- /**
631
- * A dictionary of signatures or what not.
632
- *
633
- * @var object
634
- */
635
- dictionary = {};
636
- /**
637
- * The console command description.
638
- *
639
- * @var string
640
- */
641
- description;
642
- /**
643
- * The console command input.
644
- *
645
- * @var object
646
- */
647
- input = {
648
- options: {},
649
- arguments: {}
650
- };
651
- /**
652
- * Execute the console command.
653
- */
654
- async handle(..._args) {}
655
- setApplication(app) {
656
- this.app = app;
657
- }
658
- setInput(options, args, regArgs, dictionary, program) {
659
- this.program = program;
660
- this.dictionary = dictionary;
661
- this.input.options = options;
662
- this.input.arguments = regArgs.map((e, i) => ({ [e.name()]: args[i] })).reduce((e, x) => Object.assign(e, x), {});
663
- this.loadBaseFlags();
664
- __h3ravel_shared.Logger.configure({
665
- verbosity: this.option("verbose"),
666
- silent: this.option("silent"),
667
- quiet: this.option("quiet")
668
- });
669
- return this;
670
- }
671
- setOption(key, value) {
672
- this.program.setOptionValue(key, value);
673
- return this;
674
- }
675
- setProgram(program) {
676
- this.program = program;
677
- return this;
678
- }
679
- getSignature() {
680
- return this.signature;
681
- }
682
- getDescription() {
683
- return this.description;
684
- }
685
- option(key, def) {
686
- const option = this.input.options[key] ?? def;
687
- return option === "null" || option === "undefined" ? void 0 : option;
688
- }
689
- options(key) {
690
- if (key) return this.input.options[key];
691
- return this.input.options;
692
- }
693
- argument(key, def) {
694
- return this.input.arguments[key] ?? def;
695
- }
696
- arguments() {
697
- return this.input.arguments;
698
- }
699
- loadBaseFlags() {
700
- let verbose = 0;
701
- if (this.program.getOptionValue("verbose") == "v") verbose = 2;
702
- else if (this.program.getOptionValue("verbose") == "vv") verbose = 3;
703
- else verbose = Number(this.program.getOptionValue("verbose") ?? 0);
704
- this.input.options.quiet = this.program.getOptionValue("quiet") ?? false;
705
- this.input.options.silent = this.program.getOptionValue("silent") ?? false;
706
- this.input.options.verbose = verbose;
707
- this.input.options.interaction = this.program.getOptionValue("interaction") ?? false;
708
- }
709
- /**
710
- * Check if the command is quiet
711
- *
712
- * @returns
713
- */
714
- isQuiet() {
715
- return this.option("quiet");
716
- }
717
- /**
718
- * Check if the command is silent
719
- *
720
- * @returns
721
- */
722
- isSilent() {
723
- return this.option("silent");
724
- }
725
- /**
726
- * Check if the command is non interactive
727
- *
728
- * @returns
729
- */
730
- isNonInteractive() {
731
- return this.option("interaction") === false;
732
- }
733
- /**
734
- * Get the verbosity of the command
735
- *
736
- * @returns
737
- */
738
- getVerbosity() {
739
- return Number(this.option("verbose"));
740
- }
741
- /**
742
- * Log an info message
743
- */
744
- info(message) {
745
- __h3ravel_shared.Logger.info(message);
746
- return this;
747
- }
748
- /**
749
- * Log a warning message
750
- */
751
- warn(message) {
752
- __h3ravel_shared.Logger.warn(message);
753
- return this;
754
- }
755
- /**
756
- * Log a line message
757
- */
758
- line(message) {
759
- __h3ravel_shared.Logger.log(message, "white");
760
- return this;
761
- }
762
- /**
763
- * Log a new line
764
- */
765
- newLine(count = 1) {
766
- if (Number(this.getVerbosity()) >= 3 || !this.isSilent() && !this.isQuiet()) for (let i = 0; i < count; i++) console.log("");
767
- return this;
768
- }
769
- /**
770
- * Log a success message
771
- */
772
- success(message) {
773
- __h3ravel_shared.Logger.success(message);
774
- return this;
775
- }
776
- /**
777
- * Log an error message
778
- */
779
- error(message) {
780
- __h3ravel_shared.Logger.error(message, false);
781
- return this;
782
- }
783
- /**
784
- * Log an error message and terminate execution of the command
785
- * return an exit code of 1
786
- *
787
- * This method is not chainable
788
- */
789
- fail(message) {
790
- this.error(message);
791
- process.exit(1);
792
- }
793
- /**
794
- * Log a debug message
795
- */
796
- debug(message) {
797
- __h3ravel_shared.Logger.debug(message);
798
- return this;
799
- }
800
- };
801
-
802
- //#endregion
803
- //#region src/Console/ConsoleKernel.ts
804
- var ConsoleKernel = class {
805
- cwd;
806
- output = typeof __h3ravel_shared.Logger;
807
- basePath = "";
808
- modulePath;
809
- consolePath;
810
- modulePackage;
811
- consolePackage;
812
- constructor(app) {
813
- this.app = app;
814
- }
815
- async ensureDirectoryExists(dir) {
816
- await (0, node_fs_promises.mkdir)(dir, { recursive: true });
817
- }
818
- };
819
-
820
610
  //#endregion
821
611
  //#region src/Controller.ts
822
612
  /**
@@ -868,6 +658,35 @@ var ConfigException = class extends Error {
868
658
  }
869
659
  };
870
660
 
661
+ //#endregion
662
+ //#region src/H3ravel.ts
663
+ /**
664
+ * Simple global entry point for H3ravel applications
665
+ *
666
+ * @param providers
667
+ * @param basePath
668
+ * @param callback
669
+ */
670
+ const h3ravel = async (providers = [], basePath = process.cwd(), config = {
671
+ initialize: false,
672
+ autoload: false,
673
+ filteredProviders: []
674
+ }, middleware = async () => void 0) => {
675
+ const app = new Application(basePath);
676
+ await app.quickStartup(providers, config.filteredProviders, config.autoload);
677
+ const h3App = app.make("http.app");
678
+ const kernel = new Kernel((event) => __h3ravel_shared.HttpContext.init({
679
+ app,
680
+ request: new __h3ravel_http.Request(event, app),
681
+ response: new __h3ravel_http.Response(event, app)
682
+ }), [new __h3ravel_http.LogRequests()]);
683
+ h3App.use((event) => kernel.handle(event, middleware));
684
+ const originalFire = app.fire.bind(app);
685
+ if (config.initialize) return await originalFire(h3App);
686
+ app.fire = () => originalFire(h3App);
687
+ return app;
688
+ };
689
+
871
690
  //#endregion
872
691
  //#region src/Http/Kernel.ts
873
692
  /**
@@ -1034,8 +853,6 @@ var CoreServiceProvider = class extends ServiceProvider {
1034
853
  //#endregion
1035
854
  exports.Application = Application;
1036
855
  exports.ConfigException = ConfigException;
1037
- exports.ConsoleCommand = ConsoleCommand;
1038
- exports.ConsoleKernel = ConsoleKernel;
1039
856
  exports.Container = Container;
1040
857
  exports.ContainerResolver = ContainerResolver;
1041
858
  exports.Controller = Controller;
@@ -1046,4 +863,4 @@ exports.Kernel = Kernel;
1046
863
  exports.ProviderRegistry = ProviderRegistry;
1047
864
  exports.Registerer = Registerer;
1048
865
  exports.ServiceProvider = ServiceProvider;
1049
- //# sourceMappingURL=index.cjs.map
866
+ exports.h3ravel = h3ravel;
package/dist/index.d.cts CHANGED
@@ -1,9 +1,12 @@
1
1
  /// <reference path="./app.globals.d.ts" />
2
2
  import { Bindings, HttpContext, IApplication, IContainer, IController, IMiddleware, IPathName, IServiceProvider, PathLoader, UseKey } from "@h3ravel/shared";
3
3
  import { H3, H3Event } from "h3";
4
- import { Argument, Command } from "commander";
5
- import { XGeneric } from "@h3ravel/support";
6
4
 
5
+ //#region src/Contracts/ServiceProviderConstructor.d.ts
6
+ type ServiceProviderConstructor = (new (app: Application) => ServiceProvider) & IServiceProvider;
7
+ type AServiceProvider = (new (_app: Application) => ServiceProvider) & Partial<ServiceProvider>;
8
+ type OServiceProvider = (new (_app: Application) => Partial<ServiceProvider>) & Partial<ServiceProvider>;
9
+ //#endregion
7
10
  //#region src/Container.d.ts
8
11
  declare class Container implements IContainer {
9
12
  private bindings;
@@ -98,7 +101,6 @@ declare abstract class ServiceProvider extends Inference {
98
101
  }
99
102
  //#endregion
100
103
  //#region src/Application.d.ts
101
- type AServiceProvider = (new (_app: Application) => ServiceProvider) & Partial<ServiceProvider>;
102
104
  declare class Application extends Container implements IApplication {
103
105
  paths: PathLoader;
104
106
  private tries;
@@ -183,7 +185,8 @@ declare class Application extends Container implements IApplication {
183
185
  * @param h3App The current H3 app instance
184
186
  * @param preferedPort If provided, this will overide the port set in the evironment
185
187
  */
186
- fire(h3App: H3, preferedPort?: number): Promise<void>;
188
+ fire(): Promise<this>;
189
+ fire(h3App: H3, preferredPort?: number): Promise<this>;
187
190
  /**
188
191
  * Get the base path of the app
189
192
  *
@@ -220,138 +223,28 @@ declare class Application extends Container implements IApplication {
220
223
  static getVersion(key: string): string;
221
224
  }
222
225
  //#endregion
223
- //#region src/Console/ConsoleKernel.d.ts
224
- declare class ConsoleKernel {
225
- app: Application;
226
- cwd: string;
227
- output: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
228
- basePath: string;
229
- modulePath: string;
230
- consolePath: string;
231
- modulePackage: XGeneric<{
232
- version: string;
233
- }>;
234
- consolePackage: XGeneric<{
235
- version: string;
236
- }>;
237
- constructor(app: Application);
238
- ensureDirectoryExists(dir: string): Promise<void>;
239
- }
240
- //#endregion
241
- //#region src/Console/ConsoleCommand.d.ts
242
- declare class ConsoleCommand {
243
- protected app: Application;
244
- protected kernel: ConsoleKernel;
245
- constructor(app: Application, kernel: ConsoleKernel);
226
+ //#region src/Contracts/H3ravelContract.d.ts
227
+ interface EntryConfig {
246
228
  /**
247
- * The underlying commander instance.
229
+ * Determines if we should initialize the app on call.
248
230
  *
249
- * @var Command
231
+ * @default false
250
232
  */
251
- program: Command;
233
+ initialize?: boolean;
252
234
  /**
253
- * The name and signature of the console command.
235
+ * Determines if service providers should be auto discovered and registered or not.
254
236
  *
255
- * @var string
237
+ * @default false
256
238
  */
257
- protected signature: string;
239
+ autoload?: boolean;
258
240
  /**
259
- * A dictionary of signatures or what not.
241
+ * A list of service provider name strings we do not want to register at all cost
260
242
  *
261
- * @var object
262
- */
263
- protected dictionary: Record<string, any>;
264
- /**
265
- * The console command description.
266
- *
267
- * @var string
268
- */
269
- protected description?: string;
270
- /**
271
- * The console command input.
272
- *
273
- * @var object
274
- */
275
- private input;
276
- /**
277
- * Execute the console command.
278
- */
279
- handle(..._args: any[]): Promise<void>;
280
- setApplication(app: Application): void;
281
- setInput(options: XGeneric, args: string[], regArgs: readonly Argument[], dictionary: Record<string, any>, program: Command): this;
282
- setOption(key: string, value: unknown): this;
283
- setProgram(program: Command): this;
284
- getSignature(): string;
285
- getDescription(): string | undefined;
286
- option(key: string, def?: any): any;
287
- options(key?: string): any;
288
- argument(key: string, def?: any): any;
289
- arguments(): Record<string, any>;
290
- loadBaseFlags(): void;
291
- /**
292
- * Check if the command is quiet
293
- *
294
- * @returns
295
- */
296
- isQuiet(): any;
297
- /**
298
- * Check if the command is silent
299
- *
300
- * @returns
301
- */
302
- isSilent(): any;
303
- /**
304
- * Check if the command is non interactive
305
- *
306
- * @returns
307
- */
308
- isNonInteractive(): boolean;
309
- /**
310
- * Get the verbosity of the command
311
- *
312
- * @returns
243
+ * @default []
313
244
  */
314
- getVerbosity(): number;
315
- /**
316
- * Log an info message
317
- */
318
- info(message: string): this;
319
- /**
320
- * Log a warning message
321
- */
322
- warn(message: string): this;
323
- /**
324
- * Log a line message
325
- */
326
- line(message: string): this;
327
- /**
328
- * Log a new line
329
- */
330
- newLine(count?: number): this;
331
- /**
332
- * Log a success message
333
- */
334
- success(message: string): this;
335
- /**
336
- * Log an error message
337
- */
338
- error(message: string): this;
339
- /**
340
- * Log an error message and terminate execution of the command
341
- * return an exit code of 1
342
- *
343
- * This method is not chainable
344
- */
345
- fail(message: string): void;
346
- /**
347
- * Log a debug message
348
- */
349
- debug(message: string | string[]): this;
245
+ filteredProviders?: string[];
350
246
  }
351
247
  //#endregion
352
- //#region src/Contracts/ServiceProviderConstructor.d.ts
353
- type ServiceProviderConstructor = (new (app: Application) => ServiceProvider) & IServiceProvider;
354
- //#endregion
355
248
  //#region src/Controller.d.ts
356
249
  /**
357
250
  * Base controller class
@@ -389,6 +282,32 @@ declare class ConfigException extends Error {
389
282
  constructor(key: string, type?: 'any' | 'config' | 'env', cause?: unknown);
390
283
  }
391
284
  //#endregion
285
+ //#region src/H3ravel.d.ts
286
+ /**
287
+ * Simple global entry point for H3ravel applications
288
+ *
289
+ * @param providers
290
+ * @param basePath
291
+ * @param callback
292
+ */
293
+ declare const h3ravel: (
294
+ /**
295
+ * List of intial service providers to register with your app
296
+ */
297
+ providers?: Exclude<OServiceProvider, "app" | "commands">[],
298
+ /**
299
+ * Entry path of your app
300
+ */
301
+ basePath?: string,
302
+ /**
303
+ * Configuration option to pass to the initializer
304
+ */
305
+ config?: EntryConfig,
306
+ /**
307
+ * final middleware function to call once the server is fired up
308
+ */
309
+ middleware?: (ctx: HttpContext) => Promise<unknown>) => Promise<Application>;
310
+ //#endregion
392
311
  //#region src/Http/Kernel.d.ts
393
312
  /**
394
313
  * Kernel class handles middleware execution and response transformations.
@@ -550,5 +469,4 @@ declare class Registerer {
550
469
  private databasePath;
551
470
  }
552
471
  //#endregion
553
- export { Application, ConfigException, ConsoleCommand, ConsoleKernel, Container, ContainerResolver, Controller, CoreServiceProvider, Inject, Injectable, Kernel, ProviderRegistry, Registerer, ServiceProvider, ServiceProviderConstructor };
554
- //# sourceMappingURL=index.d.cts.map
472
+ export { AServiceProvider, Application, ConfigException, Container, ContainerResolver, Controller, CoreServiceProvider, EntryConfig, Inject, Injectable, Kernel, OServiceProvider, ProviderRegistry, Registerer, ServiceProvider, ServiceProviderConstructor, h3ravel };