{"version":3,"file":"createKeyValueStorageFromCookieStorageAdapter.mjs","sources":["../../../../src/adapter-core/storageFactories/createKeyValueStorageFromCookieStorageAdapter.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { DEFAULT_AUTH_TOKEN_COOKIES_MAX_AGE } from '../constants';\nexport const defaultSetCookieOptions = {\n    // TODO: allow configure with a public interface\n    sameSite: 'lax',\n    secure: true,\n    path: '/',\n};\n/**\n * Creates a Key Value storage interface using the `cookieStorageAdapter` as the\n * underlying storage.\n * @param cookieStorageAdapter An implementation of the `Adapter` in {@link CookieStorage}.\n * @returns An object that implements {@link KeyValueStorageInterface}.\n */\nexport const createKeyValueStorageFromCookieStorageAdapter = (cookieStorageAdapter, validator, setCookieOptions = {}) => {\n    return {\n        setItem(key, value) {\n            // Delete the cookie item first then set it. This results:\n            // SetCookie: key=;expires=1970-01-01;(path='current-path') <- remove path'ed cookies\n            // SetCookie: key=value;expires=Date.now() + 365 days;path=/;secure=true\n            cookieStorageAdapter.delete(key);\n            const mergedCookieOptions = {\n                ...defaultSetCookieOptions,\n                ...setCookieOptions,\n            };\n            // when expires and maxAge both are not specified, we set a default maxAge\n            if (!mergedCookieOptions.expires && !mergedCookieOptions.maxAge) {\n                mergedCookieOptions.maxAge = DEFAULT_AUTH_TOKEN_COOKIES_MAX_AGE;\n            }\n            cookieStorageAdapter.set(key, value, mergedCookieOptions);\n            return Promise.resolve();\n        },\n        async getItem(key) {\n            const cookie = cookieStorageAdapter.get(key);\n            const value = cookie?.value ?? null;\n            if (value && validator?.getItem) {\n                const isValid = await validator.getItem(key, value);\n                if (!isValid)\n                    return null;\n            }\n            return value;\n        },\n        removeItem(key) {\n            cookieStorageAdapter.delete(key);\n            return Promise.resolve();\n        },\n        clear() {\n            // TODO(HuiSF): follow up the implementation.\n            throw new Error('This method has not implemented.');\n        },\n    };\n};\n"],"names":[],"mappings":";;AAAA;AACA;AAEY,MAA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