(ampdoc, element, options = {})
| 47 | * result or rejected if response is invalid. |
| 48 | */ |
| 49 | export function batchFetchJsonFor(ampdoc, element, options = {}) { |
| 50 | const { |
| 51 | expr = '.', |
| 52 | refresh = false, |
| 53 | url = element.getAttribute('src'), |
| 54 | urlReplacement = UrlReplacementPolicy_Enum.NONE, |
| 55 | xssiPrefix = undefined, |
| 56 | } = options; |
| 57 | assertHttpsUrl(url, element); |
| 58 | const xhr = Services.batchedXhrFor(ampdoc.win); |
| 59 | return requestForBatchFetch(element, url, urlReplacement, refresh) |
| 60 | .then((data) => { |
| 61 | return xhr.fetchJson(data.xhrUrl, data.fetchOpt); |
| 62 | }) |
| 63 | .then((res) => Services.xhrFor(ampdoc.win).xssiJson(res, xssiPrefix)) |
| 64 | .then((data) => { |
| 65 | if (data == null) { |
| 66 | throw new Error('Response is undefined.'); |
| 67 | } |
| 68 | return getValueForExpr(data, expr || '.'); |
| 69 | }) |
| 70 | .catch((err) => { |
| 71 | throw user().createError('failed fetching JSON data', err); |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Handles url replacement and constructs the FetchInitJsonDef required for a |
no test coverage detected