Skip to content

Commit

Permalink
Merge pull request #63 from kiwilan/develop
Browse files Browse the repository at this point in the history
v2.0.01
  • Loading branch information
ewilan-riviere authored Mar 30, 2024
2 parents 848fba5 + f6a1c64 commit a3c54b8
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 109 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
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.27",
"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
9 changes: 6 additions & 3 deletions lib/src/methods/LaravelRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ export class LaravelRouter {
) {}

public static create(routes?: RoutesType): LaravelRouter {
if (typeof window === 'undefined')
return new LaravelRouter({} as Record<App.Route.Name, App.Route.Link>)

// 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 +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 {
Expand Down
9 changes: 6 additions & 3 deletions src/Typed/Route/TypeRouteListTs.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static function make(Collection $routes): self
public function get(): string
{
$head = Typescriptable::head();
$appUrl = config('app.url');

return <<<typescript
{$head}
Expand All @@ -54,12 +55,14 @@ interface Window {
}
}
const appUrl = '{$appUrl}';
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 }
export { Routes, appUrl }
typescript;
}
Expand Down
94 changes: 0 additions & 94 deletions src/Typed/Route/TypeRouteTs.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,6 @@ public function get(): string
$this->tsGlobalTypesPatch = empty($this->tsGlobalTypesPatch) ? 'never' : $this->tsGlobalTypesPatch;
$this->tsGlobalTypesDelete = empty($this->tsGlobalTypesDelete) ? 'never' : $this->tsGlobalTypesDelete;

// return <<<typescript
// // This file is auto generated by TypescriptableLaravel.
// declare namespace App.Route {
// export interface Name {$this->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 <<<typescript
Expand Down Expand Up @@ -115,15 +89,6 @@ private function parse()
$this->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
Expand Down Expand Up @@ -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 = <<<typescript
// params: {
// {$params}
// }
// typescript;
// }

// return <<<typescript
// type {$route->routeName()} = {
// name: '{$route->pathType()}',
// {$params},
// query?: Record<string, App.Route.Param>,
// hash?: string,
// }
// typescript;
// }, ";\n");
// }

// private function setTsGlobalTypes(): string
// {
// return $this->collectRoutes(function (TypeRoute $route) {
// return <<<typescript
// App.Route.Typed.{$route->routeName()}
// typescript;
// }, ' | ');
// }

// private function setTsGlobalTypesMethod(string $method): string
// {
// $routes = $this->collectRoutesMethod($method);

// return collect($routes)
// ->map(function (TypeRoute $route) {
// return <<<typescript
// App.Route.Typed.{$route->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));
Expand Down

0 comments on commit a3c54b8

Please sign in to comment.