-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from ExpressApp/feature/ki/secure-storage
Add client storage methods
- Loading branch information
Showing
9 changed files
with
133 additions
and
658 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
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,86 @@ | ||
import bridge from '@expressms/smartapp-bridge' | ||
import { ClientStorageGetResponse, ERROR_CODES, METHODS, StatusResponse, StorageValueType } from '../../types' | ||
|
||
/** | ||
* Get value for key from client storage | ||
* @param key Key | ||
* @returns Promise that'll be fullfilled with `payload.value` on success, otherwise rejected with reason | ||
*/ | ||
const clientStorageGet = ({ key }: { key: string }): Promise<ClientStorageGetResponse> => { | ||
if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) | ||
|
||
return bridge | ||
.sendClientEvent({ | ||
method: METHODS.CLIENT_STORAGE_GET, | ||
params: { key }, | ||
}) | ||
.then(event => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const { payload }: { payload: any } = event | ||
|
||
const deserializedValue = JSON.parse(payload.value); | ||
|
||
return { | ||
...event, | ||
payload: { | ||
...payload, | ||
value: deserializedValue, | ||
} | ||
} as ClientStorageGetResponse | ||
}) | ||
} | ||
|
||
/** | ||
* Save value in client storage | ||
* @param key Key | ||
* @param value Data to be stored | ||
* @returns Promise that'll be fullfilled on success or rejected with reason | ||
*/ | ||
const clientStorageSet = ({ key, value }: { key: string; value: StorageValueType }): Promise<StatusResponse> => { | ||
if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) | ||
|
||
const serializedValue = JSON.stringify(value) | ||
|
||
return bridge | ||
.sendClientEvent({ | ||
method: METHODS.CLIENT_STORAGE_SET, | ||
params: { | ||
key, | ||
value: serializedValue, | ||
}, | ||
}) | ||
.then(event => event as StatusResponse) | ||
} | ||
|
||
/** | ||
* Remove record from client storage | ||
* @param key Key | ||
* @returns Promise that'll be fullfilled on success or rejected with reason | ||
*/ | ||
const clientStorageRemove = ({ key }: { key: string }): Promise<StatusResponse> => { | ||
if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) | ||
|
||
return bridge | ||
.sendClientEvent({ | ||
method: METHODS.CLIENT_STORAGE_REMOVE, | ||
params: { key }, | ||
}) | ||
.then(event => event as StatusResponse) | ||
} | ||
|
||
/** | ||
* Clear all records from client storage | ||
* @returns Promise that'll be fullfilled on success or rejected with reason | ||
*/ | ||
const clientStorageClear = (): Promise<StatusResponse> => { | ||
if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) | ||
|
||
return bridge | ||
.sendClientEvent({ | ||
method: METHODS.CLIENT_STORAGE_CLEAR, | ||
params: {}, | ||
}) | ||
.then(event => event as StatusResponse) | ||
} | ||
|
||
export { clientStorageGet, clientStorageSet, clientStorageRemove, clientStorageClear } |
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
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,19 @@ | ||
import { EmitterEventPayload } from '@expressms/smartapp-bridge/build/main/types/eventEmitter' | ||
import { STATUS } from './bridge' | ||
|
||
export enum CLIENT_STORAGE_ERROR_CODES { | ||
keyNotFound = 'key_not_found', | ||
valueSizeExceeded = 'value_size_exceeded', | ||
storageLimitReached = 'storage_limit_reached', | ||
} | ||
|
||
export type StorageValueType = string | number | null | object | boolean | [] | ||
export type StorageErrorType = CLIENT_STORAGE_ERROR_CODES | null | ||
|
||
export interface ClientStorageGetResponse extends EmitterEventPayload { | ||
payload: { | ||
status: STATUS, | ||
errorCode?: string | null, | ||
value: StorageValueType | ||
} | ||
} |