UNPKG

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