Skip to content

Commit

Permalink
feat: browser-api 패키지 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
ksmfou98 committed Oct 18, 2023
1 parent c9f4af3 commit 254470f
Show file tree
Hide file tree
Showing 10 changed files with 461 additions and 247 deletions.
542 changes: 295 additions & 247 deletions .pnp.cjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions apps/jurumarble/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"dependencies": {
"@emotion/is-prop-valid": "^1.2.1",
"@mogakko/browser-api": "workspace:*",
"@mogakko/http-fetch": "workspace:*",
"@monorepo/hooks": "workspace:*",
"@monorepo/ui": "workspace:*",
Expand Down
4 changes: 4 additions & 0 deletions packages/browser-api/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
extends: ["mogakko"],
root: true,
};
1 change: 1 addition & 0 deletions packages/browser-api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./src";
18 changes: 18 additions & 0 deletions packages/browser-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@mogakko/browser-api",
"version": "0.1.0",
"license": "MIT",
"sideEffects": false,
"scripts": {
"eslint": "eslint -c .eslintrc.js --ignore-path ../../.eslintignore --ext .js,.jsx,.ts,.tsx . --cache",
"eslint:fix": "yarn eslint --fix"
},
"dependencies": {
"universal-cookie": "^4.0.4"
},
"devDependencies": {
"eslint": "^8.51.0",
"eslint-config-mogakko": "workspace:*",
"typescript": "4.9.3"
}
}
15 changes: 15 additions & 0 deletions packages/browser-api/src/cookie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Cookies, { CookieSetOptions } from "universal-cookie";

export const getCookie = <T = string | null>(key: string, _cookies?: string): T => {
if (_cookies) {
const cookies = new Cookies(_cookies);
return cookies.get(key);
}
const cookies = new Cookies();
return cookies.get(key);
};

export const setCookie = (key: string, value: string, options?: CookieSetOptions): void => {
const cookies = new Cookies();
cookies.set(key, value, options);
};
3 changes: 3 additions & 0 deletions packages/browser-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./cookie";
export * from "./local-storage";
export * from "./session-storage";
44 changes: 44 additions & 0 deletions packages/browser-api/src/local-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export const setLocalStorage = (key: string, value: string) =>
typeof window !== "undefined" ? window.localStorage.setItem(key, value) : undefined;
export const getLocalStorage = (key: string) =>
typeof window !== "undefined" ? window.localStorage.getItem(key) ?? null : null;
export const clearLocalStorage = (key: string): void =>
typeof window !== "undefined" ? window.localStorage.removeItem(key) : undefined;

interface LocalStorageManagerInterface<T> {
get(): T | null;
set(data: T): void;
remove(): void;
}

/**
* LocalStorage를 사용하기 쉽게 만든 클래스
* @example
* const modalStorage = new LocalStorageManager('modalStorageKey');
* modalStorage.set('value');
* modalStorage.get(); // 'value'
* modalStorage.remove();
*/
export class LocalStorageManager<T = string> implements LocalStorageManagerInterface<T> {
private readonly storageKey: string;

constructor(storageKey: string) {
this.storageKey = storageKey;
}

get(): T | null {
const data = getLocalStorage(this.storageKey);
if (!data) {
return null;
}
return JSON.parse(data);
}

set(data: T) {
setLocalStorage(this.storageKey, JSON.stringify(data));
}

remove() {
clearLocalStorage(this.storageKey);
}
}
44 changes: 44 additions & 0 deletions packages/browser-api/src/session-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export const setSessionStorage = (key: string, value: string) =>
typeof window !== "undefined" ? window.sessionStorage.setItem(key, value) : undefined;
export const getSessionStorage = (key: string) =>
typeof window !== "undefined" ? window.sessionStorage.getItem(key) ?? null : null;
export const clearSessionStorage = (key: string): void =>
typeof window !== "undefined" ? window.sessionStorage.removeItem(key) : undefined;

