@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
1 lines • 9.05 kB
Source Map (JSON)
{"version":3,"file":"getStorageAttributes.cjs","names":["DefaultValues","rest","emptyResult: ProcessedStorageAttributes"],"sources":["../../src/getStorageAttributes.ts"],"sourcesContent":["import { DefaultValues } from '@intlayer/config/client';\nimport type {\n CookiesAttributes,\n IntlayerConfig,\n StorageAttributes,\n} from '@intlayer/types';\n\n// ============================================================================\n// Types\n// ============================================================================\n\ntype CookieEntry = {\n name: string;\n attributes: Omit<CookiesAttributes, 'type' | 'name'>;\n};\n\ntype WebStorageEntry = {\n name: string;\n};\n\ntype HeaderEntry = {\n name: string;\n};\n\nexport type ProcessedStorageAttributes = {\n cookies: CookieEntry[];\n localStorage: WebStorageEntry[];\n sessionStorage: WebStorageEntry[];\n headers: HeaderEntry[];\n};\n\ntype StorageEntry =\n | 'cookie'\n | 'localStorage'\n | 'sessionStorage'\n | 'header'\n | CookiesAttributes\n | StorageAttributes;\n\n// ============================================================================\n// Helper Functions\n// ============================================================================\n\n/**\n * Creates a cookie entry with default values for missing attributes\n */\nconst createCookieEntry = (\n options?: Partial<CookiesAttributes>\n): CookieEntry => {\n const { name, path, expires, domain, secure, sameSite, httpOnly } =\n options ?? {};\n\n return {\n name: name ?? DefaultValues.Routing.COOKIE_NAME,\n attributes: {\n path,\n expires,\n domain,\n secure,\n sameSite,\n httpOnly,\n },\n };\n};\n\n/**\n * Creates a web storage entry (localStorage or sessionStorage) with default name\n */\nconst createWebStorageEntry = (\n options?: Partial<StorageAttributes>\n): WebStorageEntry => {\n const { name } = options ?? {};\n\n return {\n name: name ?? DefaultValues.Routing.LOCALE_STORAGE_NAME,\n };\n};\n\n/**\n * Creates a header entry with default name\n */\nconst createHeaderEntry = (\n options?: Partial<StorageAttributes>\n): HeaderEntry => {\n const { name } = options ?? {};\n\n return {\n name: name ?? DefaultValues.Routing.HEADER_NAME,\n };\n};\n\n/**\n * Determines if a storage entry is a cookie based on its properties\n */\nconst isCookieEntry = (entry: any): boolean => {\n return (\n entry.type === 'cookie' ||\n 'sameSite' in entry ||\n 'httpOnly' in entry ||\n 'secure' in entry\n );\n};\n\n/**\n * Determines the storage type from a string literal\n */\nconst isStorageType = (\n value: string\n): value is 'cookie' | 'localStorage' | 'sessionStorage' | 'header' => {\n return (\n value === 'cookie' ||\n value === 'localStorage' ||\n value === 'sessionStorage' ||\n value === 'header'\n );\n};\n\n// ============================================================================\n// Main Function\n// ============================================================================\n\n/**\n * Processes a single storage entry and returns the appropriate storage attributes\n */\nconst processStorageEntry = (\n entry: StorageEntry\n): Partial<ProcessedStorageAttributes> => {\n // Handle string literals\n if (typeof entry === 'string') {\n if (!isStorageType(entry)) {\n return { cookies: [], localStorage: [], sessionStorage: [], headers: [] };\n }\n\n if (entry === 'cookie') {\n return { cookies: [createCookieEntry()] };\n }\n\n if (entry === 'localStorage') {\n return { localStorage: [createWebStorageEntry()] };\n }\n\n if (entry === 'sessionStorage') {\n return { sessionStorage: [createWebStorageEntry()] };\n }\n\n if (entry === 'header') {\n return { headers: [createHeaderEntry()] };\n }\n }\n\n // Handle object entries\n if (typeof entry === 'object' && entry !== null) {\n const typedEntry = entry as CookiesAttributes | StorageAttributes;\n\n if (isCookieEntry(typedEntry)) {\n return { cookies: [createCookieEntry(typedEntry as CookiesAttributes)] };\n }\n\n // Handle localStorage\n if ('type' in typedEntry && typedEntry.type === 'localStorage') {\n const { name, ...rest } = typedEntry as StorageAttributes;\n return { localStorage: [createWebStorageEntry({ name, ...rest })] };\n }\n\n // Handle sessionStorage\n if ('type' in typedEntry && typedEntry.type === 'sessionStorage') {\n const { name, ...rest } = typedEntry as StorageAttributes;\n return { sessionStorage: [createWebStorageEntry({ name, ...rest })] };\n }\n\n // Handle header\n if ('type' in typedEntry && typedEntry.type === 'header') {\n const { name, ...rest } = typedEntry as StorageAttributes;\n return { headers: [createHeaderEntry({ name, ...rest })] };\n }\n\n // Default to localStorage for ambiguous objects\n const { name, ...rest } = typedEntry as Omit<StorageAttributes, 'type'>;\n return { localStorage: [createWebStorageEntry({ name, ...rest })] };\n }\n\n return { cookies: [], localStorage: [], sessionStorage: [], headers: [] };\n};\n\n/**\n * Merges multiple partial storage attributes into a single result\n */\nconst mergeStorageAttributes = (\n accumulated: ProcessedStorageAttributes,\n partial: Partial<ProcessedStorageAttributes>\n): ProcessedStorageAttributes => {\n return {\n cookies: [...accumulated.cookies, ...(partial.cookies ?? [])],\n localStorage: [\n ...accumulated.localStorage,\n ...(partial.localStorage ?? []),\n ],\n sessionStorage: [\n ...accumulated.sessionStorage,\n ...(partial.sessionStorage ?? []),\n ],\n headers: [...accumulated.headers, ...(partial.headers ?? [])],\n };\n};\n\n/**\n * Extracts and normalizes storage configuration into separate arrays for each storage type\n *\n * @param options - The storage configuration from IntlayerConfig\n * @returns An object containing arrays for cookies, localStorage, and sessionStorage\n */\nexport const getStorageAttributes = (\n options: IntlayerConfig['routing']['storage']\n): ProcessedStorageAttributes => {\n const emptyResult: ProcessedStorageAttributes = {\n cookies: [],\n localStorage: [],\n sessionStorage: [],\n headers: [],\n };\n\n // Storage is disabled\n if (options === false || options === undefined) {\n return emptyResult;\n }\n\n // Handle array of storage entries\n if (Array.isArray(options)) {\n return options.reduce<ProcessedStorageAttributes>((acc, entry) => {\n const partial = processStorageEntry(entry);\n return mergeStorageAttributes(acc, partial);\n }, emptyResult);\n }\n\n // Handle single storage entry\n const partial = processStorageEntry(options);\n\n return mergeStorageAttributes(emptyResult, partial);\n};\n"],"mappings":";;;;;;;;AA8CA,MAAM,qBACJ,YACgB;CAChB,MAAM,EAAE,MAAM,MAAM,SAAS,QAAQ,QAAQ,UAAU,aACrD,WAAW,EAAE;AAEf,QAAO;EACL,MAAM,QAAQA,uCAAc,QAAQ;EACpC,YAAY;GACV;GACA;GACA;GACA;GACA;GACA;GACD;EACF;;;;;AAMH,MAAM,yBACJ,YACoB;CACpB,MAAM,EAAE,SAAS,WAAW,EAAE;AAE9B,QAAO,EAC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