@@ -24,14 +24,14 @@ export async function doGetRequest<T>(reqOptions: RequestOptionsBase, pathname:
24
24
export type QueryParameters = Record < string , string | number | boolean | null | undefined >
25
25
export type BodyParameters = Record < string , any >
26
26
27
- export async function doPutRequest < T > (
27
+ export async function doPutRequest (
28
28
reqOptions : RequestOptionsBase ,
29
29
pathname : string ,
30
30
queryParams : QueryParameters ,
31
31
body : BodyParameters ,
32
- ) : Promise < Response < T > > {
33
- return doRequest < T > (
34
- got . put < T > ( SpotifyBaseUrl + pathname , {
32
+ ) : Promise < Response < void > > {
33
+ return doRequestNoResponse (
34
+ got . put < void > ( SpotifyBaseUrl + pathname , {
35
35
headers : {
36
36
Authorization : `Bearer ${ reqOptions . accessToken } ` ,
37
37
'Content-Type' : 'application/json' ,
@@ -47,13 +47,13 @@ export async function doPutRequest<T>(
47
47
)
48
48
}
49
49
50
- export async function doPostRequest < T > (
50
+ export async function doPostRequest (
51
51
reqOptions : RequestOptionsBase ,
52
52
pathname : string ,
53
53
queryParams : QueryParameters ,
54
- ) : Promise < Response < T > > {
55
- return doRequest < T > (
56
- got . post < T > ( SpotifyBaseUrl + pathname , {
54
+ ) : Promise < Response < void > > {
55
+ return doRequestNoResponse (
56
+ got . post < void > ( SpotifyBaseUrl + pathname , {
57
57
headers : {
58
58
Authorization : `Bearer ${ reqOptions . accessToken } ` ,
59
59
'Content-Type' : 'application/json' ,
@@ -68,32 +68,52 @@ export async function doPostRequest<T>(
68
68
)
69
69
}
70
70
71
+ export async function doRequestNoResponse ( req : CancelableRequest < Response < void > > ) : Promise < Response < void > > {
72
+ try {
73
+ // console.log('json', await req.json(), (await req.buffer()).length)
74
+ const res = await req
75
+
76
+ return {
77
+ headers : res . headers ,
78
+ statusCode : res . statusCode ,
79
+ body : null ,
80
+ }
81
+ } catch ( e : unknown ) {
82
+ return wrapHttpError ( e )
83
+ }
84
+ }
85
+
71
86
export async function doRequest < T > ( req : CancelableRequest < Response < T > > ) : Promise < Response < T > > {
72
87
try {
73
88
// console.log('json', await req.json(), (await req.buffer()).length)
74
89
const res = await req
75
- // console.log(res.body)
90
+
91
+ console . log ( 'body' , res . body )
76
92
return {
77
93
headers : res . headers ,
78
94
statusCode : res . statusCode ,
79
95
body : await req . json ( ) ,
80
96
}
81
97
} catch ( e : unknown ) {
82
- if ( e instanceof HTTPError ) {
83
- // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
84
- return Promise . reject ( {
85
- headers : e . response . headers ,
86
- statusCode : e . response . statusCode ,
87
- error : e ,
88
- } )
89
- } else {
90
- // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
91
- return Promise . reject ( {
92
- headers : { } ,
93
- statusCode : 500 ,
94
- error : e ,
95
- } )
96
- }
98
+ return wrapHttpError ( e )
99
+ }
100
+ }
101
+
102
+ async function wrapHttpError ( e : unknown ) : Promise < never > {
103
+ if ( e instanceof HTTPError ) {
104
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
105
+ return Promise . reject ( {
106
+ headers : e . response . headers ,
107
+ statusCode : e . response . statusCode ,
108
+ error : e ,
109
+ } )
110
+ } else {
111
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
112
+ return Promise . reject ( {
113
+ headers : { } ,
114
+ statusCode : 500 ,
115
+ error : e ,
116
+ } )
97
117
}
98
118
}
99
119
0 commit comments