@zap-studio/fetch 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,104 +0,0 @@
1
- import { FetchError } from "../errors";
2
- import type { ResponseType, ResponseTypeMap } from "../types";
3
-
4
- export async function parseResponse<TResponseType extends ResponseType>(
5
- response: Response,
6
- responseType: TResponseType,
7
- ): Promise<ResponseTypeMap[TResponseType]> {
8
- const contentType = response.headers.get("content-type");
9
-
10
- const parsers: Record<
11
- ResponseType,
12
- () => Promise<ResponseTypeMap[ResponseType]>
13
- > = {
14
- json: async () => {
15
- if (!contentType?.includes("application/json")) {
16
- throw new FetchError(
17
- "Expected JSON response but received no content type",
18
- response.status,
19
- response.statusText,
20
- response,
21
- );
22
- }
23
- return response.json();
24
- },
25
- arrayBuffer: async () => response.arrayBuffer(),
26
- blob: async () => response.blob(),
27
- bytes: async () => {
28
- const buffer = await response.arrayBuffer();
29
- return new Uint8Array(buffer);
30
- },
31
- clone: async () => response.clone(),
32
- formData: async () => {
33
- if (!contentType?.includes("multipart/form-data")) {
34
- throw new FetchError(
35
- "Expected FormData response but received different content type",
36
- response.status,
37
- response.statusText,
38
- response,
39
- );
40
- }
41
- return response.formData();
42
- },
43
- text: async () => {
44
- if (!contentType?.includes("text/")) {
45
- throw new FetchError(
46
- "Expected text response but received different content type",
47
- response.status,
48
- response.statusText,
49
- response,
50
- );
51
- }
52
- return response.text();
53
- },
54
- };
55
-
56
- const parser = parsers[responseType];
57
- if (!parser) {
58
- throw new FetchError(
59
- `Unsupported response type: ${responseType}`,
60
- response.status,
61
- response.statusText,
62
- response,
63
- );
64
- }
65
-
66
- return parser() as Promise<ResponseTypeMap[TResponseType]>;
67
- }
68
-
69
- export function prepareHeadersAndBody<TBody = unknown>(
70
- body: TBody,
71
- headers: HeadersInit | undefined,
72
- ): { body: BodyInit | null; headers: HeadersInit | undefined } {
73
- let preparedBody: BodyInit | null = null;
74
- let preparedHeaders = headers;
75
-
76
- // Handle null/undefined body
77
- if (body === null || body === undefined) {
78
- return { body: null, headers: preparedHeaders };
79
- }
80
-
81
- // Check if body is already a valid BodyInit type
82
- if (
83
- body instanceof FormData ||
84
- body instanceof URLSearchParams ||
85
- body instanceof Blob ||
86
- body instanceof ArrayBuffer ||
87
- body instanceof ReadableStream ||
88
- typeof body === "string"
89
- ) {
90
- preparedBody = body as BodyInit;
91
- } else if (typeof body === "object") {
92
- // If body is a plain object, stringify it and set Content-Type
93
- preparedBody = JSON.stringify(body);
94
- preparedHeaders = {
95
- "Content-Type": "application/json",
96
- ...headers,
97
- };
98
- } else {
99
- // Handle other primitive types by converting to string
100
- preparedBody = String(body);
101
- }
102
-
103
- return { body: preparedBody, headers: preparedHeaders };
104
- }