Skip to content

Commit 5be0f61

Browse files
authored
Merge pull request #73 from teacoder-team/dev
feat: rework proxy handling using ProxyAgent
2 parents 9179fca + d0d0c2a commit 5be0f61

8 files changed

Lines changed: 27 additions & 89 deletions

File tree

packages/nestjs-yookassa/dist/common/interfaces/yookassa-options.interface.d.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { FactoryProvider, ModuleMetadata } from '@nestjs/common';
2-
import type { HttpsProxyAgent } from 'https-proxy-agent';
32
export declare const YookassaOptionsSymbol: unique symbol;
43
/**
54
* Настройки модуля YooKassa.
@@ -13,17 +12,7 @@ export type YookassaModuleOptions = {
1312
* Секретный ключ API.
1413
*/
1514
apiKey: string;
16-
/**
17-
* Агент для отправки HTTPS-запросов через прокси.
18-
*
19-
* Обычно HttpsProxyAgent, созданный так:
20-
*
21-
* new HttpsProxyAgent("http://IP:PORT")
22-
*
23-
* Если передан agent — axios прекратит использовать встроенный proxy-режим,
24-
* и весь трафик к YooKassa *гарантированно пойдёт через прокси*.
25-
*/
26-
agent?: HttpsProxyAgent<any>;
15+
proxyUrl?: string;
2716
};
2817
/**
2918
* Асинхронная конфигурация модуля.
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { HttpService } from '@nestjs/axios';
21
import { type YookassaModuleOptions } from '../../common/interfaces';
32
export declare class YookassaHttpClient {
43
private readonly config;
5-
private readonly httpService;
64
private readonly dispatcher;
7-
constructor(config: YookassaModuleOptions, httpService: HttpService);
5+
constructor(config: YookassaModuleOptions);
86
request<T = any>(options: {
97
method: string;
108
url: string;
@@ -15,5 +13,4 @@ export declare class YookassaHttpClient {
1513
post<T>(url: string, data?: any): Promise<T>;
1614
private buildAuthHeader;
1715
private buildUrl;
18-
private extractProxyFromAgent;
1916
}

packages/nestjs-yookassa/dist/core/http/yookassa.http-client.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,22 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
1313
};
1414
Object.defineProperty(exports, "__esModule", { value: true });
1515
exports.YookassaHttpClient = void 0;
16-
const axios_1 = require("@nestjs/axios");
1716
const yookassa_error_1 = require("./yookassa.error");
1817
const yookassa_constants_1 = require("../config/yookassa.constants");
1918
const crypto_1 = require("crypto");
2019
const common_1 = require("@nestjs/common");
2120
const interfaces_1 = require("../../common/interfaces");
2221
const undici_1 = require("undici");
2322
let YookassaHttpClient = class YookassaHttpClient {
24-
constructor(config, httpService) {
23+
constructor(config) {
2524
this.config = config;
26-
this.httpService = httpService;
27-
if (this.config.agent) {
28-
const proxyUrl = this.extractProxyFromAgent();
29-
this.dispatcher = new undici_1.ProxyAgent(proxyUrl);
30-
console.log('[YooKassa] ProxyAgent enabled:', proxyUrl);
25+
if (this.config.proxyUrl) {
26+
this.dispatcher = new undici_1.ProxyAgent(this.config.proxyUrl);
27+
console.log('[YooKassa] ProxyAgent enabled:', this.config.proxyUrl);
3128
}
3229
else {
3330
this.dispatcher = undefined;
31+
console.log('[YooKassa] Proxy not configured, direct connection');
3432
}
3533
}
3634
async request(options) {
@@ -76,18 +74,10 @@ let YookassaHttpClient = class YookassaHttpClient {
7674
}
7775
return full;
7876
}
79-
extractProxyFromAgent() {
80-
var _a, _b;
81-
const proxy = (_b = (_a = this.config.agent) === null || _a === void 0 ? void 0 : _a.proxy) === null || _b === void 0 ? void 0 : _b.href;
82-
if (!proxy) {
83-
throw new Error('[YooKassa] Unable to extract proxy URL from HttpsProxyAgent');
84-
}
85-
return proxy;
86-
}
8777
};
8878
exports.YookassaHttpClient = YookassaHttpClient;
8979
exports.YookassaHttpClient = YookassaHttpClient = __decorate([
9080
(0, common_1.Injectable)(),
9181
__param(0, (0, common_1.Inject)(interfaces_1.YookassaOptionsSymbol)),
92-
__metadata("design:paramtypes", [Object, axios_1.HttpService])
82+
__metadata("design:paramtypes", [Object])
9383
], YookassaHttpClient);

packages/nestjs-yookassa/dist/yookassa.module.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
88
var YookassaModule_1;
99
Object.defineProperty(exports, "__esModule", { value: true });
1010
exports.YookassaModule = void 0;
11-
const axios_1 = require("@nestjs/axios");
1211
const common_1 = require("@nestjs/common");
1312
const yookassa_service_1 = require("./yookassa.service");
1413
const payment_module_1 = require("./modules/payment/payment.module");
@@ -36,7 +35,6 @@ let YookassaModule = YookassaModule_1 = class YookassaModule {
3635
return {
3736
module: YookassaModule_1,
3837
imports: [
39-
axios_1.HttpModule,
4038
payment_module_1.PaymentModule,
4139
refund_module_1.RefundModule,
4240
invoice_module_1.InvoiceModule,
@@ -46,8 +44,8 @@ let YookassaModule = YookassaModule_1 = class YookassaModule {
4644
{ provide: interfaces_1.YookassaOptionsSymbol, useValue: options },
4745
{
4846
provide: yookassa_http_client_1.YookassaHttpClient,
49-
useFactory: (cfg, http) => new yookassa_http_client_1.YookassaHttpClient(cfg, http),
50-
inject: [interfaces_1.YookassaOptionsSymbol, axios_1.HttpService]
47+
useFactory: (cfg) => new yookassa_http_client_1.YookassaHttpClient(cfg),
48+
inject: [interfaces_1.YookassaOptionsSymbol]
5149
},
5250
yookassa_service_1.YookassaService
5351
],
@@ -77,7 +75,6 @@ let YookassaModule = YookassaModule_1 = class YookassaModule {
7775
return {
7876
module: YookassaModule_1,
7977
imports: [
80-
axios_1.HttpModule,
8178
...(options.imports || []),
8279
payment_module_1.PaymentModule,
8380
refund_module_1.RefundModule,
@@ -92,8 +89,8 @@ let YookassaModule = YookassaModule_1 = class YookassaModule {
9289
},
9390
{
9491
provide: yookassa_http_client_1.YookassaHttpClient,
95-
useFactory: (cfg, http) => new yookassa_http_client_1.YookassaHttpClient(cfg, http),
96-
inject: [interfaces_1.YookassaOptionsSymbol, axios_1.HttpService]
92+
useFactory: (cfg) => new yookassa_http_client_1.YookassaHttpClient(cfg),
93+
inject: [interfaces_1.YookassaOptionsSymbol]
9794
},
9895
yookassa_service_1.YookassaService
9996
],

packages/nestjs-yookassa/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nestjs-yookassa",
3-
"version": "2.1.4",
3+
"version": "2.1.5",
44
"description": "A NestJS library for integrating with YooKassa API",
55
"keywords": [
66
"nest",

packages/nestjs-yookassa/src/common/interfaces/yookassa-options.interface.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,7 @@ export type YookassaModuleOptions = {
1717
*/
1818
apiKey: string
1919

