11import axios from 'axios' ;
22import { execSync } from 'child_process' ;
33
4- import { ConfigService , ProviderSession } from '../../config/env.config' ;
4+ import { Auth , ConfigService , ProviderSession } from '../../config/env.config' ;
55import { Logger } from '../../config/logger.config' ;
66
77type ResponseSuccess = { status : number ; data ?: any } ;
@@ -10,11 +10,13 @@ type ResponseProvider = Promise<[ResponseSuccess?, Error?]>;
1010export class ProviderFiles {
1111 constructor ( private readonly configService : ConfigService ) {
1212 this . baseUrl = `http://${ this . config . HOST } :${ this . config . PORT } /session/${ this . config . PREFIX } ` ;
13+ this . globalApiToken = this . configService . get < Auth > ( 'AUTHENTICATION' ) . API_KEY . KEY ;
1314 }
1415
1516 private readonly logger = new Logger ( ProviderFiles . name ) ;
1617
1718 private baseUrl : string ;
19+ private globalApiToken : string ;
1820
1921 private readonly config = Object . freeze ( this . configService . get < ProviderSession > ( 'PROVIDER' ) ) ;
2022
@@ -24,14 +26,14 @@ export class ProviderFiles {
2426
2527 public async onModuleInit ( ) {
2628 if ( this . config . ENABLED ) {
27- const client = axios . create ( {
28- baseURL : this . baseUrl ,
29- } ) ;
29+ const url = `http://${ this . config . HOST } :${ this . config . PORT } ` ;
3030 try {
31- const response = await client . options ( '/ping' ) ;
32- if ( ! response ?. data ?. pong ) {
31+ const response = await axios . options ( url + '/ping' ) ;
32+ if ( response ?. data != ' pong' ) {
3333 throw new Error ( 'Offline file provider.' ) ;
3434 }
35+
36+ await axios . post ( `${ url } /session` , { group : this . config . PREFIX } , { headers : { apikey : this . globalApiToken } } ) ;
3537 } catch ( error ) {
3638 this . logger . error ( [ 'Failed to connect to the file server' , error ?. message , error ?. stack ] ) ;
3739 const pid = process . pid ;
@@ -46,9 +48,13 @@ export class ProviderFiles {
4648
4749 public async create ( instance : string ) : ResponseProvider {
4850 try {
49- const response = await axios . post ( `${ this . baseUrl } ` , {
50- instance,
51- } ) ;
51+ const response = await axios . post (
52+ `${ this . baseUrl } ` ,
53+ {
54+ instance,
55+ } ,
56+ { headers : { apikey : this . globalApiToken } } ,
57+ ) ;
5258 return [ { status : response . status , data : response ?. data } ] ;
5359 } catch ( error ) {
5460 return [
@@ -63,7 +69,9 @@ export class ProviderFiles {
6369
6470 public async write ( instance : string , key : string , data : any ) : ResponseProvider {
6571 try {
66- const response = await axios . post ( `${ this . baseUrl } /${ instance } /${ key } ` , data ) ;
72+ const response = await axios . post ( `${ this . baseUrl } /${ instance } /${ key } ` , data , {
73+ headers : { apikey : this . globalApiToken } ,
74+ } ) ;
6775 return [ { status : response . status , data : response ?. data } ] ;
6876 } catch ( error ) {
6977 return [
@@ -78,7 +86,9 @@ export class ProviderFiles {
7886
7987 public async read ( instance : string , key : string ) : ResponseProvider {
8088 try {
81- const response = await axios . get ( `${ this . baseUrl } /${ instance } /${ key } ` ) ;
89+ const response = await axios . get ( `${ this . baseUrl } /${ instance } /${ key } ` , {
90+ headers : { apikey : this . globalApiToken } ,
91+ } ) ;
8292 return [ { status : response . status , data : response ?. data } ] ;
8393 } catch ( error ) {
8494 return [
@@ -93,7 +103,9 @@ export class ProviderFiles {
93103
94104 public async delete ( instance : string , key : string ) : ResponseProvider {
95105 try {
96- const response = await axios . delete ( `${ this . baseUrl } /${ instance } /${ key } ` ) ;
106+ const response = await axios . delete ( `${ this . baseUrl } /${ instance } /${ key } ` , {
107+ headers : { apikey : this . globalApiToken } ,
108+ } ) ;
97109 return [ { status : response . status , data : response ?. data } ] ;
98110 } catch ( error ) {
99111 return [
@@ -108,7 +120,7 @@ export class ProviderFiles {
108120
109121 public async allInstances ( ) : ResponseProvider {
110122 try {
111- const response = await axios . get ( `${ this . baseUrl } /list-instances` ) ;
123+ const response = await axios . get ( `${ this . baseUrl } /list-instances` , { headers : { apikey : this . globalApiToken } } ) ;
112124 return [ { status : response . status , data : response ?. data as string [ ] } ] ;
113125 } catch ( error ) {
114126 return [
@@ -123,7 +135,7 @@ export class ProviderFiles {
123135
124136 public async removeSession ( instance : string ) : ResponseProvider {
125137 try {
126- const response = await axios . delete ( `${ this . baseUrl } /${ instance } ` ) ;
138+ const response = await axios . delete ( `${ this . baseUrl } /${ instance } ` , { headers : { apikey : this . globalApiToken } } ) ;
127139 return [ { status : response . status , data : response ?. data } ] ;
128140 } catch ( error ) {
129141 return [
0 commit comments