@@ -13,7 +13,9 @@ import {
13
13
IBaseContentMetadata ,
14
14
IExtractedMetadata ,
15
15
ISchema ,
16
+ IMtlsConfig ,
16
17
} from "./types" ;
18
+ import { Agent } from "https" ;
17
19
18
20
const DEFAULT_SERVICE_URL = "http://localhost:8900" ; // Set your default service URL
19
21
@@ -26,24 +28,33 @@ class IndexifyClient {
26
28
constructor (
27
29
serviceUrl : string = DEFAULT_SERVICE_URL ,
28
30
namespace : string = "default" ,
29
- extractionPolicies : IExtractionPolicy [ ]
31
+ // optional mtls config
32
+ extractionPolicies : IExtractionPolicy [ ] ,
33
+ httpsAgent ?: Agent
30
34
) {
31
35
this . serviceUrl = serviceUrl ;
32
36
this . namespace = namespace ;
33
37
this . extractionPolicies = extractionPolicies ;
38
+
34
39
this . client = axios . create ( {
35
40
baseURL : `${ serviceUrl } /namespaces/${ namespace } ` ,
41
+ httpsAgent,
36
42
} ) ;
37
43
}
38
44
39
45
static async createClient ( {
40
46
serviceUrl = DEFAULT_SERVICE_URL ,
41
47
namespace = "default" ,
48
+ mtlsConfig,
42
49
} : {
43
50
serviceUrl ?: string ;
44
51
namespace ?: string ;
52
+ mtlsConfig ?: IMtlsConfig ;
45
53
} = { } ) : Promise < IndexifyClient > {
46
- const response = await axios . get ( `${ serviceUrl } /namespaces/${ namespace } ` ) ;
54
+ const response = await axios . get ( `${ serviceUrl } /namespaces/${ namespace } ` , {
55
+ httpsAgent : IndexifyClient . getHttpsAgent ( { mtlsConfig } ) ,
56
+ } ) ;
57
+
47
58
return new IndexifyClient (
48
59
serviceUrl ,
49
60
namespace ,
@@ -66,7 +77,8 @@ class IndexifyClient {
66
77
content_source : item . content_source ,
67
78
} ;
68
79
}
69
- ) as IExtractionPolicy [ ]
80
+ ) as IExtractionPolicy [ ] ,
81
+ IndexifyClient . getHttpsAgent ( { mtlsConfig } )
70
82
) ;
71
83
}
72
84
@@ -89,12 +101,12 @@ class IndexifyClient {
89
101
throw error ;
90
102
}
91
103
}
92
-
104
+
93
105
private baseContentToContentMetadata = (
94
106
content : IBaseContentMetadata
95
107
) : IContentMetadata => {
96
108
let content_url : string ;
97
-
109
+
98
110
if ( content . storage_url . startsWith ( "http" ) ) {
99
111
// if content is ingested with remote url use storage url
100
112
content_url = content . storage_url ;
@@ -109,33 +121,67 @@ class IndexifyClient {
109
121
} ;
110
122
} ;
111
123
124
+ static getHttpsAgent ( {
125
+ mtlsConfig,
126
+ } : {
127
+ mtlsConfig ?: IMtlsConfig ;
128
+ } ) : Agent | undefined {
129
+ let httpsAgent = undefined ;
130
+ if ( mtlsConfig !== undefined ) {
131
+ if ( typeof window !== "undefined" ) {
132
+ throw new Error (
133
+ "mTLS support is not available in browser environments."
134
+ ) ;
135
+ }
136
+ const fs = require ( "fs" ) ;
137
+ httpsAgent = new Agent ( {
138
+ cert : fs . readFileSync ( mtlsConfig . certPath ) ,
139
+ key : fs . readFileSync ( mtlsConfig . keyPath ) ,
140
+ ...( mtlsConfig . caPath && { ca : fs . readFileSync ( mtlsConfig . caPath ) } ) ,
141
+ rejectUnauthorized : true ,
142
+ } ) ;
143
+ }
144
+
145
+ return httpsAgent ;
146
+ }
147
+
112
148
async get ( endpoint : string ) : Promise < AxiosResponse > {
113
149
return this . request ( "GET" , endpoint ) ;
114
150
}
115
151
116
152
static async namespaces ( {
117
153
serviceUrl = DEFAULT_SERVICE_URL ,
154
+ mtlsConfig,
118
155
} : {
119
156
serviceUrl ?: string ;
157
+ mtlsConfig ?: IMtlsConfig ;
120
158
} = { } ) : Promise < INamespace [ ] > {
121
- const response = await axios . get ( `${ serviceUrl } /namespaces` ) ;
159
+ const response = await axios . get ( `${ serviceUrl } /namespaces` , {
160
+ httpsAgent : IndexifyClient . getHttpsAgent ( { mtlsConfig } ) ,
161
+ } ) ;
122
162
return response . data . namespaces ;
123
163
}
124
164
125
165
static async createNamespace ( {
126
166
namespace,
127
167
extraction_policies,
128
168
labels,
169
+ mtlsConfig,
129
170
} : {
130
171
namespace : string ;
131
172
extraction_policies ?: IExtractionPolicy [ ] ;
132
173
labels ?: Record < string , string > ;
174
+ mtlsConfig ?: IMtlsConfig ;
133
175
} ) {
134
- await axios . post ( `${ DEFAULT_SERVICE_URL } /namespaces` , {
135
- name : namespace ,
136
- extraction_policies : extraction_policies ?? [ ] ,
137
- labels : labels ?? { } ,
138
- } ) ;
176
+ await axios . post (
177
+ `${ DEFAULT_SERVICE_URL } /namespaces` ,
178
+ {
179
+ name : namespace ,
180
+ extraction_policies : extraction_policies ?? [ ] ,
181
+ labels : labels ?? { } ,
182
+ } ,
183
+ { httpsAgent : IndexifyClient . getHttpsAgent ( { mtlsConfig } ) }
184
+ ) ;
139
185
const client = await IndexifyClient . createClient ( { namespace } ) ;
140
186
return client ;
141
187
}
@@ -146,7 +192,7 @@ class IndexifyClient {
146
192
}
147
193
148
194
async extractors ( ) : Promise < Extractor [ ] > {
149
- const response = await axios . get ( `${ this . serviceUrl } /extractors` ) ;
195
+ const response = await this . client . get ( `${ this . serviceUrl } /extractors` ) ;
150
196
const extractorsData = response . data . extractors as IExtractor [ ] ;
151
197
return extractorsData . map ( ( data ) => new Extractor ( data ) ) ;
152
198
}
@@ -248,12 +294,11 @@ class IndexifyClient {
248
294
}
249
295
250
296
async downloadContent < T > ( id : string ) : Promise < T > {
251
- const url = `${ this . serviceUrl } /namespaces/${ this . namespace } /content/${ id } /download` ;
252
297
try {
253
- const response = await axios . get < T > ( url ) ;
298
+ const response = await this . client . get ( `content/ ${ id } /download` ) ;
254
299
return response . data ;
255
300
} catch ( error ) {
256
- throw new Error ( `Failed to download content ${ url } : ${ error } ` ) ;
301
+ throw new Error ( `Failed to download content ${ id } : ${ error } ` ) ;
257
302
}
258
303
}
259
304
0 commit comments