File size: 1,228 Bytes
34367da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Client } from '@opensearch-project/opensearch';
import type { ApiResponse } from '@opensearch-project/opensearch/lib/Transport.js';
import { getSecurityIntegrationConfig, isOpenSearchConfigured } from '../../config/securityConfig.js';

let cachedClient: Client | null = null;

export function getOpenSearchClient(): Client | null {
  if (!isOpenSearchConfigured()) {
    return null;
  }
  if (cachedClient) {
    return cachedClient;
  }

  const { openSearch } = getSecurityIntegrationConfig();
  cachedClient = new Client({
    node: openSearch.node,
    auth: openSearch.username && openSearch.password
      ? {
        username: openSearch.username,
        password: openSearch.password,
      }
      : undefined,
    ssl: {
      rejectUnauthorized: false,
    },
  });
  return cachedClient;
}

export function getFeedIndex(): string {
  return getSecurityIntegrationConfig().openSearch.index;
}

export async function safeCall<T>(promise: Promise<ApiResponse>): Promise<T | null> {
  try {
    const response = await promise;
    return response.body as T;
  } catch (error) {
    console.warn('⚠️  OpenSearch request failed:', error);
    return null;
  }
}