af-consul
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -44,3 +44,3 @@ "use strict"; | ||
}, | ||
deregister: (svcId) => api.deregisterIfNeed(svcId || serviceId), | ||
deregister: (svcId, agentHost, agentPort) => api.deregisterIfNeed(svcId || serviceId, agentHost, agentPort), | ||
}; | ||
@@ -47,0 +47,0 @@ Object.entries(api).forEach(([k, v]) => { |
@@ -83,3 +83,3 @@ "use strict"; | ||
// @ts-ignore request.res.body request.res.statusCode | ||
function common(fnName, { options, withError, result, }) { | ||
function common(fnName, { agentOptions, options, withError, result, }) { | ||
return new Promise((resolve, reject) => { | ||
@@ -93,3 +93,3 @@ const callback = (err, res) => { | ||
}; | ||
let fn = consulInstance; | ||
let fn = agentOptions ? Consul(agentOptions) : consulInstance; | ||
const namesArr = fnName.split('.'); | ||
@@ -112,4 +112,4 @@ const method = namesArr.pop(); | ||
// Returns the services the agent is managing. - список сервисов на этом агенте | ||
async agentServiceList(withError = false) { | ||
return common('agent.service.list', { withError }); | ||
async agentServiceList(agentOptions, withError = false) { | ||
return common('agent.service.list', { agentOptions, withError }); | ||
}, | ||
@@ -173,4 +173,5 @@ // Returns the nodes and health info of a service | ||
// Deregister a service. | ||
async agentServiceDeregister(serviceId, withError = false) { | ||
async agentServiceDeregister(serviceId, agentOptions, withError = false) { | ||
return common('agent.service.deregister', { | ||
agentOptions, | ||
options: serviceId, | ||
@@ -181,6 +182,15 @@ withError, | ||
}, | ||
async deregisterIfNeed(serviceId) { | ||
const isAlreadyRegistered = await this.checkIfServiceRegistered(serviceId); | ||
async deregisterIfNeed(serviceId, agentHost, agentPort) { | ||
agentHost = agentHost || consulAgentOptions.host; | ||
agentPort = agentPort || consulAgentOptions.port; | ||
let agentOptions; | ||
if (agentHost !== consulAgentOptions.host || agentPort !== consulAgentOptions.port) { | ||
const secure = String(agentPort) !== '8500'; | ||
agentOptions = { | ||
...consulAgentOptions, host: agentHost, port: agentPort, secure, | ||
}; | ||
} | ||
const isAlreadyRegistered = await this.checkIfServiceRegistered(serviceId, agentOptions); | ||
if (isAlreadyRegistered) { | ||
const isDeregister = await this.agentServiceDeregister(serviceId); | ||
const isDeregister = await this.agentServiceDeregister(serviceId, agentOptions); | ||
if (isDeregister) { | ||
@@ -201,4 +211,4 @@ logger.info(`Previous registration of service '${color_1.cyan}${serviceId}${color_1.reset}' removed from Consul`); | ||
agentMembers: async (withError = false) => common('agent.members', { withError }), | ||
async checkIfServiceRegistered(serviceIdOrName) { | ||
const agentServiceListR = await this.agentServiceList(); | ||
async checkIfServiceRegistered(serviceIdOrName, agentOptions) { | ||
const agentServiceListR = await this.agentServiceList(agentOptions); | ||
return agentServiceListR | ||
@@ -205,0 +215,0 @@ && Object.values(agentServiceListR) |
@@ -40,3 +40,3 @@ /* eslint-disable no-console */ | ||
}, | ||
deregister: (svcId) => api.deregisterIfNeed(svcId || serviceId), | ||
deregister: (svcId, agentHost, agentPort) => api.deregisterIfNeed(svcId || serviceId, agentHost, agentPort), | ||
}; | ||
@@ -43,0 +43,0 @@ Object.entries(api).forEach(([k, v]) => { |
@@ -80,3 +80,3 @@ /* eslint-disable no-console */ | ||
// @ts-ignore request.res.body request.res.statusCode | ||
function common(fnName, { options, withError, result, }) { | ||
function common(fnName, { agentOptions, options, withError, result, }) { | ||
return new Promise((resolve, reject) => { | ||
@@ -90,3 +90,3 @@ const callback = (err, res) => { | ||
}; | ||
let fn = consulInstance; | ||
let fn = agentOptions ? Consul(agentOptions) : consulInstance; | ||
const namesArr = fnName.split('.'); | ||
@@ -109,4 +109,4 @@ const method = namesArr.pop(); | ||
// Returns the services the agent is managing. - список сервисов на этом агенте | ||
async agentServiceList(withError = false) { | ||
return common('agent.service.list', { withError }); | ||
async agentServiceList(agentOptions, withError = false) { | ||
return common('agent.service.list', { agentOptions, withError }); | ||
}, | ||
@@ -170,4 +170,5 @@ // Returns the nodes and health info of a service | ||
// Deregister a service. | ||
async agentServiceDeregister(serviceId, withError = false) { | ||
async agentServiceDeregister(serviceId, agentOptions, withError = false) { | ||
return common('agent.service.deregister', { | ||
agentOptions, | ||
options: serviceId, | ||
@@ -178,6 +179,15 @@ withError, | ||
}, | ||
async deregisterIfNeed(serviceId) { | ||
const isAlreadyRegistered = await this.checkIfServiceRegistered(serviceId); | ||
async deregisterIfNeed(serviceId, agentHost, agentPort) { | ||
agentHost = agentHost || consulAgentOptions.host; | ||
agentPort = agentPort || consulAgentOptions.port; | ||
let agentOptions; | ||
if (agentHost !== consulAgentOptions.host || agentPort !== consulAgentOptions.port) { | ||
const secure = String(agentPort) !== '8500'; | ||
agentOptions = { | ||
...consulAgentOptions, host: agentHost, port: agentPort, secure, | ||
}; | ||
} | ||
const isAlreadyRegistered = await this.checkIfServiceRegistered(serviceId, agentOptions); | ||
if (isAlreadyRegistered) { | ||
const isDeregister = await this.agentServiceDeregister(serviceId); | ||
const isDeregister = await this.agentServiceDeregister(serviceId, agentOptions); | ||
if (isDeregister) { | ||
@@ -198,4 +208,4 @@ logger.info(`Previous registration of service '${cyan}${serviceId}${reset}' removed from Consul`); | ||
agentMembers: async (withError = false) => common('agent.members', { withError }), | ||
async checkIfServiceRegistered(serviceIdOrName) { | ||
const agentServiceListR = await this.agentServiceList(); | ||
async checkIfServiceRegistered(serviceIdOrName, agentOptions) { | ||
const agentServiceListR = await this.agentServiceList(agentOptions); | ||
return agentServiceListR | ||
@@ -202,0 +212,0 @@ && Object.values(agentServiceListR) |
{ | ||
"name": "af-consul", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "A highly specialized function library", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cjs/src/index.js", |
@@ -49,3 +49,3 @@ /* eslint-disable no-console */ | ||
}, | ||
deregister: (svcId) => api.deregisterIfNeed(svcId || serviceId), | ||
deregister: (svcId?: string, agentHost?: string, agentPort?: string) => api.deregisterIfNeed(svcId || serviceId, agentHost, agentPort), | ||
} as IApi; | ||
@@ -52,0 +52,0 @@ |
@@ -102,6 +102,7 @@ /* eslint-disable no-console */ | ||
function common(fnName: string, { | ||
agentOptions, | ||
options, | ||
withError, | ||
result, | ||
}: { options?: any; withError?: boolean; result?: any }): any { | ||
}: { agentOptions?: IConsulAgentOptions, options?: any; withError?: boolean; result?: any }): any { | ||
return new Promise((resolve, reject) => { | ||
@@ -115,3 +116,3 @@ const callback = (err: Error | any, res: any) => { | ||
}; | ||
let fn = consulInstance; | ||
let fn = agentOptions ? Consul(agentOptions) : consulInstance; | ||
const namesArr = fnName.split('.'); | ||
@@ -134,4 +135,4 @@ const method: string = namesArr.pop() as string; | ||
// Returns the services the agent is managing. - список сервисов на этом агенте | ||
async agentServiceList(withError: boolean = false) { | ||
return common('agent.service.list', { withError }); | ||
async agentServiceList(agentOptions?: IConsulAgentOptions, withError: boolean = false) { | ||
return common('agent.service.list', { agentOptions, withError }); | ||
}, | ||
@@ -207,4 +208,5 @@ | ||
// Deregister a service. | ||
async agentServiceDeregister(serviceId: string, withError: boolean = false): Promise<boolean> { | ||
async agentServiceDeregister(serviceId: string, agentOptions?: IConsulAgentOptions, withError: boolean = false): Promise<boolean> { | ||
return common('agent.service.deregister', { | ||
agentOptions, | ||
options: serviceId, | ||
@@ -216,6 +218,16 @@ withError, | ||
async deregisterIfNeed(serviceId: string): Promise<boolean> { | ||
const isAlreadyRegistered = await this.checkIfServiceRegistered(serviceId); | ||
async deregisterIfNeed(serviceId: string, agentHost?: string, agentPort?: string): Promise<boolean> { | ||
agentHost = agentHost || consulAgentOptions.host; | ||
agentPort = agentPort || consulAgentOptions.port; | ||
let agentOptions: Maybe<IConsulAgentOptions>; | ||
if (agentHost !== consulAgentOptions.host || agentPort !== consulAgentOptions.port) { | ||
const secure = String(agentPort) !== '8500'; | ||
agentOptions = { | ||
...consulAgentOptions, host: agentHost, port: agentPort, secure, | ||
}; | ||
} | ||
const isAlreadyRegistered = await this.checkIfServiceRegistered(serviceId, agentOptions); | ||
if (isAlreadyRegistered) { | ||
const isDeregister = await this.agentServiceDeregister(serviceId); | ||
const isDeregister = await this.agentServiceDeregister(serviceId, agentOptions); | ||
if (isDeregister) { | ||
@@ -236,4 +248,4 @@ logger.info(`Previous registration of service '${cyan}${serviceId}${reset}' removed from Consul`); | ||
async checkIfServiceRegistered(serviceIdOrName: string): Promise<boolean> { | ||
const agentServiceListR = await this.agentServiceList(); | ||
async checkIfServiceRegistered(serviceIdOrName: string, agentOptions?: IConsulAgentOptions): Promise<boolean> { | ||
const agentServiceListR = await this.agentServiceList(agentOptions); | ||
return agentServiceListR | ||
@@ -240,0 +252,0 @@ && Object.values(agentServiceListR) |
@@ -122,3 +122,3 @@ import Consul from "consul"; | ||
export interface IConsulAPI { | ||
agentServiceList: (withError?: boolean) => Promise<{ [serviceName: string]: IConsulServiceInfo }>, | ||
agentServiceList: (agentOptions?: IConsulAgentOptions, withError?: boolean) => Promise<{ [serviceName: string]: IConsulServiceInfo }>, | ||
consulHealthService: (options: Consul.Health.ServiceOptions, withError?: boolean) => Promise<TCommonFnResult>, | ||
@@ -128,6 +128,6 @@ getServiceInfo: (serviceName: string, withError?: boolean) => Promise<Maybe<IConsulServiceInfo>>, | ||
agentServiceRegister: (options: IRegisterConfig, withError?: boolean) => Promise<boolean>, | ||
agentServiceDeregister: (serviceId: string, withError?: boolean) => Promise<TCommonFnResult>, | ||
deregisterIfNeed: (serviceId: string) => Promise<boolean>, | ||
agentServiceDeregister: (serviceId: string, agentOptions?: IConsulAgentOptions, withError?: boolean) => Promise<TCommonFnResult>, | ||
deregisterIfNeed: (serviceId: string, agentHost?: string, agentPort?: string) => Promise<boolean>, | ||
agentMembers: (withError?: boolean) => Promise<TCommonFnResult>, | ||
checkIfServiceRegistered: (serviceIdOrName: string) => Promise<boolean>, | ||
checkIfServiceRegistered: (serviceIdOrName: string, agentOptions?: IConsulAgentOptions) => Promise<boolean>, | ||
registerService: (registerConfig: IRegisterConfig, registerOptions: IRegisterOptions) => Promise<TRegisterResult>, | ||
@@ -168,3 +168,3 @@ | ||
} | ||
deregister: (svcId?: string) => Promise<boolean> | ||
deregister: (svcId?: string, agentHost?: string, agentPort?: string) => Promise<boolean> | ||
} | ||
@@ -171,0 +171,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
264642
3941