File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -40,20 +40,36 @@ export default class Client {
4040 } ) ;
4141 }
4242
43+ /**
44+ * Base request method that handles common request logic
45+ * @param {string } path - API endpoint path
46+ * @param {Object } options - Fetch options
47+ * @returns {Promise<Object> } Response data
48+ */
4349 async request ( path , options = { } ) {
4450 try {
51+ const controller = new AbortController ( ) ;
52+ const timeoutId = setTimeout ( ( ) => {
53+ controller . abort ( ) ;
54+ } , this . timeout ) ;
55+
4556 const response = await fetch ( this . baseURL + path , {
4657 headers : this . headers ,
47- timeout : this . timeout ,
58+ signal : controller . signal ,
4859 ...options ,
4960 } ) ;
5061
62+ clearTimeout ( timeoutId ) ;
63+
5164 if ( ! response . ok ) {
5265 throw new Error ( await response . text ( ) ) ;
5366 }
5467
5568 return await response . json ( ) ;
5669 } catch ( e ) {
70+ if ( e . name === 'AbortError' ) {
71+ throw new Error ( `Request timeout after ${ this . timeout } ms` ) ;
72+ }
5773 return handleError ( e ) ;
5874 }
5975 }
Original file line number Diff line number Diff line change 11const { version } = require ( '../package.json' ) ;
22
33module . exports = {
4- timeout : 60 ,
4+ timeout : 60 , // seconds
55 protocol : 'https' ,
66 host : 'ws.detectlanguage.com' ,
77 apiVersion : 'v3' ,
You can’t perform that action at this time.
0 commit comments