diff --git a/composer.json b/composer.json index e9e572e..6283f27 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "kiwilan/typescriptable-laravel", "description": "PHP package for Laravel to type Eloquent models, routes, Spatie Settings with autogenerated TypeScript. If you want to use some helpers with Inertia, you can install associated NPM package.", - "version": "2.0.0", + "version": "2.0.01", "keywords": [ "kiwilan", "laravel", diff --git a/lib/global.d.ts b/lib/global.d.ts index cad1b6d..0ec5067 100644 --- a/lib/global.d.ts +++ b/lib/global.d.ts @@ -5,6 +5,6 @@ declare global { } // @ts-expect-error - Routes is defined in the global scope -window.Routes = window.Routes || {} +window?.Routes = window?.Routes || {} export {} diff --git a/lib/package.json b/lib/package.json index 27477ac..ed17f69 100644 --- a/lib/package.json +++ b/lib/package.json @@ -1,7 +1,7 @@ { "name": "@kiwilan/typescriptable-laravel", "type": "module", - "version": "2.4.25", + "version": "2.4.27", "description": "Add some helpers for your Inertia app with TypeScript.", "author": "Ewilan Rivière ", "license": "MIT", diff --git a/lib/routes.ts b/lib/routes.ts index 0699c62..78413a4 100644 --- a/lib/routes.ts +++ b/lib/routes.ts @@ -280,8 +280,8 @@ declare global { if (typeof window !== 'undefined') { // eslint-disable-next-line valid-typeof - if (typeof window !== undefined && typeof window.Routes !== undefined) - window.Routes = Routes + if (typeof window !== undefined && typeof window?.Routes !== undefined) + window?.Routes = Routes } export { Routes } diff --git a/lib/src/composables/useLazy.ts b/lib/src/composables/useLazy.ts index 6348916..641efe9 100644 --- a/lib/src/composables/useLazy.ts +++ b/lib/src/composables/useLazy.ts @@ -4,7 +4,7 @@ import { ref } from 'vue' * Lazy load images */ export function useLazy(loadingColor?: string) { - let url = window.location.href + let url = window?.location.href const baseURL = url.split('/') baseURL.pop() url = baseURL.join('/') diff --git a/lib/src/composables/usePagination.ts b/lib/src/composables/usePagination.ts index 0ff3a5b..7971e51 100644 --- a/lib/src/composables/usePagination.ts +++ b/lib/src/composables/usePagination.ts @@ -15,7 +15,7 @@ export function usePagination(models: App.Paginate) { const nextPage = ref() function getQuery() { - const query = new URLSearchParams(window.location.search) + const query = new URLSearchParams(window?.location.search) query.delete('page') return query.toString() @@ -112,7 +112,7 @@ export function usePagination(models: App.Paginate) { paginate() function convertUrl(queryName: string, queryValue: number | string) { - let currentUrl = window.location.href + let currentUrl = window?.location.href if (currentUrl.includes(`${queryName}=`)) currentUrl = currentUrl.replace(/page=\d+/, `${queryName}=${queryValue}`) else if (currentUrl.includes('?')) diff --git a/lib/src/composables/useQuery.ts b/lib/src/composables/useQuery.ts index 12c5b20..994e42b 100644 --- a/lib/src/composables/useQuery.ts +++ b/lib/src/composables/useQuery.ts @@ -28,7 +28,7 @@ export function useQuery(propQuery: App.Paginate, prop: string = 'query') * Set the sort value to the query. */ function initializeSort() { - const query = new URLSearchParams(window.location.search) + const query = new URLSearchParams(window?.location.search) const querySort = query.get('sort') if (querySort) sort.value = querySort diff --git a/lib/src/methods/LaravelRouter.ts b/lib/src/methods/LaravelRouter.ts index ad0ddd3..b1349d0 100644 --- a/lib/src/methods/LaravelRouter.ts +++ b/lib/src/methods/LaravelRouter.ts @@ -6,9 +6,12 @@ export class LaravelRouter { ) {} public static create(routes?: RoutesType): LaravelRouter { + if (typeof window === 'undefined') + return new LaravelRouter({} as Record) + // eslint-disable-next-line valid-typeof - if (!routes && typeof window !== undefined && typeof window.Routes !== undefined) - routes = window.Routes + if (!routes && typeof window !== undefined && typeof window?.Routes !== undefined) + routes = window?.Routes return new LaravelRouter(routes as Record) } @@ -50,7 +53,7 @@ export class LaravelRouter { } public getCurrentUrl(): string { - return window.location.pathname + return window?.location.pathname } public matchRoute(route: App.Route.Link, parts: string[], partsRoute: string[]): App.Route.Link | undefined { diff --git a/src/Typed/Route/TypeRouteListTs.php b/src/Typed/Route/TypeRouteListTs.php index b690fa2..dcf26df 100644 --- a/src/Typed/Route/TypeRouteListTs.php +++ b/src/Typed/Route/TypeRouteListTs.php @@ -41,6 +41,7 @@ public static function make(Collection $routes): self public function get(): string { $head = Typescriptable::head(); + $appUrl = config('app.url'); return <<tsGlobalTypesPatch = empty($this->tsGlobalTypesPatch) ? 'never' : $this->tsGlobalTypesPatch; $this->tsGlobalTypesDelete = empty($this->tsGlobalTypesDelete) ? 'never' : $this->tsGlobalTypesDelete; - // return <<tsNames} - // export interface Path {$this->tsPaths}; - // export interface Params { - // {$this->tsParams} - // }; - - // export interface Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'; - // export interface Entity { name: App.Route.Name; path: App.Route.Path; params?: App.Route.Params[Route.Name], method: App.Route.Method; } - - // export interface Param = string | number | boolean | undefined - // export interface Type {$this->tsGlobalTypes} - // export interface TypeGet {$this->tsGlobalTypesGet} - // export interface TypePost {$this->tsGlobalTypesPost} - // export interface TypePut {$this->tsGlobalTypesPut} - // export interface TypePatch {$this->tsGlobalTypesPatch} - // export interface TypeDelete {$this->tsGlobalTypesDelete} - // } - - // declare namespace App.Typed { - // {$this->tsTypes} - // } - // typescript; - $head = Typescriptable::head(); return <<tsNames = $this->setTsNames(); $this->tsPaths = $this->setTsPaths(); $this->tsParams = $this->setTsParams(); - - // $this->tsTypes = $this->setTsTypes(); - // $this->tsGlobalTypes = $this->setTsGlobalTypes(); - - // $this->tsGlobalTypesGet = $this->setTsGlobalTypesMethod('GET'); - // $this->tsGlobalTypesPost = $this->setTsGlobalTypesMethod('POST'); - // $this->tsGlobalTypesPut = $this->setTsGlobalTypesMethod('PUT'); - // $this->tsGlobalTypesPatch = $this->setTsGlobalTypesMethod('PATCH'); - // $this->tsGlobalTypesDelete = $this->setTsGlobalTypesMethod('DELETE'); } private function setTsNames(): string @@ -175,65 +140,6 @@ private function setTsParams(): string }, "\n"); } - // private function setTsTypes(): string - // { - // return $this->collectRoutes(function (TypeRoute $route) { - // $params = ''; - - // if (empty($route->parameters())) { - // $params = 'params?: undefined'; - // } else { - // $params = collect($route->parameters()) - // ->map(function (TypeRouteParam $param) { - // $required = $param->required() ? '' : '?'; - - // return "{$param->name()}{$required}: App.Route.Param,"; - // }) - // ->join(' '); - // $params = <<routeName()} = { - // name: '{$route->pathType()}', - // {$params}, - // query?: Record, - // hash?: string, - // } - // typescript; - // }, ";\n"); - // } - - // private function setTsGlobalTypes(): string - // { - // return $this->collectRoutes(function (TypeRoute $route) { - // return <<routeName()} - // typescript; - // }, ' | '); - // } - - // private function setTsGlobalTypesMethod(string $method): string - // { - // $routes = $this->collectRoutesMethod($method); - - // return collect($routes) - // ->map(function (TypeRoute $route) { - // return <<routeName()} - // typescript; - // })->join(' | '); - // } - - // private function collectRoutesMethod(string $method): Collection - // { - // return $this->routes->filter(fn (TypeRoute $route) => $route->method() === $method); - // } - private function collectRoutes(Closure $closure, ?string $join = null): string|Collection { $routes = $this->routes->map(fn (TypeRoute $route, string $key) => $closure($route, $key));