diff --git a/package.json b/package.json index b6c1a47..1945d7d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@expressms/smartapp-sdk", - "version": "1.13.0", + "version": "1.14.0-alpha.3", "description": "Smartapp SDK", "main": "build/main/index.js", "typings": "build/main/index.d.ts", diff --git a/src/lib/devices/nfc.spec.ts b/src/lib/devices/nfc.spec.ts index e2a702b..b6f2cb0 100644 --- a/src/lib/devices/nfc.spec.ts +++ b/src/lib/devices/nfc.spec.ts @@ -66,4 +66,33 @@ describe('devices:nfc', () => { await expect(writeTag([] as never)).rejects.toBe(ERROR_CODES.NO_BRIDGE) }) }) + + describe('getStatus', () => { + it('success response', async () => { + const response = { ref: 'nfc-status', payload: { status: STATUS.SUCCESS, nfcEnabled: true } } + const sendClientEventMock = vi.fn().mockResolvedValue(response) + + vi.doMock('@expressms/smartapp-bridge', () => ({ + default: { sendClientEvent: sendClientEventMock }, + })) + + const { getStatus } = await import('./nfc') + + const result = await getStatus() + + expect(sendClientEventMock).toHaveBeenCalledTimes(1) + expect(sendClientEventMock).toHaveBeenCalledWith({ + method: METHODS.GET_NFC_STATUS, + params: {}, + }) + expect(result).toBe(response) + }) + + it('no bridge', async () => { + vi.doMock('@expressms/smartapp-bridge', () => ({ default: undefined })) + + const { getStatus } = await import('./nfc') + await expect(getStatus()).rejects.toBe(ERROR_CODES.NO_BRIDGE) + }) + }) }) diff --git a/src/lib/devices/nfc.ts b/src/lib/devices/nfc.ts index 08bcfed..cbe8231 100644 --- a/src/lib/devices/nfc.ts +++ b/src/lib/devices/nfc.ts @@ -1,5 +1,5 @@ import bridge from '@expressms/smartapp-bridge' -import { ERROR_CODES, METHODS, NfcReadTagResponse, NfcWriteMessage, StatusResponse } from '../../types' +import { ERROR_CODES, METHODS, NfcReadTagResponse, NfcStatusResponse, NfcWriteMessage, StatusResponse } from '../../types' /** * Read NFC tag @@ -33,3 +33,18 @@ export const writeTag = (messages: Array): Promise event as StatusResponse) } + +/** + * Check NFC reader status + * @returns Promise that'll be fullfilled on success, otherwise rejected with reason + */ +export const getStatus = (): Promise => { + if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) + + return bridge + .sendClientEvent({ + method: METHODS.GET_NFC_STATUS, + params: {}, + }) + .then(event => event as NfcStatusResponse) +} diff --git a/src/types/bridge.ts b/src/types/bridge.ts index d60f15a..3077906 100644 --- a/src/types/bridge.ts +++ b/src/types/bridge.ts @@ -56,6 +56,7 @@ export enum METHODS { ALLOW_IOS_SWIPE_NAVIGATION = 'allow_ios_swipe_navigation', HIDE_RECV_DATA = 'hide_recv_data', GET_EXPRESS_DISK_AUTH_CODE = 'get_express_disk_auth_code', + GET_NFC_STATUS = 'get_nfc_status', } export enum STATUS { diff --git a/src/types/devices.ts b/src/types/devices.ts index 0dcd311..5291634 100644 --- a/src/types/devices.ts +++ b/src/types/devices.ts @@ -86,4 +86,13 @@ export interface NfcReadTagResponse extends EmitterEventPayload { export interface NfcWriteMessage { mimeType: string bytes: number[] -} \ No newline at end of file +} + +export interface NfcStatusResponse extends Omit { + payload: { + status: STATUS + errorCode?: NfcErrorCode + nfcEnabled: boolean + nfcAvailable: boolean + } +}