@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
129 lines • 4.58 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { COUNTRY as defaultCountry } from '../../../shared/defaults';
export function createContext() {
let generalConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 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: _objectSpread({
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;
}
}
}
export async function fetchData(value, options) {
var _options$preResponseR;
const {
generalConfig,
parameters
} = options || {};
const result = options === null || options === void 0 ? 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(_objectSpread(_objectSpread({}, generalConfig), {}, {
fetchConfig: _objectSpread(_objectSpread({}, 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
};
}
export function getCountryCodeValue(_ref) {
var _additionalArgs$props;
let {
countryCode: givenCountryCode,
additionalArgs
} = _ref;
const countryCodeValue = givenCountryCode || ((_additionalArgs$props = additionalArgs.props) === null || _additionalArgs$props === void 0 ? void 0 : _additionalArgs$props['data-country-code']) || defaultCountry;
const countryCode = additionalArgs.getSourceValue(countryCodeValue) || givenCountryCode;
return {
countryCode,
countryCodeValue
};
}
export function handleCountryPath(_ref2) {
var _additionalArgs$props2;
let {
value,
countryCode: givenCountryCode,
additionalArgs,
handler
} = _ref2;
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
};
}
export function isSupportedCountryCode(countryCode, supportedCountryCodes) {
if (!countryCode) {
return false;
}
return supportedCountryCodes.includes(String(countryCode).toUpperCase());
}
//# sourceMappingURL=createContext.js.map