@@ -14,45 +14,48 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
1414Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
1515exports . YookassaHttpClient = void 0 ;
1616const axios_1 = require ( "@nestjs/axios" ) ;
17- const rxjs_1 = require ( "rxjs" ) ;
1817const yookassa_error_1 = require ( "./yookassa.error" ) ;
1918const yookassa_constants_1 = require ( "../config/yookassa.constants" ) ;
2019const crypto_1 = require ( "crypto" ) ;
2120const common_1 = require ( "@nestjs/common" ) ;
2221const interfaces_1 = require ( "../../common/interfaces" ) ;
22+ const undici_1 = require ( "undici" ) ;
2323let YookassaHttpClient = class YookassaHttpClient {
2424 constructor ( config , httpService ) {
2525 this . config = config ;
2626 this . httpService = httpService ;
27- const client = this . httpService . axiosRef ;
28- client . defaults . baseURL = yookassa_constants_1 . YOOKASSA_API_URL ;
29- client . defaults . timeout = 15000 ;
30- client . defaults . auth = {
31- username : this . config . shopId ,
32- password : this . config . apiKey
33- } ;
34- client . defaults . headers . common [ 'Content-Type' ] = 'application/json' ;
35- client . defaults . proxy = false ;
3627 if ( this . config . agent ) {
37- client . defaults . httpAgent = this . config . agent ;
38- client . defaults . httpsAgent = this . config . agent ;
39- console . log ( `[YooKassa] Proxy agent enabled` ) ;
28+ const proxyUrl = this . extractProxyFromAgent ( ) ;
29+ this . dispatcher = new undici_1 . ProxyAgent ( proxyUrl ) ;
30+ console . log ( '[YooKassa] ProxyAgent enabled:' , proxyUrl ) ;
31+ }
32+ else {
33+ this . dispatcher = undefined ;
4034 }
4135 }
4236 async request ( options ) {
43- var _a , _b , _c , _d , _e ;
37+ const url = this . buildUrl ( options . url , options . params ) ;
4438 try {
45- options . headers = Object . assign ( Object . assign ( { } , options . headers ) , { 'Idempotence-Key' : ( 0 , crypto_1 . randomUUID ) ( ) } ) ;
46- if ( this . config . agent ) {
47- options . httpAgent = this . config . agent ;
48- options . httpsAgent = this . config . agent ;
49- options . proxy = false ;
39+ const res = await ( 0 , undici_1 . request ) ( url , {
40+ method : options . method ,
41+ dispatcher : this . dispatcher ,
42+ headersTimeout : 15000 ,
43+ bodyTimeout : 15000 ,
44+ headers : {
45+ 'Content-Type' : 'application/json' ,
46+ 'Idempotence-Key' : ( 0 , crypto_1 . randomUUID ) ( ) ,
47+ Authorization : this . buildAuthHeader ( )
48+ } ,
49+ body : options . data ? JSON . stringify ( options . data ) : undefined
50+ } ) ;
51+ if ( res . statusCode >= 400 ) {
52+ const text = await res . body . text ( ) ;
53+ throw new yookassa_error_1 . YookassaError ( 'yookassa_error' , text , text ) ;
5054 }
51- const res = await ( 0 , rxjs_1 . firstValueFrom ) ( this . httpService . request ( options ) ) ;
52- return res . data ;
55+ return ( await res . body . json ( ) ) ;
5356 }
5457 catch ( error ) {
55- throw new yookassa_error_1 . YookassaError ( ( ( _b = ( _a = error === null || error === void 0 ? void 0 : error . response ) === null || _a === void 0 ? void 0 : _a . data ) === null || _b === void 0 ? void 0 : _b . type ) || 'yookassa_error' , ( ( _d = ( _c = error === null || error === void 0 ? void 0 : error . response ) === null || _c === void 0 ? void 0 : _c . data ) === null || _d === void 0 ? void 0 : _d . description ) || error . message , ( _e = error === null || error === void 0 ? void 0 : error . response ) === null || _e === void 0 ? void 0 : _e . data ) ;
58+ throw new yookassa_error_1 . YookassaError ( ( error === null || error === void 0 ? void 0 : error . type ) || 'yookassa_error' , ( error === null || error === void 0 ? void 0 : error . message ) || 'Unknown Yookassa error' , error ) ;
5659 }
5760 }
5861 get ( url , params ) {
@@ -61,6 +64,26 @@ let YookassaHttpClient = class YookassaHttpClient {
6164 post ( url , data ) {
6265 return this . request ( { method : 'POST' , url, data } ) ;
6366 }
67+ buildAuthHeader ( ) {
68+ const creds = Buffer . from ( `${ this . config . shopId } :${ this . config . apiKey } ` ) . toString ( 'base64' ) ;
69+ return `Basic ${ creds } ` ;
70+ }
71+ buildUrl ( url , params ) {
72+ let full = `${ yookassa_constants_1 . YOOKASSA_API_URL } ${ url } ` ;
73+ if ( params && typeof params === 'object' ) {
74+ const qp = new URLSearchParams ( params ) ;
75+ full += `?${ qp . toString ( ) } ` ;
76+ }
77+ return full ;
78+ }
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+ }
6487} ;
6588exports . YookassaHttpClient = YookassaHttpClient ;
6689exports . YookassaHttpClient = YookassaHttpClient = __decorate ( [
0 commit comments