Skip to content

Commit

Permalink
Fix undefined window.Routes assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Mar 17, 2024
1 parent d7728bc commit 381d7e3
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@kiwilan/typescriptable-laravel",
"type": "module",
"version": "2.4.25",
"version": "2.4.26",
"description": "Add some helpers for your Inertia app with TypeScript.",
"author": "Ewilan Rivière <ewilan.riviere@gmail.com>",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions lib/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
2 changes: 1 addition & 1 deletion lib/src/composables/useLazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')
Expand Down
4 changes: 2 additions & 2 deletions lib/src/composables/usePagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function usePagination(models: App.Paginate) {
const nextPage = ref<string>()

function getQuery() {
const query = new URLSearchParams(window.location.search)
const query = new URLSearchParams(window?.location.search)
query.delete('page')

return query.toString()
Expand Down Expand Up @@ -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('?'))
Expand Down
2 changes: 1 addition & 1 deletion lib/src/composables/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function useQuery<T>(propQuery: App.Paginate<T>, 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
Expand Down
6 changes: 3 additions & 3 deletions lib/src/methods/LaravelRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export class LaravelRouter {

public static create(routes?: RoutesType): LaravelRouter {
// 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<App.Route.Name, App.Route.Link>)
}
Expand Down Expand Up @@ -50,7 +50,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 {
Expand Down
4 changes: 2 additions & 2 deletions src/Typed/Route/TypeRouteListTs.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ interface Window {
}
if (typeof window !== 'undefined') {
if (typeof window !== undefined && typeof window.Routes !== undefined)
window.Routes = Routes
if (typeof window !== undefined && typeof window?.Routes !== undefined)
window?.Routes = Routes
}
export { Routes }
Expand Down

0 comments on commit 381d7e3

Please sign in to comment.