UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

136 lines (135 loc) 3.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createContext = createContext; exports.fetchData = fetchData; exports.getCountryCodeValue = getCountryCodeValue; exports.handleCountryPath = handleCountryPath; exports.isSupportedCountryCode = isSupportedCountryCode; var _defaults = require("../../../shared/defaults.js"); function createContext(generalConfig = null) { return { withConfig(fn, handlerConfig) { return fn(generalConfig, handlerConfig); } }; } async function fetchDataFromAPI(generalConfig, options) { const { fetchConfig } = generalConfig; const controller = options === null || options === void 0 ? void 0 : options.abortControllerRef; if (controller) { if (controller.current) { controller.current.abort(); controller.current = null; } if (!controller.current) { controller.current = new AbortController(); } } const { signal } = (controller === null || controller === void 0 ? void 0 : controller.current) || {}; const fetchOptions = { method: 'GET', headers: { Accept: 'application/json', ...fetchConfig.headers }, signal }; try { const response = await fetch(fetchConfig.url, fetchOptions); if (controller) { controller.current = null; } return { response, data: await response.json() }; } catch (error) { if (error.name !== 'AbortError') { return error; } } } async function fetchData(value, options) { var _options$preResponseR; const { generalConfig, parameters } = options || {}; const result = options === null || options === void 0 || (_options$preResponseR = options.preResponseResolver) === null || _options$preResponseR === void 0 ? void 0 : _options$preResponseR.call(options, { value }); if (typeof result !== 'undefined') { return result; } const u = generalConfig.fetchConfig.url; const url = typeof u === 'function' ? await u(value, parameters) : u; const { data, response } = await fetchDataFromAPI({ ...generalConfig, fetchConfig: { ...generalConfig.fetchConfig, url } }, options); if (!response) { throw new Error('Please try again!'); } if (!response.ok) { throw new Error(`${response.statusText} – Status: ${response.status}`); } return { data, status: response.status }; } function getCountryCodeValue({ countryCode: givenCountryCode, additionalArgs }) { var _additionalArgs$props; const countryCodeValue = givenCountryCode || ((_additionalArgs$props = additionalArgs.props) === null || _additionalArgs$props === void 0 ? void 0 : _additionalArgs$props['data-country-code']) || _defaults.COUNTRY; const countryCode = additionalArgs.getSourceValue(countryCodeValue) || givenCountryCode; return { countryCode, countryCodeValue }; } function handleCountryPath({ value, countryCode: givenCountryCode, additionalArgs, handler }) { var _additionalArgs$props2; const { countryCode, countryCodeValue } = getCountryCodeValue({ countryCode: givenCountryCode || ((_additionalArgs$props2 = additionalArgs.props) === null || _additionalArgs$props2 === void 0 ? void 0 : _additionalArgs$props2['data-country-code']), additionalArgs }); if (String(countryCodeValue).startsWith('/') && additionalArgs[handler.name] !== handler) { additionalArgs[handler.name] = handler; additionalArgs.setFieldEventListener(countryCodeValue, 'onPathChange', () => { handler(value, additionalArgs); }); } return { countryCode }; } function isSupportedCountryCode(countryCode, supportedCountryCodes) { if (!countryCode) { return false; } return supportedCountryCodes.includes(String(countryCode).toUpperCase()); } //# sourceMappingURL=createContext.js.map