feat: added cookie manager for cli
This commit is contained in:
parent
b8afc30292
commit
8292cfc1c7
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,3 +6,4 @@ lib
|
||||
test.js
|
||||
bun.lockb
|
||||
tsconfig.tsbuildinfo
|
||||
cookies.json
|
||||
@ -10,3 +10,4 @@ src
|
||||
test.js
|
||||
bun.lockb
|
||||
tsconfig.tsbuildinfo
|
||||
cookies.json
|
||||
14
src/types/cookieManager.ts
Normal file
14
src/types/cookieManager.ts
Normal file
@ -0,0 +1,14 @@
|
||||
export interface Cookie {
|
||||
name: string
|
||||
value: string
|
||||
domain?: string
|
||||
path?: string
|
||||
expires?: Date
|
||||
secure?: boolean
|
||||
}
|
||||
|
||||
export interface ICookieManager {
|
||||
getCookie(): string | null
|
||||
setCookie(value: string): void
|
||||
deleteCookie(): void
|
||||
}
|
||||
33
src/utils/cookieManager.ts
Normal file
33
src/utils/cookieManager.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { ICookieManager } from "../types/cookieManager"
|
||||
|
||||
export class CookieManager implements ICookieManager {
|
||||
private cookieName: string
|
||||
|
||||
constructor(cookieName: string) {
|
||||
this.cookieName = cookieName
|
||||
}
|
||||
|
||||
public getCookie(): string | null {
|
||||
if (typeof window === "undefined") return null
|
||||
const cookies = document.cookie.split(";")
|
||||
const cookie = cookies.find((c) =>
|
||||
c.trim().startsWith(`${this.cookieName}=`)
|
||||
)
|
||||
if (!cookie) return null
|
||||
return decodeURIComponent(cookie.split("=")[1])
|
||||
}
|
||||
|
||||
public setCookie(value: string): void {
|
||||
if (typeof window === "undefined") return
|
||||
const date = new Date()
|
||||
date.setTime(date.getTime() + 30 * 24 * 60 * 60 * 1000) // 30 days
|
||||
document.cookie = `${this.cookieName}=${encodeURIComponent(
|
||||
value
|
||||
)}; expires=${date.toUTCString()}; path=/`
|
||||
}
|
||||
|
||||
public deleteCookie(): void {
|
||||
if (typeof window === "undefined") return
|
||||
document.cookie = `${this.cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user