diff --git a/src/config.js b/src/config.js index 096f432..1dd8173 100644 --- a/src/config.js +++ b/src/config.js @@ -14,6 +14,7 @@ export default class RTConfig { this.debugMode = false this.connectQuery = {} this.socketConfigTransform = null + this.hostResolver = null this.socketConfig = null @@ -25,6 +26,12 @@ export default class RTConfig { return } + if (!isUndefined(config.hostResolver)) { + if (isFunction(config.hostResolver)) { + this.hostResolver = config.hostResolver + } + } + if (!isUndefined(config.appId)) { if (!isString(config.appId)) { throw new Error('"appId" must be String.') @@ -79,18 +86,33 @@ export default class RTConfig { return this.connectQuery } - async prepare() { - if (!isString(this.lookupPath)) { - throw new Error('lookupPath must be String') + getSocketConfig() { + return this.socketConfig + } + + async getHost() { + let host + + if (this.hostResolver) { + host = await this.hostResolver() + } else { + host = await Request.get(this.lookupPath).set(this.lookupHeaders) } - const host = await Request.get(this.lookupPath).set(this.lookupHeaders) + return host + } + + async prepare() { + const query = this.getConnectQuery() + const host = await this.getHost() + + if (!host) { + throw new Error('Host is not defined') + } const url = this.appId ? `${ host }/${ this.appId }` : host const path = this.appId ? `/${ this.appId }` : undefined - const query = this.getConnectQuery() - this.socketConfig = { host, url, @@ -104,16 +126,8 @@ export default class RTConfig { } if (this.socketConfigTransform) { - const socketConfig = await this.socketConfigTransform(this.socketConfig) - - if (socketConfig) { - this.socketConfig = socketConfig - } + this.socketConfig = await this.socketConfigTransform(this.socketConfig) || this.socketConfig } } - - getSocketConfig() { - return this.socketConfig - } }