-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: allows UPS to receive cookie config options #261
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
517d030
feat: allows UPS to receive cookie config options instead of only the…
tibuurcio 4399d6d
chore: fix tests
tibuurcio 7133cbf
chore(release): 1.17.0-ups-configurable-cookie.1 [skip ci]
mparticle-automation 4eb37d4
Merge branch 'main' into feat/ups-configurable-cookie
tibuurcio 3d1c139
Merge branch 'main' into feat/ups-configurable-cookie
tibuurcio ce59d6f
feature: new option for permanent cookie expire date
tibuurcio 0a64297
Merge branch 'feat/ups-configurable-cookie' of https://github.com/mPa…
tibuurcio 3497173
chore(release): 1.17.0-ups-configurable-cookie.2 [skip ci]
mparticle-automation 6d2277d
chore: trigger checks
tibuurcio 1185884
Merge branch 'feat/ups-configurable-cookie' of https://github.com/mPa…
tibuurcio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { RequireOneOrNone } from 'type-fest' | ||
|
||
export function get(key: string): string | null { | ||
const cookies = getAll() | ||
return cookies?.[key] ? cookies[key] : null | ||
|
@@ -12,23 +14,58 @@ export function getObject(key: string): string | null { | |
return value ? JSON.parse(value) : value | ||
} | ||
|
||
export function put(key: string, value: string | null, options: any /* TODO fix any */ = {}): void { | ||
let expires = options.expires | ||
if (value == null) expires = 'Thu, 01 Jan 1970 00:00:01 GMT' | ||
if (typeof expires === 'string') expires = new Date(expires) | ||
export type CookieOptions = RequireOneOrNone< | ||
{ | ||
path?: string | ||
domain?: string | ||
secure?: boolean | ||
expiresISOString: string | ||
permanent: boolean | ||
}, | ||
'expiresISOString' | 'permanent' | ||
> | ||
Comment on lines
+17
to
+26
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. 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. 🔥 nice |
||
|
||
export function put(key: string, value: string | null, options: CookieOptions = {}): void { | ||
let str = `${_encode(key)}=${value != null ? _encode(value) : ''}` | ||
if (options.path) str += `; path=${options.path}` | ||
if (options.domain) str += `; domain=${options.domain}` | ||
if (options.expires) str += `; expires=${expires.toUTCString()}` | ||
if (options.permanent || options.expiresISOString) | ||
str += `; expires=${calculateExpires(value, options.permanent, options.expiresISOString)}` | ||
if (options.secure) str += '; secure' | ||
document.cookie = str | ||
} | ||
|
||
export function putObject(key: string, value: Record<string, unknown>, options = {}): void { | ||
/** | ||
* This code came from the aurelia-cookie plugin initially and the way they remove a cookie | ||
* is by calling the put method with a null value and the same options as the cookie that needs to be removed. | ||
* | ||
* This null value makes the cookie expire immediately by setting the expires attribute to a date in the past. | ||
* I'm keeping the same logic, but we should consider using a more robust library for cookie management. | ||
* | ||
* If we don't set the expires option, the cookie Expires property in the browser becomes "Session", which | ||
* doesn't seem to have a predictable behaviour across different browsers. | ||
* | ||
* @see https://stackoverflow.com/questions/4132095/when-does-a-cookie-with-expiration-time-at-end-of-session-expire | ||
*/ | ||
function calculateExpires(value: string | null, permanent?: boolean, expires?: string): string { | ||
const defaultExpires = 'Thu, 01 Jan 1970 00:00:01 GMT' | ||
|
||
if (value === null) { | ||
return defaultExpires | ||
} | ||
|
||
if (permanent) { | ||
return 'Sat, 31 Dec 2044 23:59:59 GMT' | ||
} | ||
|
||
return expires ?? defaultExpires | ||
} | ||
|
||
export function putObject(key: string, value: Record<string, unknown>, options: CookieOptions = {}): void { | ||
put(key, JSON.stringify(value), options) | ||
} | ||
|
||
export function remove(key: string, options = {}): void { | ||
export function remove(key: string, options: CookieOptions = {}): void { | ||
put(key, null, options) | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
No need for dateFormatter anymore cause that was used for the
expires
option which is now configurable from the client.