@@ -12,10 +12,14 @@ const defaultOption: RequestOptions = {
12
12
13
13
type OptionReturnType < Opt , T > = Opt extends { unwrapData : false } ? AxiosResponse < T > : Opt extends { unwrapData : true } ? T : T
14
14
15
+ export type APIClientOptions = {
16
+ wrapResponseErrors : boolean
17
+ }
18
+
15
19
export class API {
16
20
private axios : AxiosInstance
17
21
18
- constructor ( readonly accessToken : string , public hackmdAPIEndpointURL : string = "https://api.hackmd.io/v1" ) {
22
+ constructor ( readonly accessToken : string , public hackmdAPIEndpointURL : string = "https://api.hackmd.io/v1" , public options : APIClientOptions = { wrapResponseErrors : true } ) {
19
23
if ( ! accessToken ) {
20
24
throw new HackMDErrors . MissingRequiredArgument ( 'Missing access token when creating HackMD client' )
21
25
}
@@ -37,30 +41,41 @@ export class API {
37
41
}
38
42
)
39
43
40
- this . axios . interceptors . response . use (
41
- ( response : AxiosResponse ) => {
42
- return response
43
- } ,
44
- async ( err : AxiosError ) => {
45
- if ( ! err . response ) {
46
- return Promise . reject ( err )
47
- }
48
-
49
- if ( err . response . status >= 500 ) {
50
- throw new HackMDErrors . InternalServerError (
51
- `HackMD internal error (${ err . response . status } ${ err . response . statusText } )` ,
52
- err . response . status ,
53
- err . response . statusText ,
54
- )
55
- } else {
56
- throw new HackMDErrors . HttpResponseError (
57
- `Received an error response (${ err . response . status } ${ err . response . statusText } ) from HackMD` ,
58
- err . response . status ,
59
- err . response . statusText ,
60
- )
44
+ if ( options . wrapResponseErrors ) {
45
+ this . axios . interceptors . response . use (
46
+ ( response : AxiosResponse ) => {
47
+ return response
48
+ } ,
49
+ async ( err : AxiosError ) => {
50
+ if ( ! err . response ) {
51
+ return Promise . reject ( err )
52
+ }
53
+
54
+ if ( err . response . status >= 500 ) {
55
+ throw new HackMDErrors . InternalServerError (
56
+ `HackMD internal error (${ err . response . status } ${ err . response . statusText } )` ,
57
+ err . response . status ,
58
+ err . response . statusText ,
59
+ )
60
+ } else if ( err . response . status === 429 ) {
61
+ throw new HackMDErrors . TooManyRequestsError (
62
+ `Too many requests (${ err . response . status } ${ err . response . statusText } )` ,
63
+ err . response . status ,
64
+ err . response . statusText ,
65
+ parseInt ( err . response . headers [ 'x-ratelimit-userlimit' ] , 10 ) ,
66
+ parseInt ( err . response . headers [ 'x-ratelimit-userremaining' ] , 10 ) ,
67
+ parseInt ( err . response . headers [ 'x-ratelimit-userreset' ] , 10 ) ,
68
+ )
69
+ } else {
70
+ throw new HackMDErrors . HttpResponseError (
71
+ `Received an error response (${ err . response . status } ${ err . response . statusText } ) from HackMD` ,
72
+ err . response . status ,
73
+ err . response . statusText ,
74
+ )
75
+ }
61
76
}
62
- }
63
- )
77
+ )
78
+ }
64
79
}
65
80
66
81
async getMe < Opt extends RequestOptions > ( options = defaultOption as Opt ) : Promise < OptionReturnType < Opt , GetMe > > {
0 commit comments