Skip to content
Open
Show file tree
Hide file tree
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
49 changes: 25 additions & 24 deletions src/apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,20 @@ export default class Apollo extends EventEmitter {

this._envReader = new EnvReader({
env_file_type: this.env_file_type,
logger: this.logger
logger: this.logger,
});

if (config.token && config.portal_address) {
this._openApi = new OpenApi({
token: this.token,
portal_address: this.portal_address,
app_id: this.app_id,
cluster_name: this.cluster_name,
namespace_name: this.namespace_name,
}, this.logger);
this._openApi = new OpenApi(
{
token: this.token,
portal_address: this.portal_address,
app_id: this.app_id,
cluster_name: this.cluster_name,
namespace_name: this.namespace_name,
},
this.logger,
);
}
}

Expand Down Expand Up @@ -223,7 +226,9 @@ export default class Apollo extends EventEmitter {
url,
method: CurlMethods.GET,
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' } as http.OutgoingHttpHeaders,
headers: {
'Content-Type': 'application/json',
} as http.OutgoingHttpHeaders,
};
if (this.secret) {
const timestamp = Date.now().toString();
Expand All @@ -232,18 +237,16 @@ export default class Apollo extends EventEmitter {
options.headers = {
...options.headers,
Authorization: sign,
Timestamp: timestamp
}
Timestamp: timestamp,
};
}
response = curl(options);
} catch (err) {
error = err;
} finally {
if (error) {
error = new ApolloInitConfigError(error);
}

else if (response) {
} else if (response) {
const { body, status, message } = response;

if (!response.isJSON()) {
Expand All @@ -256,7 +259,7 @@ export default class Apollo extends EventEmitter {
}

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

if (this.set_env_file) {
this.readFromEnvFile();
Expand Down Expand Up @@ -290,7 +293,7 @@ export default class Apollo extends EventEmitter {
releaseKey: release_key,
ip,
},
headers: {}
headers: {},
};

if (this.secret) {
Expand All @@ -300,7 +303,7 @@ export default class Apollo extends EventEmitter {
options.headers = {
Authorization: sign,
Timestamp: timestamp,
}
};
}

const response = await request(url, options);
Expand Down Expand Up @@ -332,7 +335,7 @@ export default class Apollo extends EventEmitter {
options.headers = {
Authorization: sign,
Timestamp: timestamp,
}
};
}

const response = await request(url, options);
Expand All @@ -342,8 +345,7 @@ export default class Apollo extends EventEmitter {
this.emit('config.updated', response.data);
}
return response.data;
}
else {
} else {
const error = new RequestError(response.data);
this.logger.error('[egg-apollo-client] %j', error);
}
Expand Down Expand Up @@ -376,7 +378,7 @@ export default class Apollo extends EventEmitter {

if (retryTimes < 10) {
retryTimes++;
await new Promise(resolve => setTimeout(resolve, this.delay));
await new Promise((resolve) => setTimeout(resolve, this.delay));
// 每次重试都要加长延时时间
this._setDelay();
} else {
Expand Down Expand Up @@ -417,7 +419,7 @@ export default class Apollo extends EventEmitter {
options.headers = {
Authorization: sign,
Timestamp: timestamp,
}
};
}

const response = await request(url, options);
Expand Down Expand Up @@ -500,8 +502,7 @@ export default class Apollo extends EventEmitter {
const rename = `${envPath}.${Date.now()}`;
try {
fs.renameSync(envPath, rename);
}
catch (e) {
} catch (e) {
process.env.NODE_ENV !== 'production' && console.error(e);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/error/ApolloConfigError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ export class ApolloConfigError extends Error {
super(message);
this.message = `ApolloConfigError: ${message}`;
}
toString() {
return this.message;
}
}
3 changes: 3 additions & 0 deletions src/error/ApolloInitConfigError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ export class ApolloInitConfigError extends Error {
super(message);
this.message = `ApolloInitConfigError: ${message}`;
}
toString() {
return this.message;
}
}
23 changes: 16 additions & 7 deletions src/lib/curl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ICurlOptions, ICurlResponse } from './interface';
export * from './enum/CurlMethods';
export * from './interface';


export default function request(options: ICurlOptions): ICurlResponse {
if (!options.method) {
options.method = CurlMethods.GET;
Expand All @@ -18,17 +17,27 @@ export default function request(options: ICurlOptions): ICurlResponse {
}

const url = options.url;
const result = spawnSync('node', ['./child.js', url, '-o', JSON.stringify(options)], {
cwd: __dirname
});
const { stdout } = result;
const result = spawnSync(
'node',
['./child.js', url, '-o', JSON.stringify(options)],
{
cwd: __dirname,
}
);
const { stdout, stderr } = result;
const errorString = stderr.toString();
if (errorString.length) {
throw errorString;
}
const resultString = stdout.toString();
const response = resultString ? JSON.parse(resultString) : {};

return {
...response,
isJSON() {
return (this.headers['content-type'] as string || '').startsWith('application/json');
isJSON() {
return ((this.headers['content-type'] as string) || '').startsWith(
'application/json'
);
},
} as ICurlResponse;
}
3 changes: 3 additions & 0 deletions src/lib/request/error/RequestError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ export class RequestError extends Error {

this.message = `RequestError: ${msg}`;
}
toString() {
return this.message;
}
}
21 changes: 12 additions & 9 deletions src/lib/request/error/RequestMethodError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
* @extends {Error}
*/
export class RequestMethodError extends Error {
/**
* Creates an instance of RequestMethodError.
* @author tunan
* @memberof RequestMethodError
*/
constructor(message?: string) {
super(message);
/**
* Creates an instance of RequestMethodError.
* @author tunan
* @memberof RequestMethodError
*/
constructor(message?: string) {
super(message);

this.message = `RequestMethodError: ${message || this.message}`;
}
this.message = `RequestMethodError: ${message || this.message}`;
}
toString() {
return this.message;
}
}
11 changes: 7 additions & 4 deletions src/lib/request/error/UnknowReuqestError.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export class UnknowReuqestError extends Error {
constructor(message?: string) {
super(message);
this.message = `UnknowReuqestError: ${message || this.message}`;
}
constructor(message?: string) {
super(message);
this.message = `UnknowReuqestError: ${message || this.message}`;
}
toString() {
return this.message;
}
}