diff --git a/packages/core/src/app.globals.d.ts b/packages/core/src/app.globals.d.ts index 3f869216..6225e0ab 100644 --- a/packages/core/src/app.globals.d.ts +++ b/packages/core/src/app.globals.d.ts @@ -75,8 +75,8 @@ declare global { /** * Get the database path - * - * @param path + * + * @param path */ function database_path (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/app.globals.d.ts b/packages/http/src/app.globals.d.ts new file mode 100644 index 00000000..72a5d21c --- /dev/null +++ b/packages/http/src/app.globals.d.ts @@ -0,0 +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 +} 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'