UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

137 lines (136 loc) 3.63 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 _includes = _interopRequireDefault(require("core-js-pure/stable/instance/includes.js")); var _index = require("../utils/json-pointer/index.js"); var _defaults = require("../../../shared/defaults.js"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function createContext(generalConfig = null) { return { withConfig(fn, handlerConfig) { return fn(generalConfig, handlerConfig); } }; } async function fetchDataFromAPI(generalConfig, options) { const { fetchConfig } = generalConfig; const controller = options?.abortControllerRef; if (controller) { if (controller.current) { controller.current.abort(); controller.current = null; } if (!controller.current) { controller.current = new AbortController(); } } const { signal } = 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 instanceof DOMException && error.name === 'AbortError')) { return error; } } return undefined; } async function fetchData(value, options) { const { generalConfig, parameters } = options || {}; const result = options?.preResponseResolver?.({ 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 }) { const countryCodeValue = givenCountryCode || additionalArgs.props?.['data-country-code'] || _defaults.COUNTRY; const countryCode = additionalArgs.getSourceValue(countryCodeValue) || givenCountryCode; return { countryCode, countryCodeValue }; } function handleCountryPath({ value, countryCode: givenCountryCode, additionalArgs, handler }) { const { countryCode, countryCodeValue } = getCountryCodeValue({ countryCode: givenCountryCode || additionalArgs.props?.['data-country-code'], additionalArgs }); if ((0, _index.isPath)(countryCodeValue) && 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 (0, _includes.default)(supportedCountryCodes).call(supportedCountryCodes, String(countryCode).toUpperCase()); } //# sourceMappingURL=createContext.js.map