Skip to content

Commit

Permalink
Add client storage methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Kudinov committed Jan 17, 2024
1 parent 0406b7b commit 2012007
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 658 deletions.
658 changes: 1 addition & 657 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@expressms/smartapp-sdk",
"version": "1.4.2-alpha.1",
"version": "1.5.0-alpha.5",
"description": "Smartapp SDK",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Bridge from '@expressms/smartapp-bridge'
import {
clientStorageClear,
clientStorageGet,
clientStorageRemove,
clientStorageSet,
createDeeplink,
getChats,
getConnectionStatus,
Expand Down Expand Up @@ -62,4 +66,8 @@ export {
unsubscribeClientEvents,
createDeeplink,
openChatMessage,
clientStorageGet,
clientStorageSet,
clientStorageRemove,
clientStorageClear,
}
2 changes: 2 additions & 0 deletions src/lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import bridge from '@expressms/smartapp-bridge'
import { EmitterEventPayload } from '@expressms/smartapp-bridge/build/main/types/eventEmitter'
import { CreateDeeplinkResponse, ERROR_CODES, File, GetConnectionStatusResponse, METHODS } from '../../types'
export * from './events'
export * from './storage'


const openClientSettings = () => {
return bridge?.sendClientEvent({
Expand Down
68 changes: 68 additions & 0 deletions src/lib/client/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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 => event 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)

return bridge
.sendClientEvent({
method: METHODS.CLIENT_STORAGE_SET,
params: { key, value },
})
.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 }
1 change: 1 addition & 0 deletions src/lib/notification/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const onNotification = async (handleNotification: Function) => {
params: {},
})

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return bridge?.onReceive((event: any) => {
if (event?.type === METHODS.NOTIFICATION) {
handleNotification(response)
Expand Down
14 changes: 14 additions & 0 deletions src/types/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { EmitterEventPayload } from '@expressms/smartapp-bridge/build/main/types/eventEmitter'


export enum METHODS {
READY = 'ready',
ROUTING_CHANGED = 'routing_changes',
Expand All @@ -24,6 +27,10 @@ export enum METHODS {
GET_CONNECTION_STATUS = 'get_connection_status',
CREATE_DEEPLINK = 'create_deeplink',
OPEN_CHAT_MESSAGE = 'open_chat_message',
CLIENT_STORAGE_GET = 'client_storage_get',
CLIENT_STORAGE_SET = 'client_storage_set',
CLIENT_STORAGE_REMOVE = 'client_storage_remove',
CLIENT_STORAGE_CLEAR = 'client_storage_clear',
}

export enum STATUS {
Expand Down Expand Up @@ -62,3 +69,10 @@ export interface File {
fileId: string | null
key: {} | null
}

export interface StatusResponse extends EmitterEventPayload {
payload: {
status: STATUS
errorCode?: string | null,
}
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./bridge"
export * from "./routing"
export * from "./contacts"
export * from "./client"
export * from "./storage"
19 changes: 19 additions & 0 deletions src/types/storage.ts
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
}
}

0 comments on commit 2012007

Please sign in to comment.