UNPKG

@openade/pel

Version:

Punto di Elaborazione (Elaboration Point) - Server library for managing PEMs and communicating with ADE

94 lines 4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ADEClient = void 0; const common_1 = require("@openade/common"); const DEFAULT_CONFIG = { baseURL: 'https://corrispettivi.agenziaentrate.gov.it/api/v1', timeout: 30000, debug: false, }; class ADEClient { constructor(config = {}) { this.config = { ...DEFAULT_CONFIG, ...config }; } async request(endpoint, method, body, headers = {}) { const url = `${this.config.baseURL}${endpoint}`; const requestHeaders = { 'Content-Type': 'application/xml', Accept: 'application/xml', ...headers, }; if (this.config.authToken) { requestHeaders['Authorization'] = `Bearer ${this.config.authToken}`; } if (this.config.debug) { console.log(`[ADE API] ${method} ${url}`); if (body) { console.log('[ADE API] Request body:', body); } } try { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), this.config.timeout || 30000); const response = await fetch(url, { method, headers: requestHeaders, body: body ? (typeof body === 'string' ? body : JSON.stringify(body)) : undefined, signal: controller.signal, }); clearTimeout(timeoutId); const responseText = await response.text(); if (this.config.debug) { console.log(`[ADE API] Response status: ${response.status}`); console.log('[ADE API] Response body:', responseText); } if (!response.ok) { throw new Error(`API request failed: ${response.status} ${response.statusText}\n${responseText}`); } return responseText; } catch (error) { if (this.config.debug) { console.error('[ADE API] Request error:', error); } throw error; } } async censusEmissionPoint(census) { const xml = (0, common_1.buildEmissionPointCensusXML)(census); return await this.request('/pem/censimento', 'POST', xml); } async getEmissionPointStatus(vatNumber, emissionPointId) { return await this.request(`/pem/${vatNumber}/${emissionPointId}`, 'GET'); } async deactivateEmissionPoint(vatNumber, emissionPointId) { return await this.request(`/pem/${vatNumber}/${emissionPointId}`, 'DELETE'); } async trasmissioneCorrispettivi(corrispettivi) { const xml = (0, common_1.buildDailyReceiptsXML)(corrispettivi); return await this.request('/corrispettivi', 'POST', xml); } async getEsitoCorrispettivi(partitaIVA, identificativoPEM, dataRiferimento) { return await this.request(`/corrispettivi/${partitaIVA}/${identificativoPEM}/${dataRiferimento}`, 'GET'); } async trasmissioneDocumento(documento) { const xml = (0, common_1.buildCommercialDocumentXML)(documento); return await this.request('/documenti', 'POST', xml); } async trasmissioneJournal(journal) { const xml = (0, common_1.buildJournalXML)(journal); return await this.request('/journal', 'POST', xml); } async richiestaJournal(partitaIVA, identificativoPEM, dataDa, dataA) { return await this.request(`/journal/${partitaIVA}/${identificativoPEM}?dataDa=${dataDa}&dataA=${dataA}`, 'GET'); } async richiestaDocumenti(partitaIVA, identificativoPEM, dataDa, dataA) { return await this.request(`/documenti/${partitaIVA}/${identificativoPEM}?dataDa=${dataDa}&dataA=${dataA}`, 'GET'); } async trasmissioneAnomalie(anomalies) { const xml = (0, common_1.buildAnomaliesXML)(anomalies); return await this.request('/anomalie', 'POST', xml); } } exports.ADEClient = ADEClient; //# sourceMappingURL=ade.client.js.map