-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(service-connector): added type support
Closes #92
- Loading branch information
1 parent
b0cefcb
commit 25bad6a
Showing
2 changed files
with
46 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
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,45 @@ | ||
import { InternalAxiosRequestConfig, AxiosResponse, CreateAxiosDefaults, AxiosInstance } from "axios"; | ||
|
||
declare module "axios" { | ||
interface AxiosInstance { | ||
/** | ||
* @description Proxies a request as a whole to another host | ||
* @param host The host to proxy the request to | ||
* @param req The express request object | ||
* @param res The express response object | ||
* @example | ||
* router.get('/users', async (req, res) => { | ||
* await instance.proxy('https://example.com', req, res); | ||
* }); | ||
*/ | ||
proxy: (host: string, req: any, res?: any) => any; | ||
/** | ||
* @description Extracts and returns the value at response.data.data or response.data if response.data.data is not present | ||
* @param response The axios response object | ||
* @example | ||
* const data = await instance.get('https://example.com/users').then(resolve); | ||
*/ | ||
resolve: (response: AxiosResponse) => any; | ||
} | ||
} | ||
|
||
interface ServiceConnectorOptions extends CreateAxiosDefaults { | ||
service?: string; | ||
headerIntercepts?: (config: InternalAxiosRequestConfig) => Record<string, string>; | ||
loggable?: (response: AxiosResponse) => Record<string, string>; | ||
logs?: boolean; | ||
} | ||
/** | ||
* @description Creates a wrapper around axios with extended support for logging and error handling | ||
* @param options The options to configure the service connector. This is an extension of the options provided by axios | ||
* @returns Returns an axios instance | ||
* @example | ||
* const instance = serviceConnector({ | ||
* baseURL: 'https://example.com', | ||
* service: 'example-service', | ||
* }); | ||
* instance.get('/users'); | ||
*/ | ||
function serviceConnector(options: ServiceConnectorOptions): AxiosInstance; | ||
|
||
export default serviceConnector; |