UNPKG

4.62 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const axios_1 = require("axios");
4const node_stream_1 = require("node:stream");
5const exceptions_1 = require("./exceptions");
6const pkg = require("../package.json");
7class HTTPClient {
8 constructor(config = {}) {
9 this.config = config;
10 const { baseURL, defaultHeaders } = config;
11 this.instance = axios_1.default.create({
12 baseURL,
13 headers: Object.assign({}, defaultHeaders, {
14 "User-Agent": `${pkg.name}/${pkg.version}`,
15 }),
16 });
17 this.instance.interceptors.response.use(res => res, err => Promise.reject(this.wrapError(err)));
18 }
19 async get(url, params) {
20 const res = await this.instance.get(url, { params });
21 return res.data;
22 }
23 async getStream(url, params) {
24 const res = await this.instance.get(url, {
25 params,
26 responseType: "stream",
27 });
28 return res.data;
29 }
30 async post(url, body, config) {
31 const res = await this.instance.post(url, body, Object.assign({ headers: Object.assign({ "Content-Type": "application/json" }, (config && config.headers)) }, config));
32 return this.responseParse(res);
33 }
34 responseParse(res) {
35 const { responseParser } = this.config;
36 if (responseParser)
37 return responseParser(res);
38 else
39 return res.data;
40 }
41 async put(url, body, config) {
42 const res = await this.instance.put(url, body, Object.assign({ headers: Object.assign({ "Content-Type": "application/json" }, (config && config.headers)) }, config));
43 return this.responseParse(res);
44 }
45 async postForm(url, body) {
46 const params = new URLSearchParams();
47 for (const key in body) {
48 if (body.hasOwnProperty(key)) {
49 params.append(key, body[key]);
50 }
51 }
52 const res = await this.instance.post(url, params.toString(), {
53 headers: { "Content-Type": "application/x-www-form-urlencoded" },
54 });
55 return res.data;
56 }
57 async postFormMultipart(url, form) {
58 const res = await this.instance.post(url, form);
59 return res.data;
60 }
61 async putFormMultipart(url, form, config) {
62 const res = await this.instance.put(url, form, config);
63 return res.data;
64 }
65 async toBuffer(data) {
66 if (Buffer.isBuffer(data)) {
67 return data;
68 }
69 else if (data instanceof node_stream_1.Readable) {
70 return await new Promise((resolve, reject) => {
71 const buffers = [];
72 let size = 0;
73 data.on("data", (chunk) => {
74 buffers.push(chunk);
75 size += chunk.length;
76 });
77 data.on("end", () => resolve(Buffer.concat(buffers, size)));
78 data.on("error", reject);
79 });
80 }
81 else {
82 throw new Error("invalid data type for binary data");
83 }
84 }
85 async postBinary(url, data, contentType) {
86 const buffer = await this.toBuffer(data);
87 const res = await this.instance.post(url, buffer, {
88 headers: {
89 "Content-Type": contentType || "image/png",
90 "Content-Length": buffer.length,
91 },
92 });
93 return res.data;
94 }
95 async postBinaryContent(url, body) {
96 const res = await this.instance.post(url, body, {
97 headers: {
98 "Content-Type": body.type,
99 "Content-Length": body.size,
100 },
101 });
102 return res.data;
103 }
104 async delete(url, params) {
105 const res = await this.instance.delete(url, { params });
106 return res.data;
107 }
108 wrapError(err) {
109 if (err.response) {
110 const { status, statusText } = err.response;
111 const { message } = err;
112 return new exceptions_1.HTTPError(message, {
113 statusCode: status,
114 statusMessage: statusText,
115 originalError: err,
116 });
117 }
118 else if (err.code) {
119 const { message, code } = err;
120 return new exceptions_1.RequestError(message, { code, originalError: err });
121 }
122 else if (err.config) {
123 // unknown, but from axios
124 const { message } = err;
125 return new exceptions_1.ReadError(message, { originalError: err });
126 }
127 // otherwise, just rethrow
128 return err;
129 }
130}
131exports.default = HTTPClient;