@clerk/shared
Version:
Internal package utils used by the Clerk SDKs
1 lines • 3.22 kB
Source Map (JSON)
{"version":3,"file":"cookie.mjs","names":[],"sources":["../src/cookie.ts"],"sourcesContent":["import Cookies from 'js-cookie';\n\n/**\n * Mirrors js-cookie's `CookieAttributes`. Defined locally so the published d.ts is\n * self-contained: the bundler drops the js-cookie import from the declaration output\n * and `@types/js-cookie` is only a devDependency, so consumers could never resolve\n * the original reference.\n */\nexport interface CookieAttributes {\n /**\n * Define when the cookie will be removed. Value can be a Number\n * which will be interpreted as days from time of creation or a\n * Date instance. If omitted, the cookie becomes a session cookie.\n */\n expires?: number | Date | undefined;\n\n /**\n * Define the path where the cookie is available. Defaults to '/'\n */\n path?: string | undefined;\n\n /**\n * Define the domain where the cookie is available. Defaults to\n * the domain of the page where the cookie was created.\n */\n domain?: string | undefined;\n\n /**\n * A Boolean indicating if the cookie transmission requires a\n * secure protocol (https). Defaults to false.\n */\n secure?: boolean | undefined;\n\n /**\n * Asserts that a cookie must not be sent with cross-origin requests,\n * providing some protection against cross-site request forgery\n * attacks (CSRF)\n */\n sameSite?: 'strict' | 'Strict' | 'lax' | 'Lax' | 'none' | 'None' | undefined;\n\n /**\n * A Boolean indicating whether the cookie is partitioned per top-level\n * site (CHIPS). js-cookie serializes it through the attribute passthrough;\n * declared explicitly because Clerk sets it at several call sites.\n */\n partitioned?: boolean | undefined;\n\n /**\n * An attribute which will be serialized, conformably to RFC 6265\n * section 5.2.\n */\n [property: string]: any;\n}\n\n/**\n * Creates helper methods for dealing with a specific cookie.\n *\n * @example\n * ```ts\n * const cookie = createCookieHandler('my_cookie')\n *\n * cookie.set('my_value');\n * cookie.get() // 'my_value';\n * cookie.remove()\n * ```\n */\nexport function createCookieHandler(cookieName: string) {\n return {\n get() {\n return Cookies.get(cookieName);\n },\n /**\n * Setting a cookie will use some defaults such as path being set to \"/\".\n */\n set(newValue: string, options: CookieAttributes = {}): void {\n Cookies.set(cookieName, newValue, options);\n },\n /**\n * On removing a cookie, you have to pass the exact same path/domain attributes used to set it initially\n * > IMPORTANT! When deleting a cookie and you're not relying on the default attributes, you must pass the exact same path, domain, secure and sameSite attributes that were used to set the cookie.\n *\n * @see https://github.com/js-cookie/js-cookie#basic-usage\n */\n remove(cookieAttributes?: CookieAttributes): void {\n Cookies.remove(cookieName, cookieAttributes);\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;AAkEA,SAAgB,oBAAoB,YAAoB;CACtD,OAAO;EACL,MAAM;GACJ,OAAO,QAAQ,IAAI,UAAU;EAC/B;;;;EAIA,IAAI,UAAkB,UAA4B,CAAC,GAAS;GAC1D,QAAQ,IAAI,YAAY,UAAU,OAAO;EAC3C;;;;;;;EAOA,OAAO,kBAA2C;GAChD,QAAQ,OAAO,YAAY,gBAAgB;EAC7C;CACF;AACF"}