Skip to content

Commit 4a69984

Browse files
committed
chore: add HttpClient lookup type
1 parent 8776b54 commit 4a69984

File tree

1 file changed

+91
-60
lines changed

1 file changed

+91
-60
lines changed

index.d.ts

Lines changed: 91 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import accepts = require('accepts');
22
import { AsyncLocalStorage } from 'async_hooks';
3-
import { EventEmitter } from 'events'
3+
import { EventEmitter } from 'events';
44
import { Readable } from 'stream';
5-
import { Socket } from 'net';
5+
import { LookupFunction, Socket } from 'net';
66
import { IncomingMessage, ServerResponse } from 'http';
77
import KoaApplication = require('koa');
88
import KoaRouter = require('koa-router');
@@ -23,10 +23,7 @@ import {
2323
RequestOptions,
2424
HttpClientResponse as HttpClientResponseNext,
2525
} from 'urllib-next';
26-
import {
27-
FetchFactory,
28-
fetch,
29-
} from 'urllib4';
26+
import { FetchFactory, fetch } from 'urllib4';
3027
import {
3128
EggCoreBase,
3229
FileLoaderOption,
@@ -69,32 +66,50 @@ declare module 'egg' {
6966
export type HttpClientResponse<T = any> = HttpClientResponseNext<T>;
7067
// Compatible with both urllib@2 and urllib@3 RequestOptions to request
7168
export interface EggHttpClient extends EventEmitter {
72-
request<T = any>(url: HttpClientRequestURL): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
73-
request<T = any>(url: HttpClientRequestURL, options: RequestOptionsOld | HttpClientRequestOptions):
74-
Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
75-
curl<T = any>(url: HttpClientRequestURL): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
76-
curl<T = any>(url: HttpClientRequestURL, options: RequestOptionsOld | HttpClientRequestOptions):
77-
Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
78-
safeCurl<T = any>(url: HttpClientRequestURL): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
79-
safeCurl<T = any>(url: HttpClientRequestURL, options: RequestOptionsOld | HttpClientRequestOptions):
80-
Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
69+
request<T = any>(
70+
url: HttpClientRequestURL
71+
): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
72+
request<T = any>(
73+
url: HttpClientRequestURL,
74+
options: RequestOptionsOld | HttpClientRequestOptions
75+
): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
76+
curl<T = any>(
77+
url: HttpClientRequestURL
78+
): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
79+
curl<T = any>(
80+
url: HttpClientRequestURL,
81+
options: RequestOptionsOld | HttpClientRequestOptions
82+
): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
83+
safeCurl<T = any>(
84+
url: HttpClientRequestURL
85+
): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
86+
safeCurl<T = any>(
87+
url: HttpClientRequestURL,
88+
options: RequestOptionsOld | HttpClientRequestOptions
89+
): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
8190
}
8291

8392
interface EggHttpConstructor {
84-
new(app: Application): EggHttpClient;
93+
new (app: Application): EggHttpClient;
8594
}
8695

87-
export interface EggContextHttpClient extends EggHttpClient { }
96+
export interface EggContextHttpClient extends EggHttpClient {}
8897
interface EggContextHttpClientConstructor {
89-
new(ctx: Context): EggContextHttpClient;
98+
new (ctx: Context): EggContextHttpClient;
9099
}
91100

92101
/**
93102
* BaseContextClass is a base class that can be extended,
94103
* it's instantiated in context level,
95104
* {@link Helper}, {@link Service} is extending it.
96105
*/
97-
export class BaseContextClass extends CoreBaseContextClass<Context, Application, EggAppConfig, IService> { // tslint:disable-line
106+
export class BaseContextClass extends CoreBaseContextClass<
107+
Context,
108+
Application,
109+
EggAppConfig,
110+
IService
111+
> {
112+
// tslint:disable-line
98113
/**
99114
* logger
100115
*/
@@ -129,7 +144,8 @@ declare module 'egg' {
129144

130145
export type RequestArrayBody = any[];
131146
export type RequestObjectBody = PlainObject;
132-
export interface Request extends KoaApplication.Request { // tslint:disable-line
147+
export interface Request extends KoaApplication.Request {
148+
// tslint:disable-line
133149
/**
134150
* detect if response should be json
135151
* 1. url path ends with `.json`
@@ -211,7 +227,9 @@ declare module 'egg' {
211227
body: any;
212228
}
213229

214-
export interface Response<ResponseBodyT = any> extends KoaApplication.Response { // tslint:disable-line
230+
export interface Response<ResponseBodyT = any>
231+
extends KoaApplication.Response {
232+
// tslint:disable-line
215233
/**
216234
* read response real status code.
217235
*
@@ -227,7 +245,6 @@ declare module 'egg' {
227245

228246
export type LoggerLevel = EggLoggerLevel;
229247

230-
231248
/**
232249
* egg app info
233250
* @example
@@ -255,7 +272,8 @@ declare module 'egg' {
255272
type IgnoreOrMatch = IgnoreItem | IgnoreItem[];
256273

257274
/** logger config of egg */
258-
export interface EggLoggerConfig extends RemoveSpecProp<EggLoggersOptions, 'type'> {
275+
export interface EggLoggerConfig
276+
extends RemoveSpecProp<EggLoggersOptions, 'type'> {
259277
/** custom config of coreLogger */
260278
coreLogger?: Partial<EggLoggerOptions>;
261279
/** allow debug log at prod, defaults to `false` */
@@ -269,7 +287,8 @@ declare module 'egg' {
269287
}
270288

271289
/** Custom Loader Configuration */
272-
export interface CustomLoaderConfig extends RemoveSpecProp<FileLoaderOption, 'inject' | 'target'> {
290+
export interface CustomLoaderConfig
291+
extends RemoveSpecProp<FileLoaderOption, 'inject' | 'target'> {
273292
/**
274293
* an object you wanner load to, value can only be 'ctx' or 'app'. default to app
275294
*/
@@ -317,6 +336,8 @@ declare module 'egg' {
317336
useHttpClientNext?: boolean;
318337
/** Allow to use HTTP2 first, only work on `useHttpClientNext = true`. Default is `false` */
319338
allowH2?: boolean;
339+
/** Custom lookup function for DNS resolution */
340+
lookup?: LookupFunction;
320341
}
321342

322343
export interface EggAppConfig {
@@ -531,7 +552,7 @@ declare module 'egg' {
531552
};
532553
hsts: any;
533554
methodnoallow: { enable: boolean };
534-
noopen: { enable: boolean; }
555+
noopen: { enable: boolean };
535556
xssProtection: any;
536557
csp: any;
537558
};
@@ -540,7 +561,11 @@ declare module 'egg' {
540561

541562
watcher: PlainObject;
542563

543-
onClientError(err: Error, socket: Socket, app: EggApplication): ClientErrorResponse | Promise<ClientErrorResponse>;
564+
onClientError(
565+
err: Error,
566+
socket: Socket,
567+
app: EggApplication
568+
): ClientErrorResponse | Promise<ClientErrorResponse>;
544569

545570
/**
546571
* server timeout in milliseconds, default to 0 (no timeout).
@@ -587,7 +612,8 @@ declare module 'egg' {
587612
methods: string[];
588613
}
589614

590-
export interface EggApplication extends Omit<EggCoreBase<EggAppConfig>, 'ctxStorage' | 'currentContext'> {
615+
export interface EggApplication
616+
extends Omit<EggCoreBase<EggAppConfig>, 'ctxStorage' | 'currentContext'> {
591617
/**
592618
* HttpClient instance
593619
*/
@@ -597,7 +623,7 @@ declare module 'egg' {
597623
* node fetch
598624
*/
599625
FetchFactory: FetchFactory;
600-
fetch: typeof fetch,
626+
fetch: typeof fetch;
601627

602628
/**
603629
* Logger for Application, wrapping app.coreLogger with context infomation
@@ -751,7 +777,10 @@ declare module 'egg' {
751777
* @param {Function} scope - the first args is an anonymous ctx, scope should be async function
752778
* @param {Request} req - if you want to mock request like querystring, you can pass an object to this function.
753779
*/
754-
runInAnonymousContextScope<R>(scope: (ctx: Context) => Promise<R>, req?: Request): Promise<R>;
780+
runInAnonymousContextScope<R>(
781+
scope: (ctx: Context) => Promise<R>,
782+
req?: Request
783+
): Promise<R>;
755784

756785
/**
757786
* Get current execute ctx async local storage
@@ -766,9 +795,10 @@ declare module 'egg' {
766795
get currentContext(): Context;
767796
}
768797

769-
export interface IApplicationLocals extends PlainObject { }
798+
export interface IApplicationLocals extends PlainObject {}
770799

771-
export interface FileStream extends Readable { // tslint:disable-line
800+
export interface FileStream extends Readable {
801+
// tslint:disable-line
772802
fields: any;
773803

774804
filename: string;
@@ -808,19 +838,20 @@ declare module 'egg' {
808838
}
809839

810840
/**
811-
* KoaApplication's Context will carry the default 'cookie' property in
812-
* the egg's Context interface, which is wrong here because we have our own
813-
* special properties (e.g: encrypted). So we must remove this property and
814-
* create our own with the same name.
815-
* @see https://github.com/eggjs/egg/pull/2958
816-
*
817-
* However, the latest version of Koa has "[key: string]: any" on the
818-
* context, and there'll be a type error for "keyof koa.Context".
819-
* So we have to directly inherit from "KoaApplication.BaseContext" and
820-
* rewrite all the properties to be compatible with types in Koa.
821-
* @see https://github.com/eggjs/egg/pull/3329
822-
*/
823-
export interface Context<ResponseBodyT = any> extends KoaApplication.BaseContext {
841+
* KoaApplication's Context will carry the default 'cookie' property in
842+
* the egg's Context interface, which is wrong here because we have our own
843+
* special properties (e.g: encrypted). So we must remove this property and
844+
* create our own with the same name.
845+
* @see https://github.com/eggjs/egg/pull/2958
846+
*
847+
* However, the latest version of Koa has "[key: string]: any" on the
848+
* context, and there'll be a type error for "keyof koa.Context".
849+
* So we have to directly inherit from "KoaApplication.BaseContext" and
850+
* rewrite all the properties to be compatible with types in Koa.
851+
* @see https://github.com/eggjs/egg/pull/3329
852+
*/
853+
export interface Context<ResponseBodyT = any>
854+
extends KoaApplication.BaseContext {
824855
[key: string]: any;
825856
body: ResponseBodyT;
826857

@@ -1048,13 +1079,13 @@ declare module 'egg' {
10481079
httpclient: EggContextHttpClient;
10491080
}
10501081

1051-
export interface IContextLocals extends PlainObject { }
1082+
export interface IContextLocals extends PlainObject {}
10521083

1053-
export class Controller extends BaseContextClass { }
1084+
export class Controller extends BaseContextClass {}
10541085

1055-
export class Service extends BaseContextClass { }
1086+
export class Service extends BaseContextClass {}
10561087

1057-
export class Subscription extends BaseContextClass { }
1088+
export class Subscription extends BaseContextClass {}
10581089

10591090
/**
10601091
* The empty interface `IService` is a placeholder, for egg
@@ -1075,11 +1106,11 @@ declare module 'egg' {
10751106
*
10761107
* Now I can get ctx.service.foo at controller and other service file.
10771108
*/
1078-
export interface IService extends PlainObject { } // tslint:disable-line
1109+
export interface IService extends PlainObject {} // tslint:disable-line
10791110

1080-
export interface IController extends PlainObject { } // tslint:disable-line
1111+
export interface IController extends PlainObject {} // tslint:disable-line
10811112

1082-
export interface IMiddleware extends PlainObject { } // tslint:disable-line
1113+
export interface IMiddleware extends PlainObject {} // tslint:disable-line
10831114

10841115
export interface IHelper extends PlainObject, BaseContextClass {
10851116
/**
@@ -1151,8 +1182,7 @@ declare module 'egg' {
11511182
/**
11521183
* Singleton instance in Agent Worker, extend {@link EggApplication}
11531184
*/
1154-
export class Agent extends EggApplication {
1155-
}
1185+
export class Agent extends EggApplication {}
11561186

11571187
export interface ClusterOptions {
11581188
/** specify framework that can be absolute path or npm package */
@@ -1174,7 +1204,10 @@ declare module 'egg' {
11741204
[prop: string]: any;
11751205
}
11761206

1177-
export function startCluster(options: ClusterOptions, callback: (...args: any[]) => any): void;
1207+
export function startCluster(
1208+
options: ClusterOptions,
1209+
callback: (...args: any[]) => any
1210+
): void;
11781211

11791212
export interface StartOptions {
11801213
/** specify framework that can be absolute path or npm package */
@@ -1185,7 +1218,7 @@ declare module 'egg' {
11851218
ignoreWarning?: boolean;
11861219
}
11871220

1188-
export function start(options?: StartOptions): Promise<Application>
1221+
export function start(options?: StartOptions): Promise<Application>;
11891222

11901223
/**
11911224
* Powerful Partial, Support adding ? modifier to a mapped property in deep level
@@ -1196,9 +1229,7 @@ declare module 'egg' {
11961229
* type EggConfig = PowerPartial<EggAppConfig>
11971230
*/
11981231
export type PowerPartial<T> = {
1199-
[U in keyof T]?: T[U] extends object
1200-
? PowerPartial<T[U]>
1201-
: T[U]
1232+
[U in keyof T]?: T[U] extends object ? PowerPartial<T[U]> : T[U];
12021233
};
12031234

12041235
// send data can be number|string|boolean|object but not Set|Map
@@ -1233,8 +1264,8 @@ declare module 'egg' {
12331264
}
12341265

12351266
// compatible
1236-
export interface EggLoaderOptions extends CoreLoaderOptions { }
1237-
export interface EggLoader extends CoreLoader { }
1267+
export interface EggLoaderOptions extends CoreLoaderOptions {}
1268+
export interface EggLoader extends CoreLoader {}
12381269

12391270
/**
12401271
* App worker process Loader, will load plugins

0 commit comments

Comments
 (0)