1
1
import { readFileSync } from 'fs' ;
2
+ import { dirname , join } from 'node:path' ;
3
+ import { fileURLToPath } from 'node:url' ;
2
4
import paypalhttp , { type HttpRequest } from '@paypal/paypalhttp' ;
3
5
import { AccessToken } from './AccessToken.js' ;
4
6
import { AccessTokenRequest } from './AccessTokenRequest.js' ;
5
7
import { type HttpRequestBase } from './HttpRequestBase.js' ;
6
8
import { type PayPalEnvironment } from './PayPalEnvironment.js' ;
7
9
import { TokenCache } from './TokenCache.js' ;
8
10
11
+ /**
12
+ * Provider method to simulate __dirname veriable.
13
+ * @param url import.meta.url
14
+ * @param paths paths to join.
15
+ */
16
+ const getDirname = ( url : string , ...paths : string [ ] ) => {
17
+ return join ( dirname ( fileURLToPath ( url ) ) , ...paths ) ;
18
+ } ;
19
+
9
20
/**
10
21
* PayPal Http client
11
22
* Documentation
@@ -27,7 +38,9 @@ export class PayPalHttpClient extends paypalhttp.HttpClient {
27
38
this . _cache = TokenCache . cacheForEnvironment ( environment , refreshToken ) ;
28
39
this . refreshToken = refreshToken ;
29
40
this . addInjector ( this . authInjector ) ;
30
- this . package = JSON . parse ( readFileSync ( '../../package.json' , 'utf8' ) ) ;
41
+ this . package = JSON . parse (
42
+ readFileSync ( getDirname ( import . meta. url , '../../package.json' ) , 'utf8' )
43
+ ) ;
31
44
this . addInjector ( function ( req ) {
32
45
req . headers [ 'Accept-Encoding' ] = 'gzip' ;
33
46
req . headers [ 'sdk_name' ] = 'Checkout SDK' ;
0 commit comments