Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): get/set/delete cookies + domstorage
# Conflicts: # client/package.json # core-interfaces/package.json # core/package.json
- Loading branch information
1 parent
3bd89e3
commit 2e2de6b
Showing
36 changed files
with
645 additions
and
151 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import initializeConstantsAndProperties from 'awaited-dom/base/initializeConstantsAndProperties'; | ||
import StateMachine from 'awaited-dom/base/StateMachine'; | ||
import ISetCookieOptions from '@secret-agent/core-interfaces/ISetCookieOptions'; | ||
import CoreTab from './CoreTab'; | ||
|
||
const { getState, setState } = StateMachine<CookieStorage, IState>(); | ||
|
||
interface IState { | ||
coreTab: Promise<CoreTab>; | ||
} | ||
|
||
export default class CookieStorage { | ||
constructor() { | ||
initializeConstantsAndProperties(this, [], []); | ||
} | ||
|
||
public get length() { | ||
return this.getItems().then(x => x.length); | ||
} | ||
|
||
public async getItems() { | ||
const coreTab = await getState(this).coreTab; | ||
return await coreTab.getCookies(); | ||
} | ||
|
||
public async key(index: number) { | ||
const cookies = await this.getItems(); | ||
return Object.keys(cookies)[index]; | ||
} | ||
|
||
public async clear() { | ||
const coreTab = await getState(this).coreTab; | ||
const cookies = await this.getItems(); | ||
for (const cookie of cookies) { | ||
await coreTab.removeCookie(cookie.name); | ||
} | ||
} | ||
|
||
public async getItem(key: string) { | ||
const cookies = await this.getItems(); | ||
return cookies.find(x => x.name === key); | ||
} | ||
|
||
public async setItem(key: string, value: string, options?: ISetCookieOptions) { | ||
const coreTab = await getState(this).coreTab; | ||
return coreTab.setCookie(key, value, options); | ||
} | ||
|
||
public async removeItem(name: string) { | ||
const coreTab = await getState(this).coreTab; | ||
return coreTab.removeCookie(name); | ||
} | ||
} | ||
|
||
export function createCookieStorage(coreTab: Promise<CoreTab>) { | ||
const cookieStorage = new CookieStorage(); | ||
setState(cookieStorage, { coreTab }); | ||
return cookieStorage; | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ICookie } from './ICookie'; | ||
|
||
export default interface ISetCookieOptions | ||
extends Pick<ICookie, 'httpOnly' | 'secure' | 'sameSite'> { | ||
expires?: Date | number; | ||
} |
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
Oops, something went wrong.