From 12acd8923b937ed5ac421ddf119c91903d500517 Mon Sep 17 00:00:00 2001 From: Martins-O Date: Mon, 29 Sep 2025 22:01:12 +0100 Subject: [PATCH 1/6] feat(http): add Url class binding to service container - Create Url class with URL generation functionality - Bind Url to container as 'app.url' in HttpServiceProvider - Add global url() helper that resolves via container - Support both url() (returns instance) and url(path) (returns string) - Add TypeScript definitions for global url() function --- packages/core/src/app.globals.d.ts | 12 ++++- .../http/src/Providers/HttpServiceProvider.ts | 16 +++++++ packages/http/src/Url.ts | 47 +++++++++++++++++++ packages/http/src/index.ts | 1 + 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 packages/http/src/Url.ts diff --git a/packages/core/src/app.globals.d.ts b/packages/core/src/app.globals.d.ts index 3f869216..416bff95 100644 --- a/packages/core/src/app.globals.d.ts +++ b/packages/core/src/app.globals.d.ts @@ -75,8 +75,16 @@ declare global { /** * Get the database path - * - * @param path + * + * @param path */ function database_path (path?: string): string + + /** + * Generate URLs or get the Url instance + * + * @param path + */ + function url (): import('../../../http/src/Url').Url + function url (path: string): string } diff --git a/packages/http/src/Providers/HttpServiceProvider.ts b/packages/http/src/Providers/HttpServiceProvider.ts index 54d21c2e..60d095aa 100644 --- a/packages/http/src/Providers/HttpServiceProvider.ts +++ b/packages/http/src/Providers/HttpServiceProvider.ts @@ -4,6 +4,7 @@ import { H3, serve } from 'h3' import { FireCommand } from '../Commands/FireCommand' import { ServiceProvider } from '@h3ravel/core' +import { Url } from '../Url' /** * Sets up HTTP kernel and request lifecycle. @@ -26,7 +27,22 @@ export class HttpServiceProvider extends ServiceProvider { /** Bind the HTTP server to the service container */ this.app.singleton('http.serve', () => serve) + /** Bind Url class to the service container */ + this.app.singleton('app.url', () => { + return new Url() + }) + /** Register Musket Commands */ this.commands([FireCommand]) } + + boot () { + globalThis.url = (...args: any[]) => { + const urlInstance = this.app.make('app.url') as Url + if (args.length === 0) { + return urlInstance + } + return urlInstance.to(args[0]) + } + } } diff --git a/packages/http/src/Url.ts b/packages/http/src/Url.ts new file mode 100644 index 00000000..3227f0db --- /dev/null +++ b/packages/http/src/Url.ts @@ -0,0 +1,47 @@ +export class Url { + private baseUrl: string + + constructor(baseUrl?: string) { + this.baseUrl = baseUrl || process.env.APP_URL || 'http://localhost:3000' + } + + /** + * Generate a URL for the given path + * + * @param path - The path to append to the base URL + * @returns The full URL + */ + to(path?: string): string { + if (!path) return this.baseUrl + + const cleanPath = path.startsWith('/') ? path : `/${path}` + return `${this.baseUrl.replace(/\/$/, '')}${cleanPath}` + } + + /** + * Generate a secure URL (HTTPS) for the given path + * + * @param path - The path to append to the base URL + * @returns The full HTTPS URL + */ + secure(path?: string): string { + const url = this.to(path) + return url.replace(/^http:/, 'https:') + } + + /** + * Get the current base URL + */ + current(): string { + return this.baseUrl + } + + /** + * Set the base URL + * + * @param url - The new base URL + */ + setBaseUrl(url: string): void { + this.baseUrl = url + } +} \ No newline at end of file diff --git a/packages/http/src/index.ts b/packages/http/src/index.ts index 25c6a2a9..0c9d1cf3 100644 --- a/packages/http/src/index.ts +++ b/packages/http/src/index.ts @@ -7,3 +7,4 @@ export * from './Request' export * from './Resources/ApiResource' export * from './Resources/JsonResource' export * from './Response' +export * from './Url' From ca78780c7a08312b9dbd37c2237ffe9e74e46520 Mon Sep 17 00:00:00 2001 From: Martins-O Date: Tue, 30 Sep 2025 07:17:05 +0100 Subject: [PATCH 2/6] Revert "feat(http): add Url class binding to service container" This reverts commit 12acd8923b937ed5ac421ddf119c91903d500517. --- packages/core/src/app.globals.d.ts | 12 +---- .../http/src/Providers/HttpServiceProvider.ts | 16 ------- packages/http/src/Url.ts | 47 ------------------- packages/http/src/index.ts | 1 - 4 files changed, 2 insertions(+), 74 deletions(-) delete mode 100644 packages/http/src/Url.ts diff --git a/packages/core/src/app.globals.d.ts b/packages/core/src/app.globals.d.ts index 416bff95..3f869216 100644 --- a/packages/core/src/app.globals.d.ts +++ b/packages/core/src/app.globals.d.ts @@ -75,16 +75,8 @@ declare global { /** * Get the database path - * - * @param path + * + * @param path */ function database_path (path?: string): string - - /** - * Generate URLs or get the Url instance - * - * @param path - */ - function url (): import('../../../http/src/Url').Url - function url (path: string): string } diff --git a/packages/http/src/Providers/HttpServiceProvider.ts b/packages/http/src/Providers/HttpServiceProvider.ts index 60d095aa..54d21c2e 100644 --- a/packages/http/src/Providers/HttpServiceProvider.ts +++ b/packages/http/src/Providers/HttpServiceProvider.ts @@ -4,7 +4,6 @@ import { H3, serve } from 'h3' import { FireCommand } from '../Commands/FireCommand' import { ServiceProvider } from '@h3ravel/core' -import { Url } from '../Url' /** * Sets up HTTP kernel and request lifecycle. @@ -27,22 +26,7 @@ export class HttpServiceProvider extends ServiceProvider { /** Bind the HTTP server to the service container */ this.app.singleton('http.serve', () => serve) - /** Bind Url class to the service container */ - this.app.singleton('app.url', () => { - return new Url() - }) - /** Register Musket Commands */ this.commands([FireCommand]) } - - boot () { - globalThis.url = (...args: any[]) => { - const urlInstance = this.app.make('app.url') as Url - if (args.length === 0) { - return urlInstance - } - return urlInstance.to(args[0]) - } - } } diff --git a/packages/http/src/Url.ts b/packages/http/src/Url.ts deleted file mode 100644 index 3227f0db..00000000 --- a/packages/http/src/Url.ts +++ /dev/null @@ -1,47 +0,0 @@ -export class Url { - private baseUrl: string - - constructor(baseUrl?: string) { - this.baseUrl = baseUrl || process.env.APP_URL || 'http://localhost:3000' - } - - /** - * Generate a URL for the given path - * - * @param path - The path to append to the base URL - * @returns The full URL - */ - to(path?: string): string { - if (!path) return this.baseUrl - - const cleanPath = path.startsWith('/') ? path : `/${path}` - return `${this.baseUrl.replace(/\/$/, '')}${cleanPath}` - } - - /** - * Generate a secure URL (HTTPS) for the given path - * - * @param path - The path to append to the base URL - * @returns The full HTTPS URL - */ - secure(path?: string): string { - const url = this.to(path) - return url.replace(/^http:/, 'https:') - } - - /** - * Get the current base URL - */ - current(): string { - return this.baseUrl - } - - /** - * Set the base URL - * - * @param url - The new base URL - */ - setBaseUrl(url: string): void { - this.baseUrl = url - } -} \ No newline at end of file diff --git a/packages/http/src/index.ts b/packages/http/src/index.ts index 0c9d1cf3..25c6a2a9 100644 --- a/packages/http/src/index.ts +++ b/packages/http/src/index.ts @@ -7,4 +7,3 @@ export * from './Request' export * from './Resources/ApiResource' export * from './Resources/JsonResource' export * from './Response' -export * from './Url' From 3b17e5d9936c7a695e20e0f0baff06bc28bb5ac1 Mon Sep 17 00:00:00 2001 From: Martins-O Date: Mon, 29 Sep 2025 22:01:12 +0100 Subject: [PATCH 3/6] feat(http): add Url class binding to service container - Create Url class with URL generation functionality - Bind Url to container as 'app.url' in HttpServiceProvider - Add global url() helper that resolves via container - Support both url() (returns instance) and url(path) (returns string) - Add TypeScript definitions for global url() function --- packages/core/src/app.globals.d.ts | 12 ++++- .../http/src/Providers/HttpServiceProvider.ts | 16 +++++++ packages/http/src/Url.ts | 47 +++++++++++++++++++ packages/http/src/index.ts | 1 + 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 packages/http/src/Url.ts diff --git a/packages/core/src/app.globals.d.ts b/packages/core/src/app.globals.d.ts index 3f869216..416bff95 100644 --- a/packages/core/src/app.globals.d.ts +++ b/packages/core/src/app.globals.d.ts @@ -75,8 +75,16 @@ declare global { /** * Get the database path - * - * @param path + * + * @param path */ function database_path (path?: string): string + + /** + * Generate URLs or get the Url instance + * + * @param path + */ + function url (): import('../../../http/src/Url').Url + function url (path: string): string } diff --git a/packages/http/src/Providers/HttpServiceProvider.ts b/packages/http/src/Providers/HttpServiceProvider.ts index 54d21c2e..60d095aa 100644 --- a/packages/http/src/Providers/HttpServiceProvider.ts +++ b/packages/http/src/Providers/HttpServiceProvider.ts @@ -4,6 +4,7 @@ import { H3, serve } from 'h3' import { FireCommand } from '../Commands/FireCommand' import { ServiceProvider } from '@h3ravel/core' +import { Url } from '../Url' /** * Sets up HTTP kernel and request lifecycle. @@ -26,7 +27,22 @@ export class HttpServiceProvider extends ServiceProvider { /** Bind the HTTP server to the service container */ this.app.singleton('http.serve', () => serve) + /** Bind Url class to the service container */ + this.app.singleton('app.url', () => { + return new Url() + }) + /** Register Musket Commands */ this.commands([FireCommand]) } + + boot () { + globalThis.url = (...args: any[]) => { + const urlInstance = this.app.make('app.url') as Url + if (args.length === 0) { + return urlInstance + } + return urlInstance.to(args[0]) + } + } } diff --git a/packages/http/src/Url.ts b/packages/http/src/Url.ts new file mode 100644 index 00000000..3227f0db --- /dev/null +++ b/packages/http/src/Url.ts @@ -0,0 +1,47 @@ +export class Url { + private baseUrl: string + + constructor(baseUrl?: string) { + this.baseUrl = baseUrl || process.env.APP_URL || 'http://localhost:3000' + } + + /** + * Generate a URL for the given path + * + * @param path - The path to append to the base URL + * @returns The full URL + */ + to(path?: string): string { + if (!path) return this.baseUrl + + const cleanPath = path.startsWith('/') ? path : `/${path}` + return `${this.baseUrl.replace(/\/$/, '')}${cleanPath}` + } + + /** + * Generate a secure URL (HTTPS) for the given path + * + * @param path - The path to append to the base URL + * @returns The full HTTPS URL + */ + secure(path?: string): string { + const url = this.to(path) + return url.replace(/^http:/, 'https:') + } + + /** + * Get the current base URL + */ + current(): string { + return this.baseUrl + } + + /** + * Set the base URL + * + * @param url - The new base URL + */ + setBaseUrl(url: string): void { + this.baseUrl = url + } +} \ No newline at end of file diff --git a/packages/http/src/index.ts b/packages/http/src/index.ts index 25c6a2a9..0c9d1cf3 100644 --- a/packages/http/src/index.ts +++ b/packages/http/src/index.ts @@ -7,3 +7,4 @@ export * from './Request' export * from './Resources/ApiResource' export * from './Resources/JsonResource' export * from './Response' +export * from './Url' From 9044483552623c7a6364a12795f3074515863f27 Mon Sep 17 00:00:00 2001 From: Legacy <52163001+3m1n3nc3@users.noreply.github.com> Date: Fri, 3 Oct 2025 15:41:39 +0100 Subject: [PATCH 4/6] Create app.globals.d.ts --- packages/http/src/app.globals.d.ts | 1 + 1 file changed, 1 insertion(+) create mode 100644 packages/http/src/app.globals.d.ts diff --git a/packages/http/src/app.globals.d.ts b/packages/http/src/app.globals.d.ts new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/packages/http/src/app.globals.d.ts @@ -0,0 +1 @@ + From 7dbf78f79b159eda5401413e8f04bed6b609854e Mon Sep 17 00:00:00 2001 From: Legacy <52163001+3m1n3nc3@users.noreply.github.com> Date: Fri, 3 Oct 2025 15:44:20 +0100 Subject: [PATCH 5/6] refactor: remove url helper hint --- packages/core/src/app.globals.d.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/core/src/app.globals.d.ts b/packages/core/src/app.globals.d.ts index 416bff95..6225e0ab 100644 --- a/packages/core/src/app.globals.d.ts +++ b/packages/core/src/app.globals.d.ts @@ -79,12 +79,4 @@ declare global { * @param path */ function database_path (path?: string): string - - /** - * Generate URLs or get the Url instance - * - * @param path - */ - function url (): import('../../../http/src/Url').Url - function url (path: string): string } From ee098566958884a81d9cf94ff3c8c35b1b2a9ccf Mon Sep 17 00:00:00 2001 From: Legacy <52163001+3m1n3nc3@users.noreply.github.com> Date: Fri, 3 Oct 2025 15:45:44 +0100 Subject: [PATCH 6/6] Update app.globals.d.ts --- packages/http/src/app.globals.d.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/http/src/app.globals.d.ts b/packages/http/src/app.globals.d.ts index 8b137891..72a5d21c 100644 --- a/packages/http/src/app.globals.d.ts +++ b/packages/http/src/app.globals.d.ts @@ -1 +1,13 @@ +import { Url } from './Url' +export { } + +declare global { + /** + * Generate URLs or get the Url instance + * + * @param path + */ + function url (): Url + function url (path: string): string +}