diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..6feb225 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,3 @@ +node_modules +dist +public diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0eaa7f1..d1c3e1f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -38,7 +38,7 @@ jobs: - run: yarn lint - run: yarn typecheck - licenselint: + size: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 @@ -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 diff --git a/.lintstagedrc.json b/.lintstagedrc.json index dba8ea0..8509693 100644 --- a/.lintstagedrc.json +++ b/.lintstagedrc.json @@ -1,4 +1,5 @@ { "*.{js,ts,tsx}": ["prettier --write", "eslint --fix"], - "*.{json,md}": ["prettier --write"] + "*.md": ["prettier --write"], + "!(packages/*/tsconfig).json": ["prettier --write"] } diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..6feb225 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,3 @@ +node_modules +dist +public diff --git a/.size-limit.json b/.size-limit.json index fe51488..094a66d 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -1 +1,6 @@ -[] +[ + { + "path": "packages/cookie-storage/dist/index.js", + "limit": "990 B" + } +] diff --git a/packages/.gitkeep b/packages/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/packages/cookie-storage/index.test.ts b/packages/cookie-storage/index.test.ts new file mode 100644 index 0000000..fca4431 --- /dev/null +++ b/packages/cookie-storage/index.test.ts @@ -0,0 +1,33 @@ +import {getAll, getItem, setItem, removeItem} from './' + +const TEST_VALUES: Record = { + 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() + }) +}) diff --git a/packages/cookie-storage/index.ts b/packages/cookie-storage/index.ts new file mode 100644 index 0000000..0014749 --- /dev/null +++ b/packages/cookie-storage/index.ts @@ -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 | 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 = (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 = ( + 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 {} +} diff --git a/packages/cookie-storage/package.json b/packages/cookie-storage/package.json new file mode 100644 index 0000000..62078dc --- /dev/null +++ b/packages/cookie-storage/package.json @@ -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" + } +} diff --git a/packages/cookie-storage/tsconfig.json b/packages/cookie-storage/tsconfig.json new file mode 120000 index 0000000..238bf1b --- /dev/null +++ b/packages/cookie-storage/tsconfig.json @@ -0,0 +1 @@ +../../tsconfig.package.json \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index b9fefa6..093f221 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,6 @@ "strictPropertyInitialization": false, "lib": ["dom", "esnext"], "baseUrl": ".", - "outDir": "dist", "declaration": true, "paths": { "@rambler-tech/*": ["./packages/*"] diff --git a/tsconfig.package.json b/tsconfig.package.json new file mode 100644 index 0000000..c9bbdd7 --- /dev/null +++ b/tsconfig.package.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "baseUrl": ".", + "outDir": "dist" + }, + "include": [".", "../../types"] +} diff --git a/typedoc.json b/typedoc.json index 493c130..29ed4a5 100644 --- a/typedoc.json +++ b/typedoc.json @@ -1,5 +1,6 @@ { "extends": "@rambler-tech/typedoc-config", + "readme": "README.md", "name": "@rambler-tech/", "entryPoints": "packages/*", "out": "public" diff --git a/types/global.d.ts b/types/global.d.ts new file mode 100644 index 0000000..27ba67a --- /dev/null +++ b/types/global.d.ts @@ -0,0 +1,10 @@ +interface Window { + msCrypto?: any + grecaptcha?: any + onGoogleLibraryLoad?: any + google?: any +} + +interface Crypto { + webkitSubtle: any +} diff --git a/yarn.lock b/yarn.lock index 89d869e..4264db0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"