myinvois-sdk
Version:
TypeScript SDK for interacting with the Malaysia e-invoicing system (MyInvois) API
192 lines (191 loc) • 6.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeService = void 0;
const config_1 = require("../config");
/**
* Service for retrieving code tables from MyInvois API
*/
class CodeService {
/**
* Creates a new code service
* @param httpClient The HTTP client to use
* @param authService The authentication service to use
* @param config The MyInvois configuration
*/
constructor(httpClient, authService, config) {
// Cache the code tables to avoid multiple API calls
this.codeTablesCache = new Map();
this.httpClient = httpClient;
this.authService = authService;
this.config = config;
}
/**
* Get the transaction URL
* @returns The transaction URL
*/
getTransactionUrl() {
if (!this.config.transactionUrl) {
const environment = this.config.environment || 'sandbox';
return config_1.DefaultUrls[environment].transactionUrl;
}
return this.config.transactionUrl;
}
/**
* Get a code table from the API
* @param codeType The type of code table to retrieve
* @returns A promise resolving to the code table
*/
async getCodeTable(codeType) {
// Check if we have the code table in cache
if (this.codeTablesCache.has(codeType)) {
return this.codeTablesCache.get(codeType);
}
const headers = {
'Content-Type': 'application/json'
};
const url = `https://sdk.myinvois.hasil.gov.my/files/${codeType}.json`;
try {
const response = await this.httpClient.get(url, { headers });
// Cache the response
this.codeTablesCache.set(codeType, response);
return response;
}
catch (error) {
console.error(`Failed to get code table for ${codeType}:`, error);
throw new Error(`Failed to get code table for ${codeType}`);
}
}
/**
* Get all document types
* @param authTIN The TIN to use for authentication (optional)
* @returns A promise resolving to the document types
*/
async getDocumentTypes(authTIN) {
const token = await this.authService.getToken(authTIN);
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
};
const url = `${this.config.transactionUrl}/api/v1.0/codes/documenttypes`;
try {
const response = await this.httpClient.get(url, { headers });
return response;
}
catch (error) {
console.error('Failed to get document types:', error);
throw new Error('Failed to get document types');
}
}
/**
* Get all currency codes
* @param authTIN The TIN to use for authentication (optional)
* @returns A promise resolving to the currency codes
*/
async getCurrencyCodes(authTIN) {
const token = await this.authService.getToken(authTIN);
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
};
const url = `${this.config.transactionUrl}/api/v1.0/codes/currencies`;
try {
const response = await this.httpClient.get(url, { headers });
return response;
}
catch (error) {
console.error('Failed to get currency codes:', error);
throw new Error('Failed to get currency codes');
}
}
/**
* Get all state codes
* @returns A promise resolving to the state codes
*/
async getStateCodes() {
return this.getCodeTable('StateCodes');
}
/**
* Get all country codes
* @param authTIN The TIN to use for authentication (optional)
* @returns A promise resolving to the country codes
*/
async getCountryCodes(authTIN) {
const token = await this.authService.getToken(authTIN);
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
};
const url = `${this.config.transactionUrl}/api/v1.0/codes/countries`;
try {
const response = await this.httpClient.get(url, { headers });
return response;
}
catch (error) {
console.error('Failed to get country codes:', error);
throw new Error('Failed to get country codes');
}
}
/**
* Get all tax types
* @returns A promise resolving to the tax types
*/
async getTaxTypes() {
return this.getCodeTable('TaxTypes');
}
/**
* Get all industry codes (MSIC)
* @returns A promise resolving to the industry codes
*/
async getMSICCodes() {
return this.getCodeTable('MSICSubCategoryCodes');
}
/**
* Get all unit codes
* @returns A promise resolving to the unit codes
*/
async getUnitCodes() {
return this.getCodeTable('UnitTypes');
}
/**
* Get all payment means codes
* @returns A promise resolving to the payment means codes
*/
async getPaymentMethods() {
return this.getCodeTable('PaymentMethods');
}
/**
* Get all commodity classification codes
* @returns A promise resolving to the commodity classification codes
*/
async getClassificationCodes() {
return this.getCodeTable('ClassificationCodes');
}
/**
* Get all tax codes
* @param authTIN The TIN to use for authentication (optional)
* @returns A promise resolving to the tax codes
*/
async getTaxCodes(authTIN) {
const token = await this.authService.getToken(authTIN);
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
};
const url = `${this.config.transactionUrl}/api/v1.0/codes/taxcodes`;
try {
const response = await this.httpClient.get(url, { headers });
return response;
}
catch (error) {
console.error('Failed to get tax codes:', error);
throw new Error('Failed to get tax codes');
}
}
/**
* Clear the code tables cache
*/
clearCache() {
this.codeTablesCache.clear();
}
}
exports.CodeService = CodeService;