mws-zodane-advanced
Version:
fixed throtal resend
258 lines (241 loc) • 16.3 kB
HTML
<html>
<head>
<meta charset="utf-8">
<base data-ice="baseUrl" href="../../">
<title data-ice="title">lib/validation.js | mws-advanced</title>
<link type="text/css" rel="stylesheet" href="css/style.css">
<link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
<script src="script/prettify/prettify.js"></script>
<script src="script/manual.js"></script>
</head>
<body class="layout-container" data-ice="rootContainer">
<header>
<a href="./">Home</a>
<a href="identifiers.html">Reference</a>
<a href="source.html">Source</a>
<div class="search-box">
<span>
<img src="./image/search.png">
<span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
</span>
<ul class="search-result"></ul>
</div>
</header>
<nav class="navigation" data-ice="nav"><div>
<ul>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/errors.js~InvalidUsage.html">InvalidUsage</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/errors.js~RequestCancelled.html">RequestCancelled</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/errors.js~ServiceError.html">ServiceError</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/errors.js~ValidationError.html">ValidationError</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-callEndpoint">callEndpoint</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-init">init</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getLowestPricedOffersForASIN">getLowestPricedOffersForASIN</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getMarketplaces">getMarketplaces</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getMatchingProductForId">getMatchingProductForId</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-listFinancialEvents">listFinancialEvents</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-listInventorySupply">listInventorySupply</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-listOrderItems">listOrderItems</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-listOrders">listOrders</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getReport">getReport</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getReportList">getReportList</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getReportListAll">getReportListAll</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getReportListByNextToken">getReportListByNextToken</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getReportRequestList">getReportRequestList</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-requestAndDownloadReport">requestAndDownloadReport</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-requestReport">requestReport</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-MWS_ENDPOINTS">MWS_ENDPOINTS</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-MWS_MARKETPLACES">MWS_MARKETPLACES</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-BuyBoxPrice">BuyBoxPrice</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-DetailedShippingTime">DetailedShippingTime</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-LowestPrice">LowestPrice</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-LowestPricedOffers">LowestPricedOffers</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-Money">Money</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-Offer">Offer</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-OfferCount">OfferCount</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-OfferSummary">OfferSummary</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-SellerFeedbackRating">SellerFeedbackRating</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-ShipsFrom">ShipsFrom</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-MarketDetail">MarketDetail</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-OrderItemList">OrderItemList</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-GetReportRequestListResult">GetReportRequestListResult</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-GetReportRequestListResult">GetReportRequestListResult</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-ReportRequestInfo">ReportRequestInfo</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#endpoints">endpoints</a><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-REPORT_PROCESSING_STATUS_TYPES">REPORT_PROCESSING_STATUS_TYPES</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-REQUEST_REPORT_TYPES">REQUEST_REPORT_TYPES</a></span></span></li>
</ul>
</div>
</nav>
<div class="content" data-ice="content"><h1 data-ice="title">lib/validation.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">const errors = require('./errors');
// TODO: testing indicates some weirdness in this call. It throws for things that are outside of
// ranges, but otherwise valid, it throws for things that are not in a list of valid values, but it
// does not throw for basic type incompatibility, relying on it's caller to throw. Should this throw
// on all errors, or should it not throw on any errors? It's a lot easier to determine the *reason*
// for the throw here, so I lean towards fixing this to throw an error for all invalid things. For
// right now, I'm going to leave it exactly as is, because there's always a throw at the end user,
// but this makes unit testing weird.
// we expect xs:dateTime to come in as a ISO string. This is a little confusing because the
// consumer, validateAndTransform allows Date objects as input.
/* eslint-disable no-param-reassign */
/**
*
*
* @param {string} type - type of integer to validate (xs:int, xs:positiveInteger, etc)
* @param {any} test - value to test
* @param {object} minmax - minimum and maximum values to test for
* @param {integer} minmax.minValue - minimum value to test for
* @param {integer} minmax.maxValue - maximum value to test for
*/
const validateInteger = (type, test, { minValue, maxValue } = { }) => {
if (minValue === undefined) {
if (type === 'xs:positiveInteger') minValue = 1;
else if (type === 'xs:nonNegativeInteger') minValue = 0;
else minValue = -2147483658;
}
if (maxValue === undefined) {
maxValue = 2147483647;
}
const newTest = parseInt(test, 10);
// eslint-disable-next-line eqeqeq
const valid = test == newTest && (minValue == null || newTest >= minValue) && (maxValue == null || newTest <= maxValue);
if (!valid) {
throw new errors.ValidationError(`Expected type ${type} ${minValue} <= ${test} <= ${maxValue}`);
}
return valid;
};
/**
* Tests if an item belongs to a given type, and validates values and other stuff.
* This function is currently overloaded to do too much. Sorry. The end consumer of *this* function,
* validateAndTransform, is handling crazier stuff.
* @private
* @param {string} type data type referenced from MWS documentation (ie, 'xs:positiveInteger')
* @param {any} test data to test against the given type
* @param {any} definition validation definition (see lib/endpoints)
* @returns Boolean true if correct type AND valid, false if not. Throws on many errors.
*/
const isType = (type, test, definition) => {
let valid = false;
switch (type) {
case 'xs:int':
case 'xs:positiveInteger': // fallthrough from xs:int intentional
case 'xs:nonNegativeInteger': // fallthrough from xs:positiveInteger intentional
valid = validateInteger(type, test, definition);
break;
case 'xs:string':
if (typeof test === 'string' || test instanceof String) {
valid = true;
}
break;
case 'xs:dateTime': // test for exact match to ISO8601
if (new Date(test).toISOString() === test) {
valid = true;
}
break;
default:
console.log(`** isType: dont know how to handle type ${type}, hope its good`);
valid = true;
}
if (valid && definition && definition.values && !definition.values.includes(test)) {
throw new errors.ValidationError(`Value ${test} is not in allowed values list: ${definition.values}`);
}
return valid;
};
/**
* The "validate" step of "validateAndTransform" will happen here. See discussion here:
* https://github.com/ericblade/mws-advanced/pull/20
*
* @private
* @param {any} test
* @param {any} definition
* @returns
*/
function validate(test, definition) {
let validOrderId = false;
const re = /\d{3}-\d{7}-\d{3}/g;
if (re.test(test) && definition.stringFormat === 'amazonOrderId') {
validOrderId = true;
}
return validOrderId;
}
/**
* Used to both validate and transform "normal" javascript parameters (such as regular strings,
* arrays, object hashes, and Date objects) into something that the underlying MWS system can
* understand.
*
* @private
* @param {any} valid
* @param {any} options
* @returns
*/
const validateAndTransformParameters = (valid, options) => {
if (!options) {
return {};
}
if (!valid) {
console.warn('**** no validation parameters passed to validateAndTransform, no checking will be performed');
return options;
}
const newOptions = {};
// check for unknown parameters
Object.keys(options).forEach((k) => {
if (!valid[k]) {
throw new errors.ValidationError(`Unknown parameter ${k}`);
}
});
// check for required, list, and type (inside and outside of list)
// transform lists into the expected keys at the MWS side.
// ie key AmazonOrderId becomes AmazonOrderId.Id.{index}
Object.keys(valid).forEach((k) => {
const v = valid[k];
const o = options[k];
// if required and not found, throw
if (v.required && !o) {
throw new errors.ValidationError(`Required parameter ${k} missing`);
}
// transform Date objects into ISO strings
if (v.type === 'xs:dateTime' && o instanceof Date) {
newOptions[k] = o.toISOString();
} else if (v.list && o) { // transform lists
if (!Array.isArray(o)) {
throw new errors.ValidationError(`Parameter ${k} expected an array`);
}
if (v.listMax && o.length > v.listMax) {
throw new errors.ValidationError(`List parameter ${k} can only take up to ${v.listMax} items`);
}
if (!o.every(val => isType(v.type, val, v))) {
throw new errors.ValidationError(`List ${k} expects type ${v.type}`);
}
o.forEach((item, index) => {
newOptions[`${v.list}.${index + 1}`] = item;
});
} else { // if not already handled, then run it through isType
if (v && o && !isType(v.type, o, v)) {
throw new errors.ValidationError(`Expected type ${v.type} for ${k}`);
}
if (k && o) {
newOptions[k] = o;
}
}
});
return newOptions;
};
module.exports = {
isType,
validate,
validateAndTransformParameters,
};
</code></pre>
</div>
<footer class="footer">
Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.0.4)</span><img src="./image/esdoc-logo-mini-black.png"></a>
</footer>
<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>