Skip to content

Commit

Permalink
refactor(typescript): use egg-logger definition (#3078)
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Oct 9, 2018
1 parent 04d9a3b commit eb1eae7
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 33 deletions.
69 changes: 39 additions & 30 deletions index.d.ts
Expand Up @@ -2,9 +2,10 @@ import * as accepts from 'accepts';
import * as KoaApplication from 'koa';
import * as KoaRouter from 'koa-router';
import { EventEmitter } from 'events'
import { RequestOptions } from 'urllib';
import { Readable } from 'stream';
import { Socket } from 'net';
import { EggLogger, EggLoggers, LoggerLevel as EggLoggerLevel, EggContextLogger } from 'egg-logger';
import { HttpClient2, RequestOptions } from 'urllib';
import EggCookies = require('egg-cookies');
import 'egg-onerror';
import 'egg-session';
Expand All @@ -26,6 +27,13 @@ declare module 'egg' {
// Remove specific property from the specific class
type RemoveSpecProp<T, P> = Pick<T, Exclude<keyof T, P>>;

class EggHttpClient extends HttpClient2 {
constructor(app: Application);
}
class EggContextHttpClient extends HttpClient2 {
constructor(ctx: Context);
}

/**
* BaseContextClass is a base class that can be extended,
* it's instantiated in context level,
Expand Down Expand Up @@ -55,18 +63,11 @@ declare module 'egg' {
/**
* logger
*/
logger: Logger;
logger: EggLogger;

constructor(ctx: Context);
}

export interface Logger {
info(msg: any, ...args: any[]): void;
warn(msg: any, ...args: any[]): void;
debug(msg: any, ...args: any[]): void;
error(msg: any, ...args: any[]): void;
}

export type RequestArrayBody = any[];
export type RequestObjectBody = PlainObject;
export interface Request extends KoaApplication.Request { // tslint:disable-line
Expand Down Expand Up @@ -184,7 +185,7 @@ declare module 'egg' {
renderString(name: string, locals?: any, options?: any): Promise<string>;
}

export type LoggerLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'NONE';
export type LoggerLevel = EggLoggerLevel;

/**
* egg app info
Expand Down Expand Up @@ -492,11 +493,6 @@ declare module 'egg' {
*/
env: EggEnvType;

/**
* core logger for framework and plugins, log file is $HOME/logs/{appname}/egg-web
*/
coreLogger: Logger;

/**
* Alias to https://npmjs.com/package/depd
*/
Expand All @@ -505,7 +501,7 @@ declare module 'egg' {
/**
* HttpClient instance
*/
httpclient: any;
httpclient: EggHttpClient;

/**
* The loader instance, the default class is EggLoader. If you want define
Expand All @@ -523,12 +519,17 @@ declare module 'egg' {
* this.logger.warn('WARNING!!!!');
* ```
*/
logger: Logger;
logger: EggLogger;

/**
* core logger for framework and plugins, log file is $HOME/logs/{appname}/egg-web
*/
coreLogger: EggLogger;

/**
* All loggers contain logger, coreLogger and customLogger
*/
loggers: { [loggerName: string]: Logger };
loggers: EggLoggers;

/**
* messenger instance
Expand All @@ -542,8 +543,6 @@ declare module 'egg' {
*/
router: Router;

Service: Service;

/**
* Whether `application` or `agent`
*/
Expand Down Expand Up @@ -584,7 +583,7 @@ declare module 'egg' {
/**
* Get logger by name, it's equal to app.loggers['name'], but you can extend it with your own logical
*/
getLogger(name: string): Logger;
getLogger(name: string): EggLogger;

/**
* print the infomation when console.log(app)
Expand All @@ -595,6 +594,18 @@ declare module 'egg' {
* Alias to Router#url
*/
url(name: string, params: any): any;


/**
* export context base classes, let framework can impl sub class and over context extend easily.
*/
ContextCookies: typeof EggCookies;
ContextLogger: typeof EggContextLogger;
ContextHttpClient: typeof EggContextHttpClient;
HttpClient: typeof EggHttpClient;
Subscription: typeof Subscription;
Controller: typeof Controller;
Service: typeof Service;
}

export type RouterPath = string | RegExp;
Expand Down Expand Up @@ -640,8 +651,6 @@ declare module 'egg' {

controller: IController;

Controller: Controller;

middleware: KoaApplication.Middleware[] & IMiddleware;

/**
Expand Down Expand Up @@ -854,7 +863,12 @@ declare module 'egg' {
* this.logger.warn('WARNING!!!!');
* ```
*/
logger: Logger;
logger: EggLogger;

/**
* Get logger by name, it's equal to app.loggers['name'], but you can extend it with your own logical
*/
getLogger(name: string): EggLogger;

/**
* Request start time
Expand All @@ -873,11 +887,6 @@ declare module 'egg' {
*/
curl(url: string, opt?: RequestOptions): Promise<any>;

/**
* Get logger by name, it's equal to app.loggers['name'], but you can extend it with your own logical
*/
getLogger(name: string): Logger;

/**
* Render a file by view engine
* @param {String} name - the file path based on root
Expand Down Expand Up @@ -1089,7 +1098,7 @@ declare module 'egg' {
baseDir: string;
typescript?: boolean;
app: Application;
logger: Logger;
logger: EggLogger;
plugins?: any;
}

Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/apps/app-ts/app.ts
@@ -0,0 +1,8 @@
import { Application } from 'egg';
import testExportClass from './lib/export-class';
import testLogger from './lib/logger';

export default (app: Application) => {
testExportClass(app);
testLogger(app);
};
12 changes: 12 additions & 0 deletions test/fixtures/apps/app-ts/app/controller/foo.ts
Expand Up @@ -11,6 +11,7 @@ declare module 'egg' {
export default class FooController extends Controller {
async getData() {
try {
this.ctx.logger.info('getData');
this.ctx.body = await this.ctx.service.foo.bar();
this.ctx.proxy.foo.bar();
} catch (e) {
Expand All @@ -25,4 +26,15 @@ export default class FooController extends Controller {
this.ctx.logger.error(e);
}
}
async httpclient() {
await this.app.httpclient.request('url', {
method: 'POST',
});
await this.ctx.curl('url', {
method: 'POST',
});
await this.app.curl('url', {
method: 'POST',
});
}
}
26 changes: 26 additions & 0 deletions test/fixtures/apps/app-ts/lib/export-class.ts
@@ -0,0 +1,26 @@
import { Application } from 'egg';

export default (app: Application) => {
const ctx = app.createAnonymousContext();

class HttpClient extends app.HttpClient {};
new HttpClient(app);

class Controller extends app.Controller {};
new Controller(ctx);

class Service extends app.Service {};
new Service(ctx);

class Subscription extends app.Subscription {};
new Subscription(ctx);

class ContextHttpClient extends app.ContextHttpClient {};
new ContextHttpClient(ctx);

class ContextLogger extends app.ContextLogger {};
new ContextLogger(ctx, app.logger);

class ContextCookies extends app.ContextCookies {};
new ContextCookies(ctx);
};
16 changes: 16 additions & 0 deletions test/fixtures/apps/app-ts/lib/logger.ts
@@ -0,0 +1,16 @@
import { Application, LoggerLevel } from 'egg';

export default (app: Application) => {
app.logger.info('test');
app.coreLogger.info('test');
app.loggers.logger.info('test');
app.loggers.coreLogger.info('test');
app.getLogger('logger').info('test');

const ctx = app.createAnonymousContext();
ctx.logger.info('test');
ctx.coreLogger.info('test');
ctx.getLogger('logger').info('test');

const level: LoggerLevel = 'DEBUG';
};
6 changes: 3 additions & 3 deletions test/fixtures/apps/app-ts/tsconfig.json
@@ -1,12 +1,12 @@
{
"compilerOptions": {
"target": "es2015",
"target": "es2017",
"baseUrl": ".",
"paths": {
"egg": ["../../../../index"]
},
"module": "commonjs",
"lib": ["es7"],
"strict": true
"strict": true,
"noImplicitAny": false
}
}

0 comments on commit eb1eae7

Please sign in to comment.