1+ import { resolve } from "path" ;
2+ import fs from "fs" ;
13import { Client } from "soap" ;
24import { AfipContext } from "../afip-context" ;
35import { AccessTicket } from "../auth/access-ticket" ;
@@ -14,6 +16,14 @@ type AfipServiceSoapParam = SoapClientParams & {
1416 wsdl_test ?: WsdlPathEnum ;
1517} & { serviceName : ServiceNamesEnum } ;
1618
19+ type SoapServices < T > = Record <
20+ "Service" ,
21+ Record <
22+ "ServiceSoap" | "ServiceSoap12" ,
23+ Record < keyof T , Record < "input" | "output" , Record < string , any > > >
24+ >
25+ > ;
26+
1727export class AfipService < T extends Client > {
1828 private _soapCliente ?: T ;
1929 private _tokens ?: WSAuthTokens ;
@@ -35,25 +45,50 @@ export class AfipService<T extends Client> {
3545 }
3646 }
3747
38- public setTokens ( tokens : WSAuthTokens ) : void {
39- this . _tokens = tokens ;
48+ private async proxySoapClient ( ) : Promise < T > {
49+ const client = await this . instanceSoapClient ( ) ;
50+ return new Proxy ( client , {
51+ get : ( target : T , prop : string ) => {
52+ const func = prop . endsWith ( "Async" ) ? prop . slice ( 0 , - 5 ) : prop ;
53+ if ( target [ func ] instanceof Function ) {
54+ const soapServices : SoapServices < T > = client . describe ( ) ;
55+
56+ // Get tokens only if the method exist and require Auth.
57+ if ( soapServices ?. Service ?. ServiceSoap ?. [ func ] ?. input ?. [ "Auth" ] ) {
58+ return async ( req : Record < string , any > ) => {
59+ return target [ prop ] ( {
60+ ...( await this . getAuthTokens ( ) ) ,
61+ ...req ,
62+ } ) ;
63+ } ;
64+ }
65+ }
66+ return target [ prop ] ;
67+ } ,
68+ } ) ;
4069 }
4170
42- protected async soapClient ( ) : Promise < T > {
43- if ( ! this . _soapCliente ) {
44- this . _soapCliente = await SoapClientFacade . create < T > ( {
45- wsdl : this . _soapParams . wsdl ,
46- options : {
47- disableCache : true ,
48- ... this . _soapParams . options ,
49- } ,
50- } ) ;
51- this . _soapCliente . setEndpoint ( this . _soapParams . url ) ;
52- }
71+ private async instanceSoapClient ( ) : Promise < T > {
72+ const client = await SoapClientFacade . create < T > ( {
73+ wsdl : this . _soapParams . wsdl ,
74+ options : {
75+ disableCache : true ,
76+ ... this . _soapParams . options ,
77+ } ,
78+ } ) ;
79+ client . setEndpoint ( this . _soapParams . url ) ;
80+ return client ;
81+ }
5382
83+ protected async getClient ( ) : Promise < T > {
84+ if ( ! this . _soapCliente ) this . _soapCliente = await this . proxySoapClient ( ) ;
5485 return this . _soapCliente ;
5586 }
5687
88+ public setTokens ( tokens : WSAuthTokens ) : void {
89+ this . _tokens = tokens ;
90+ }
91+
5792 /**
5893 * I generate signatures through the WSAA. If handleTicket is not defined, the function will save the tokens locally.
5994 * @returns tokens
@@ -82,7 +117,7 @@ export class AfipService<T extends Client> {
82117 *
83118 * @param params Parameters to send
84119 **/
85- protected async getAuthTokens ( ) : Promise < WSAuthParam > {
120+ private async getAuthTokens ( ) : Promise < WSAuthParam > {
86121 if ( this . _tokens ) {
87122 if ( AccessTicket . hasExpired ( this . _tokens . expirationDate ) ) {
88123 if ( this . context . handleTicket ) {
0 commit comments