@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
138 lines (137 loc) • 5.29 kB
JavaScript
;
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");
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; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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;
}
}
}
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.ok) {
throw new Error(`${response.statusText} – Status: ${response.status}`);
}
return {
data,
status: response.status
};
}
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']) || _defaults.COUNTRY;
const countryCode = additionalArgs.getSourceValue(countryCodeValue) || givenCountryCode;
return {
countryCode,
countryCodeValue
};
}
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
};
}
function isSupportedCountryCode(countryCode, supportedCountryCodes) {
if (!countryCode) {
return false;
}
return supportedCountryCodes.includes(String(countryCode).toUpperCase());
}
//# sourceMappingURL=createContext.js.map