Skip to content

Commit

Permalink
feat: allows UPS to receive cookie config options instead of only the…
Browse files Browse the repository at this point in the history
… key
  • Loading branch information
tibuurcio committed Jun 3, 2024
1 parent ff68fa7 commit 517d030
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
13 changes: 6 additions & 7 deletions src/services/user-preferences/user-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { type CompositeUserPreferences } from 'src/services/user-preferences/mod
import { type UserPreferenceScope } from 'src/services/user-preferences/models/storage-models/user-preference-scope'
import { type UserPreferenceDefinitions } from 'src/services/user-preferences/models/definitions/user-preference-definitions'
import { type CompositeUserPreferencesService } from 'src/services/user-preferences/composite-user-preferences-service'
import { type CookieOptions } from 'src/utils/Cookies'

export class UserPreferencesService<TUserPreferenceId extends PropertyKey> {
public preferences!: CompositeUserPreferences<TUserPreferenceId>

constructor(
private readonly definitions: UserPreferenceDefinitions<TUserPreferenceId>,
private readonly compositeUserPreferencesService: CompositeUserPreferencesService<TUserPreferenceId>,
private readonly cookieKey: string,
private readonly currentScope: UserPreferenceScope,
public dateFormatter: () => Date,
private readonly cookieOptions: CookieOptions & { key: string },
private readonly onUpdate?: (resolvedPreferences: CompositeUserPreferences<TUserPreferenceId>) => void,
) {}

Expand Down Expand Up @@ -43,7 +43,7 @@ export class UserPreferencesService<TUserPreferenceId extends PropertyKey> {
// @ts-expect-error
const { allowedScope } = this.definitions[userPreferenceId]

const currentStoredPreferences = Cookies.getObject(this.cookieKey)
const currentStoredPreferences = Cookies.getObject(this.cookieOptions.key)

const storedPreferences = this.compositeUserPreferencesService.getUpdatedUserPreferenceStorageObject(
userPreferenceId,
Expand All @@ -69,13 +69,12 @@ export class UserPreferencesService<TUserPreferenceId extends PropertyKey> {
}

private async getStoredPreferences(): Promise<UserPreferences<TUserPreferenceId>> {
return await Promise.resolve(Cookies.getObject(this.cookieKey) ?? {})
return await Promise.resolve(Cookies.getObject(this.cookieOptions.key) ?? {})
}

private async setStoredPreferences(storedPreferences: UserPreferences<TUserPreferenceId>): Promise<void> {
Cookies.putObject(this.cookieKey, storedPreferences, {
expires: this.dateFormatter(),
path: '/',
Cookies.putObject(this.cookieOptions.key, storedPreferences, {
...this.cookieOptions,
})

await Promise.resolve()
Expand Down
18 changes: 13 additions & 5 deletions src/utils/Cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,31 @@ 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 {
export type CookieOptions = {
path?: string
domain?: string
expires?: string | Date
secure?: boolean
}

export function put(key: string, value: string | null, options: CookieOptions = {}): void {
const defaultExpires = 'Thu, 01 Jan 1970 00:00:01 GMT'
let expires = options.expires
if (value == null) expires = 'Thu, 01 Jan 1970 00:00:01 GMT'
if (value == null) expires = defaultExpires
if (typeof expires === 'string') expires = new Date(expires)
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.expires) str += `; expires=${expires?.toUTCString() ?? defaultExpires}`
if (options.secure) str += '; secure'
document.cookie = str
}

export function putObject(key: string, value: Record<string, unknown>, options = {}): void {
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)
}

Expand Down

0 comments on commit 517d030

Please sign in to comment.