generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- axios request logging in debug mode - implements `axios-debug` with some changes - supports log output & format of homebridge
- Loading branch information
1 parent
6ee2c66
commit d4be51d
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { | ||
AxiosError, | ||
AxiosInstance, | ||
AxiosRequestConfig, | ||
AxiosResponse, | ||
} from 'axios'; | ||
import { Logger } from 'homebridge'; | ||
import { DingzLogger } from './dingzLogHelper'; | ||
|
||
/** | ||
* Implements AxiosDebug for classes | ||
*/ | ||
export class AxiosDebugHelper { | ||
private readonly isAbsoluteURL = require('axios/lib/helpers/isAbsoluteURL'); | ||
private readonly buildURL = require('axios/lib/helpers/buildURL'); | ||
private readonly combineURLs = require('axios/lib/helpers/combineURLs'); | ||
private requestUrl = ''; | ||
|
||
/** | ||
* | ||
* @param instance AxiosInstance | ||
* @param logger Logger | ||
*/ | ||
constructor( | ||
readonly instance: AxiosInstance, | ||
readonly logger: Logger | DingzLogger, | ||
) { | ||
this.addLogger(instance); | ||
} | ||
|
||
private getURL(config: AxiosRequestConfig) { | ||
let url = config.url; | ||
if (config.baseURL && !this.isAbsoluteURL(url)) { | ||
url = this.combineURLs(config.baseURL, url); | ||
} | ||
return this.buildURL(url, config.params, config.paramsSerializer); | ||
} | ||
|
||
public addLogger( | ||
instance: AxiosInstance, | ||
): AxiosRequestConfig | AxiosResponse | void { | ||
instance.interceptors.request.use((config: AxiosRequestConfig) => { | ||
this.request(config); | ||
return config; | ||
}); | ||
instance.interceptors.response.use( | ||
(response: AxiosResponse) => { | ||
this.response(response); | ||
return response; | ||
}, | ||
(error) => { | ||
this.error(error); | ||
throw error; | ||
}, | ||
); | ||
} | ||
|
||
private request(config: AxiosRequestConfig) { | ||
this.requestUrl = this.getURL(config); | ||
Object.defineProperty(config, this.requestUrl, { value: this.requestUrl }); | ||
this.logger.debug(config.method?.toUpperCase() + ' ' + this.requestUrl); | ||
} | ||
|
||
private response(response: AxiosResponse) { | ||
this.logger.debug( | ||
response.status + | ||
' ' + | ||
response.statusText + | ||
' (' + | ||
response.config?.method?.toUpperCase() + | ||
' ' + | ||
this.requestUrl + | ||
')', | ||
); | ||
} | ||
|
||
private error(error: AxiosError) { | ||
if (error.config) { | ||
this.logger.error( | ||
error.name + ': ' + error.message, | ||
'(' + error.config?.method?.toUpperCase() + ' ' + this.requestUrl + ')', | ||
); | ||
} else { | ||
this.logger.error(error.name + ': ' + error.message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters