lib/index.js
/**
     * Call a known endpoint at MWS, returning the raw data from the function. Parameters are
     * transformed and validated according to the rules defined in lib/endpoints
     *
     * @async
     * @public
     * @param {string} name - name of MWS API function to call
     * @param {object.<string,any>} [apiParams] - named hash object of the parameters to pass to the API
     * @param {string} [apiParams.feedContent] - if calling a function that submits a feed, supply the feed data here
     * @param {any} [apiParams.AnythingElse] - other parameters will be passed through validation and into the API
     * @param {object} [options] - options for callEndpoint
     * @param {boolean} [options.noFlatten] - do not flatten results
     * @param {boolean} [options.returnRaw] - return only the raw data (may or may not be flattened)
     * @param {string} [options.saveRaw] - filename to save raw data to (may or may not be flattened)
     * @param {string} [options.saveParsed] - filename to save final parsed data to (not compatible with returnRaw, since parsing won't happen)
     * @param {int} [options.maxThrottleRetries=2] - maximum number of retries for throttling
     * @returns {any} - Results of the call to MWS
     */
Call a known endpoint at MWS, returning the raw data from the function. Parameters are
transformed and validated according to the rules defined in lib/endpoints


Call a known endpoint at MWS returning the raw data from the function . Parameters are transformed and validated according to the rules defined in lib / endpoints
async callEndpoint(name, apiParams = {}, options = { maxThrottleRetries: 2 }) {
        const endpoint = endpoints[name];
        if (!endpoint) {
            throw new errors.InvalidUsage(`Unknown endpoint name supplied to callEndpoint, ${name}`);
        }
        const newOptions = endpoint.params
            ? validateAndTransformParameters(endpoint.params, apiParams)
            : apiParams;
        const { feedContent, ...newOptionsExclFeed } = newOptions;
        const queryOptions = {
            ...newOptionsExclFeed,
            Action: endpoint.action,
            Version: endpoint.version,
        };
        const params = {
            path: `/${endpoint.category}/${endpoint.version}`,
            query: queryOptions,
            feedContent,
        };
        const queueName = `${this.mws.merchantId}/${endpoint.category}/${endpoint.action}`;
        let q = QueueSchedule.getQueue(queueName);
        if (!q) {
            q = new Queue({
                api: this,
                category: endpoint.category,
                action: endpoint.action,
                maxInFlight: (endpoint.throttle && endpoint.throttle.maxInFlight) || 0,
                restoreRate: (endpoint.throttle && endpoint.throttle.restoreRate) || 0,
            }, () => QueueSchedule.deleteQueue(queueName));
            QueueSchedule.registerQueue(q, queueName);
        }
        try {
            const result = await q.request(params, { noFlatten: options.noFlatten });
            if (options.saveRaw) {
                fs.writeFileSync(path.join(process.cwd(), options.saveRaw), JSON.stringify(result));
            }
            if (options.returnRaw) {
                return result;
            }
            const digResult = digResponseResult(name, result);
            if (options.saveParsed) {
                fs.writeFileSync(path.join(process.cwd(), options.saveParsed), JSON.stringify(digResult));
            }
            return digResult;
        } catch (error) {
            throw error;
        }
    }
async callEndpoint ( name , apiParams = { } , options = { maxThrottleRetries : 2 } ) { const endpoint = endpoints [ name ] ; if ( ! endpoint ) { throw new errors . InvalidUsage ( ` ${ name } ` ) ; } const newOptions = endpoint . params ? validateAndTransformParameters ( endpoint . params , apiParams ) : apiParams ; const { feedContent , ... newOptionsExclFeed } = newOptions ; const queryOptions = { ... newOptionsExclFeed , Action : endpoint . action , Version : endpoint . version , } ; const params = { path : ` ${ endpoint . category } ${ endpoint . version } ` , query : queryOptions , feedContent , } ; const queueName = ` ${ this . mws . merchantId } ${ endpoint . category } ${ endpoint . action } ` ; let q = QueueSchedule . getQueue ( queueName ) ; if ( ! q ) { q = new Queue ( { api : this , category : endpoint . category , action : endpoint . action , maxInFlight : ( endpoint . throttle && endpoint . throttle . maxInFlight ) || 0 , restoreRate : ( endpoint . throttle && endpoint . throttle . restoreRate ) || 0 , } , ( ) => QueueSchedule . deleteQueue ( queueName ) ) ; QueueSchedule . registerQueue ( q , queueName ) ; } try { const result = await q . request ( params , { noFlatten : options . noFlatten } ) ; if ( options . saveRaw ) { fs . writeFileSync ( path . join ( process . cwd ( ) , options . saveRaw ) , JSON . stringify ( result ) ) ; } if ( options . returnRaw ) { return result ; } const digResult = digResponseResult ( name , result ) ; if ( options . saveParsed ) { fs . writeFileSync ( path . join ( process . cwd ( ) , options . saveParsed ) , JSON . stringify ( digResult ) ) ; } return digResult ; } catch ( error ) { throw error ; } }
Call a known endpoint at MWS, returning the raw data from the function.
Call a known endpoint at MWS returning the raw data from the function .
// TODO: reduce the size of this try!