Skip to content

Commit

Permalink
Feat(service-connector): added type support
Browse files Browse the repository at this point in the history
Closes #92
  • Loading branch information
Akalanka47000 committed Jan 7, 2024
1 parent b0cefcb commit 25bad6a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/service-connector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "2.0.1",
"description": "A package to isolate filters and sorts from a given request's query parameters",
"main": "dist/index.js",
"types": "types/index.d.ts",
"scripts": {
"build": "node ../../scripts/esbuild.config.js",
"build:watch": "bash ../../scripts/esbuild.watch.sh",
Expand Down
45 changes: 45 additions & 0 deletions packages/service-connector/types/index.d.ts
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;

0 comments on commit 25bad6a

Please sign in to comment.