UNPKG

@cafecafe/afip.ts

Version:
112 lines 5.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AfipService = void 0; const access_ticket_1 = require("../auth/access-ticket"); const afip_auth_1 = require("../auth/afip-auth"); const soap_client_facade_1 = require("../soap/soap-client-facade"); const enums_1 = require("../enums"); class AfipService { constructor(context, _soapParams) { var _a, _b; this.context = context; this._soapParams = _soapParams; this._afipAuth = new afip_auth_1.AfipAuth(context); this._serviceName = this._soapParams.serviceName; this._soapParams.v12 = this._soapParams.v12 || false; this.context.credentials && this.setCredentials(this.context.credentials); if (!this.context.production) { this._soapParams.url = (_a = this._soapParams.url_test) !== null && _a !== void 0 ? _a : this._soapParams.url; this._soapParams.wsdl = (_b = this._soapParams.wsdl_test) !== null && _b !== void 0 ? _b : this._soapParams.wsdl; } } setCredentials(credentials) { this._credentials = new access_ticket_1.AccessTicket(credentials); } isCredentialStillValid() { return this._credentials ? !this._credentials.isExpired() : false; } getClient() { return __awaiter(this, void 0, void 0, function* () { if (!this._soapCliente) this._soapCliente = yield this.proxySoapClient(); return this._soapCliente; }); } instanceSoapClient() { return __awaiter(this, void 0, void 0, function* () { const client = yield soap_client_facade_1.SoapClientFacade.create({ wsdl: this._soapParams.wsdl, options: Object.assign({ disableCache: true, forceSoap12Headers: this._soapParams.v12 }, this._soapParams.options), }); client.setEndpoint(this._soapParams.url); return client; }); } proxySoapClient() { return __awaiter(this, void 0, void 0, function* () { const client = yield this.instanceSoapClient(); return new Proxy(client, { get: (target, prop) => { var _a, _b, _c, _d; const func = prop.endsWith("Async") ? prop.slice(0, -5) : prop; if (target[func] instanceof Function) { const soapServices = client.describe(); const soapVersion = this._soapParams.v12 ? enums_1.SoapServiceVersion.ServiceSoap12 : enums_1.SoapServiceVersion.ServiceSoap; // Get tokens only if the method exist and require Auth. if ((_d = (_c = (_b = (_a = soapServices === null || soapServices === void 0 ? void 0 : soapServices.Service) === null || _a === void 0 ? void 0 : _a[soapVersion]) === null || _b === void 0 ? void 0 : _b[func]) === null || _c === void 0 ? void 0 : _c.input) === null || _d === void 0 ? void 0 : _d["Auth"]) { return (req) => __awaiter(this, void 0, void 0, function* () { const auth = yield this.getWsAuth(); return target[prop](Object.assign(Object.assign({}, auth), req)); }); } } return target[prop]; }, }); }); } /** * Generate signatures through the WSAA. * **/ login() { return __awaiter(this, void 0, void 0, function* () { return this._afipAuth.login(this._serviceName); }); } /** * Generate signatures through the WSAA. If handleTicket is not defined, the function will save the tokens locally. * * @param params Parameters to send **/ getWsAuth() { return __awaiter(this, void 0, void 0, function* () { if (this.context.handleTicket) { if (!this._credentials) { throw new Error("Credentials are not defined yet, and handleTicket param is 'true'. Set them when the Afip class is instantiated."); } else if (this._credentials.isExpired()) { throw new Error("Credentials expired."); } } else if (!this._credentials || this._credentials.isExpired()) { this._credentials = yield this.login(); } return this._credentials.getWSAuthFormat(this.context.cuit); }); } } exports.AfipService = AfipService; //# sourceMappingURL=afip.service.js.map