UNPKG

myinvois-sdk

Version:

TypeScript SDK for interacting with the Malaysia e-invoicing system (MyInvois) API

109 lines (108 loc) 4.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DocumentService = void 0; /** * Service for document operations */ class DocumentService { /** * Creates a new document service * @param httpClient The HTTP client to use * @param authService The authentication service to use * @param config The MyInvois configuration */ constructor(httpClient, authService, config) { this.httpClient = httpClient; this.authService = authService; this.config = config; } /** * Get document status * @param documentUuid The UUID of the document * @param taxpayerTIN The taxpayer's TIN (for whom the document was created) * @param authTIN The TIN to use for authentication * @returns A promise resolving to the document status */ async getDocumentStatus(documentUuid, taxpayerTIN, authTIN) { // Use authTIN for authentication if provided, otherwise use taxpayerTIN const token = await this.authService.getToken(authTIN || taxpayerTIN); const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }; const url = `${this.config.transactionUrl}/api/v1.0/documents/${documentUuid}/details`; try { const response = await this.httpClient.get(url, { headers }); return response; } catch (error) { console.error('Failed to get document status:', error); throw new Error('Failed to get document status'); } } /** * Get submission details * @param submissionUuid The UUID of the submission * @param taxpayerTIN The taxpayer's TIN (for whom the submission was made) * @param authTIN The TIN to use for authentication * @param pageNo The page number (default: 1) * @param pageSize The page size (default: 100) * @returns A promise resolving to the submission details */ async getSubmissionDetails(submissionUuid, taxpayerTIN, authTIN, pageNo = 1, pageSize = 100) { // Use authTIN for authentication if provided, otherwise use taxpayerTIN const token = await this.authService.getToken(authTIN || taxpayerTIN); const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }; const url = `${this.config.transactionUrl}/api/v1.0/documentsubmissions/${submissionUuid}?pageNo=${pageNo}&pageSize=${pageSize}`; try { const response = await this.httpClient.get(url, { headers }); return response; } catch (error) { console.error('Failed to get submission details:', error); throw new Error('Failed to get submission details'); } } /** * List documents * @param taxpayerTIN The taxpayer's TIN (for whom the documents were created) * @param authTIN The TIN to use for authentication * @param options The query options * @returns A promise resolving to the list of documents */ async listDocuments(taxpayerTIN, authTIN, options = {}) { // Use authTIN for authentication if provided, otherwise use taxpayerTIN const token = await this.authService.getToken(authTIN || taxpayerTIN); const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }; // Build query parameters const queryParams = new URLSearchParams(); if (options.pageNo) queryParams.append('pageNo', options.pageNo.toString()); if (options.pageSize) queryParams.append('pageSize', options.pageSize.toString()); if (options.documentNumber) queryParams.append('documentNumber', options.documentNumber); if (options.fromDate) queryParams.append('fromDate', options.fromDate); if (options.toDate) queryParams.append('toDate', options.toDate); if (options.status) queryParams.append('status', options.status); const url = `${this.config.transactionUrl}/api/v1.0/documents?${queryParams.toString()}`; try { const response = await this.httpClient.get(url, { headers }); return response; } catch (error) { console.error('Failed to list documents:', error); throw new Error('Failed to list documents'); } } } exports.DocumentService = DocumentService;