-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8b9389
commit b69379b
Showing
15 changed files
with
168 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
dist | ||
public |
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,4 +1,5 @@ | ||
{ | ||
"*.{js,ts,tsx}": ["prettier --write", "eslint --fix"], | ||
"*.{json,md}": ["prettier --write"] | ||
"*.md": ["prettier --write"], | ||
"!(packages/*/tsconfig).json": ["prettier --write"] | ||
} |
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,3 @@ | ||
node_modules | ||
dist | ||
public |
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 +1,6 @@ | ||
[] | ||
[ | ||
{ | ||
"path": "packages/cookie-storage/dist/index.js", | ||
"limit": "990 B" | ||
} | ||
] |
Empty file.
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,33 @@ | ||
import {getAll, getItem, setItem, removeItem} from './' | ||
|
||
const TEST_VALUES: Record<string, any> = { | ||
string: 'bar', | ||
boolean: true, | ||
number: 1, | ||
object: {foo: true}, | ||
array: [1, 2, 'asd'] | ||
} | ||
|
||
Object.entries(TEST_VALUES).forEach(([key, value]) => { | ||
test(`cookies ${key}`, () => { | ||
expect(getItem(key)).toBeNull() | ||
expect(getAll()).toBeNull() | ||
setItem(key, value) | ||
expect(getItem(key)).toEqual(value) | ||
expect(getAll()).toEqual({[key]: value}) | ||
removeItem(key) | ||
expect(getItem(key)).toBeNull() | ||
expect(getAll()).toBeNull() | ||
}) | ||
|
||
test(`cookies raw ${key}`, () => { | ||
expect(getItem(`raw${key}`, {raw: true})).toBeNull() | ||
expect(getAll({raw: true})).toBeNull() | ||
setItem(`raw${key}`, value, {raw: true}) | ||
expect(getItem(`raw${key}`, {raw: true})).toEqual(String(value)) | ||
expect(getAll({raw: true})).toEqual({[`raw${key}`]: String(value)}) | ||
removeItem(`raw${key}`) | ||
expect(getItem(`raw${key}`, {raw: true})).toBeNull() | ||
expect(getAll({raw: true})).toBeNull() | ||
}) | ||
}) |
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,67 @@ | ||
import {get, set, remove, CookieOptions} from 'koo' | ||
|
||
/** Cookie storage item options */ | ||
export interface CookieItemOptions { | ||
/** Use the raw value, by default the value is converted to JSON */ | ||
raw?: boolean | ||
} | ||
|
||
/** Cookie storage set item options */ | ||
export interface SetCookieItemOptions | ||
extends CookieOptions, | ||
CookieItemOptions {} | ||
|
||
/** Get all items from cookie storage */ | ||
export const getAll = ( | ||
options: CookieItemOptions = {} | ||
): Record<string, string> | null => { | ||
const {raw} = options | ||
let items | ||
|
||
try { | ||
items = Object.fromEntries( | ||
Object.entries(get()).map(([key, value]) => [ | ||
key, | ||
raw ? value : JSON.parse(value ?? '') | ||
]) | ||
) | ||
} catch {} | ||
|
||
return items ?? null | ||
} | ||
|
||
/** Get an item from cookie storage */ | ||
export const getItem = <T>(key: string, options: CookieItemOptions = {}): T => { | ||
const {raw} = options | ||
let item | ||
|
||
try { | ||
const value = get(key) | ||
|
||
item = raw ? value : JSON.parse(value ?? '') | ||
} catch {} | ||
|
||
return item ?? null | ||
} | ||
|
||
/** Put an item to cookie storage */ | ||
export const setItem = <T>( | ||
key: string, | ||
value: T, | ||
options: SetCookieItemOptions = {} | ||
): void => { | ||
const {raw, ...cookieOptions} = options | ||
|
||
try { | ||
const resultValue = raw ? String(value) : JSON.stringify(value) | ||
|
||
set(key, resultValue, cookieOptions) | ||
} catch {} | ||
} | ||
|
||
/** Remove an item from cookie storage */ | ||
export const removeItem = (key: string, options?: CookieOptions): void => { | ||
try { | ||
remove(key, options) | ||
} catch {} | ||
} |
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,15 @@ | ||
{ | ||
"name": "@rambler-tech/cookie-storage", | ||
"version": "0.0.0", | ||
"main": "dist", | ||
"module": "dist", | ||
"types": "dist/index.d.ts", | ||
"license": "MIT", | ||
"dependencies": { | ||
"koo": "^1.5.0" | ||
}, | ||
"sideEffects": false, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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 @@ | ||
../../tsconfig.package.json |
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,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"outDir": "dist" | ||
}, | ||
"include": [".", "../../types"] | ||
} |
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,10 @@ | ||
interface Window { | ||
msCrypto?: any | ||
grecaptcha?: any | ||
onGoogleLibraryLoad?: any | ||
google?: any | ||
} | ||
|
||
interface Crypto { | ||
webkitSubtle: any | ||
} |
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