set-cookie.d.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. export interface Cookie {
  2. /** Cookie name */
  3. name: string;
  4. /** Cookie value */
  5. value: string;
  6. /** Cookie path */
  7. path?: string;
  8. /** Absolute expiration date for the cookie */
  9. expires?: Date;
  10. /**
  11. * Relative max age of the cookie in seconds from when the client receives it.
  12. * Note: when using with express's res.cookie() method, multiply maxAge by 1000 to convert to milliseconds.
  13. */
  14. maxAge?: number;
  15. /**
  16. * Domain for the cookie,
  17. * may begin with "." to indicate the named domain or any subdomain of it
  18. */
  19. domain?: string;
  20. /** Indicates that this cookie should only be sent over HTTPS */
  21. secure?: boolean;
  22. /** Indicates that this cookie should not be accessible to client-side JavaScript */
  23. httpOnly?: boolean;
  24. /** Indicates a cookie ought not to be sent along with cross-site requests */
  25. sameSite?: string;
  26. /** Indicates the cookie should be stored using partitioned storage */
  27. partitioned?: boolean;
  28. }
  29. export interface CookieMap {
  30. [name: string]: Cookie;
  31. }
  32. export interface Options {
  33. /**
  34. * Calls decodeURIComponent on each value.
  35. * @default true
  36. */
  37. decodeValues?: boolean;
  38. /**
  39. * Return an object instead of an array.
  40. * @default false
  41. */
  42. map?: boolean;
  43. /**
  44. * Suppress the warning that is logged when called on a request instead of a response.
  45. * @default false
  46. */
  47. silent?: boolean;
  48. /**
  49. * Controls whether combined cookie strings are split.
  50. * - `true`: always split
  51. * - `false`: never split
  52. * - `"auto"`: split strings but not arrays
  53. * @default "auto"
  54. */
  55. split?: boolean | "auto";
  56. }
  57. /** Object with a `headers` property (e.g. Node.js IncomingMessage or fetch Response) */
  58. type ResponseLike = {
  59. headers:
  60. | { getSetCookie(): string[] }
  61. | { "set-cookie"?: string | readonly string[] }
  62. | Record<string, string | string[] | undefined>;
  63. };
  64. type SetCookieInput = string | readonly string[] | ResponseLike;
  65. /**
  66. * Parses set-cookie headers into objects.
  67. */
  68. export function parseSetCookie(
  69. input: SetCookieInput,
  70. options: Options & { map: true }
  71. ): CookieMap;
  72. export function parseSetCookie(
  73. input: SetCookieInput,
  74. options?: Options & { map?: false }
  75. ): Cookie[];
  76. export function parseSetCookie(
  77. input: SetCookieInput,
  78. options?: Options
  79. ): Cookie[] | CookieMap;
  80. /**
  81. * Parses a single set-cookie header value string.
  82. * @deprecated Use `parseSetCookie` instead.
  83. */
  84. export function parseString(
  85. setCookieValue: string,
  86. options?: Options
  87. ): Cookie | null;
  88. /**
  89. * Splits a combined set-cookie header string into individual set-cookie header strings.
  90. * @deprecated Use `parseSetCookie` with the `split` option instead.
  91. */
  92. export function splitCookiesString(
  93. input: string | readonly string[] | undefined
  94. ): string[];
  95. /**
  96. * @deprecated Renamed to `parseSetCookie`. Kept for backward compatibility.
  97. */
  98. export {
  99. parseSetCookie as parse,
  100. };
  101. /**
  102. * Default export — the `parseSetCookie` function with additional properties for backward compatibility.
  103. */
  104. declare const _default: typeof parseSetCookie & {
  105. parseSetCookie: typeof parseSetCookie;
  106. parse: typeof parseSetCookie;
  107. parseString: typeof parseString;
  108. splitCookiesString: typeof splitCookiesString;
  109. };
  110. export default _default;