Skip to content

Commit

Permalink
feat(cookie-storage): init
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepolischuk committed Sep 21, 2023
1 parent c8b9389 commit b69379b
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
public
16 changes: 14 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- run: yarn lint
- run: yarn typecheck

licenselint:
size:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -48,7 +48,19 @@ jobs:
cache: yarn
node-version: '18.x'
- run: yarn
- run: yarn licenselint
- run: yarn sizecheck

# licenselint:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# - name: Use Node.js
# uses: actions/setup-node@v3
# with:
# cache: yarn
# node-version: '18.x'
# - run: yarn
# - run: yarn licenselint

commitlint:
runs-on: ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion .lintstagedrc.json
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"]
}
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
public
7 changes: 6 additions & 1 deletion .size-limit.json
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 removed packages/.gitkeep
Empty file.
33 changes: 33 additions & 0 deletions packages/cookie-storage/index.test.ts
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()
})
})
67 changes: 67 additions & 0 deletions packages/cookie-storage/index.ts
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 {}
}
15 changes: 15 additions & 0 deletions packages/cookie-storage/package.json
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"
}
}
1 change: 1 addition & 0 deletions packages/cookie-storage/tsconfig.json
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"strictPropertyInitialization": false,
"lib": ["dom", "esnext"],
"baseUrl": ".",
"outDir": "dist",
"declaration": true,
"paths": {
"@rambler-tech/*": ["./packages/*"]
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "dist"
},
"include": [".", "../../types"]
}
1 change: 1 addition & 0 deletions typedoc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": "@rambler-tech/typedoc-config",
"readme": "README.md",
"name": "@rambler-tech/",
"entryPoints": "packages/*",
"out": "public"
Expand Down
10 changes: 10 additions & 0 deletions types/global.d.ts
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
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5236,6 +5236,11 @@ kleur@^3.0.3:
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==

koo@^1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/koo/-/koo-1.5.0.tgz#2ceae4d01d7a63fbd2760e0cd345b1c8bf89325b"
integrity sha512-aiNHS2HPjsN1l/IC1Wcfx4LvQDTO3BqwCnWSl4IE0pTX0RVRgQgNMqEd1Df1jt23WFBsNIp/Azx5K6ZbgkQmqw==

language-subtag-registry@~0.3.2:
version "0.3.22"
resolved "https://registry.yarnpkg.com/language-subtag-registry/-/language-subtag-registry-0.3.22.tgz#2e1500861b2e457eba7e7ae86877cbd08fa1fd1d"
Expand Down

0 comments on commit b69379b

Please sign in to comment.