Skip to content

Commit

Permalink
types: add types for throwOnError
Browse files Browse the repository at this point in the history
  • Loading branch information
fzn0x committed Jun 25, 2024
1 parent 92ea200 commit ef576c3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
2 changes: 1 addition & 1 deletion browser/hyperfetch-browser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion browser/hyperfetch-browser.min.js.map

Large diffs are not rendered by default.

69 changes: 39 additions & 30 deletions src/types/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,57 @@ export interface RequestOptions extends RequestInit {
initOptions?: InitOptions
}

export type RequestFunction = (
url: string,
options?: RequestOptions,
data?: { [key: string]: unknown },
initOptions?: InitOptions
) => Promise<[Error | null, null]>
export type RequestFunction = {
<T>(
url: string,
options?: RequestOptions & { initOptions: { throwOnError: true } },
data?: { [key: string]: unknown },
initOptions?: InitOptions & { throwOnError: true }
): Promise<Error | T>

export interface HttpRequestFunctions {
get<T = unknown>(
<T>(
url: string,
options?: RequestOptions,
data?: { [key: string]: unknown }
options?: RequestOptions & { initOptions: { throwOnError: false } },
data?: { [key: string]: unknown },
initOptions?: InitOptions & { throwOnError: false }
): Promise<[Error | null, T | null]>
post<T = unknown>(
url: string,
options?: RequestOptions,
data?: { [key: string]: unknown }
): Promise<[Error | null, T | null]>
put<T = unknown>(

<T>(
url: string,
options?: RequestOptions,
data?: { [key: string]: unknown }
options?: RequestOptions & { initOptions?: { throwOnError?: boolean } },
data?: { [key: string]: unknown },
initOptions?: InitOptions
): Promise<[Error | null, T | null]>
delete<T = unknown>(
url: string,
options?: RequestOptions,
data?: { [key: string]: unknown }
): Promise<[Error | null, T | null]>
patch<T = unknown>(
}

export type HttpRequestFunction = {
<T>(
url: string,
options?: RequestOptions,
options?: RequestOptions & { initOptions: { throwOnError: true } },
data?: { [key: string]: unknown }
): Promise<[Error | null, T | null]>
head<T = unknown>(
): Promise<Error | T>

<T>(
url: string,
options?: RequestOptions,
options?: RequestOptions & { initOptions: { throwOnError: false } },
data?: { [key: string]: unknown }
): Promise<[Error | null, T | null]>
options<T = unknown>(

<T>(
url: string,
options?: RequestOptions,
options?: RequestOptions & { initOptions?: { throwOnError?: boolean } },
data?: { [key: string]: unknown }
): Promise<[Error | null, T | null]>
}

export interface HttpRequestFunctions {
get: HttpRequestFunction
post: HttpRequestFunction
put: HttpRequestFunction
delete: HttpRequestFunction
patch: HttpRequestFunction
head: HttpRequestFunction
options: HttpRequestFunction

getAbortController(): AbortController | null
}
2 changes: 2 additions & 0 deletions src/utils/create-http-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ export const createHTTPMethod = (
initOptions = options.initOptions
}

delete options.initOptions

return createRequest(url, { method, ...options }, data, initOptions)
}
2 changes: 1 addition & 1 deletion src/utils/create-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const createRequest: RequestFunction = async (
options = {},
data,
{ baseUrl, hooks, debug, throwOnError }: InitOptions = Object.create(null)
): Promise<[Error | null, null]> => {
) => {
const {
method = 'GET',
retries = 0,
Expand Down

0 comments on commit ef576c3

Please sign in to comment.