Is it convenient to export the code in the helper directory
hoist1999 opened this issue · comments
I have a need to let axios make requests through a network proxy, so I want to call the code in the helper directory for configuration. thank you.
You can already do that by passing an axios
instance to the client constructor.
import { NotificationsApiClient } from '@scaleleap/selling-partner-api-sdk'
import axios from 'axios'
new NotificationsApiClient({
basePath,
axios: axios.create({
// ... create axios however you want
}),
})
Thank you very much for your reply. I know that the instance of axios can be passed to the API in this way, but there is a troublesome place in this way, that is, I cannot reuse the logic of ApiClientHelpers. In other words, in order to set up the network proxy, I have to completely re-customize an axios instance, and I have to reconfigure the interceptors.
Is it possible to add network proxy configuration to the interface, for example as follows:
thanks.
export const ApiClientHelpers = {
getAxiosInstance(parameters: APIConfigurationParameters): AxiosInstance {
let axiosInstance: AxiosInstance;
const { axios } = parameters;
if (axios) {
axiosInstance = axios;
} else {
const { accessToken, credentials, region, roleArn } =
ApiClientHelpers.validateRegion(parameters);
const config: AxiosRequestConfig = {
headers: {
"user-agent": USER_AGENT,
"x-amz-access-token": accessToken ?? "",
},
};
// ==== could you add a proxy parameter here? or something support proxy.
if (parameters.proxy) {
// add proxy here
let httpsAgent = new SocksProxyAgent(parameters.proxy);
let httpAgent = httpsAgent;
config.httpAgent = httpAgent;
config.httpsAgent = httpsAgent;
}
axiosInstance = globalAxios.create(config);
axiosInstance.interceptors.request.use(
aws4Interceptor(
{
region,
service: "execute-api",
assumeRoleArn: roleArn,
},
credentials
)
);
}
axiosInstance.interceptors.response.use(
(response: AxiosResponse) => response,
(error: AxiosError) => {
throw apiErrorFactory(error);
}
);
return axiosInstance;
},
I understand the concern of having to reimplement a lot of logic. But I disagree, as a principal, of having to add extra config options. My philosophy is you either use defaults, or implement your own axios solution. Because, later, maybe someone will come along and will want another axios config, and then yet another, and eventually, we have a kitchen sink of axios options, and basically ported over the entire axios config. It's a slippery slope.
Also, the solution we use, is simply to set the basePath
to an endpoint, which then proxies to the SP API endpoint.
Effectively, it is the same thing.
You can use something like this: https://github.com/http-party/node-http-proxy
Our proxy also does the AWS4 signing.
Thank you very much, your principle makes sense, I'll take a closer look at the solution you provided.