myinvois-sdk
Version:
TypeScript SDK for interacting with the Malaysia e-invoicing system (MyInvois) API
143 lines (142 loc) • 5.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MyInvoisClient = void 0;
const auth_service_1 = require("./services/auth-service");
const invoice_service_1 = require("./services/invoice-service");
const document_service_1 = require("./services/document-service");
const http_client_1 = require("./utils/http-client");
const certificate_handler_1 = require("./utils/certificate-handler");
const config_1 = require("./config");
const code_service_1 = require("./services/code-service");
/**
* Main client for interacting with the MyInvois API
*/
class MyInvoisClient {
/**
* Creates a new MyInvois client
* @param config Configuration for the MyInvois client
*/
constructor(config) {
// Ensure config has all required properties
this.config = {
...config,
environment: config.environment || 'sandbox'
};
// Set default URLs if not provided
if (!this.config.authUrl || !this.config.transactionUrl) {
const environment = this.config.environment;
const urls = config_1.DefaultUrls[environment];
if (!this.config.authUrl) {
this.config.authUrl = urls.authUrl;
}
if (!this.config.transactionUrl) {
this.config.transactionUrl = urls.transactionUrl;
}
}
this.httpClient = new http_client_1.HttpClient();
this.certificateHandler = new certificate_handler_1.CertificateHandler({
certificatePath: this.config.certificatePath,
privateKeyPath: this.config.privateKeyPath,
privateKeyPassphrase: this.config.privateKeyPassphrase
});
this.authService = new auth_service_1.AuthService(this.httpClient, this.config);
this.invoiceService = new invoice_service_1.InvoiceService(this.httpClient, this.authService, this.certificateHandler, this.config);
this.documentService = new document_service_1.DocumentService(this.httpClient, this.authService, this.config);
this.codeService = new code_service_1.CodeService(this.httpClient, this.authService, this.config);
}
/**
* Authenticate with the MyInvois system
* @returns A promise resolving to the authentication token
*/
async authenticate() {
return this.authService.authenticateSystem();
}
/**
* Authenticate on behalf of a taxpayer
* @param taxpayerTIN The taxpayer's TIN
* @returns A promise resolving to the authentication token
*/
async authenticateAsIntermediary(taxpayerTIN) {
return this.authService.authenticateAsIntermediary(taxpayerTIN);
}
/**
* Get a valid token for authentication
* @param authTIN The TIN to use for authentication (optional)
* @returns A promise resolving to the authentication token
*/
async getToken(authTIN) {
return this.authService.getToken(authTIN);
}
/**
* Get all TINs that have valid authentication tokens
* @returns An array of TINs
*/
getAllAuthenticatedTINs() {
return this.authService.getAllAuthenticatedTINs();
}
/**
* Get details about the certificate chain
* @returns A promise resolving to the certificate details
*/
async getCertificateDetails() {
try {
// Ensure the certificate handler is initialized
await this.certificateHandler.initialize();
const now = new Date();
const formatCertInfo = (cert) => {
const daysUntilExpiry = Math.ceil((cert.validTo.getTime() - now.getTime()) / (1000 * 60 * 60 * 24));
return {
subject: cert.subject,
issuer: cert.issuer,
serialNumber: cert.serialNumber,
validFrom: cert.validFrom,
validTo: cert.validTo,
daysUntilExpiry,
isValid: now >= cert.validFrom && now <= cert.validTo
};
};
return {
signing: formatCertInfo(this.certificateHandler.getSigningCertificate()),
intermediate: formatCertInfo(this.certificateHandler.getIntermediateCertificate()),
root: formatCertInfo(this.certificateHandler.getRootCertificate())
};
}
catch (error) {
console.error('Error fetching certificate details:', error);
throw new Error(error.message || 'An error occurred while fetching certificate details');
}
}
/**
* Validate a taxpayer's TIN
* @param taxpayerTIN The taxpayer's TIN
* @param idType The type of ID
* @param idValue The value of the ID
* @param authTIN The TIN to use for authentication (optional)
* @returns A promise resolving to the validation result with consistent format
*/
async validateTaxpayerTIN(taxpayerTIN, idType, idValue, authTIN) {
return this.authService.validateTaxpayerTIN(taxpayerTIN, idType, idValue, authTIN);
}
/**
* Get the invoice service for creating and managing invoices
* @returns The invoice service
*/
get invoices() {
return this.invoiceService;
}
/**
* Get the document service for managing documents
* @returns The document service
*/
get documents() {
return this.documentService;
}
/**
* Get the code service for retrieving code tables
* @returns The code service
*/
get codes() {
return this.codeService;
}
}
exports.MyInvoisClient = MyInvoisClient;