types.ts (3555B)
1 import type { ResolveErrorMsg } from "@radroots/utils"; 2 import type { NfcErrorMessage } from "./error.js"; 3 4 export type NfcRecordData = string | ArrayBuffer | ArrayBufferView; 5 6 export type NfcTextEncoding = "utf-8" | "utf-16"; 7 8 export type NfcRecordInput = { 9 record_type: string; 10 data?: NfcRecordData; 11 media_type?: string; 12 id?: string; 13 encoding?: NfcTextEncoding; 14 lang?: string; 15 }; 16 17 export type NfcMessageInput = { 18 records: NfcRecordInput[]; 19 }; 20 21 export type NfcRecord = { 22 record_type: string; 23 data?: NfcRecordData; 24 media_type?: string; 25 id?: string; 26 encoding?: string; 27 lang?: string; 28 }; 29 30 export type NfcMessage = { 31 records: NfcRecord[]; 32 }; 33 34 export type NfcReadPayload = { 35 message: NfcMessage; 36 serial_number?: string; 37 timestamp_ms: number; 38 }; 39 40 export type NfcAvailability = { 41 supported: boolean; 42 secure_context: boolean; 43 window_available: boolean; 44 reader_available: boolean; 45 }; 46 47 export type NfcPermissionState = PermissionState | "unknown"; 48 49 export type NfcReadHandler = (payload: NfcReadPayload) => void; 50 export type NfcErrorHandler = (error: NfcErrorMessage) => void; 51 52 export type NfcScanOptions = { 53 signal?: AbortSignal; 54 timeout_ms?: number; 55 on_read?: NfcReadHandler; 56 on_error?: NfcErrorHandler; 57 }; 58 59 export type NfcWriteOptions = { 60 signal?: AbortSignal; 61 timeout_ms?: number; 62 overwrite?: boolean; 63 }; 64 65 export type NfcWriteInput = string | NfcMessageInput | NfcRecordInput | NfcRecordInput[]; 66 67 export interface INfcScanSession { 68 start(): Promise<ResolveErrorMsg<void, NfcErrorMessage>>; 69 stop(): Promise<void>; 70 get_active(): boolean; 71 get_signal(): AbortSignal; 72 set_on_read(handler?: NfcReadHandler): void; 73 set_on_error(handler?: NfcErrorHandler): void; 74 } 75 76 export interface INfc { 77 availability(): NfcAvailability; 78 permission_state(): Promise<NfcPermissionState>; 79 scan_start(opts?: NfcScanOptions): Promise<ResolveErrorMsg<INfcScanSession, NfcErrorMessage>>; 80 scan_once(opts?: NfcScanOptions): Promise<ResolveErrorMsg<NfcReadPayload, NfcErrorMessage>>; 81 write(message: NfcWriteInput, opts?: NfcWriteOptions): Promise<ResolveErrorMsg<void, NfcErrorMessage>>; 82 make_read_only(opts?: NfcWriteOptions): Promise<ResolveErrorMsg<void, NfcErrorMessage>>; 83 } 84 85 export type NdefRecordData = NfcRecordData; 86 87 export type NdefRecordInit = { 88 recordType: string; 89 mediaType?: string; 90 id?: string; 91 encoding?: string; 92 lang?: string; 93 data?: NdefRecordData; 94 }; 95 96 export type NdefMessageInit = { 97 records: NdefRecordInit[]; 98 }; 99 100 export type NdefRecord = { 101 recordType: string; 102 mediaType?: string; 103 id?: string; 104 encoding?: string; 105 lang?: string; 106 data?: NdefRecordData; 107 }; 108 109 export type NdefMessage = { 110 records: NdefRecord[]; 111 }; 112 113 export type NdefReadingEvent = Event & { 114 message: NdefMessage; 115 serialNumber?: string; 116 }; 117 118 export type NdefScanOptions = { 119 signal?: AbortSignal; 120 }; 121 122 export type NdefWriteOptions = { 123 signal?: AbortSignal; 124 overwrite?: boolean; 125 }; 126 127 export interface NdefReader { 128 scan(options?: NdefScanOptions): Promise<void>; 129 write(message: string | NdefMessageInit, options?: NdefWriteOptions): Promise<void>; 130 makeReadOnly?(options?: NdefWriteOptions): Promise<void>; 131 onreading: ((event: NdefReadingEvent) => void) | null; 132 onreadingerror: ((event: Event) => void) | null; 133 } 134 135 export type NdefReaderConstructor = new () => NdefReader; 136 137 declare global { 138 interface Window { 139 NDEFReader?: NdefReaderConstructor; 140 } 141 }