Skip to content

Commit

Permalink
fix(typings): optimize declaration of httpclient (#3665)
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes authored and atian25 committed Apr 30, 2019
1 parent e8dc7db commit 670ba34
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
42 changes: 33 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,37 @@ declare module 'egg' {
loadunit?: boolean;
}

export interface HttpClientBaseConfig {
/** Whether use http keepalive */
keepAlive?: boolean;
/** Free socket after keepalive timeout */
freeSocketKeepAliveTimeout?: number;
/** Free socket after request timeout */
freeSocketTimeout?: number;
/** Request timeout */
timeout?: number;
/** Determines how many concurrent sockets the agent can have open per origin */
maxSockets?: number;
/** The maximum number of sockets that will be left open in the free state */
maxFreeSockets?: number;
}

/** HttpClient config */
export interface HttpClientConfig extends HttpClientBaseConfig {
/** http.Agent */
httpAgent?: HttpClientBaseConfig;
/** https.Agent */
httpsAgent?: HttpClientBaseConfig;
/** Default request args for httpclient */
request?: RequestOptions;
/** Whether enable dns cache */
enableDNSCache?: boolean;
/** Enable proxy request, default is false. */
enableProxy?: boolean;
/** proxy agent uri or options, default is null. */
proxy?: string | { [key: string]: any };
}

export interface EggAppConfig {
workerStartTimeout: number;
baseDir: string;
Expand Down Expand Up @@ -298,15 +329,8 @@ declare module 'egg' {
allowDebugAtProd: boolean;
};

httpclient: {
keepAlive: boolean;
freeSocketKeepAliveTimeout?: number;
freeSocketTimeout: number;
timeout: number;
maxSockets: number;
maxFreeSockets: number;
enableDNSCache: boolean;
};
/** Configuration of httpclient in egg. */
httpclient: HttpClientConfig;

development: {
/**
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/apps/app-ts-type-check/normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ config.customLoader = {
inject: 'app',
}
}
const httpclientOption = {
keepAlive: true,
freeSocketKeepAliveTimeout: 10 * 60 * 1000,
freeSocketTimeout: 10 * 60 * 1000,
timeout: 60 * 1000,
maxSockets: 20,
maxFreeSockets: 100,
};
config.httpclient = {
...httpclientOption,
httpAgent: httpclientOption,
httpsAgent: httpclientOption,
enableProxy: true,
request: {
method: 'GET',
},
proxy: 'http://127.0.0.1:8888'
}
config.httpclient = httpclientOption;

// partial config
const config2 = {} as PowerPartial<EggAppConfig>;
Expand Down

0 comments on commit 670ba34

Please sign in to comment.