-
Notifications
You must be signed in to change notification settings - Fork 4
Setting up setCookie and getCookie modules #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,15 +32,56 @@ type Cookie = { | |
| secure?: boolean; | ||
| }; | ||
|
|
||
| export const setCookie = ( | ||
| headers: Headers, | ||
| cookie: Cookie | ||
| ): Headers => { | ||
| throw new Error('unimplemented'); | ||
|
|
||
| export const setCookie = (headers: Headers) => (cookie: Cookie): Headers => { | ||
| const { | ||
| name, | ||
| value, | ||
| domain, | ||
| path = '/', | ||
| expires, | ||
| httpOnly, | ||
| maxAge, | ||
| sameSite, | ||
| secure, | ||
| } = cookie; | ||
|
|
||
| let cookieString = `${name}=${value}; Path=${path}`; | ||
| if (domain) cookieString += `; Domain=${domain}`; | ||
| if (maxAge) cookieString += `; Max-Age=${maxAge}`; | ||
| if (expires !== undefined) { | ||
| cookieString += `; Expires=${expires === 0 ? 'Thu, 01 Jan 1970 00:00:00 GMT' : (expires as Date).toUTCString()}`; | ||
| } | ||
| if (secure) cookieString += `; Secure`; | ||
| if (httpOnly) cookieString += `; HttpOnly`; | ||
| if (sameSite) cookieString += `; SameSite=${sameSite}`; | ||
|
|
||
| const key = Object.keys(headers).find(k => k.toLowerCase() === 'set-cookie') || 'set-cookie'; | ||
| const existing = headers[key] || []; | ||
|
|
||
| return { | ||
| ...headers, | ||
| [key]: [...existing, cookieString], | ||
| }; | ||
| }; | ||
|
|
||
| export const getCookie = ( | ||
| headers: Headers | ||
| ) => (name: string): string => { | ||
| throw new Error('unimplemented'); | ||
| } | ||
|
|
||
| export const getCookie = (headers: Headers) => (name: string): Cookie | undefined => { | ||
| const cookieKey = Object.keys(headers).find(k => k.toLowerCase() === 'cookie'); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, instead of iterating through the headers, couldn't we do |
||
| if (!cookieKey) return undefined; | ||
|
|
||
| const cookieString = headers[cookieKey].join('; '); | ||
| const cookieParts = cookieString.split(';').map(c => c.trim()); | ||
|
|
||
|
Comment on lines
+73
to
+75
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
| for (const part of cookieParts) { | ||
| const [k, ...rest] = part.split('='); | ||
| if (k === name) { | ||
| return { | ||
| name: k, | ||
| value: rest.join('=') | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to to iterate all the headers every time? Why not do