1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const axios_1 = require("axios");
|
4 | const stream_1 = require("stream");
|
5 | const exceptions_1 = require("./exceptions");
|
6 | const fileType = require("file-type");
|
7 | const qs = require("querystring");
|
8 | const pkg = require("../package.json");
|
9 | class HTTPClient {
|
10 | constructor(config = {}) {
|
11 | this.config = config;
|
12 | const { baseURL, defaultHeaders } = config;
|
13 | this.instance = axios_1.default.create({
|
14 | baseURL,
|
15 | headers: Object.assign({}, defaultHeaders, {
|
16 | "User-Agent": `${pkg.name}/${pkg.version}`,
|
17 | }),
|
18 | });
|
19 | this.instance.interceptors.response.use(res => res, err => Promise.reject(this.wrapError(err)));
|
20 | }
|
21 | async get(url, params) {
|
22 | const res = await this.instance.get(url, { params });
|
23 | return res.data;
|
24 | }
|
25 | async getStream(url, params) {
|
26 | const res = await this.instance.get(url, {
|
27 | params,
|
28 | responseType: "stream",
|
29 | });
|
30 | return res.data;
|
31 | }
|
32 | async post(url, body, config) {
|
33 | const res = await this.instance.post(url, body, Object.assign({ headers: Object.assign({ "Content-Type": "application/json" }, (config && config.headers)) }, config));
|
34 | return this.responseParse(res);
|
35 | }
|
36 | responseParse(res) {
|
37 | const { responseParser } = this.config;
|
38 | if (responseParser)
|
39 | return responseParser(res);
|
40 | else
|
41 | return res.data;
|
42 | }
|
43 | async put(url, body, config) {
|
44 | const res = await this.instance.put(url, body, Object.assign({ headers: Object.assign({ "Content-Type": "application/json" }, (config && config.headers)) }, config));
|
45 | return this.responseParse(res);
|
46 | }
|
47 | async postForm(url, body) {
|
48 | const res = await this.instance.post(url, qs.stringify(body), {
|
49 | headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
50 | });
|
51 | return res.data;
|
52 | }
|
53 | async toBuffer(data) {
|
54 | if (Buffer.isBuffer(data)) {
|
55 | return data;
|
56 | }
|
57 | else if (data instanceof stream_1.Readable) {
|
58 | return await new Promise((resolve, reject) => {
|
59 | const buffers = [];
|
60 | let size = 0;
|
61 | data.on("data", (chunk) => {
|
62 | buffers.push(chunk);
|
63 | size += chunk.length;
|
64 | });
|
65 | data.on("end", () => resolve(Buffer.concat(buffers, size)));
|
66 | data.on("error", reject);
|
67 | });
|
68 | }
|
69 | else {
|
70 | throw new Error("invalid data type for binary data");
|
71 | }
|
72 | }
|
73 | async postBinary(url, data, contentType) {
|
74 | const buffer = await this.toBuffer(data);
|
75 | const res = await this.instance.post(url, buffer, {
|
76 | headers: {
|
77 | "Content-Type": contentType || (await fileType.fromBuffer(buffer)).mime,
|
78 | "Content-Length": buffer.length,
|
79 | },
|
80 | });
|
81 | return res.data;
|
82 | }
|
83 | async delete(url, params) {
|
84 | const res = await this.instance.delete(url, { params });
|
85 | return res.data;
|
86 | }
|
87 | wrapError(err) {
|
88 | if (err.response) {
|
89 | return new exceptions_1.HTTPError(err.message, err.response.status, err.response.statusText, err);
|
90 | }
|
91 | else if (err.code) {
|
92 | return new exceptions_1.RequestError(err.message, err.code, err);
|
93 | }
|
94 | else if (err.config) {
|
95 |
|
96 | return new exceptions_1.ReadError(err);
|
97 | }
|
98 |
|
99 | return err;
|
100 | }
|
101 | }
|
102 | exports.default = HTTPClient;
|