Skip to content

Commit

Permalink
Add additional documentation, fix type on axios 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
k-grube committed Feb 29, 2024
1 parent c636249 commit 8c8cd07
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"homepage": "https://github.com/covenanttechnologysolutions/connectwise-rest",
"dependencies": {
"axios": "^0.26.1",
"axios": "^1.6.7",
"promise-retry": "^2.0.1"
},
"devDependencies": {
Expand Down
35 changes: 27 additions & 8 deletions src/Manage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
import axios, { AxiosInstance, InternalAxiosRequestConfig } from 'axios'
import { makeRequest, makePaginate, PaginationOptions, PaginationApiMethod } from './BaseAPI'
import promiseRetry from 'promise-retry'
import type { CWMOptions } from './ManageAPI'
import { CWLogger, DataResponse, ErrorResponse, RequestOptions, RetryOptions } from './types'

const CW_MANAGE_DEBUG = !!process.env.CW_MANAGE_DEBUG

/**
* DEFAULTS variable.
* @type {Object}
* @property {RetryOptions} retryOptions - Retry options for API requests.
* @property {string} apiPath - The endpoint path for API requests.
* @property {(debug: boolean) => CWLogger} logger - Logger function that takes a boolean flag to enable debug mode.
*/
export const DEFAULTS: {
retryOptions: RetryOptions
apiPath: string
Expand Down Expand Up @@ -43,6 +50,12 @@ export const DEFAULTS: {
},
}

/**
* Represents a class for managing configuration options.
*
* @interface
* @extends CWMOptions
*/
export interface ManageConfig extends CWMOptions {
authorization: string
entryPoint: string
Expand Down Expand Up @@ -133,19 +146,25 @@ export default class Manage {
},
})

this.instance.interceptors.request.use((config: AxiosRequestConfig) => {
if (config.url && config.headers && config.method === 'get' && config.headers.Accept && typeof config.headers.Accept === 'string') {
this.instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
if (
config.url &&
config.headers &&
config.method === 'get' &&
config.headers.Accept &&
typeof config.headers.Accept === 'string'
) {
//check for requests to /system/documents/{id}/download
const documentDownloadEndpointRegExp = /^\/system\/documents\/[0-9]*\/download$/;
const documentDownloadEndpointRegExp = /^\/system\/documents\/[0-9]*\/download$/
if (documentDownloadEndpointRegExp.test(config.url)) {
//replace the string "application/json" with "blob" in the Accept header
config.headers.Accept = config.headers.Accept.replace('application\/json', 'blob');
config.headers.Accept = config.headers.Accept.replace('application/json', 'blob')
//add response type 'stream' to axios response type
config.responseType = 'stream';
config.responseType = 'stream'
}
}
return config;
});
return config
})

this.request = makeRequest({ config: this.config, api: this.api, thisObj: this })
this.paginate = makePaginate({ thisObj: this })
Expand Down

0 comments on commit 8c8cd07

Please sign in to comment.