Skip to content

Commit d269a95

Browse files
committed
fix: test case and style
fix
1 parent 3113a31 commit d269a95

File tree

12 files changed

+269
-169
lines changed

12 files changed

+269
-169
lines changed

index.d.ts

Lines changed: 61 additions & 90 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 { LookupFunction, Socket } from 'net';
5+
import { Socket, LookupFunction } from 'net';
66
import { IncomingMessage, ServerResponse } from 'http';
77
import KoaApplication = require('koa');
88
import KoaRouter = require('koa-router');
@@ -23,7 +23,10 @@ import {
2323
RequestOptions,
2424
HttpClientResponse as HttpClientResponseNext,
2525
} from 'urllib-next';
26-
import { FetchFactory, fetch } from 'urllib4';
26+
import {
27+
FetchFactory,
28+
fetch,
29+
} from 'urllib4';
2730
import {
2831
EggCoreBase,
2932
FileLoaderOption,
@@ -66,50 +69,32 @@ declare module 'egg' {
6669
export type HttpClientResponse<T = any> = HttpClientResponseNext<T>;
6770
// Compatible with both urllib@2 and urllib@3 RequestOptions to request
6871
export interface EggHttpClient extends EventEmitter {
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>>;
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>>;
9081
}
9182

9283
interface EggHttpConstructor {
93-
new (app: Application): EggHttpClient;
84+
new(app: Application): EggHttpClient;
9485
}
9586

96-
export interface EggContextHttpClient extends EggHttpClient {}
87+
export interface EggContextHttpClient extends EggHttpClient { }
9788
interface EggContextHttpClientConstructor {
98-
new (ctx: Context): EggContextHttpClient;
89+
new(ctx: Context): EggContextHttpClient;
9990
}
10091

10192
/**
10293
* BaseContextClass is a base class that can be extended,
10394
* it's instantiated in context level,
10495
* {@link Helper}, {@link Service} is extending it.
10596
*/
106-
export class BaseContextClass extends CoreBaseContextClass<
107-
Context,
108-
Application,
109-
EggAppConfig,
110-
IService
111-
> {
112-
// tslint:disable-line
97+
export class BaseContextClass extends CoreBaseContextClass<Context, Application, EggAppConfig, IService> { // tslint:disable-line
11398
/**
11499
* logger
115100
*/
@@ -144,8 +129,7 @@ declare module 'egg' {
144129

145130
export type RequestArrayBody = any[];
146131
export type RequestObjectBody = PlainObject;
147-
export interface Request extends KoaApplication.Request {
148-
// tslint:disable-line
132+
export interface Request extends KoaApplication.Request { // tslint:disable-line
149133
/**
150134
* detect if response should be json
151135
* 1. url path ends with `.json`
@@ -227,9 +211,7 @@ declare module 'egg' {
227211
body: any;
228212
}
229213

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

246228
export type LoggerLevel = EggLoggerLevel;
247229

230+
248231
/**
249232
* egg app info
250233
* @example
@@ -272,8 +255,7 @@ declare module 'egg' {
272255
type IgnoreOrMatch = IgnoreItem | IgnoreItem[];
273256

274257
/** logger config of egg */
275-
export interface EggLoggerConfig
276-
extends RemoveSpecProp<EggLoggersOptions, 'type'> {
258+
export interface EggLoggerConfig extends RemoveSpecProp<EggLoggersOptions, 'type'> {
277259
/** custom config of coreLogger */
278260
coreLogger?: Partial<EggLoggerOptions>;
279261
/** allow debug log at prod, defaults to `false` */
@@ -287,8 +269,7 @@ declare module 'egg' {
287269
}
288270

289271
/** Custom Loader Configuration */
290-
export interface CustomLoaderConfig
291-
extends RemoveSpecProp<FileLoaderOption, 'inject' | 'target'> {
272+
export interface CustomLoaderConfig extends RemoveSpecProp<FileLoaderOption, 'inject' | 'target'> {
292273
/**
293274
* an object you wanner load to, value can only be 'ctx' or 'app'. default to app
294275
*/
@@ -552,7 +533,7 @@ declare module 'egg' {
552533
};
553534
hsts: any;
554535
methodnoallow: { enable: boolean };
555-
noopen: { enable: boolean };
536+
noopen: { enable: boolean; }
556537
xssProtection: any;
557538
csp: any;
558539
};
@@ -561,11 +542,7 @@ declare module 'egg' {
561542

562543
watcher: PlainObject;
563544

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

570547
/**
571548
* server timeout in milliseconds, default to 0 (no timeout).
@@ -612,8 +589,7 @@ declare module 'egg' {
612589
methods: string[];
613590
}
614591

615-
export interface EggApplication
616-
extends Omit<EggCoreBase<EggAppConfig>, 'ctxStorage' | 'currentContext'> {
592+
export interface EggApplication extends Omit<EggCoreBase<EggAppConfig>, 'ctxStorage' | 'currentContext'> {
617593
/**
618594
* HttpClient instance
619595
*/
@@ -623,7 +599,7 @@ declare module 'egg' {
623599
* node fetch
624600
*/
625601
FetchFactory: FetchFactory;
626-
fetch: typeof fetch;
602+
fetch: typeof fetch,
627603

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

785758
/**
786759
* Get current execute ctx async local storage
@@ -795,10 +768,9 @@ declare module 'egg' {
795768
get currentContext(): Context;
796769
}
797770

798-
export interface IApplicationLocals extends PlainObject {}
771+
export interface IApplicationLocals extends PlainObject { }
799772

800-
export interface FileStream extends Readable {
801-
// tslint:disable-line
773+
export interface FileStream extends Readable { // tslint:disable-line
802774
fields: any;
803775

804776
filename: string;
@@ -838,20 +810,19 @@ declare module 'egg' {
838810
}
839811

840812
/**
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 {
813+
* KoaApplication's Context will carry the default 'cookie' property in
814+
* the egg's Context interface, which is wrong here because we have our own
815+
* special properties (e.g: encrypted). So we must remove this property and
816+
* create our own with the same name.
817+
* @see https://github.com/eggjs/egg/pull/2958
818+
*
819+
* However, the latest version of Koa has "[key: string]: any" on the
820+
* context, and there'll be a type error for "keyof koa.Context".
821+
* So we have to directly inherit from "KoaApplication.BaseContext" and
822+
* rewrite all the properties to be compatible with types in Koa.
823+
* @see https://github.com/eggjs/egg/pull/3329
824+
*/
825+
export interface Context<ResponseBodyT = any> extends KoaApplication.BaseContext {
855826
[key: string]: any;
856827
body: ResponseBodyT;
857828

@@ -1079,13 +1050,13 @@ declare module 'egg' {
10791050
httpclient: EggContextHttpClient;
10801051
}
10811052

1082-
export interface IContextLocals extends PlainObject {}
1053+
export interface IContextLocals extends PlainObject { }
10831054

1084-
export class Controller extends BaseContextClass {}
1055+
export class Controller extends BaseContextClass { }
10851056

1086-
export class Service extends BaseContextClass {}
1057+
export class Service extends BaseContextClass { }
10871058

1088-
export class Subscription extends BaseContextClass {}
1059+
export class Subscription extends BaseContextClass { }
10891060

10901061
/**
10911062
* The empty interface `IService` is a placeholder, for egg
@@ -1106,11 +1077,11 @@ declare module 'egg' {
11061077
*
11071078
* Now I can get ctx.service.foo at controller and other service file.
11081079
*/
1109-
export interface IService extends PlainObject {} // tslint:disable-line
1080+
export interface IService extends PlainObject { } // tslint:disable-line
11101081

1111-
export interface IController extends PlainObject {} // tslint:disable-line
1082+
export interface IController extends PlainObject { } // tslint:disable-line
11121083

1113-
export interface IMiddleware extends PlainObject {} // tslint:disable-line
1084+
export interface IMiddleware extends PlainObject { } // tslint:disable-line
11141085

11151086
export interface IHelper extends PlainObject, BaseContextClass {
11161087
/**
@@ -1182,7 +1153,8 @@ declare module 'egg' {
11821153
/**
11831154
* Singleton instance in Agent Worker, extend {@link EggApplication}
11841155
*/
1185-
export class Agent extends EggApplication {}
1156+
export class Agent extends EggApplication {
1157+
}
11861158

11871159
export interface ClusterOptions {
11881160
/** specify framework that can be absolute path or npm package */
@@ -1204,10 +1176,7 @@ declare module 'egg' {
12041176
[prop: string]: any;
12051177
}
12061178

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

12121181
export interface StartOptions {
12131182
/** specify framework that can be absolute path or npm package */
@@ -1218,7 +1187,7 @@ declare module 'egg' {
12181187
ignoreWarning?: boolean;
12191188
}
12201189

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

12231192
/**
12241193
* Powerful Partial, Support adding ? modifier to a mapped property in deep level
@@ -1229,7 +1198,9 @@ declare module 'egg' {
12291198
* type EggConfig = PowerPartial<EggAppConfig>
12301199
*/
12311200
export type PowerPartial<T> = {
1232-
[U in keyof T]?: T[U] extends object ? PowerPartial<T[U]> : T[U];
1201+
[U in keyof T]?: T[U] extends object
1202+
? PowerPartial<T[U]>
1203+
: T[U]
12331204
};
12341205

12351206
// send data can be number|string|boolean|object but not Set|Map
@@ -1264,8 +1235,8 @@ declare module 'egg' {
12641235
}
12651236

12661237
// compatible
1267-
export interface EggLoaderOptions extends CoreLoaderOptions {}
1268-
export interface EggLoader extends CoreLoader {}
1238+
export interface EggLoaderOptions extends CoreLoaderOptions { }
1239+
export interface EggLoader extends CoreLoader { }
12691240

12701241
/**
12711242
* App worker process Loader, will load plugins
@@ -1328,4 +1299,4 @@ declare module 'egg' {
13281299
export interface Singleton<T> {
13291300
get(id: string): T;
13301301
}
1331-
}
1302+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@
7979
"eslint": "^8.23.1",
8080
"eslint-config-egg": "^12.0.0",
8181
"formstream": "^1.1.1",
82-
"https-pem": "^3.0.0",
8382
"jsdoc": "^3.6.11",
8483
"koa": "^2.13.4",
8584
"koa-static": "^5.0.0",
85+
"node-forge": "^1.3.3",
8686
"node-libs-browser": "^2.2.1",
8787
"pedding": "^1.1.0",
8888
"prettier": "^2.7.1",

test/fixtures/apps/dns_resolver/app/controller/home.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
module.exports = function* () {
3+
module.exports = async function () {
44
let args;
55
if (this.query.host) {
66
args = {};
@@ -13,7 +13,7 @@ module.exports = function* () {
1313
if (this.query.disableDNSCache) {
1414
args = { enableDNSCache: false };
1515
}
16-
const result = yield this.curl(this.query.url, args);
16+
const result = await this.curl(this.query.url, args);
1717
this.status = result.status;
1818
this.set(result.headers);
1919
this.body = result.data;

test/fixtures/apps/dns_resolver/config/config.default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ exports.httpclient = {
1111
callback(null, hostname, family);
1212
}
1313
} else {
14-
const resultIp = '127.0.0.1'
14+
const resultIp = '127.0.0.1';
1515
if (options.all) {
1616
callback(null, [{ address: resultIp, family: 4 }]);
1717
} else {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
module.exports = async function () {
4+
let args;
5+
if (this.query.host) {
6+
args = {};
7+
args.headers = { host: this.query.host };
8+
}
9+
if (this.query.Host) {
10+
args = {};
11+
args.headers = { Host: this.query.Host };
12+
}
13+
if (this.query.disableDNSCache) {
14+
args = { enableDNSCache: false };
15+
}
16+
const result = await this.curl(this.query.url, args);
17+
this.status = result.status;
18+
this.set(result.headers);
19+
this.body = result.data;
20+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
app.get('/', app.controller.home);
5+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
exports.keys = 'test key';

0 commit comments

Comments
 (0)