-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathbody.ts
27 lines (26 loc) · 999 Bytes
/
body.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import Stream from "stream";
import { isArrayBuffer } from "../compat/arrayBuffer.js";
import { isBuffer } from "../compat/buffer.js";
import { isReactNative, isWeb } from "../compat/env.js";
import { Headers, RequestDataPayload } from "../types.js";
export function requestDataToFetchBody(data: RequestDataPayload): [BodyInit, Headers] {
if (!isWeb() && !isReactNative() && data instanceof Stream.Readable) {
// @ts-ignore
return [data, {}];
}
if (typeof data === "string") {
return [data, {}];
} else if (isBuffer(data)) {
return [data as Buffer, {}];
} else if (isArrayBuffer(data)) {
return [data as ArrayBuffer, {}];
} else if (data && typeof data === "object") {
return [
JSON.stringify(data as Record<string, any>),
{
"content-type": "application/json"
}
];
}
throw new Error(`Unable to convert request body: Unexpected body type: ${typeof data}`);
}