Skip to content

Commit

Permalink
fix(types): import types from egg-core (#3545)
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes authored Mar 21, 2019
1 parent 04adb93 commit a73cfd0
Show file tree
Hide file tree
Showing 16 changed files with 459 additions and 151 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ yarn.lock
*clinic-flame*
*clinic-doctor*
.nyc_output/
test/fixtures/apps/app-ts/**/*.js
!test/fixtures/apps/app-ts/node_modules
!test/fixtures/apps/app-ts/node_modules/**/*.js
test/fixtures/apps/app-ts-esm/**/*.js
test/fixtures/apps/app-ts-type-check/**/*.js
144 changes: 31 additions & 113 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="node" />
import accepts = require('accepts');
import KoaApplication = require('koa');
import KoaRouter = require('koa-router');
Expand All @@ -7,6 +8,13 @@ import { Socket } from 'net';
import { IncomingMessage, ServerResponse } from 'http';
import { EggLogger, EggLoggers, LoggerLevel as EggLoggerLevel, EggContextLogger } from 'egg-logger';
import { HttpClient2, RequestOptions } from 'urllib';
import {
EggCoreBase,
EggLoader as CoreLoader,
EggCoreOptions as CoreOptions,
EggLoaderOptions as CoreLoaderOptions,
BaseContextClass as CoreBaseContextClass,
} from 'egg-core';
import EggCookies = require('egg-cookies');
import 'egg-onerror';
import 'egg-session';
Expand All @@ -28,45 +36,26 @@ 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);
interface EggHttpClient extends HttpClient2 {}
interface EggHttpConstructor {
new (app: Application): EggHttpClient;
}
class EggContextHttpClient extends HttpClient2 {
constructor(ctx: Context);

interface EggContextHttpClient extends HttpClient2 {}
interface EggContextHttpClientConstructor {
new (ctx: Context): EggContextHttpClient;
}

/**
* BaseContextClass is a base class that can be extended,
* it's instantiated in context level,
* {@link Helper}, {@link Service} is extending it.
*/
export class BaseContextClass { // tslint:disable-line
/**
* request context
*/
protected ctx: Context;

/**
* Application
*/
protected app: Application;

/**
* Application config object
*/
protected config: EggAppConfig;

/**
* service
*/
protected service: IService;

export class BaseContextClass extends CoreBaseContextClass<Context, Application, EggAppConfig, IService> { // tslint:disable-line
/**
* logger
*/
protected logger: EggLogger;

constructor(ctx: Context);
}

export type RequestArrayBody = any[];
Expand Down Expand Up @@ -460,37 +449,12 @@ declare module 'egg' {
url(name: string, params: any): any;
}

export class EggApplication extends KoaApplication { // tslint:disable-line
/**
* The current directory of application
*/
baseDir: string;

/**
* The configuration of application
*/
config: EggAppConfig;

/**
* app.env delegate app.config.env
*/
env: EggEnvType;

/**
* Alias to https://npmjs.com/package/depd
*/
deprecate: any;

export interface EggApplication extends EggCoreBase<EggAppConfig> { // tslint:disable-line
/**
* HttpClient instance
*/
httpclient: EggHttpClient;

/**
* The loader instance, the default class is EggLoader. If you want define
*/
loader: any;

/**
* Logger for Application, wrapping app.coreLogger with context infomation
*
Expand Down Expand Up @@ -519,48 +483,18 @@ declare module 'egg' {
*/
messenger: Messenger;

plugins: any;

/**
* get router
*/
router: Router;

/**
* Whether `application` or `agent`
*/
type: string;

/**
* create a singleton instance
*/
addSingleton(name: string, create: any): void;

/**
* Register a function that will be called when app close
*/
beforeClose(fn: () => void): void;

/**
* Excute scope after loaded and before app start
*/
beforeStart(scrope: () => void): void;

runSchedule(schedulePath: string): Promise<any>;

/**
* Close all, it wil close
* - callbacks registered by beforeClose
* - emit `close` event
* - remove add listeners
*
* If error is thrown when it's closing, the promise will reject.
* It will also reject after following call.
* @return {Promise} promise
* @since 1.0.0
*/
close(): Promise<any>;

/**
* http request helper base on httpclient, it will auto save httpclient log.
* Keep the same api with httpclient.request(url, args).
Expand Down Expand Up @@ -597,13 +531,18 @@ declare module 'egg' {
*/
ContextCookies: typeof EggCookies;
ContextLogger: typeof EggContextLogger;
ContextHttpClient: typeof EggContextHttpClient;
HttpClient: typeof EggHttpClient;
ContextHttpClient: EggContextHttpClientConstructor;
HttpClient: EggHttpConstructor;
Subscription: typeof Subscription;
Controller: typeof Controller;
Service: typeof Service;
}

// compatible
export class EggApplication {
constructor(options?: CoreOptions);
}

export type RouterPath = string | RegExp;

export class Application extends EggApplication {
Expand Down Expand Up @@ -1092,46 +1031,25 @@ declare module 'egg' {
sendToApp(action: string, data: any): void;
}

export interface EggLoaderOptions {
baseDir: string;
typescript?: boolean;
app: Application;
logger: EggLogger;
plugins?: any;
}

// egg-core
export class EggLoader {
options: EggLoaderOptions;

constructor(options: EggLoaderOptions);

getHomedir(): EggAppInfo['HOME']

getAppInfo(): EggAppInfo;
}
// compatible
export interface EggLoaderOptions extends CoreLoaderOptions {}
export interface EggLoader extends CoreLoader {}

/**
* App worker process Loader, will load plugins
* @see https://github.com/eggjs/egg-core
*/
export class AppWorkerLoader extends EggLoader {
constructor(options: EggLoaderOptions);

export class AppWorkerLoader extends CoreLoader {
loadConfig(): void;

load(): void;
}

/**
* Agent worker process loader
* @see https://github.com/eggjs/egg-loader
*/
export class AgentWorkerLoader extends EggLoader {
constructor(options: EggLoaderOptions);

export class AgentWorkerLoader extends CoreLoader {
loadConfig(): void;

load(): void;
}

Expand Down Expand Up @@ -1174,8 +1092,8 @@ declare module 'egg' {
*/
beforeClose?(): Promise<void>;
}
export class Singleton<T> {

export interface Singleton<T> {
get(id: string): T;
}
}
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"delegates": "^1.0.0",
"egg-cluster": "^1.23.0",
"egg-cookies": "^2.2.6",
"egg-core": "^4.15.0",
"egg-core": "^4.16.1",
"egg-development": "^2.4.2",
"egg-i18n": "^2.0.0",
"egg-jsonp": "^2.0.0",
Expand Down Expand Up @@ -56,7 +56,6 @@
"ylru": "^1.2.1"
},
"devDependencies": {
"@types/node": "^10.12.18",
"address": "^1.0.3",
"assert-extends": "^1.0.1",
"autod": "^3.0.1",
Expand All @@ -81,7 +80,7 @@
"runscript": "^1.3.0",
"spy": "^1.0.0",
"supertest": "^3.4.2",
"ts-node": "^3.0.6",
"ts-node": "^8.0.3",
"typescript": "^3.3.3333",
"webstorm-disable-index": "^1.1.2"
},
Expand Down
14 changes: 3 additions & 11 deletions test/fixtures/apps/app-ts-esm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
{
"extends": "../app-ts/tsconfig.json",
"compilerOptions": {
"target": "es2017",
"baseUrl": ".",
"paths": {
"egg": [
"../../../../index"
]
},
"module": "commonjs",
"strict": true,
"noImplicitAny": false,
"esModuleInterop": true
"esModuleInterop": true,
"baseUrl": "."
}
}
48 changes: 48 additions & 0 deletions test/fixtures/apps/app-ts-type-check/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
BaseContextClass,
Context,
Application,
Agent,
Controller,
Service,
} from 'egg';

new BaseContextClass({} as Context).ctx;

class MyController extends Controller {
async test() {
this.ctx.locals.test.localsCheckAny();
this.app.config.keys.configKeysCheckAny();
this.app.appCheckAny();
}
}
new MyController();

// service
class MyService extends Service {
async test() {
this.ctx.locals.test.serviceLocalCheckAny();
this.app.config.keys.serviceConfigCheckAny();
this.app.serviceAppCheckAny();
}
}
new MyService();

const app = new Application({ baseDir: __dirname, plugins: {}, type: 'application' });
new app.ContextHttpClient();
new app.HttpClient();

new Agent(undefined, 1123);

// test error in yadan
import {
BaseContextClass as YadanBaseContextClass,
Application as YadanApplication,
Agent as YadanAgent,
} from 'yadan';

new YadanBaseContextClass();
const yadan = new YadanApplication({ baseDir: __dirname, plugins: {}, type: 'application' });
new yadan.ContextHttpClient();
new yadan.HttpClient();
new YadanAgent(undefined, 1123);
Loading

0 comments on commit a73cfd0

Please sign in to comment.