20-
/**
21-
* Агент для отправки HTTPS-запросов через прокси.
22-
*
23-
* Обычно HttpsProxyAgent, созданный так:
24-
*
25-
* new HttpsProxyAgent("http://IP:PORT")
26-
*
27-
* Если передан agent — axios прекратит использовать встроенный proxy-режим,
28-
* и весь трафик к YooKassa *гарантированно пойдёт через прокси*.
29-
*/
30-
agent?: HttpsProxyAgent<any>
20+
proxyUrl?: string
3121
}
3222

3323
/**

packages/nestjs-yookassa/src/core/http/yookassa.http-client.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { HttpService } from '@nestjs/axios'
2-
import { firstValueFrom } from 'rxjs'
3-
import type { AxiosRequestConfig } from 'axios'
41
import { YookassaError } from './yookassa.error'
52
import { YOOKASSA_API_URL } from '../config/yookassa.constants'
63
import { randomUUID } from 'crypto'
@@ -9,25 +6,22 @@ import {
96
type YookassaModuleOptions,
107
YookassaOptionsSymbol
118
} from '../../common/interfaces'
12-
import { request, Agent, ProxyAgent } from 'undici'
9+
import { request, ProxyAgent } from 'undici'
1310

1411
@Injectable()
1512
export class YookassaHttpClient {
16-
private readonly dispatcher: any
13+
private readonly dispatcher: ProxyAgent | undefined
1714

1815
public constructor(
1916
@Inject(YookassaOptionsSymbol)
20-
private readonly config: YookassaModuleOptions,
21-
private readonly httpService: HttpService
17+
private readonly config: YookassaModuleOptions
2218
) {
23-
if (this.config.agent) {
24-
const proxyUrl = this.extractProxyFromAgent()
25-
26-
this.dispatcher = new ProxyAgent(proxyUrl)
27-
28-
console.log('[YooKassa] ProxyAgent enabled:', proxyUrl)
19+
if (this.config.proxyUrl) {
20+
this.dispatcher = new ProxyAgent(this.config.proxyUrl)
21+
console.log('[YooKassa] ProxyAgent enabled:', this.config.proxyUrl)
2922
} else {
3023
this.dispatcher = undefined
24+
console.log('[YooKassa] Proxy not configured, direct connection')
3125
}
3226
}
3327

@@ -94,16 +88,4 @@ export class YookassaHttpClient {
9488

9589
return full
9690
}
97-
98-
private extractProxyFromAgent(): string {
99-
const proxy = this.config.agent?.proxy?.href
100-
101-
if (!proxy) {
102-
throw new Error(
103-
'[YooKassa] Unable to extract proxy URL from HttpsProxyAgent'
104-
)
105-
}
106-
107-
return proxy
108-
}
10991
}

packages/nestjs-yookassa/src/yookassa.module.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { HttpModule, HttpService } from '@nestjs/axios'
21
import { type DynamicModule, Global, Module } from '@nestjs/common'
32

43
import { YookassaService } from './yookassa.service'
@@ -34,7 +33,6 @@ export class YookassaModule {
3433
return {
3534
module: YookassaModule,
3635
imports: [
37-
HttpModule,
3836
PaymentModule,
3937
RefundModule,
4038
InvoiceModule,
@@ -45,11 +43,9 @@ export class YookassaModule {
4543

4644
{
4745
provide: YookassaHttpClient,
48-
useFactory: (
49-
cfg: YookassaModuleOptions,
50-
http: HttpService
51-
) => new YookassaHttpClient(cfg, http),
52-
inject: [YookassaOptionsSymbol, HttpService]
46+
useFactory: (cfg: YookassaModuleOptions) =>
47+
new YookassaHttpClient(cfg),
48+
inject: [YookassaOptionsSymbol]
5349
},
5450

5551
YookassaService
@@ -83,7 +79,6 @@ export class YookassaModule {
8379
return {
8480
module: YookassaModule,
8581
imports: [
86-
HttpModule,
8782
...(options.imports || []),
8883
PaymentModule,
8984
RefundModule,
@@ -99,11 +94,9 @@ export class YookassaModule {
9994

10095
{
10196
provide: YookassaHttpClient,
102-
useFactory: (
103-
cfg: YookassaModuleOptions,
104-
http: HttpService
105-
) => new YookassaHttpClient(cfg, http),
106-
inject: [YookassaOptionsSymbol, HttpService]
97+
useFactory: (cfg: YookassaModuleOptions) =>
98+
new YookassaHttpClient(cfg),
99+
inject: [YookassaOptionsSymbol]
107100
},
108101

109102
YookassaService

0 commit comments

Comments
 (0)