interface SessionStorageManagerInterface<T> {
get(): T | null;
set(data: T): void;
remove(): void;
}

/**
* SessionStorage를 사용하기 쉽게 만든 클래스
* @example
* const modalStorage = new SessionStorageManager('modalStorageKey');
* modalStorage.set('value');
* modalStorage.get(); // 'value'
* modalStorage.remove();
*/
export class SessionStorageManager<T = string> implements SessionStorageManagerInterface<T> {
private readonly storageKey: string;

constructor(storageKey: string) {
this.storageKey = storageKey;
}

get(): T | null {
const data = getSessionStorage(this.storageKey);
if (!data) {
return null;
}
return JSON.parse(data);
}

set(data: T) {
setSessionStorage(this.storageKey, JSON.stringify(data));
}

remove() {
clearSessionStorage(this.storageKey);
}
}
36 changes: 36 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,17 @@ __metadata:
languageName: node
linkType: hard

"@mogakko/browser-api@workspace:*, @mogakko/browser-api@workspace:packages/browser-api":
version: 0.0.0-use.local
resolution: "@mogakko/browser-api@workspace:packages/browser-api"
dependencies:
eslint: ^8.51.0
eslint-config-mogakko: "workspace:*"
typescript: 4.9.3
universal-cookie: ^4.0.4
languageName: unknown
linkType: soft

"@mogakko/http-fetch@workspace:*, @mogakko/http-fetch@workspace:packages/http-fetch":
version: 0.0.0-use.local
resolution: "@mogakko/http-fetch@workspace:packages/http-fetch"
Expand Down Expand Up @@ -1726,6 +1737,7 @@ __metadata:
resolution: "@monorepo/jurumarble@workspace:apps/jurumarble"
dependencies:
"@emotion/is-prop-valid": ^1.2.1
"@mogakko/browser-api": "workspace:*"
"@mogakko/http-fetch": "workspace:*"
"@monorepo/hooks": "workspace:*"
"@monorepo/ui": "workspace:*"
Expand Down Expand Up @@ -2535,6 +2547,13 @@ __metadata:
languageName: node
linkType: hard

"@types/cookie@npm:^0.3.3":
version: 0.3.3
resolution: "@types/cookie@npm:0.3.3"
checksum: 450c930d792a4fd5a93645b4123f02596368f904dbb1fe6fbb5043bce8f6ecf877a08511c6ba11c8e28168f62bc278e68d214f002fab927c9056c0bc69f21370
languageName: node
linkType: hard

"@types/eslint@npm:^8":
version: 8.44.4
resolution: "@types/eslint@npm:8.44.4"
Expand Down Expand Up @@ -3667,6 +3686,13 @@ __metadata:
languageName: node
linkType: hard

"cookie@npm:^0.4.0":
version: 0.4.2
resolution: "cookie@npm:0.4.2"
checksum: a00833c998bedf8e787b4c342defe5fa419abd96b32f4464f718b91022586b8f1bafbddd499288e75c037642493c83083da426c6a9080d309e3bd90fd11baa9b
languageName: node
linkType: hard

"copy-anything@npm:^2.0.1":
version: 2.0.6
resolution: "copy-anything@npm:2.0.6"
Expand Down Expand Up @@ -8228,6 +8254,16 @@ __metadata:
languageName: node
linkType: hard

"universal-cookie@npm:^4.0.4":
version: 4.0.4
resolution: "universal-cookie@npm:4.0.4"
dependencies:
"@types/cookie": ^0.3.3
cookie: ^0.4.0
checksum: bb2bafa7eb7e213e5448924329572dd1a913be00e23906189be2a0dc889b0eea1750f9c33462fc1c911d89092dbd82f6e220c2d61c4057bb3f69e7665d9d8ddf
languageName: node
linkType: hard

"update-browserslist-db@npm:^1.0.11":
version: 1.0.11
resolution: "update-browserslist-db@npm:1.0.11"
Expand Down

0 comments on commit 254470f

Please sign in to comment.