Skip to content

Commit af717bd

Browse files
committed
feat(proxy): add proxy for auth methods
1 parent 76f1bcf commit af717bd

File tree

4 files changed

+57
-28
lines changed

4 files changed

+57
-28
lines changed

src/services/afip.service.ts

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { resolve } from "path";
2+
import fs from "fs";
13
import { Client } from "soap";
24
import { AfipContext } from "../afip-context";
35
import { 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+
1727
export 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) {

src/services/electronic-billing.service.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class ElectronicBillingService extends AfipService<IServiceSoapSoap> {
2929
* Asks to web service for servers status
3030
**/
3131
async getServerStatus(): Promise<IFEDummyOutput> {
32-
const client = await this.soapClient();
32+
const client = await this.getClient();
3333
const [output] = await client.FEDummyAsync({});
3434
return output;
3535
}
@@ -41,10 +41,8 @@ export class ElectronicBillingService extends AfipService<IServiceSoapSoap> {
4141
* @return array All sales points availables
4242
**/
4343
public async getSalesPoints(): Promise<IGetSalesPointsResult> {
44-
const client = await this.soapClient();
45-
const [output] = await client.FEParamGetPtosVentaAsync(
46-
await this.getAuthTokens()
47-
);
44+
const client = await this.getClient();
45+
const [output] = await client.FEParamGetPtosVentaAsync({});
4846
return output;
4947
}
5048

@@ -60,10 +58,8 @@ export class ElectronicBillingService extends AfipService<IServiceSoapSoap> {
6058
salesPoint: number,
6159
type: number
6260
): Promise<IFECompUltimoAutorizadoOutput> {
63-
const auth = await this.getAuthTokens();
64-
const client = await this.soapClient();
61+
const client = await this.getClient();
6562
const [output] = await client.FECompUltimoAutorizadoAsync({
66-
...auth,
6763
PtoVta: salesPoint,
6864
CbteTipo: type,
6965
});
@@ -79,10 +75,8 @@ export class ElectronicBillingService extends AfipService<IServiceSoapSoap> {
7975
* @param req data Voucher parameter
8076
**/
8177
public async createVoucher(req: IVoucher): Promise<ICreateVoucherResult> {
82-
const auth = await this.getAuthTokens();
83-
const client = await this.soapClient();
78+
const client = await this.getClient();
8479
const [output] = await client.FECAESolicitarAsync({
85-
...auth,
8680
FeCAEReq: {
8781
FeCabReq: {
8882
CantReg: req.CbteHasta - req.CbteDesde + 1,

src/soap/interfaces/Service/ServiceSoap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Client } from "soap";
22

33
/* tslint:disable:max-line-length no-empty-interface */
44
export interface IFECAESolicitarInput {
5-
Auth: ServiceSoapTypes.IAuth;
5+
// Auth: ServiceSoapTypes.IAuth;
66
FeCAEReq: ServiceSoapTypes.IFeCAEReq;
77
}
88

@@ -153,7 +153,7 @@ export interface IFEParamGetTiposConceptoOutput {
153153
}
154154

155155
export interface IFEParamGetPtosVentaInput {
156-
Auth: ServiceSoapTypes.IAuth;
156+
// Auth: ServiceSoapTypes.IAuth;
157157
}
158158

159159
export interface IFEParamGetPtosVentaOutput {

tests/electronic-billings.service.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe("Electronic Billings Service Test", () => {
5252
console.dir(voucher, { depth: 50 });
5353
expect(voucher).not.toBeNull();
5454
} catch (error) {
55-
// console.log(error);
55+
console.log(error);
5656
throw error;
5757
}
5858
});

0 commit comments

Comments
 (0)