Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 43 additions & 34 deletions src/apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,57 +210,66 @@ 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,
};

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: 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
};
}
}
response = curl(options);
const response = curl.default(options);
responses.push(response);
});
} catch (err) {
error = err;
} finally {
if (error) {
error = new ApolloInitConfigError(error);
} else if (responses && responses.length > 0) {
responses.forEach(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);
}
});
}

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);
}
}

if (error) {
this.logger.warn('[egg-apollo-client] %j', error);

if (this.set_env_file) {
this.readFromEnvFile();
}
if (this.set_env_file) this.readFromEnvFile();
}
}
}
Expand Down