1+ import { deleteCookie , getCookies , setCookie } from './.depts.ts'
2+
3+ type value = string | { [ key : string ] : value }
4+ export interface CookieOptions {
5+ /** Expiration date of the cookie. */
6+ expires ?: Date ;
7+ /** Max-Age of the Cookie. Max-Age must be an integer superior or equal to 0. */
8+ maxAge ?: number ;
9+ /** Specifies those hosts to which the cookie will be sent. */
10+ domain ?: string ;
11+ /** Indicates a URL path that must exist in the request. */
12+ path ?: string ;
13+ /** Indicates if the cookie is made using SSL & HTTPS. */
14+ secure ?: boolean ;
15+ /** Indicates that cookie is not accessible via JavaScript. */
16+ httpOnly ?: boolean ;
17+ /**
18+ * Allows servers to assert that a cookie ought not to
19+ * be sent along with cross-site requests.
20+ */
21+ sameSite ?: "Strict" | "Lax" | "None" ;
22+ /** Additional key value pairs with the form "key=value" */
23+ unparsed ?: string [ ] ;
24+ }
25+
26+ export class CookieParser {
27+ #requestHeaders
28+ #responseHeaders
29+ readonly cache : Map < string , string >
30+ constructor ( requestHeaders : Headers , responseHeaders : Headers ) {
31+ this . cache = new Map ( )
32+ this . #requestHeaders = requestHeaders
33+ this . #responseHeaders = responseHeaders
34+ const cookies = < { [ key : string ] : string } > getCookies ( this . #requestHeaders)
35+ Object . keys ( cookies ) . forEach ( ( name ) => {
36+ const value = cookies [ name ]
37+ this . cache . set ( name , value )
38+ } )
39+ }
40+ get ( name : string ) {
41+ return this . cache . get ( name )
42+ }
43+ set ( name : string , value : string , options ?: CookieOptions ) {
44+ setCookie ( this . #responseHeaders, { name, value, ...options } )
45+ this . cache . set ( name , value )
46+ }
47+ delete ( name : string , options ?: { path ?: string ; domain ?: string } ) {
48+ deleteCookie ( this . #responseHeaders, name , options )
49+ this . cache . delete ( name )
50+ }
51+ }
0 commit comments