UNPKG

@openade/pem

Version:

Punto di Emissione (Emission Point) - Device library for fiscal receipts

75 lines 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PELClient = void 0; class PELClient { constructor(config) { this.baseUrl = config.pelBaseUrl.replace(/\/$/, ''); this.timeout = config.timeout || 30000; } async getSessionSeed() { const response = await this.request('GET', '/api/session/seed'); return response; } async sendDocument(document) { try { const response = await this.request('POST', '/api/document', document); return { success: true, messageId: response.messageId, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Unknown error', }; } } async sendJournal(journal) { try { const response = await this.request('POST', '/api/journal', journal); return { success: true, messageId: response.messageId, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Unknown error', }; } } async reportConnectionError(error) { await this.request('POST', '/api/anomaly', error); } async request(method, path, body) { const url = `${this.baseUrl}${path}`; const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), this.timeout); try { const response = await fetch(url, { method, headers: { 'Content-Type': 'application/json', }, body: body ? JSON.stringify(body) : undefined, signal: controller.signal, }); clearTimeout(timeoutId); if (!response.ok) { throw new Error(`PEL request failed: ${response.status} ${response.statusText}`); } return await response.json(); } catch (error) { clearTimeout(timeoutId); if (error instanceof Error && error.name === 'AbortError') { throw new Error('PEL request timeout'); } throw error; } } } exports.PELClient = PELClient; //# sourceMappingURL=pel.client.js.map