@@ -6,6 +6,7 @@ export default class Client {
6
6
const config = { ...defaults , ...options } ;
7
7
8
8
const headers = {
9
+ 'Content-Type' : 'application/json' ,
9
10
'User-Agent' : config . userAgent ,
10
11
Authorization : `Bearer ${ apiKey } ` ,
11
12
} ;
@@ -15,31 +16,39 @@ export default class Client {
15
16
this . headers = headers ;
16
17
}
17
18
19
+ /**
20
+ * Make a GET request
21
+ * @param {string } path - API endpoint path
22
+ * @returns {Promise<Object> } Response data
23
+ */
18
24
async get ( path ) {
19
- try {
20
- const response = await fetch ( this . baseURL + path , {
21
- headers : this . headers ,
22
- timeout : this . timeout ,
23
- } ) ;
24
-
25
- if ( ! response . ok ) {
26
- throw new Error ( await response . text ( ) ) ;
27
- }
28
-
29
- return await response . json ( ) ;
30
- } catch ( e ) {
31
- return handleError ( e ) ;
32
- }
25
+ return this . request ( path , {
26
+ method : 'GET' ,
27
+ } ) ;
33
28
}
34
29
30
+ /**
31
+ * Make a POST request
32
+ * @param {string } path - API endpoint path
33
+ * @param {Object } data - Request body data
34
+ * @returns {Promise<Object> } Response data
35
+ */
35
36
async post ( path , data ) {
37
+ return this . request ( path , {
38
+ method : 'POST' ,
39
+ body : JSON . stringify ( data ) ,
40
+ } ) ;
41
+ }
42
+
43
+ async request ( path , options = { } ) {
36
44
try {
37
- const response = await fetch ( this . baseURL + path , {
38
- method : 'POST' ,
39
- headers : Object . assign ( this . headers , { 'Content-Type' : 'application/json' } ) ,
40
- body : JSON . stringify ( data ) ,
45
+ const fetchOptions = {
46
+ headers : this . headers ,
41
47
timeout : this . timeout ,
42
- } ) ;
48
+ ...options ,
49
+ } ;
50
+
51
+ const response = await fetch ( this . baseURL + path , fetchOptions ) ;
43
52
44
53
if ( ! response . ok ) {
45
54
throw new Error ( await response . text ( ) ) ;
0 commit comments