-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (106 loc) · 5.56 KB
/
index.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const requestManager = require('./requestManager');
class benxApi {
/**
* Creates the benxAPI Client
* @param {Object} options - Configuration options for the client
* @param {string} options.urlBase - The base URL for the API
* @param {string} options.token - The API token for authentication
*
* @example
* const benxApi = require('benxapi');
* const request = new benxApi({ urlBase: 'http://api.example.com', token: 'yourToken' });
*/
constructor(options) {
if (!options) throw new SyntaxError('No options provided.');
if (typeof options !== 'object') throw new SyntaxError('Options must be an object.');
if (options.urlBase) {
if (typeof options.urlBase !== 'string') throw new SyntaxError('The "urlBase" must be a string.');
this.urlBase = options.urlBase;
} else {
this.urlBase = '';
}
if (options.token) {
if (typeof options.token !== 'string') throw new SyntaxError('The "token" must be a string.');
this.token = options.token;
}
}
/**
* 'GET' method to query something
* @param {string} url - The API endpoint
* @param {Object} [options={}] - Request options like maxBytes, returnData
* @param {number} [options.maxBytes=0] - Limit the response size in bytes
* @param {boolean} [options.returnData=true] - Whether to return the data or just the response headers
* @returns {Promise<Object>} - The response of the API
* @throws {Error} - Throws an error if the URL is not a string
*/
async get(url, options = { maxBytes: 0, returnData: true }) {
if (typeof url !== 'string') throw new Error('"url" must be a string');
const requestURL = this.urlBase + url;
try {
return await requestManager('GET', this.token, requestURL, {}, options.maxBytes, options.returnData);
} catch (error) {
throw new Error(`GET request (${requestURL}) failed: ${error.message}`);
}
}
/**
* 'POST' method to query something
* @param {string} url - The API endpoint
* @param {Object} data - The data transmitted in the body
* @param {Object} [options={}] - Request options like maxBytes, returnData
* @param {number} [options.maxBytes=0] - Limit the response size in bytes
* @param {boolean} [options.returnData=true] - Whether to return the data or just the response headers
* @returns {Promise<Object>} - The response of the API
* @throws {Error} - Throws an error if the URL is not a string or options is not an object
*/
async post(url, data = {}, options = { maxBytes: 0, returnData: true }) {
if (typeof url !== 'string') throw new Error('"url" must be a string');
if (typeof data !== 'object' || data === null) throw new Error('"data" must be an object');
const requestURL = this.urlBase + url;
try {
return await requestManager('POST', this.token, requestURL, data, options.maxBytes, options.returnData);
} catch (error) {
throw new Error(`POST request (${requestURL}) failed: ${error.message}`);
}
}
/**
* 'PUT' method to query something
* @param {string} url - The API endpoint
* @param {Object} data - The data transmitted in the body
* @param {Object} [options={}] - Request options like maxBytes, returnData
* @param {number} [options.maxBytes=0] - Limit the response size in bytes
* @param {boolean} [options.returnData=true] - Whether to return the data or just the response headers
* @returns {Promise<Object>} - The response of the API
* @throws {Error} - Throws an error if the URL is not a string or options is not an object
*/
async put(url, data = {}, options = { maxBytes: 0, returnData: true }) {
if (typeof url !== 'string') throw new Error('"url" must be a string');
if (typeof data !== 'object' || data === null) throw new Error('"data" must be an object');
const requestURL = this.urlBase + url;
try {
return await requestManager('PUT', this.token, requestURL, data, options.maxBytes, options.returnData);
} catch (error) {
throw new Error(`PUT request (${requestURL}) failed: ${error.message}`);
}
}
/**
* 'DELETE' method to query something
* @param {string} url - The API endpoint
* @param {Object} data - The data transmitted in the body
* @param {Object} [options={}] - Request options like maxBytes, returnData
* @param {number} [options.maxBytes=0] - Limit the response size in bytes
* @param {boolean} [options.returnData=true] - Whether to return the data or just the response headers
* @returns {Promise<Object>} - The response of the API
* @throws {Error} - Throws an error if the URL is not a string or options is not an object
*/
async delete(url, data = {}, options = { maxBytes: 0, returnData: true }) {
if (typeof url !== 'string') throw new Error('"url" must be a string');
if (typeof data !== 'object' || data === null) throw new Error('"data" must be an object');
const requestURL = this.urlBase + url;
try {
return await requestManager('DELETE', this.token, requestURL, data, options.maxBytes, options.returnData);
} catch (error) {
throw new Error(`DELETE request (${requestURL}) failed: ${error.message}`);
}
}
};
module.exports = benxApi;