-
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
Showing
10 changed files
with
461 additions
and
247 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
extends: ["mogakko"], | ||
root: true, | ||
}; |
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 @@ | ||
export * from "./src"; |
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,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" | ||
} | ||
} |
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 @@ | ||
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); | ||
}; |
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 @@ | ||
export * from "./cookie"; | ||
export * from "./local-storage"; | ||
export * from "./session-storage"; |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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