11import { AxiosRequestConfig } from 'axios' ;
22import { Adapter } from './types/adapters' ;
33
4- export class AuthenticatedAdapter {
5- protected authenticated = false ;
6- mageId ?: string ;
4+ const replaceMageIdInURL = ( url : string , mageId : string ) => url . replace ( / \| M A G E _ I D \| / , mageId ) ;
5+ const addHeaders = ( config : AxiosRequestConfig | undefined , headers : Record < string , string > ) => ( { ...config , headers : { ...config ?. headers , ...headers } } ) ;
76
7+ export class AuthenticatedAdapter {
88 constructor (
99 protected readonly baseAdapter : Adapter ,
1010 protected readonly credentials : {
1111 appSecret : string ;
1212 appId : string ;
13- autoRefresh ?: boolean ;
14- tokenTTL ?: number ;
1513 }
16- ) { }
17-
18- protected replaceMageIdInURL ( url : string ) : string {
19- return url . replace ( / \| M A G E _ I D \| / , this . mageId as string ) ;
20- }
14+ ) { }
2115
2216 async get < T > ( url : string , config ?: AxiosRequestConfig ) : Promise < T > {
23- await this . authenticate ( ) ;
17+ const [ mageId , headers ] = await this . authenticate ( ) ;
2418
25- return this . baseAdapter . get ( this . replaceMageIdInURL ( url ) , config ) ;
19+ return this . baseAdapter . get ( replaceMageIdInURL ( url , mageId ) , addHeaders ( config , headers ) ) ;
2620 }
2721
2822 async post < T > ( url : string , body : unknown , config ?: AxiosRequestConfig ) : Promise < T > {
29- await this . authenticate ( ) ;
23+ const [ mageId , headers ] = await this . authenticate ( ) ;
3024
31- return this . baseAdapter . post ( this . replaceMageIdInURL ( url ) , body , config ) ;
25+ return this . baseAdapter . post ( replaceMageIdInURL ( url , mageId ) , body , addHeaders ( config , headers ) ) ;
3226 }
3327
3428 async put < T > ( url : string , body : unknown , config ?: AxiosRequestConfig ) : Promise < T > {
35- await this . authenticate ( ) ;
29+ const [ mageId , headers ] = await this . authenticate ( ) ;
3630
37- return this . baseAdapter . put ( this . replaceMageIdInURL ( url ) , body , config ) ;
31+ return this . baseAdapter . put ( replaceMageIdInURL ( url , mageId ) , body , addHeaders ( config , headers ) ) ;
3832 }
3933
4034 async delete < T > ( url : string , config ?: AxiosRequestConfig ) : Promise < T > {
41- await this . authenticate ( ) ;
35+ const [ mageId , headers ] = await this . authenticate ( ) ;
4236
43- return this . baseAdapter . delete ( this . replaceMageIdInURL ( url ) , config ) ;
37+ return this . baseAdapter . delete ( replaceMageIdInURL ( url , mageId ) , addHeaders ( config , headers ) ) ;
4438 }
4539
4640 async getMageId ( ) : Promise < string > {
47- await this . authenticate ( ) ;
41+ const [ mageId ] = await this . authenticate ( ) ;
4842
49- return this . mageId as string ;
43+ return mageId ;
5044 }
5145
5246 /**
5347 * Authenticate with the API. You can find the App ID and secret with the next link.
5448 * @see https://developer.magento.com/account/apikeys
5549 * @see https://devdocs.magento.com/marketplace/eqp/v1/auth.html#session-token
5650 */
57- protected async authenticate ( ) : Promise < void > {
58- this . mageId = undefined ;
59-
60- const { expires_in, mage_id, ust } = await this . baseAdapter . post < {
51+ protected async authenticate ( ) {
52+ const { mage_id, ust } = await this . baseAdapter . post < {
6153 mage_id : string ;
6254 ust : string ;
6355 expires_in : number ;
6456 } > (
6557 '/app/session/token' ,
66- { grant_type : 'session' , expires_in : this . credentials . tokenTTL ?? 7200 } ,
58+ { grant_type : 'session' , expires_in : 360 } ,
6759 {
6860 auth : {
6961 username : this . credentials . appId ,
@@ -72,19 +64,6 @@ export class AuthenticatedAdapter {
7264 }
7365 ) ;
7466
75- this . mageId = mage_id ;
76-
77- this . baseAdapter . setHeader ( 'Authorization' , `Bearer ${ ust } ` ) ;
78-
79- if ( this . credentials . autoRefresh ) {
80- // Re-run this function 5 seconds before the token expires
81- setTimeout ( async ( ) => {
82- this . authenticated = false ;
83-
84- await this . authenticate ( ) ;
85- } , expires_in * 1000 - 5 ) ;
86- }
87-
88- this . authenticated = true ;
67+ return [ mage_id , { authorization : `Bearer ${ ust } ` } ] as const ;
8968 }
9069}
0 commit comments