JezK
Edit File: fetch-5e8dc1d5.js.map
{"file":"fetch-5e8dc1d5.js","mappings":";;;;;;;AAIA;AAEA;AACA;AACA;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mCAqC2B,UAAE;;;;;iCAKR,eAAO;;wBAEd,UAAE,2LACyE,UAAE;;;;;;;;;;;;;;;;mCAkBhE,UAAE;;;;;;AAO7B;AAIA;;;;;;;;;;iBAUa,UAAE;;;;;;;;;;;AAcf;;;;;;;;;;;;;;;;;;;;;;;AA4BA;;;;","names":[],"sources":["src/functions/fetch.ts"],"sourcesContent":["import apiFetch from '@wordpress/api-fetch';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { addQueryArgs, getQueryArg } from '@wordpress/url';\n\nconst rootURL = window?.parent?.scData?.root_url || window?.scData?.root_url;\n\n// Only register middleware when we have a root URL. Without scData (e.g. an editor/admin\n// context that loads this bundle without its localized data) the root-url middleware would\n// be created with `undefined` and throw on the first request, poisoning the global apiFetch.\nif (rootURL) {\n apiFetch.fetchAllMiddleware = null;\n\n apiFetch.use(apiFetch.createRootURLMiddleware(rootURL));\n\n if (window?.scData?.nonce) {\n // @ts-ignore\n apiFetch.nonceMiddleware = apiFetch.createNonceMiddleware(window?.scData?.nonce);\n // @ts-ignore\n apiFetch.use(apiFetch.nonceMiddleware);\n }\n\n if (window?.scData?.nonce_endpoint) {\n // @ts-ignore\n apiFetch.nonceEndpoint = window?.scData?.nonce_endpoint;\n }\n\n // Add a timestamp so it can bypass cache rest api\n apiFetch.use((options, next) => {\n options.path = addQueryArgs(options.path, { t: Date.now() });\n return next(options);\n });\n\n // Add selected currency to the request\n apiFetch.use((options, next) => {\n options.path = addQueryArgs(options.path, {\n ...(!!getQueryArg(window.location.href, 'currency') && {\n currency: getQueryArg(window.location.href, 'currency'),\n }),\n });\n return next(options);\n });\n\n apiFetch.use((options, next) => {\n const result = next(options);\n result.catch(response => {\n if (response.code === 'invalid_json') {\n response.message = __('The response is not a valid JSON response.', 'surecart');\n const debugSettingsUrl = 'https://surecart.com/docs/is-not-a-valid-json-response/';\n response.additional_errors = [\n {\n code: 'invalid_json',\n message: sprintf(\n /* translators: %s: URL to debug settings page */\n __('Please ensure that your site is not in debug mode as this may interfere with API responses. %s', 'surecart'),\n `<a href=\"${debugSettingsUrl}\" target=\"_blank\" rel=\"noopener noreferrer\">${__('More Information', 'surecart')}</a>`,\n ),\n },\n ];\n }\n\n if (response.code === 'checkout.finalize_error') {\n response.additional_errors = [\n ...(!response?.additional_errors?.length\n ? [\n {\n code: 'checkout.finalize_error',\n message: response.message,\n },\n ]\n : []),\n ...(response.additional_errors || []),\n ];\n response.message = __('We were not able to process this order', 'surecart');\n }\n return Promise.reject(response);\n });\n\n return result;\n });\n}\n\nexport default apiFetch;\n\n/**\n * Calls the `json` function on the Response, throwing an error if the response\n * doesn't have a json function or if parsing the json itself fails.\n *\n * @param {Response} response\n * @return {Promise<any>} Parsed response.\n */\nexport const parseJsonAndNormalizeError = response => {\n const invalidJsonError = {\n code: 'invalid_json',\n message: __('The response is not a valid JSON response.', 'surecart'),\n };\n\n if (response?.code && response?.message) {\n throw response;\n }\n\n if (!response || !response.json) {\n throw invalidJsonError;\n }\n\n return response.json().catch(() => {\n throw invalidJsonError;\n });\n};\n\nexport const handleNonceError = async response => {\n // need to parse the error response since we are bypassing the apiFetch middleware.\n const error = await parseJsonAndNormalizeError(response);\n\n if (error.code !== 'rest_cookie_invalid_nonce') {\n // console.error(error);\n throw error;\n }\n\n // If the nonce is invalid, refresh it and try again.\n return (\n window\n // @ts-ignore\n .fetch(apiFetch.nonceEndpoint)\n .then(response => {\n if (response.status >= 200 && response.status < 300) {\n return response;\n }\n throw response;\n })\n .then(data => data.text())\n .then(text => {\n // @ts-ignore\n apiFetch.nonceMiddleware.nonce = text;\n })\n );\n};\n"],"version":3}