-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.js
More file actions
130 lines (122 loc) · 2.79 KB
/
http.js
File metadata and controls
130 lines (122 loc) · 2.79 KB
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
125
126
127
128
129
130
'use strict';
const fetch = require('node-fetch');
const shoppyURL = 'https://shoppy.gg';
/**
* Class for making more simplified calls to the shoppy API
*/
module.exports = class http {
/**
* Initializes a new HTTP object
* @param {string} apiKey Sets the API key to be used when talking to the shoppy API
*/
constructor(apiKey) {
this.apiKey = apiKey;
}
/**
* HTTP Get request
* @param {string} endpoint The API endpoint to send the request to
* @returns {Promise<Object>} Result of the HTTP GET request
*/
get(endpoint) {
return new Promise(async (resolve, reject) => {
try {
const res = await fetch(`${shoppyURL}${endpoint}`, {
headers: {
Authorization: this.apiKey,
},
});
const json = res.json();
resolve(json);
} catch (e) {
reject(e);
}
});
}
/**
* HTTP Post request
* @param {string} endpoint The API endpoint to send the request to
* @param {Object} payload The JSON payload to be sent
* @returns {Promise<Object>} Result of the HTTP POST request
*/
post(endpoint, payload) {
return new Promise(async (resolve, reject) => {
try {
const res = await fetch(`${shoppyURL}${endpoint}`, {
method: 'POST',
headers: {
Authorization: this.apiKey,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
const json = res.json();
resolve(json);
} catch (e) {
reject(e);
}
});
}
/**
* HTTP Put request
* @param {string} endpoint The API endpoint to send the request to
* @param {Object} payload The JSON payload to be sent
* @returns {Promise<Object>} Result of the HTTP PUT request
*/
put(endpoint, payload) {
return new Promise(async (resolve, reject) => {
try {
await fetch(`${shoppyURL}${endpoint}`, {
method: 'PUT',
headers: {
Authorization: this.apiKey,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
resolve();
} catch (e) {
reject(e);
}
});
}
/**
* HTTP Delete request
* @param {string} endpoint The API endpoint to send the request to
*/
delete(endpoint) {
return new Promise(async (resolve, reject) => {
try {
await fetch(`${shoppyURL}${endpoint}`, {
method: 'DELETE',
headers: {
Authorization: this.apiKey,
},
});
resolve();
} catch (e) {
reject(e);
}
});
}
/**
* Get the raw HTTP response
* @param {string} endpoint The API endpoint to send the request to
* @param {?*} options Custom HTTP options
* @returns {Promise<Object>}
*/
raw(endpoint, options) {
return new Promise(async (resolve, reject) => {
try {
resolve(
await fetch(`${shoppyURL}${endpoint}`, {
headers: {
Authorization: this.apiKey,
},
})
);
} catch (e) {
reject(e);
}
});
}
};