@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.d.ts CHANGED
@@ -1,10 +1,13 @@
1
1
  /// <reference path="./app.globals.d.ts" />
2
2
  import "reflect-metadata";
3
3
  import { Bindings, HttpContext, IApplication, IContainer, IController, IMiddleware, IPathName, IServiceProvider, PathLoader, UseKey } from "@h3ravel/shared";
4
- import { XGeneric } from "@h3ravel/support";
5
4
  import { H3, H3Event } from "h3";
6
- import { Argument, Command } from "commander";
7
5
 
6
+ //#region src/Contracts/ServiceProviderConstructor.d.ts
7
+ type ServiceProviderConstructor = (new (app: Application) => ServiceProvider) & IServiceProvider;
8
+ type AServiceProvider = (new (_app: Application) => ServiceProvider) & Partial<ServiceProvider>;
9
+ type OServiceProvider = (new (_app: Application) => Partial<ServiceProvider>) & Partial<ServiceProvider>;
10
+ //#endregion
8
11
  //#region src/Container.d.ts
9
12
  declare class Container implements IContainer {
10
13
  private bindings;
@@ -99,7 +102,6 @@ declare abstract class ServiceProvider extends Inference {
99
102
  }
100
103
  //#endregion
101
104
  //#region src/Application.d.ts
102
- type AServiceProvider = (new (_app: Application) => ServiceProvider) & Partial<ServiceProvider>;
103
105
  declare class Application extends Container implements IApplication {
104
106
  paths: PathLoader;
105
107
  private tries;
@@ -184,7 +186,8 @@ declare class Application extends Container implements IApplication {
184
186
  * @param h3App The current H3 app instance
185
187
  * @param preferedPort If provided, this will overide the port set in the evironment
186
188
  */
187
- fire(h3App: H3, preferedPort?: number): Promise<void>;
189
+ fire(): Promise<this>;
190
+ fire(h3App: H3, preferredPort?: number): Promise<this>;
188
191
  /**
189
192
  * Get the base path of the app
190
193
  *
@@ -221,138 +224,28 @@ declare class Application extends Container implements IApplication {
221
224
  static getVersion(key: string): string;
222
225
  }
223
226
  //#endregion
224
- //#region src/Console/ConsoleKernel.d.ts
225
- declare class ConsoleKernel {
226
- app: Application;
227
- cwd: string;
228
- output: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
229
- basePath: string;
230
- modulePath: string;
231
- consolePath: string;
232
- modulePackage: XGeneric<{
233
- version: string;
234
- }>;
235
- consolePackage: XGeneric<{
236
- version: string;
237
- }>;
238
- constructor(app: Application);
239
- ensureDirectoryExists(dir: string): Promise<void>;
240
- }
241
- //#endregion
242
- //#region src/Console/ConsoleCommand.d.ts
243
- declare class ConsoleCommand {
244
- protected app: Application;
245
- protected kernel: ConsoleKernel;
246
- constructor(app: Application, kernel: ConsoleKernel);
227
+ //#region src/Contracts/H3ravelContract.d.ts
228
+ interface EntryConfig {
247
229
  /**
248
- * The underlying commander instance.
230
+ * Determines if we should initialize the app on call.
249
231
  *
250
- * @var Command
232
+ * @default false
251
233
  */
252
- program: Command;
234
+ initialize?: boolean;
253
235
  /**
254
- * The name and signature of the console command.
236
+ * Determines if service providers should be auto discovered and registered or not.
255
237
  *
256
- * @var string
238
+ * @default false
257
239
  */
258
- protected signature: string;
240
+ autoload?: boolean;
259
241
  /**
260
- * A dictionary of signatures or what not.
242
+ * A list of service provider name strings we do not want to register at all cost
261
243
  *
262
- * @var object
263
- */
264
- protected dictionary: Record<string, any>;
265
- /**
266
- * The console command description.
267
- *
268
- * @var string
269
- */
270
- protected description?: string;
271
- /**
272
- * The console command input.
273
- *
274
- * @var object
275
- */
276
- private input;
277
- /**
278
- * Execute the console command.
279
- */
280
- handle(..._args: any[]): Promise<void>;
281
- setApplication(app: Application): void;
282
- setInput(options: XGeneric, args: string[], regArgs: readonly Argument[], dictionary: Record<string, any>, program: Command): this;
283
- setOption(key: string, value: unknown): this;
284
- setProgram(program: Command): this;
285
- getSignature(): string;
286
- getDescription(): string | undefined;
287
- option(key: string, def?: any): any;
288
- options(key?: string): any;
289
- argument(key: string, def?: any): any;
290
- arguments(): Record<string, any>;
291
- loadBaseFlags(): void;
292
- /**
293
- * Check if the command is quiet
294
- *
295
- * @returns
296
- */
297
- isQuiet(): any;
298
- /**
299
- * Check if the command is silent
300
- *
301
- * @returns
302
- */
303
- isSilent(): any;
304
- /**
305
- * Check if the command is non interactive
306
- *
307
- * @returns
308
- */
309
- isNonInteractive(): boolean;
310
- /**
311
- * Get the verbosity of the command
312
- *
313
- * @returns
244
+ * @default []
314
245
  */
315
- getVerbosity(): number;
316
- /**
317
- * Log an info message
318
- */
319
- info(message: string): this;
320
- /**
321
- * Log a warning message
322
- */
323
- warn(message: string): this;
324
- /**
325
- * Log a line message
326
- */
327
- line(message: string): this;
328
- /**
329
- * Log a new line
330
- */
331
- newLine(count?: number): this;
332
- /**
333
- * Log a success message
334
- */
335
- success(message: string): this;
336
- /**
337
- * Log an error message
338
- */
339
- error(message: string): this;
340
- /**
341
- * Log an error message and terminate execution of the command
342
- * return an exit code of 1
343
- *
344
- * This method is not chainable
345
- */
346
- fail(message: string): void;
347
- /**
348
- * Log a debug message
349
- */
350
- debug(message: string | string[]): this;
246
+ filteredProviders?: string[];
351
247
  }
352
248
  //#endregion
353
- //#region src/Contracts/ServiceProviderConstructor.d.ts
354
- type ServiceProviderConstructor = (new (app: Application) => ServiceProvider) & IServiceProvider;
355
- //#endregion
356
249
  //#region src/Controller.d.ts
357
250
  /**
358
251
  * Base controller class
@@ -390,6 +283,32 @@ declare class ConfigException extends Error {
390
283
  constructor(key: string, type?: 'any' | 'config' | 'env', cause?: unknown);
391
284
  }
392
285
  //#endregion
286
+ //#region src/H3ravel.d.ts
287
+ /**
288
+ * Simple global entry point for H3ravel applications
289
+ *
290
+ * @param providers
291
+ * @param basePath
292
+ * @param callback
293
+ */
294
+ declare const h3ravel: (
295
+ /**
296
+ * List of intial service providers to register with your app
297
+ */
298
+ providers?: Exclude<OServiceProvider, "app" | "commands">[],
299
+ /**
300
+ * Entry path of your app
301
+ */
302
+ basePath?: string,
303
+ /**
304
+ * Configuration option to pass to the initializer
305
+ */
306
+ config?: EntryConfig,
307
+ /**
308
+ * final middleware function to call once the server is fired up
309
+ */
310
+ middleware?: (ctx: HttpContext) => Promise<unknown>) => Promise<Application>;
311
+ //#endregion
393
312
  //#region src/Http/Kernel.d.ts
394
313
  /**
395
314
  * Kernel class handles middleware execution and response transformations.
@@ -551,5 +470,4 @@ declare class Registerer {
551
470
  private databasePath;
552
471
  }
553
472
  //#endregion
554
- export { Application, ConfigException, ConsoleCommand, ConsoleKernel, Container, ContainerResolver, Controller, CoreServiceProvider, Inject, Injectable, Kernel, ProviderRegistry, Registerer, ServiceProvider, ServiceProviderConstructor };
555
- //# sourceMappingURL=index.d.ts.map
473
+ export { AServiceProvider, Application, ConfigException, Container, ContainerResolver, Controller, CoreServiceProvider, EntryConfig, Inject, Injectable, Kernel, OServiceProvider, ProviderRegistry, Registerer, ServiceProvider, ServiceProviderConstructor, h3ravel };
package/dist/index.js CHANGED
@@ -1,13 +1,14 @@
1
1
  import "reflect-metadata";
2
- import { FileSystem, Logger, PathLoader } from "@h3ravel/shared";
2
+ import { FileSystem, HttpContext, Logger, PathLoader } from "@h3ravel/shared";
3
+ import { InvalidArgumentException, Str, dd, dump, str } from "@h3ravel/support";
3
4
  import fg from "fast-glob";
4
5
  import path from "node:path";
5
- import { Str, dd, dump, str } from "@h3ravel/support";
6
6
  import { detect } from "detect-port";
7
7
  import dotenv from "dotenv";
8
8
  import dotenvExpand from "dotenv-expand";
9
- import { mkdir, readFile } from "node:fs/promises";
9
+ import { readFile } from "node:fs/promises";
10
10
  import semver from "semver";
11
+ import { LogRequests, Request, Response } from "@h3ravel/http";
11
12
 
12
13
  //#region src/Container.ts
13
14
  var Container = class {
@@ -79,7 +80,8 @@ var ContainerResolver = class ContainerResolver {
79
80
  /**
80
81
  * Resolve the bound dependencies
81
82
  */
82
- const args = params.filter((e) => ContainerResolver.isClass(e)).map((type) => {
83
+ const args = params.filter((e) => ContainerResolver.isClass(e) || e instanceof Application).map((type) => {
84
+ if (type instanceof Application) return type;
83
85
  return this.app.make(type);
84
86
  });
85
87
  return new Promise((resolve) => {
@@ -261,13 +263,15 @@ var ProviderRegistry = class {
261
263
  "node_modules/h3ravel-*/package.json"
262
264
  ]);
263
265
  const providers = [];
264
- for (const manifestPath of manifests) {
265
- const pkg = await this.getManifest(path.resolve(manifestPath));
266
- if (pkg.h3ravel?.providers) providers.push(...await Promise.all(pkg.h3ravel.providers.map(async (name) => (await import(path.resolve(path.dirname(manifestPath), "dist/index.js")))[name])));
267
- }
268
- if (autoRegister) for (const provider of providers) {
269
- const key = this.getKey(provider);
270
- this.providers.set(key, provider);
266
+ if (autoRegister) {
267
+ for (const manifestPath of manifests) {
268
+ const pkg = await this.getManifest(path.resolve(manifestPath));
269
+ if (pkg.h3ravel?.providers) providers.push(...await Promise.all(pkg.h3ravel.providers.map(async (name) => (await import(path.resolve(path.dirname(manifestPath), "dist/index.js")))[name])));
270
+ }
271
+ for (const provider of providers) {
272
+ const key = this.getKey(provider);
273
+ this.providers.set(key, provider);
274
+ }
271
275
  }
272
276
  return providers;
273
277
  }
@@ -427,13 +431,13 @@ var Application = class Application extends Container {
427
431
  ProviderRegistry.setSortable(false);
428
432
  ProviderRegistry.setFiltered(this.filteredProviders);
429
433
  ProviderRegistry.registerMany(providers);
430
- if (autoRegister) await ProviderRegistry.discoverProviders();
434
+ if (autoRegister) await ProviderRegistry.discoverProviders(autoRegister);
431
435
  ProviderRegistry.doSort();
432
- for (const ProviderClass of ProviderRegistry.all()) {
433
- if (!ProviderClass) continue;
436
+ ProviderRegistry.all().forEach(async (ProviderClass) => {
437
+ if (!ProviderClass) return;
434
438
  const provider = new ProviderClass(this);
435
439
  await this.register(provider);
436
- }
440
+ });
437
441
  }
438
442
  /**
439
443
  * Register service providers
@@ -481,7 +485,7 @@ var Application = class Application extends Container {
481
485
  /**
482
486
  * If debug is enabled, let's show the loaded service provider info
483
487
  */
484
- if (process.env.APP_DEBUG === "true" && process.env.EXTENDED_DEBUG !== "false" && !this.providers.some((e) => e.runsInConsole)) ProviderRegistry.log(this.providers);
488
+ 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);
485
489
  for (const provider of this.providers) if (provider.boot) if (Container.hasAnyDecorator(provider.boot))
486
490
  /**
487
491
  * If the service provider is decorated use the IoC container
@@ -496,17 +500,10 @@ var Application = class Application extends Container {
496
500
  this.booted = true;
497
501
  return this;
498
502
  }
499
- /**
500
- * Fire up the developement server using the user provided arguments
501
- *
502
- * Port will be auto assigned if provided one is not available
503
- *
504
- * @param h3App The current H3 app instance
505
- * @param preferedPort If provided, this will overide the port set in the evironment
506
- */
507
- async fire(h3App, preferedPort) {
503
+ async fire(h3App, preferredPort) {
504
+ if (!h3App) throw new InvalidArgumentException("No valid H3 app instance was provided.");
508
505
  const serve = this.make("http.serve");
509
- const port = preferedPort ?? env("PORT", 3e3);
506
+ const port = preferredPort ?? env("PORT", 3e3);
510
507
  const tries = env("RETRIES", 1);
511
508
  const hostname = env("HOSTNAME", "localhost");
512
509
  try {
@@ -529,6 +526,7 @@ var Application = class Application extends Container {
529
526
  [e.stack, "red"]
530
527
  ], "\n");
531
528
  }
529
+ return this;
532
530
  }
533
531
  /**
534
532
  * Get the base path of the app
@@ -576,215 +574,6 @@ var Application = class Application extends Container {
576
574
  }
577
575
  };
578
576
 
579
- //#endregion
580
- //#region src/Console/ConsoleCommand.ts
581
- var ConsoleCommand = class {
582
- constructor(app, kernel) {
583
- this.app = app;
584
- this.kernel = kernel;
585
- }
586
- /**
587
- * The underlying commander instance.
588
- *
589
- * @var Command
590
- */
591
- program;
592
- /**
593
- * The name and signature of the console command.
594
- *
595
- * @var string
596
- */
597
- signature;
598
- /**
599
- * A dictionary of signatures or what not.
600
- *
601
- * @var object
602
- */
603
- dictionary = {};
604
- /**
605
- * The console command description.
606
- *
607
- * @var string
608
- */
609
- description;
610
- /**
611
- * The console command input.
612
- *
613
- * @var object
614
- */
615
- input = {
616
- options: {},
617
- arguments: {}
618
- };
619
- /**
620
- * Execute the console command.
621
- */
622
- async handle(..._args) {}
623
- setApplication(app) {
624
- this.app = app;
625
- }
626
- setInput(options, args, regArgs, dictionary, program) {
627
- this.program = program;
628
- this.dictionary = dictionary;
629
- this.input.options = options;
630
- this.input.arguments = regArgs.map((e, i) => ({ [e.name()]: args[i] })).reduce((e, x) => Object.assign(e, x), {});
631
- this.loadBaseFlags();
632
- Logger.configure({
633
- verbosity: this.option("verbose"),
634
- silent: this.option("silent"),
635
- quiet: this.option("quiet")
636
- });
637
- return this;
638
- }
639
- setOption(key, value) {
640
- this.program.setOptionValue(key, value);
641
- return this;
642
- }
643
- setProgram(program) {
644
- this.program = program;
645
- return this;
646
- }
647
- getSignature() {
648
- return this.signature;
649
- }
650
- getDescription() {
651
- return this.description;
652
- }
653
- option(key, def) {
654
- const option = this.input.options[key] ?? def;
655
- return option === "null" || option === "undefined" ? void 0 : option;
656
- }
657
- options(key) {
658
- if (key) return this.input.options[key];
659
- return this.input.options;
660
- }
661
- argument(key, def) {
662
- return this.input.arguments[key] ?? def;
663
- }
664
- arguments() {
665
- return this.input.arguments;
666
- }
667
- loadBaseFlags() {
668
- let verbose = 0;
669
- if (this.program.getOptionValue("verbose") == "v") verbose = 2;
670
- else if (this.program.getOptionValue("verbose") == "vv") verbose = 3;
671
- else verbose = Number(this.program.getOptionValue("verbose") ?? 0);
672
- this.input.options.quiet = this.program.getOptionValue("quiet") ?? false;
673
- this.input.options.silent = this.program.getOptionValue("silent") ?? false;
674
- this.input.options.verbose = verbose;
675
- this.input.options.interaction = this.program.getOptionValue("interaction") ?? false;
676
- }
677
- /**
678
- * Check if the command is quiet
679
- *
680
- * @returns
681
- */
682
- isQuiet() {
683
- return this.option("quiet");
684
- }
685
- /**
686
- * Check if the command is silent
687
- *
688
- * @returns
689
- */
690
- isSilent() {
691
- return this.option("silent");
692
- }
693
- /**
694
- * Check if the command is non interactive
695
- *
696
- * @returns
697
- */
698
- isNonInteractive() {
699
- return this.option("interaction") === false;
700
- }
701
- /**
702
- * Get the verbosity of the command
703
- *
704
- * @returns
705
- */
706
- getVerbosity() {
707
- return Number(this.option("verbose"));
708
- }
709
- /**
710
- * Log an info message
711
- */
712
- info(message) {
713
- Logger.info(message);
714
- return this;
715
- }
716
- /**
717
- * Log a warning message
718
- */
719
- warn(message) {
720
- Logger.warn(message);
721
- return this;
722
- }
723
- /**
724
- * Log a line message
725
- */
726
- line(message) {
727
- Logger.log(message, "white");
728
- return this;
729
- }
730
- /**
731
- * Log a new line
732
- */
733
- newLine(count = 1) {
734
- if (Number(this.getVerbosity()) >= 3 || !this.isSilent() && !this.isQuiet()) for (let i = 0; i < count; i++) console.log("");
735
- return this;
736
- }
737
- /**
738
- * Log a success message
739
- */
740
- success(message) {
741
- Logger.success(message);
742
- return this;
743
- }
744
- /**
745
- * Log an error message
746
- */
747
- error(message) {
748
- Logger.error(message, false);
749
- return this;
750
- }
751
- /**
752
- * Log an error message and terminate execution of the command
753
- * return an exit code of 1
754
- *
755
- * This method is not chainable
756
- */
757
- fail(message) {
758
- this.error(message);
759
- process.exit(1);
760
- }
761
- /**
762
- * Log a debug message
763
- */
764
- debug(message) {
765
- Logger.debug(message);
766
- return this;
767
- }
768
- };
769
-
770
- //#endregion
771
- //#region src/Console/ConsoleKernel.ts
772
- var ConsoleKernel = class {
773
- cwd;
774
- output = typeof Logger;
775
- basePath = "";
776
- modulePath;
777
- consolePath;
778
- modulePackage;
779
- consolePackage;
780
- constructor(app) {
781
- this.app = app;
782
- }
783
- async ensureDirectoryExists(dir) {
784
- await mkdir(dir, { recursive: true });
785
- }
786
- };
787
-
788
577
  //#endregion
789
578
  //#region src/Controller.ts
790
579
  /**
@@ -836,6 +625,35 @@ var ConfigException = class extends Error {
836
625
  }
837
626
  };
838
627
 
628
+ //#endregion
629
+ //#region src/H3ravel.ts
630
+ /**
631
+ * Simple global entry point for H3ravel applications
632
+ *
633
+ * @param providers
634
+ * @param basePath
635
+ * @param callback
636
+ */
637
+ const h3ravel = async (providers = [], basePath = process.cwd(), config = {
638
+ initialize: false,
639
+ autoload: false,
640
+ filteredProviders: []
641
+ }, middleware = async () => void 0) => {
642
+ const app = new Application(basePath);
643
+ await app.quickStartup(providers, config.filteredProviders, config.autoload);
644
+ const h3App = app.make("http.app");
645
+ const kernel = new Kernel((event) => HttpContext.init({
646
+ app,
647
+ request: new Request(event, app),
648
+ response: new Response(event, app)
649
+ }), [new LogRequests()]);
650
+ h3App.use((event) => kernel.handle(event, middleware));
651
+ const originalFire = app.fire.bind(app);
652
+ if (config.initialize) return await originalFire(h3App);
653
+ app.fire = () => originalFire(h3App);
654
+ return app;
655
+ };
656
+
839
657
  //#endregion
840
658
  //#region src/Http/Kernel.ts
841
659
  /**
@@ -1000,5 +818,4 @@ var CoreServiceProvider = class extends ServiceProvider {
1000
818
  };
1001
819
 
1002
820
  //#endregion
1003
- export { Application, ConfigException, ConsoleCommand, ConsoleKernel, Container, ContainerResolver, Controller, CoreServiceProvider, Inject, Injectable, Kernel, ProviderRegistry, Registerer, ServiceProvider };
1004
- //# sourceMappingURL=index.js.map
821
+ export { Application, ConfigException, Container, ContainerResolver, Controller, CoreServiceProvider, Inject, Injectable, Kernel, ProviderRegistry, Registerer, ServiceProvider, h3ravel };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h3ravel/core",
3
- "version": "1.16.2",
3
+ "version": "1.18.0",
4
4
  "description": "Core application container, lifecycle management and service providers for H3ravel.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -54,8 +54,8 @@
54
54
  "semver": "^7.7.2",
55
55
  "srvx": "^0.8.7",
56
56
  "tslib": "^2.8.1",
57
- "@h3ravel/support": "^0.14.1",
58
- "@h3ravel/shared": "^0.22.2"
57
+ "@h3ravel/shared": "^0.22.3",
58
+ "@h3ravel/support": "^0.14.2"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@types/semver": "^7.7.1",