1 | import { Buffer } from "node:buffer";
|
2 | import { Readable } from "node:stream";
|
3 | import { HTTPFetchError } from "./exceptions.js";
|
4 | import { USER_AGENT } from "./version.js";
|
5 | export function convertResponseToReadable(response) {
|
6 | const reader = response.body.getReader();
|
7 | return new Readable({
|
8 | async read() {
|
9 | const { done, value } = await reader.read();
|
10 | if (done) {
|
11 | this.push(null);
|
12 | }
|
13 | else {
|
14 | this.push(Buffer.from(value));
|
15 | }
|
16 | },
|
17 | });
|
18 | }
|
19 | export default class HTTPFetchClient {
|
20 | baseURL;
|
21 | defaultHeaders;
|
22 | constructor(config) {
|
23 | this.baseURL = config.baseURL;
|
24 | this.defaultHeaders = {
|
25 | "User-Agent": USER_AGENT,
|
26 | ...config.defaultHeaders,
|
27 | };
|
28 | }
|
29 | async get(url, params) {
|
30 | const requestUrl = new URL(url, this.baseURL);
|
31 | if (params) {
|
32 | const searchParams = new URLSearchParams();
|
33 | for (const key in params) {
|
34 | if (params.hasOwnProperty(key)) {
|
35 | searchParams.append(key, params[key]);
|
36 | }
|
37 | }
|
38 | requestUrl.search = searchParams.toString();
|
39 | }
|
40 | const response = await fetch(requestUrl, {
|
41 | headers: this.defaultHeaders,
|
42 | });
|
43 | await this.checkResponseStatus(response);
|
44 | return response;
|
45 | }
|
46 | async post(url, body, config) {
|
47 | const requestUrl = new URL(url, this.baseURL);
|
48 | const response = await fetch(requestUrl, {
|
49 | method: "POST",
|
50 | headers: {
|
51 | "Content-Type": "application/json",
|
52 | ...this.defaultHeaders,
|
53 | ...(config && config.headers),
|
54 | },
|
55 | body: JSON.stringify(body),
|
56 | });
|
57 | await this.checkResponseStatus(response);
|
58 | return response;
|
59 | }
|
60 | async put(url, body, config) {
|
61 | const requestUrl = new URL(url, this.baseURL);
|
62 | const response = await fetch(requestUrl, {
|
63 | method: "PUT",
|
64 | headers: {
|
65 | "Content-Type": "application/json",
|
66 | ...this.defaultHeaders,
|
67 | ...(config && config.headers),
|
68 | },
|
69 | body: JSON.stringify(body),
|
70 | });
|
71 | await this.checkResponseStatus(response);
|
72 | return response;
|
73 | }
|
74 | async postForm(url, body) {
|
75 | const requestUrl = new URL(url, this.baseURL);
|
76 | const params = new URLSearchParams();
|
77 | for (const key in body) {
|
78 | if (body.hasOwnProperty(key)) {
|
79 | params.append(key, body[key]);
|
80 | }
|
81 | }
|
82 | const response = await fetch(requestUrl, {
|
83 | method: "POST",
|
84 | headers: {
|
85 | "Content-Type": "application/x-www-form-urlencoded",
|
86 | ...this.defaultHeaders,
|
87 | },
|
88 | body: params.toString(),
|
89 | });
|
90 | await this.checkResponseStatus(response);
|
91 | return response;
|
92 | }
|
93 | async postFormMultipart(url, form) {
|
94 | const requestUrl = new URL(url, this.baseURL);
|
95 | const response = await fetch(requestUrl, {
|
96 | method: "POST",
|
97 | headers: {
|
98 | ...this.defaultHeaders,
|
99 | },
|
100 | body: form,
|
101 | });
|
102 | await this.checkResponseStatus(response);
|
103 | return response;
|
104 | }
|
105 | async putFormMultipart(url, form, config) {
|
106 | const requestUrl = new URL(url, this.baseURL);
|
107 | const response = await fetch(requestUrl, {
|
108 | method: "PUT",
|
109 | headers: {
|
110 | ...this.defaultHeaders,
|
111 | ...(config && (config.headers ? config.headers : {})),
|
112 | },
|
113 | body: form,
|
114 | });
|
115 | await this.checkResponseStatus(response);
|
116 | return response;
|
117 | }
|
118 | async postBinaryContent(url, body) {
|
119 | const requestUrl = new URL(url, this.baseURL);
|
120 | const response = await fetch(requestUrl, {
|
121 | method: "POST",
|
122 | headers: {
|
123 | "Content-Type": body.type,
|
124 | ...this.defaultHeaders,
|
125 | },
|
126 | body: body,
|
127 | });
|
128 | await this.checkResponseStatus(response);
|
129 | return response;
|
130 | }
|
131 | async delete(url, params) {
|
132 | const requestUrl = new URL(url, this.baseURL);
|
133 | if (params) {
|
134 | requestUrl.search = new URLSearchParams(params).toString();
|
135 | }
|
136 | const response = await fetch(requestUrl, {
|
137 | method: "DELETE",
|
138 | headers: {
|
139 | ...this.defaultHeaders,
|
140 | },
|
141 | });
|
142 | await this.checkResponseStatus(response);
|
143 | return response;
|
144 | }
|
145 | async checkResponseStatus(response) {
|
146 | const { ok, status, statusText, headers } = response;
|
147 | const message = `${status} - ${statusText}`;
|
148 | if (!ok) {
|
149 | const body = await response.text();
|
150 | throw new HTTPFetchError(message, {
|
151 | status,
|
152 | statusText,
|
153 | headers,
|
154 | body,
|
155 | });
|
156 | }
|
157 | }
|
158 | }
|
159 |
|
\ | No newline at end of file |