Skip to content

Commit 6bbbca2

Browse files
authored
fix: change non-exports type to interface (#206)
1 parent 1f1b40c commit 6bbbca2

4 files changed

Lines changed: 103 additions & 25 deletions

File tree

index.d.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export interface PluginInfo {
5353

5454
export interface Plugins extends PlainObject<PluginInfo> { }
5555

56-
export class EggCore<Config = PlainObject> extends KoaApplication {
56+
export interface EggCoreBase<Config> extends KoaApplication {
5757
/**
5858
* Whether `application` or `agent`
5959
* @member {String}
@@ -77,16 +77,6 @@ export class EggCore<Config = PlainObject> extends KoaApplication {
7777
*/
7878
name: EggAppInfo['name'];
7979

80-
/**
81-
* @constructor
82-
* @param {Object} options - options
83-
* @param {String} [options.baseDir=process.cwd()] - the directory of application
84-
* @param {String} [options.type=application|agent] - whether it's running in app worker or agent worker
85-
* @param {Object} [options.plugins] - custom plugins
86-
* @since 1.0.0
87-
*/
88-
constructor(options?: EggCoreOptions);
89-
9080
/**
9181
* Convert a generator function to a promisable one.
9282
*
@@ -207,11 +197,25 @@ export class EggCore<Config = PlainObject> extends KoaApplication {
207197
* @since 1.0.0
208198
*/
209199
deprecate: depd.Deprecate;
200+
}
210201

202+
export interface EggCore<Config = PlainObject> extends EggCoreBase<Config> {
211203
Controller: typeof BaseContextClass;
212204
Service: typeof BaseContextClass;
213205
}
214206

207+
export class EggCore {
208+
/**
209+
* @constructor
210+
* @param {Object} options - options
211+
* @param {String} [options.baseDir=process.cwd()] - the directory of application
212+
* @param {String} [options.type=application|agent] - whether it's running in app worker or agent worker
213+
* @param {Object} [options.plugins] - custom plugins
214+
* @since 1.0.0
215+
*/
216+
constructor(options?: EggCoreOptions);
217+
}
218+
215219
/**
216220
* egg app info
217221
* @example
@@ -301,13 +305,7 @@ export interface ContextLoaderOption extends Partial<FileLoaderOption> {
301305
fieldClass?: string;
302306
}
303307

304-
declare class FileLoader {
305-
/**
306-
* Load files from directory to target object.
307-
* @since 1.0.0
308-
*/
309-
constructor(options: FileLoaderOption);
310-
308+
declare interface FileLoaderBase {
311309
/**
312310
* attach items to target object. Mapping the directory to properties.
313311
* `app/controller/group/repository.js` => `target.group.repository`
@@ -345,13 +343,23 @@ declare class FileLoader {
345343
parse(): Array<{ fullpath: string; properties: string[]; exports: any; }>;
346344
}
347345

348-
declare class ContextLoader extends FileLoader {
346+
declare interface ContextLoaderBase extends FileLoaderBase {}
347+
348+
export interface FileLoader {
349+
/**
350+
* Load files from directory to target object.
351+
* @since 1.0.0
352+
*/
353+
new (options: FileLoaderOption): FileLoaderBase;
354+
}
355+
356+
export interface ContextLoader {
349357
/**
350358
* Same as {@link FileLoader}, but it will attach file to `inject[fieldClass]`. The exports will be lazy loaded, such as `ctx.group.repository`.
351359
* @extends FileLoader
352360
* @since 1.0.0
353361
*/
354-
constructor(options: ContextLoaderOption);
362+
new (options: ContextLoaderOption): ContextLoaderBase;
355363
}
356364

357365
export class EggLoader<T = EggCore, Config = any> {
@@ -442,8 +450,8 @@ export class EggLoader<T = EggCore, Config = any> {
442450
getTypeFiles(filename: string): string[];
443451
resolveModule(filepath: string): string | undefined;
444452

445-
FileLoader: typeof FileLoader;
446-
ContextLoader: typeof ContextLoader;
453+
FileLoader: FileLoader;
454+
ContextLoader: ContextLoader;
447455

448456
// load methods
449457
protected loadConfig(): void;

test/egg-ts.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ describe('test/egg-ts.test.js', () => {
148148
TS_NODE_PROJECT: path.resolve(__dirname, './fixtures/app-ts/tsconfig.json'),
149149
}),
150150
})
151-
.debug()
151+
// .debug()
152152
.expect('code', 0)
153153
.end();
154154
});
@@ -160,7 +160,7 @@ describe('test/egg-ts.test.js', () => {
160160
TS_NODE_PROJECT: path.resolve(__dirname, './fixtures/app-ts/tsconfig.json'),
161161
}),
162162
})
163-
.debug()
163+
// .debug()
164164
.expect('stderr', /Property 'abb' does not exist on type 'EggCore<{ env: string; }>'/)
165165
.expect('stderr', /Property 'abc' does not exist on type 'typeof BaseContextClass'/)
166166
.expect('stderr', /'loadPlugin' is protected/)
@@ -174,6 +174,7 @@ describe('test/egg-ts.test.js', () => {
174174
.expect('stderr', /'loadCustomAgent' is protected/)
175175
.expect('stderr', /'loadService' is protected/)
176176
.expect('stderr', /'loadController' is protected/)
177+
.expect('stderr', /Property 'checkEnvType' does not exist on type 'string'/)
177178
.expect('stderr', /'ctx' is protected/)
178179
.expect('code', 1)
179180
.end();

test/fixtures/app-ts/app-error.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const app = new EggCore<{ env: string }>();
55
console.info(app.abb);
66
console.info(app.Controller.abc);
77
console.info(app.Service.bbc);
8+
console.info(app.config.env);
9+
console.info(app.config.env.substring(0));
10+
console.info(app.config.env.checkEnvType());
811
app.loader.loadPlugin();
912
app.loader.loadConfig();
1013
app.loader.loadApplicationExtend();

test/fixtures/app-ts/app.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as assert from 'assert';
22
import * as path from 'path';
33
const EGG_LOADER = Symbol.for('egg#loader');
44
const EGG_PATH = Symbol.for('egg#eggPath');
5-
import { BaseContextClass, EggCore, EggLoader, EggLoaderOptions } from '../../..';
5+
import { BaseContextClass, EggCore, EggCoreOptions, EggLoader, EggLoaderOptions } from '../../..';
66

77
// normal
88
const app = new EggCore<{ env: string }>();
@@ -15,6 +15,27 @@ assert(app.loader.app === app);
1515
assert(app.loader.eggPaths.length === 0);
1616
assert(app.type);
1717

18+
// custom egg core
19+
class CustomEggCore extends EggCore {
20+
constructor(options: EggCoreOptions) {
21+
super(options);
22+
}
23+
24+
get [EGG_PATH]() {
25+
return __dirname;
26+
}
27+
28+
customFn() {
29+
console.info(this.config);
30+
}
31+
}
32+
const customApp = new CustomEggCore({ baseDir: undefined });
33+
customApp.customFn();
34+
assert(customApp.Controller);
35+
assert(customApp.Service);
36+
assert(customApp.loader.ContextLoader);
37+
assert(customApp.loader.FileLoader);
38+
1839
// base class
1940
new BaseContextClass({ app: {} });
2041

@@ -121,6 +142,27 @@ new FileLoader({
121142
assert(app6.Dao.TestClass);
122143
assert(app6.Dao.TestFunction);
123144

145+
// custom file loader
146+
const app9 = {} as any;
147+
class CustomFileLoader extends FileLoader {
148+
test() {
149+
assert(this.load);
150+
assert(this.parse);
151+
}
152+
}
153+
new CustomFileLoader({
154+
directory: path.join(__dirname, '../load_dirs'),
155+
target: app9,
156+
match: [ 'dao/*' ],
157+
caseStyle: 'upper',
158+
filter(obj) { return !!obj; },
159+
initializer(obj, options) {
160+
assert(options.path);
161+
assert(options.pathName);
162+
return obj;
163+
},
164+
}).test();
165+
124166
// context loader
125167
const ContextLoader = loader.ContextLoader;
126168
const app7 = { context: {} } as any;
@@ -142,3 +184,27 @@ assert(app7.ass.Dao.TestClass);
142184
assert(app7.ass.Dao.TestFunction);
143185
assert(app7.context.kick.Dao.TestClass);
144186
assert(app7.context.kick.Dao.TestFunction);
187+
188+
189+
// custom context loader
190+
const app8 = { context: {} } as any;
191+
class CustomContextLoader extends ContextLoader {
192+
test() {
193+
this.load();
194+
assert(this.parse);
195+
}
196+
}
197+
new CustomContextLoader({
198+
directory: path.join(__dirname, '../load_dirs'),
199+
property: 'kick',
200+
fieldClass: 'ass',
201+
inject: app8,
202+
match: [ 'dao/*' ],
203+
caseStyle: 'upper',
204+
filter(obj) { return !!obj; },
205+
initializer(obj, options) {
206+
assert(options.path);
207+
assert(options.pathName);
208+
return obj;
209+
},
210+
}).test();

0 commit comments

Comments
 (0)