Skip to content

Commit

Permalink
Improve type checking of request data
Browse files Browse the repository at this point in the history
  • Loading branch information
7nohe committed Apr 28, 2023
1 parent d8a181b commit ab020ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
29 changes: 15 additions & 14 deletions packages/core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ export class Router {
}
}

public visit(
public visit<TData extends RequestPayload = RequestPayload>(
href: string | URL,
{
method = 'get',
data = {},
data: visitData = {} as TData,
replace = false,
preserveScroll = false,
preserveState = false,
Expand All @@ -272,9 +272,10 @@ export class Router {
onSuccess = () => {},
onError = () => {},
queryStringArrayFormat = 'brackets',
}: VisitOptions = {},
}: VisitOptions<TData> = {},
): void {
let url = typeof href === 'string' ? hrefToUrl(href) : href
let data: RequestPayload = visitData;

if ((hasFiles(data) || forceFormData) && !(data instanceof FormData)) {
data = objectToFormData(data)
Expand Down Expand Up @@ -498,19 +499,19 @@ export class Router {
}
}

public get(
public get<TData extends RequestPayload = RequestPayload>(
url: URL | string,
data: RequestPayload = {},
data: TData = {} as TData,
options: Exclude<VisitOptions, 'method' | 'data'> = {},
): void {
return this.visit(url, { ...options, method: 'get', data })
}

public reload(options: Exclude<VisitOptions, 'preserveScroll' | 'preserveState'> = {}): void {
public reload<TData extends RequestPayload = RequestPayload>(options: Exclude<VisitOptions<TData>, 'preserveScroll' | 'preserveState'> = {}): void {
return this.visit(window.location.href, { ...options, preserveScroll: true, preserveState: true })
}

public replace(url: URL | string, options: Exclude<VisitOptions, 'replace'> = {}): void {
public replace<TData extends RequestPayload = RequestPayload>(url: URL | string, options: Exclude<VisitOptions<TData>, 'replace'> = {}): void {
console.warn(
`Inertia.replace() has been deprecated and will be removed in a future release. Please use Inertia.${
options.method ?? 'get'
Expand All @@ -519,31 +520,31 @@ export class Router {
return this.visit(url, { preserveState: true, ...options, replace: true })
}

public post(
public post<TData extends RequestPayload = RequestPayload>(
url: URL | string,
data: RequestPayload = {},
data: TData = {} as TData,
options: Exclude<VisitOptions, 'method' | 'data'> = {},
): void {
return this.visit(url, { preserveState: true, ...options, method: 'post', data })
}

public put(
public put<TData extends RequestPayload = RequestPayload>(
url: URL | string,
data: RequestPayload = {},
data: TData = {} as TData,
options: Exclude<VisitOptions, 'method' | 'data'> = {},
): void {
return this.visit(url, { preserveState: true, ...options, method: 'put', data })
}

public patch(
public patch<TData extends RequestPayload = RequestPayload>(
url: URL | string,
data: RequestPayload = {},
data: TData = {} as TData,
options: Exclude<VisitOptions, 'method' | 'data'> = {},
): void {
return this.visit(url, { preserveState: true, ...options, method: 'patch', data })
}

public delete(url: URL | string, options: Exclude<VisitOptions, 'method'> = {}): void {
public delete<TData extends RequestPayload = RequestPayload>(url: URL | string, options: Exclude<VisitOptions<TData>, 'method'> = {}): void {
return this.visit(url, { preserveState: true, ...options, method: 'delete' })
}

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export type LocationVisit = {
preserveScroll: boolean
}

export type Visit = {
export type Visit<TData extends RequestPayload = RequestPayload> = {
method: Method
data: RequestPayload
data: TData
replace: boolean
preserveScroll: PreserveStateOption
preserveState: PreserveStateOption
Expand Down Expand Up @@ -165,8 +165,8 @@ export type GlobalEventCallback<TEventName extends GlobalEventNames> = (
...params: GlobalEventParameters<TEventName>
) => GlobalEventResult<TEventName>

export type VisitOptions = Partial<
Visit & {
export type VisitOptions<TData extends RequestPayload = RequestPayload> = Partial<
Visit<TData> & {
onCancelToken: { ({ cancel }: { cancel: VoidFunction }): void }
onBefore: GlobalEventCallback<'before'>
onStart: GlobalEventCallback<'start'>
Expand Down

0 comments on commit ab020ac

Please sign in to comment.