refactor: safer config

This commit is contained in:
EnixCoda 2019-02-20 14:16:22 +08:00
parent 2efb424981
commit deb16704f2
No known key found for this signature in database
GPG key ID: 6825847C88AA329A
3 changed files with 8 additions and 6 deletions

View file

@ -70,7 +70,7 @@ const init: MethodCreator<Props, ConnectorState> = dispatch => async () => {
compressSingletonFolder,
copyFileButton,
copySnippetButton,
} = await configHelper.get()
} = await configHelper.getAll()
DOMHelper.decorateGitHubPageContent({ copyFileButton, copySnippetButton })
dispatch.set({
accessToken,

View file

@ -19,12 +19,12 @@ export enum config {
const configKeys = Object.values(config)
function get(): any {
function getAll(): any {
return storageHelper.get(configKeys) || {}
}
function getOne(key: keyof Config) {
return get()[key]
return getAll()[key]
}
function set(partialConfig: Partial<Config>) {
@ -38,7 +38,7 @@ function setOne(key: config, value: any) {
}
export default {
get,
getAll,
getOne,
set,
setOne,

View file

@ -2,10 +2,12 @@ export function pick<T>(source: T, keys: string[]): Partial<T> {
if (keys && typeof keys === 'object') {
return (Array.isArray(keys) ? keys : Object.keys(keys)).reduce(
(copy, key) => {
copy[key as keyof T] = source[key as keyof T]
if (key in source) {
copy[key as keyof T] = source[key as keyof T]
}
return copy
},
{} as Partial<T>
{} as Partial<T>,
)
}
return {} as Partial<T>