Просмотр исходного кода

Implement type guard for each delegator

Shun Miyazawa 9 месяцев назад
Родитель
Сommit
dbde1b96c2

+ 2 - 0
apps/app/src/server/service/search-delegator/elasticsearch-client-delegator/es7-client-delegator.ts

@@ -11,6 +11,8 @@ export class ES7ClientDelegator {
 
   private client: Client;
 
+  delegetorVersion = 7 as const;
+
   constructor(options: ClientOptions, rejectUnauthorized: boolean) {
     this.client = new Client({ ...options, ssl: { rejectUnauthorized } });
   }

+ 2 - 0
apps/app/src/server/service/search-delegator/elasticsearch-client-delegator/es8-client-delegator.ts

@@ -4,6 +4,8 @@ export class ES8ClientDelegator {
 
   private client: Client;
 
+  delegetorVersion = 8 as const;
+
   constructor(options: ClientOptions, rejectUnauthorized: boolean) {
     this.client = new Client({ ...options, tls: { rejectUnauthorized } });
   }

+ 2 - 0
apps/app/src/server/service/search-delegator/elasticsearch-client-delegator/es9-client-delegator.ts

@@ -4,6 +4,8 @@ export class ES9ClientDelegator {
 
   private client: Client;
 
+  delegetorVersion = 9 as const;
+
   constructor(options: ClientOptions, rejectUnauthorized: boolean) {
     this.client = new Client({ ...options, tls: { rejectUnauthorized } });
   }

+ 12 - 0
apps/app/src/server/service/search-delegator/elasticsearch-client-delegator/interfaces.ts

@@ -3,3 +3,15 @@ import type { ES8ClientDelegator } from './es8-client-delegator';
 import type { ES9ClientDelegator } from './es9-client-delegator';
 
 export type ElasticsearchClientDelegator = ES7ClientDelegator | ES8ClientDelegator | ES9ClientDelegator;
+
+export const isES7Client = (delegator: ElasticsearchClientDelegator): delegator is ES7ClientDelegator => {
+  return delegator.delegetorVersion === 7;
+};
+
+export const isES8Client = (delegator: ElasticsearchClientDelegator): delegator is ES8ClientDelegator => {
+  return delegator.delegetorVersion === 8;
+};
+
+export const isES9Client = (delegator: ElasticsearchClientDelegator): delegator is ES9ClientDelegator => {
+  return delegator.delegetorVersion === 9;
+};