whitebox-co / walmart-marketplace-api

A fully typed TypeScript, Javascript, and Node.js API library for the Walmart Marketplace API
MIT License
25 stars 10 forks source link

axios request issued twice when getConfiguredApi used #179

Open maxicus opened 1 year ago

maxicus commented 1 year ago

Seems http request is issued twice on each api call when api is instantiated by getConfiguredApi call. I believe it happens because getConfiguredApi -> apiInterceptor calls target function itself by

https://github.com/whitebox-co/walmart-marketplace-api/blob/main/src/index.ts#L178

// run function with args.
api[fnName](...fnArgs);

and api interceptor calls it afterwards again by https://github.com/whitebox-co/walmart-marketplace-api/blob/main/src/util/interceptors.ts#L17

return Reflect.apply(target, thisArg, argumentsList);

To Reproduce in node_modules/@whitebox-co/walmart-marketplace-api/lib/src/apis/items.js find

    getAllItems(requestParameters, options) {
        return (0, exports.ItemsApiFp)(this.configuration).getAllItems(requestParameters.authorization, requestParameters.wMSECACCESSTOKEN, requestParameters.wMQOSCORRELATIONID, requestParameters.wMSVCNAME, requestParameters.nextCursor, requestParameters.sku, requestParameters.offset, requestParameters.limit, requestParameters.lifecycleStatus, requestParameters.publishedStatus, requestParameters.variantGroupId, requestParameters.wMCONSUMERCHANNELTYPE, options).then((request) => request(this.axios, this.basePath));
    }

and replace with some logging there

    getAllItems(requestParameters, options) {
    console.log('getAllItems call');
        return (0, exports.ItemsApiFp)(this.configuration).getAllItems(requestParameters.authorization, requestParameters.wMSECACCESSTOKEN, requestParameters.wMQOSCORRELATIONID, requestParameters.wMSVCNAME, requestParameters.nextCursor, requestParameters.sku, requestParameters.offset, requestParameters.limit, requestParameters.lifecycleStatus, requestParameters.publishedStatus, requestParameters.variantGroupId, requestParameters.wMCONSUMERCHANNELTYPE, options).then((request) => request(this.axios, this.basePath));
    }

start test script:

import walmartMarketplaceApi from '@whitebox-co/walmart-marketplace-api';

async function run() {
    let clientId = '...';
    let clientSecret = '...';
    let consumerChannelType = 'seller';

    console.log(walmartMarketplaceApi.ItemsApi);
    let api = new walmartMarketplaceApi.WalmartApi({
        clientId,
        clientSecret,
        consumerChannelType,
    });

    const itemsApi = await api.getConfiguredApi(walmartMarketplaceApi.ItemsApi);

    await itemsApi.getAllItems({
        ...walmartMarketplaceApi.defaultParams
    });
}

run();

output:

getAllItems call
getAllItems call

similarly logging can be done at axios level with the same consequences.