@@ -4,12 +4,14 @@ import { logger } from "../common/logger";
44
55const APIResponse = z . object ( { } ) . catchall ( z . any ( ) ) ;
66
7- const CustomResponse = z . object ( {
8- result : z . object ( {
9- error : z . string ( ) ,
10- status : z . number ( ) ,
11- } ) ,
12- } ) . catchall ( z . any ( ) ) ;
7+ const CustomResponse = z
8+ . object ( {
9+ result : z . object ( {
10+ error : z . string ( ) ,
11+ status : z . number ( ) ,
12+ } ) ,
13+ } )
14+ . catchall ( z . any ( ) ) ;
1315
1416class ResponseGenerator {
1517 message : string ;
@@ -31,118 +33,117 @@ class ResponseGenerator {
3133}
3234
3335export class Client {
34- protected debug : boolean ;
35- protected api_url : string ;
36- protected key : string ;
37- protected timeout : number ;
38-
39- constructor (
40- api_url : string ,
41- key : string ,
42- options : { timeout ?: number ; debug ?: boolean ; api_url ?: string } = { }
43- ) {
44- this . debug = options . debug || false ;
45- this . api_url = options . api_url || api_url ;
46- this . key = key ;
47- this . timeout = options . timeout || 5000 ;
48- axios . defaults . timeout = this . timeout ;
36+ protected debug : boolean ;
37+ protected api_url : string ;
38+ protected key : string ;
39+ protected timeout : number ;
40+
41+ constructor (
42+ api_url : string ,
43+ key : string ,
44+ options : { timeout ?: number ; debug ?: boolean ; api_url ?: string } = { }
45+ ) {
46+ this . debug = options . debug || false ;
47+ this . api_url = options . api_url || api_url ;
48+ this . key = key ;
49+ this . timeout = options . timeout || 5000 ;
50+ axios . defaults . timeout = this . timeout ;
51+ }
52+
53+ async errorHandler ( error : any ) {
54+ if ( error . response ) {
55+ // The request was made and the server responded with a status code
56+ // that falls out of the range of 2xx
57+
58+ logger ( this . debug , "error" , error . response . data ) ;
59+ logger ( this . debug , "error" , error . response . status ) ;
60+ logger ( this . debug , "error" , error . response . headers ) ;
61+
62+ let response = new ResponseGenerator (
63+ error . response . data . error ,
64+ error . response . status
65+ ) . genErrorResponse ( ) ;
66+ return CustomResponse . parse ( response ) ;
67+ } else if ( error . request ) {
68+ // The request was made but no response was received
69+ // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
70+ // http.ClientRequest in node.js
71+
72+ logger ( this . debug , "error" , error . message ) ;
73+
74+ let response = new ResponseGenerator (
75+ error . message ,
76+ 500
77+ ) . genErrorResponse ( ) ;
78+ return CustomResponse . parse ( response ) ;
79+ } else {
80+ logger ( this . debug , "error" , error . message ) ;
81+
82+ let response = new ResponseGenerator (
83+ error . message ,
84+ 500
85+ ) . genErrorResponse ( ) ;
86+ return CustomResponse . parse ( response ) ;
4987 }
50-
51- async errorHandler ( error : any ) {
52- if ( error . response ) {
53- // The request was made and the server responded with a status code
54- // that falls out of the range of 2xx
55-
56- logger ( this . debug , "error" , error . response . data ) ;
57- logger ( this . debug , "error" , error . response . status ) ;
58- logger ( this . debug , "error" , error . response . headers ) ;
59-
60- let response = new ResponseGenerator (
61- error . response . data . error ,
62- error . response . status
63- ) . genErrorResponse ( ) ;
64- return CustomResponse . parse ( response ) ;
65- } else if ( error . request ) {
66- // The request was made but no response was received
67- // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
68- // http.ClientRequest in node.js
69-
70- logger ( this . debug , "error" , error . message ) ;
71-
72- let response = new ResponseGenerator (
73- error . message ,
74- 500
75- ) . genErrorResponse ( ) ;
76- return CustomResponse . parse ( response ) ;
77- } else {
78- logger ( this . debug , "error" , error . message ) ;
79-
80- let response = new ResponseGenerator (
81- error . message ,
82- 500
83- ) . genErrorResponse ( ) ;
84- return CustomResponse . parse ( response ) ;
85- }
88+ }
89+
90+ async httpDELETE ( path : string , body : object ) {
91+ try {
92+ const response = await axios . delete ( this . api_url + path , {
93+ headers : {
94+ "x-api-key" : this . key ,
95+ } ,
96+ } ) ;
97+
98+ return APIResponse . parse ( response . data ) ;
99+ } catch ( error ) {
100+ return this . errorHandler ( error ) ;
86101 }
87-
88- async httpDELETE ( path : string , body : object ) {
89- try {
90- const response = await axios . delete ( this . api_url + path , {
91- headers : {
92- "x-api-key" : this . key ,
93- } ,
94- } ) ;
95-
96- return APIResponse . parse ( response . data ) ;
97- } catch ( error ) {
98- return this . errorHandler ( error ) ;
99- }
100- }
101-
102- async httpGET ( path ?: string , params ?: object ) {
103- try {
104- const response = await axios . get ( this . api_url + path , {
105- params : params ,
106- headers : {
107- "x-api-key" : this . key ,
108- } ,
109- } ) ;
110- return APIResponse . parse ( response . data ) ;
111- } catch ( error ) {
112- return this . errorHandler ( error ) ;
113- }
102+ }
103+
104+ async httpGET ( path ?: string , params ?: object ) {
105+ try {
106+ const response = await axios . get ( this . api_url + path , {
107+ params : params ,
108+ headers : {
109+ "x-api-key" : this . key ,
110+ } ,
111+ } ) ;
112+ return APIResponse . parse ( response . data ) ;
113+ } catch ( error ) {
114+ return this . errorHandler ( error ) ;
114115 }
115-
116- async httpPOST (
117- path : string ,
118- body ?: Record < string , any > ,
119- headers ?: Record < string , any > ,
120- schema : z . Schema = APIResponse
121- ) {
122- try {
123- const response = await axios . post ( this . api_url + path , body , {
124- headers : {
125- "x-api-key" : this . key ,
126- ... headers ,
127- } ,
128- } ) ;
129- return schema . parse ( response . data ) ;
130- } catch ( error ) {
131- return this . errorHandler ( error ) ;
132- }
116+ }
117+
118+ async httpPOST (
119+ path : string ,
120+ body ?: Record < string , any > ,
121+ headers ?: Record < string , any > ,
122+ schema : z . Schema = APIResponse
123+ ) {
124+ try {
125+ const response = await axios . post ( this . api_url + path , body , {
126+ headers : {
127+ "x-api-key" : this . key ,
128+ ... headers ,
129+ } ,
130+ } ) ;
131+ return schema . parse ( response . data ) ;
132+ } catch ( error ) {
133+ return this . errorHandler ( error ) ;
133134 }
134-
135- async httpPUT ( path : string , body : object ) {
136- try {
137- const response = await axios . put ( this . api_url + path , body , {
138- headers : {
139- "x-api-key" : this . key ,
140- } ,
141- } ) ;
142- return APIResponse . parse ( response . data ) ;
143- } catch ( error ) {
144- return this . errorHandler ( error ) ;
145- }
135+ }
136+
137+ async httpPUT ( path : string , body : object ) {
138+ try {
139+ const response = await axios . put ( this . api_url + path , body , {
140+ headers : {
141+ "x-api-key" : this . key ,
142+ } ,
143+ } ) ;
144+ return APIResponse . parse ( response . data ) ;
145+ } catch ( error ) {
146+ return this . errorHandler ( error ) ;
146147 }
148+ }
147149}
148-
0 commit comments