From fc48d0361c7e40529c34e5bb00964bcfe37feb12 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 26 Oct 2023 19:03:38 +0200 Subject: [PATCH] refactor: move to types --- src/fetch.ts | 11 ++++++++--- src/index.ts | 3 ++- src/types.ts | 14 ++++++++++++++ src/utils.ts | 14 +------------- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/fetch.ts b/src/fetch.ts index 740a9f30..0670ecc2 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -1,14 +1,19 @@ import type { Readable } from "node:stream"; import destr from "destr"; import { withBase, withQuery } from "ufo"; -import type { Fetch, RequestInfo, RequestInit, Response } from "./types"; +import type { + Fetch, + RequestInfo, + RequestInit, + Response, + ResponseType, + MappedType, +} from "./types"; import { createFetchError } from "./error"; import { isPayloadMethod, isJSONSerializable, detectResponseType, - ResponseType, - MappedType, mergeFetchOptions, } from "./utils"; diff --git a/src/index.ts b/src/index.ts index f05dc1c6..0907354b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,8 @@ import { createFetch } from "./base"; export * from "./base"; -export type { ResponseType, ResponseMap, MappedType } from "./utils"; + +export type { ResponseType, ResponseMap, MappedType } from "./types"; // ref: https://github.com/tc39/proposal-global const _globalThis = (function () { diff --git a/src/types.ts b/src/types.ts index b3ffc047..4c1407c5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,3 +2,17 @@ export type Fetch = typeof globalThis.fetch; export type RequestInfo = globalThis.RequestInfo; export type RequestInit = globalThis.RequestInit; export type Response = globalThis.Response; + +export interface ResponseMap { + blob: Blob; + text: string; + arrayBuffer: ArrayBuffer; + stream: ReadableStream; +} + +export type ResponseType = keyof ResponseMap | "json"; + +export type MappedType< + R extends ResponseType, + JsonType = any, +> = R extends keyof ResponseMap ? ResponseMap[R] : JsonType; diff --git a/src/utils.ts b/src/utils.ts index 86a5de98..9e418411 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,5 @@ import type { FetchOptions } from "./fetch"; +import type { ResponseType } from "./types"; const payloadMethods = new Set( Object.freeze(["PATCH", "POST", "PUT", "DELETE"]) @@ -39,19 +40,6 @@ const textTypes = new Set([ const JSON_RE = /^application\/(?:[\w!#$%&*.^`~-]*\+)?json(;.+)?$/i; -export interface ResponseMap { - blob: Blob; - text: string; - arrayBuffer: ArrayBuffer; - stream: ReadableStream; -} - -export type ResponseType = keyof ResponseMap | "json"; -export type MappedType< - R extends ResponseType, - JsonType = any, -> = R extends keyof ResponseMap ? ResponseMap[R] : JsonType; - // This provides reasonable defaults for the correct parser based on Content-Type header. export function detectResponseType(_contentType = ""): ResponseType { if (!_contentType) {