diff --git a/package.json b/package.json index e0cb636..0fee4cc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@expressms/smartapp-sdk", - "version": "1.8.2", + "version": "1.9.0-alpha.0", "description": "Smartapp SDK", "main": "build/main/index.js", "typings": "build/main/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 2745ec3..a9dd02a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,6 @@ import { handleDeeplink, openChatMessage, openClientSettings, - openFile, openGroupChat, requestLocation, searchCorporatePhonebook, @@ -21,6 +20,7 @@ import { subscribeClientEvents, unsubscribeClientEvents, } from './lib/client' +import { openFile, openFiles, uploadFile, uploadFiles } from './lib/client/file' import { addContact, createPersonalChat, @@ -30,9 +30,9 @@ import { requestSelfProfile, sendMessage, } from './lib/contacts' -import {useQuery} from './lib/helpers/helpers' -import {ready} from './lib/logging' -import {onNotification} from './lib/notification' +import { useQuery } from './lib/helpers/helpers' +import { ready } from './lib/logging' +import { onNotification } from './lib/notification' import { closeSmartApp, exitSmartAppToCatalog, @@ -54,6 +54,9 @@ export { sendMessage, openSmartApp, openFile, + openFiles, + uploadFile, + uploadFiles, exitSmartAppToCatalog, useQuery, openClientSettings, diff --git a/src/lib/client/file.ts b/src/lib/client/file.ts new file mode 100644 index 0000000..6023089 --- /dev/null +++ b/src/lib/client/file.ts @@ -0,0 +1,79 @@ +import bridge from '@expressms/smartapp-bridge' +import { + ERROR_CODES, + File, + METHODS, +} from '../../types' + +/** + * Download and open single file with client + * @param file File data to be opened + * @returns Promise that'll be fullfilled, otherwise rejected with reason + */ +const openFile = (file: File) => { + if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) + + return bridge.sendClientEvent({ + method: METHODS.OPEN_FILE, + params: file, + }) +} + +/** + * Download file list with client + * @param files Files list to be opened + * @returns Promise that'll be fullfilled, otherwise rejected with reason + */ +const openFiles = (files: File[]) => { + if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) + + return bridge.sendClientEvent({ + method: METHODS.OPEN_FILES, + params: files, + }) +} + + + +/** + * Upload single file with client + * @param mimeType Mime type of allowed files + * @param maxSize Max file size in bytes + * @returns Promise that'll be fullfilled with file metadata on success, otherwise rejected with reason + */ +const uploadFile = ({ mimeType, maxSize }: { mimeType: string, maxSize?: number }) => { + if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) + + return bridge.sendClientEvent({ + method: METHODS.UPLOAD_FILE, + params: { + type: mimeType, + maxSize, + }, + }) +} + +/** + * Upload files list with client + * @param mimeType Mime type of allowed files + * @param maxSize Max file size in bytes + * @returns Promise that'll be fullfilled with files metadata on success, otherwise rejected with reason + */ +const uploadFiles = ({ mimeType, maxSize }: { mimeType: string, maxSize?: number }) => { + if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) + + return bridge.sendClientEvent({ + method: METHODS.UPLOAD_FILES, + params: { + type: mimeType, + maxSize, + }, + }) +} + +export { + openFile, + openFiles, + uploadFile, + uploadFiles, +} \ No newline at end of file diff --git a/src/lib/client/index.ts b/src/lib/client/index.ts index d93b403..95f3092 100644 --- a/src/lib/client/index.ts +++ b/src/lib/client/index.ts @@ -3,7 +3,6 @@ import { EmitterEventPayload } from '@expressms/smartapp-bridge/build/main/types import { CreateDeeplinkResponse, ERROR_CODES, - File, GetConnectionStatusResponse, GetLayoutTypeResponse, GetUnreadCounterResponse, @@ -43,13 +42,6 @@ const openGroupChat = ({ groupChatId }: { groupChatId: string }) => { }) } -const openFile = (file: File) => { - return bridge?.sendClientEvent({ - method: METHODS.OPEN_FILE, - params: file, - }) -} - const sendBotCommand = ({ userHuid, body, @@ -199,7 +191,6 @@ const getLayoutType = async (): Promise => { } export { - openFile, openClientSettings, getChats, searchCorporatePhonebook, diff --git a/src/types/bridge.ts b/src/types/bridge.ts index a40ad55..c337101 100644 --- a/src/types/bridge.ts +++ b/src/types/bridge.ts @@ -21,6 +21,9 @@ export enum METHODS { REQUEST_SELF_PROFILE = 'request_self_profile', CLOSE_SMART_APP = 'close_smart_app', OPEN_FILE = 'open_file', + OPEN_FILES = 'open_files', + UPLOAD_FILE = 'upload_file', + UPLOAD_FILES = 'upload_files', SUBSCRIBE_CLIENT_EVENTS = 'subscribe_client_events', UNSUBSCRIBE_CLIENT_EVENTS = 'unsubscribe_client_events', GET_CONNECTION_STATUS = 'get_connection_status', diff --git a/src/types/client.ts b/src/types/client.ts index 83f3203..01b62de 100644 --- a/src/types/client.ts +++ b/src/types/client.ts @@ -30,7 +30,7 @@ export type CreateDeeplinkResponse = { } } -type LocalPhonebookEntry = { +export type LocalPhonebookEntry = { avatar: string | null name: string | null contacts: {