Skip to content

Commit

Permalink
fix: known typings issue of type field (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Dec 14, 2023
1 parent 5e40f3d commit 5d91037
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-plants-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"x-fetch": patch
---

fix: known typings issue of `type` field
17 changes: 11 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ export const ApiMethod = {

export type ApiMethod = ValueOf<typeof ApiMethod>

export interface FetchApiOptions extends Omit<RequestInit, 'body' | 'method'> {
export interface FetchApiBaseOptions
extends Omit<RequestInit, 'body' | 'method'> {
method?: ApiMethod
body?: BodyInit | object
query?: URLSearchParametersOptions
json?: boolean
}

export interface FetchApiOptions extends FetchApiBaseOptions {
type?: 'arrayBuffer' | 'blob' | 'json' | 'text' | null
}

Expand Down Expand Up @@ -67,23 +71,24 @@ export const createFetchApi = () => {

function fetchApi(
url: string,
options: FetchApiOptions & { type: null },
options: FetchApiBaseOptions & { type: null },
): Promise<Response>
// @ts-expect-error -- no idea, it sucks
function fetchApi(
url: string,
options: FetchApiOptions & { type: 'arraybuffer' },
options: FetchApiBaseOptions & { type: 'arraybuffer' },
): Promise<ArrayBuffer>
function fetchApi(
url: string,
options: FetchApiOptions & { type: 'blob' },
options: FetchApiBaseOptions & { type: 'blob' },
): Promise<Blob>
function fetchApi(
url: string,
options: FetchApiOptions & { type: 'text' },
options: FetchApiBaseOptions & { type: 'text' },
): Promise<string>
function fetchApi<T>(
url: string,
options?: FetchApiOptions & { type?: 'json' },
options?: FetchApiBaseOptions & { type?: 'json' },
): Promise<T>
// eslint-disable-next-line sonarjs/cognitive-complexity
async function fetchApi(
Expand Down

0 comments on commit 5d91037

Please sign in to comment.