@@ -2,49 +2,9 @@ import type {
22 ElasticsearchBulkParams ,
33 ElasticsearchBulkResponse ,
44} from '@/tools/elasticsearch/types'
5+ import { buildAuthHeaders , buildBaseUrl } from '@/tools/elasticsearch/utils'
56import type { ToolConfig } from '@/tools/types'
67
7- function buildBaseUrl ( params : ElasticsearchBulkParams ) : string {
8- if ( params . deploymentType === 'cloud' && params . cloudId ) {
9- const parts = params . cloudId . split ( ':' )
10- if ( parts . length >= 2 ) {
11- try {
12- const decoded = Buffer . from ( parts [ 1 ] , 'base64' ) . toString ( 'utf-8' )
13- const [ esHost ] = decoded . split ( '$' )
14- if ( esHost ) {
15- return `https://${ parts [ 0 ] } .${ esHost } `
16- }
17- } catch {
18- // Fallback
19- }
20- }
21- throw new Error ( 'Invalid Cloud ID format' )
22- }
23-
24- if ( ! params . host ) {
25- throw new Error ( 'Host is required for self-hosted deployments' )
26- }
27-
28- return params . host . replace ( / \/ $ / , '' )
29- }
30-
31- function buildAuthHeaders ( params : ElasticsearchBulkParams ) : Record < string , string > {
32- const headers : Record < string , string > = {
33- 'Content-Type' : 'application/x-ndjson' ,
34- }
35-
36- if ( params . authMethod === 'api_key' && params . apiKey ) {
37- headers . Authorization = `ApiKey ${ params . apiKey } `
38- } else if ( params . authMethod === 'basic_auth' && params . username && params . password ) {
39- const credentials = Buffer . from ( `${ params . username } :${ params . password } ` ) . toString ( 'base64' )
40- headers . Authorization = `Basic ${ credentials } `
41- } else {
42- throw new Error ( 'Invalid authentication configuration' )
43- }
44-
45- return headers
46- }
47-
488export const bulkTool : ToolConfig < ElasticsearchBulkParams , ElasticsearchBulkResponse > = {
499 id : 'elasticsearch_bulk' ,
5010 name : 'Elasticsearch Bulk Operations' ,
@@ -127,7 +87,7 @@ export const bulkTool: ToolConfig<ElasticsearchBulkParams, ElasticsearchBulkResp
12787 return url
12888 } ,
12989 method : 'POST' ,
130- headers : ( params ) => buildAuthHeaders ( params ) ,
90+ headers : ( params ) => buildAuthHeaders ( params , 'application/x-ndjson' ) ,
13191 body : ( params ) => {
13292 // The body should be NDJSON format - we pass it as raw string
13393 // Ensure it ends with a newline
0 commit comments