Skip to content

Commit fc48d03

Browse files
committed
refactor: move to types
1 parent a9d2aef commit fc48d03

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

src/fetch.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import type { Readable } from "node:stream";
22
import destr from "destr";
33
import { withBase, withQuery } from "ufo";
4-
import type { Fetch, RequestInfo, RequestInit, Response } from "./types";
4+
import type {
5+
Fetch,
6+
RequestInfo,
7+
RequestInit,
8+
Response,
9+
ResponseType,
10+
MappedType,
11+
} from "./types";
512
import { createFetchError } from "./error";
613
import {
714
isPayloadMethod,
815
isJSONSerializable,
916
detectResponseType,
10-
ResponseType,
11-
MappedType,
1217
mergeFetchOptions,
1318
} from "./utils";
1419

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { createFetch } from "./base";
22

33
export * from "./base";
4-
export type { ResponseType, ResponseMap, MappedType } from "./utils";
4+
5+
export type { ResponseType, ResponseMap, MappedType } from "./types";
56

67
// ref: https://github.com/tc39/proposal-global
78
const _globalThis = (function () {

src/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,17 @@ export type Fetch = typeof globalThis.fetch;
22
export type RequestInfo = globalThis.RequestInfo;
33
export type RequestInit = globalThis.RequestInit;
44
export type Response = globalThis.Response;
5+
6+
export interface ResponseMap {
7+
blob: Blob;
8+
text: string;
9+
arrayBuffer: ArrayBuffer;
10+
stream: ReadableStream<Uint8Array>;
11+
}
12+
13+
export type ResponseType = keyof ResponseMap | "json";
14+
15+
export type MappedType<
16+
R extends ResponseType,
17+
JsonType = any,
18+
> = R extends keyof ResponseMap ? ResponseMap[R] : JsonType;

src/utils.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { FetchOptions } from "./fetch";
2+
import type { ResponseType } from "./types";
23

34
const payloadMethods = new Set(
45
Object.freeze(["PATCH", "POST", "PUT", "DELETE"])
@@ -39,19 +40,6 @@ const textTypes = new Set([
3940

4041
const JSON_RE = /^application\/(?:[\w!#$%&*.^`~-]*\+)?json(;.+)?$/i;
4142

42-
export interface ResponseMap {
43-
blob: Blob;
44-
text: string;
45-
arrayBuffer: ArrayBuffer;
46-
stream: ReadableStream<Uint8Array>;
47-
}
48-
49-
export type ResponseType = keyof ResponseMap | "json";
50-
export type MappedType<
51-
R extends ResponseType,
52-
JsonType = any,
53-
> = R extends keyof ResponseMap ? ResponseMap[R] : JsonType;
54-
5543
// This provides reasonable defaults for the correct parser based on Content-Type header.
5644
export function detectResponseType(_contentType = ""): ResponseType {
5745
if (!_contentType) {

0 commit comments

Comments
 (0)