Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.13.0",
"version": "1.14.0-alpha.3",
"description": "Smartapp SDK",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down
29 changes: 29 additions & 0 deletions src/lib/devices/nfc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
17 changes: 16 additions & 1 deletion src/lib/devices/nfc.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,3 +33,18 @@ export const writeTag = (messages: Array<NfcWriteMessage>): Promise<StatusRespon
})
.then(event => event as StatusResponse)
}

/**
* Check NFC reader status
* @returns Promise that'll be fullfilled on success, otherwise rejected with reason
*/
export const getStatus = (): Promise<NfcStatusResponse> => {
if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE)

return bridge
.sendClientEvent({
method: METHODS.GET_NFC_STATUS,
params: {},
})
.then(event => event as NfcStatusResponse)
}
1 change: 1 addition & 0 deletions src/types/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion src/types/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,13 @@ export interface NfcReadTagResponse extends EmitterEventPayload {
export interface NfcWriteMessage {
mimeType: string
bytes: number[]
}
}

export interface NfcStatusResponse extends Omit<EmitterEventPayload, 'payload'> {
payload: {
status: STATUS
errorCode?: NfcErrorCode
nfcEnabled: boolean
nfcAvailable: boolean
}
}