synapse-storage
Version:
Набор инструментов для управления состоянием и апи-запросами
40 lines (35 loc) • 1.64 kB
JavaScript
import { logError } from "../../_utils/error-handling.util.js";
/**
* Создает контекст для подготовки заголовков
*
* @param context - базовый контекст
* @param optionContext - дополнительный контекст из опций
* @returns - полный контекст для подготовки заголовков
*/ function createHeaderContext(context = {}, optionContext = {}) {
return {
...context,
...optionContext,
getFromStorage: context.getFromStorage || ((key)=>{
try {
if (typeof globalThis.localStorage === 'undefined') return undefined;
const item = localStorage.getItem(key);
return item ? JSON.parse(item) : undefined;
} catch (error) {
logError('createHeaderContext: error reading from localStorage', error, null, 'warn');
return undefined;
}
}),
getCookie: context.getCookie || ((name)=>{
try {
if (typeof globalThis.document === 'undefined') return undefined;
const matches = document.cookie.match(new RegExp(`(?:^|; )${name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1')}=([^;]*)`));
return matches ? decodeURIComponent(matches[1]) : undefined;
} catch (error) {
logError('createHeaderContext: error reading cookie', error, null, 'warn');
return undefined;
}
})
};
}
export { createHeaderContext };
//# sourceMappingURL=create-header-context.js.map