MCPcopy
hub / github.com/ampproject/amphtml / batchFetchJsonFor

Function batchFetchJsonFor

src/batched-json.js:49–73  ·  view source on GitHub ↗
(ampdoc, element, options = {})

Source from the content-addressed store, hash-verified

47 * result or rejected if response is invalid.
48 */
49export 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

Callers 9

fetchSrc_Method · 0.90
fetch_Method · 0.90
fetch_Method · 0.90
getFetchJsonCallbackMethod · 0.90
layoutCallbackMethod · 0.90
getRemoteData_Method · 0.90
getRemotePages_Method · 0.90
fetchConfig_Method · 0.90

Calls 10

assertHttpsUrlFunction · 0.90
getValueForExprFunction · 0.90
userFunction · 0.90
requestForBatchFetchFunction · 0.85
getAttributeMethod · 0.80
catchMethod · 0.80
fetchJsonMethod · 0.80
xssiJsonMethod · 0.80
thenMethod · 0.45
createErrorMethod · 0.45

Tested by

no test coverage detected