11/* eslint-disable max-params */
2- import { type AxiosRequestConfig , type AxiosResponseHeaders } from 'axios' ;
2+ import {
3+ type AxiosRequestConfig ,
4+ type AxiosResponseHeaders ,
5+ type AxiosInstance ,
6+ } from 'axios' ;
37
48import { getNodeClientUserAgent , getUserAgent } from './version.js' ;
59import { isBrowser , isPersonalAccessToken , mergeConfig } from './utils.js' ;
@@ -11,18 +15,21 @@ import { COZE_COM_BASE_URL } from './constant.js';
1115export type RequestOptions = Omit <
1216 AxiosRequestConfig ,
1317 'url' | 'method' | 'baseURL' | 'data' | 'responseType'
14- > ;
18+ > &
19+ Record < string , unknown > ;
1520export interface ClientOptions {
1621 /** baseURL, default is https://api.coze.com, Use https://api.coze.cn if you use https://coze.cn */
1722 baseURL ?: string ;
1823 /** Personal Access Token (PAT) or OAuth2.0 token */
1924 token : string ;
2025 /** see https://github.com/axios/axios?tab=readme-ov-file#request-config */
2126 axiosOptions ?: RequestOptions ;
27+ /** Custom axios instance */
28+ axiosInstance ?: AxiosInstance | unknown ;
2229 /** Whether to enable debug mode */
2330 debug ?: boolean ;
2431 /** Custom headers */
25- headers ?: Headers | undefined ;
32+ headers ?: Headers | Record < string , unknown > ;
2633 /** Whether Personal Access Tokens (PAT) are allowed in browser environments */
2734 allowPersonalAccessTokenInBrowser ?: boolean ;
2835}
@@ -32,15 +39,17 @@ export class APIClient {
3239 baseURL : string ;
3340 token : string ;
3441 axiosOptions ?: RequestOptions ;
42+ axiosInstance ?: AxiosInstance | unknown ;
3543 debug : boolean ;
3644 allowPersonalAccessTokenInBrowser : boolean ;
37- headers ?: Headers ;
45+ headers ?: Headers | Record < string , unknown > ;
3846
3947 constructor ( config : ClientOptions ) {
4048 this . _config = config ;
4149 this . baseURL = config . baseURL || COZE_COM_BASE_URL ;
4250 this . token = config . token ;
4351 this . axiosOptions = config . axiosOptions || { } ;
52+ this . axiosInstance = config . axiosInstance ;
4453 this . debug = config . debug || false ;
4554 this . allowPersonalAccessTokenInBrowser =
4655 config . allowPersonalAccessTokenInBrowser || false ;
@@ -101,6 +110,7 @@ export class APIClient {
101110
102111 const fetchOptions = this . buildOptions ( method , body , options ) ;
103112 fetchOptions . isStreaming = isStream ;
113+ fetchOptions . axiosInstance = this . axiosInstance ;
104114
105115 this . debugLog ( `--- request url: ${ fullUrl } ` ) ;
106116 this . debugLog ( '--- request options:' , fetchOptions ) ;
@@ -110,7 +120,9 @@ export class APIClient {
110120 this . debugLog ( `--- response status: ${ response . status } ` ) ;
111121 this . debugLog ( '--- response headers: ' , response . headers ) ;
112122
113- const contentType = response . headers [ 'content-type' ] ;
123+ // Taro use `header`
124+ const contentType = ( response . headers ??
125+ ( response as unknown as Record < string , string > ) . header ) [ 'content-type' ] ;
114126
115127 if ( isStream ) {
116128 if ( contentType && contentType . includes ( 'application/json' ) ) {
0 commit comments