Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: known typings issue of type field #8

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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