From aa01b4b7cb840d1d59db6ddab54adab143ff126d Mon Sep 17 00:00:00 2001 From: Koalinrain Date: Wed, 22 Sep 2021 10:44:46 +0800 Subject: [PATCH 1/2] Update apollo.ts Add Multi Namesapce Support --- src/apollo.ts | 78 +++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/src/apollo.ts b/src/apollo.ts index 13a6d71..9accdb7 100644 --- a/src/apollo.ts +++ b/src/apollo.ts @@ -210,7 +210,6 @@ export default class Apollo extends EventEmitter { init(config: IApolloRequestConfig = {}) { const { cluster_name = this.cluster_name, namespace_name = this.namespace_name } = config; - const url = `${this.config_server_url}/configs/${this.app_id}/${cluster_name}/${namespace_name}`; const data = { releaseKey: this.release_key, ip: this.ip, @@ -218,49 +217,56 @@ export default class Apollo extends EventEmitter { let response: ICurlResponse | undefined; let error; + let urlArray = []; + const url = namespace => { + return `${this.config_server_url}/configs/${this.app_id}/${cluster_name}/${namespace}`; + } + if (Array.isArray(namespace_name)) { + if (namespace_name.length === 0) { + urlArray = [url('application')]; + } else { + urlArray = namespace_name.map(n => url(n)); + } + } else { + urlArray = [url(namespace_name)]; + } try { - const options = { - url, - method: CurlMethods.GET, - body: JSON.stringify(data), - headers: { 'Content-Type': 'application/json' } as http.OutgoingHttpHeaders, - }; - if (this.secret) { - const timestamp = Date.now().toString(); - const sign = this.signature(timestamp, url); - - options.headers = { - ...options.headers, - Authorization: sign, - Timestamp: timestamp + urlArray.forEach(url => { + const options = { + url, + method: curl_1.CurlMethods.GET, + body: JSON.stringify(data), + headers: { 'Content-Type': 'application/json' }, + }; + if (this.secret) { + const timestamp = Date.now().toString(); + const sign = this.signature(timestamp, url); + options.headers = Object.assign(Object.assign({}, options.headers), { Authorization: sign, Timestamp: timestamp }); } - } - response = curl(options); + const response = curl_1.default(options); + responses.push(response); + }); } catch (err) { error = err; - } finally { + } + finally { if (error) { - error = new ApolloInitConfigError(error); - } - - else if (response) { - const { body, status, message } = response; - - if (!response.isJSON()) { - error = new RequestError(body); - } else if (status === 200) { - this.setEnv(body); - } else { - error = new ApolloInitConfigError(message); - } + error = new ApolloInitConfigError_1.ApolloInitConfigError(error); + } else if (responses && responses.length > 0) { + responses.forEach(response => { + const { body, status, message } = response; + if (!response.isJSON()) { + error = new request_1.RequestError(body); + } else if (status === 200) { + this.setEnv(body); + } else { + error = new ApolloInitConfigError_1.ApolloInitConfigError(message); + } + }); } - if (error) { this.logger.warn('[egg-apollo-client] %j', error); - - if (this.set_env_file) { - this.readFromEnvFile(); - } + if (this.set_env_file) this.readFromEnvFile(); } } } From 91f7229e6fa6b505d407ea972553d610182f82d9 Mon Sep 17 00:00:00 2001 From: Koalinrain Date: Wed, 22 Sep 2021 10:50:00 +0800 Subject: [PATCH 2/2] Update apollo.ts Merge Code --- src/apollo.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/apollo.ts b/src/apollo.ts index 9accdb7..624b711 100644 --- a/src/apollo.ts +++ b/src/apollo.ts @@ -234,33 +234,36 @@ export default class Apollo extends EventEmitter { urlArray.forEach(url => { const options = { url, - method: curl_1.CurlMethods.GET, + method: CurlMethods.GET, body: JSON.stringify(data), - headers: { 'Content-Type': 'application/json' }, + headers: { 'Content-Type': 'application/json' } as http.OutgoingHttpHeaders, }; if (this.secret) { const timestamp = Date.now().toString(); const sign = this.signature(timestamp, url); - options.headers = Object.assign(Object.assign({}, options.headers), { Authorization: sign, Timestamp: timestamp }); + options.headers = { + ...options.headers, + Authorization: sign, + Timestamp: timestamp + }; } - const response = curl_1.default(options); + const response = curl.default(options); responses.push(response); }); } catch (err) { error = err; - } - finally { + } finally { if (error) { - error = new ApolloInitConfigError_1.ApolloInitConfigError(error); + error = new ApolloInitConfigError(error); } else if (responses && responses.length > 0) { responses.forEach(response => { const { body, status, message } = response; if (!response.isJSON()) { - error = new request_1.RequestError(body); + error = new RequestError(body); } else if (status === 200) { this.setEnv(body); } else { - error = new ApolloInitConfigError_1.ApolloInitConfigError(message); + error = new ApolloInitConfigError(message); } }); }