Skip to content

Commit

Permalink
add SingleModeApplication type
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 9, 2025
1 parent 193319d commit 4b9276a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
*/

import { BaseContextClass } from './lib/core/base_context_class.js';
import { startEgg } from './lib/start.js';
import {
startEgg,
SingleModeApplication,
SingleModeAgent,
} from './lib/start.js';
import Helper from './app/extend/helper.js';

// export extends
Expand Down Expand Up @@ -45,6 +49,8 @@ export * from '@eggjs/cluster';
*/
export {
startEgg as start,
SingleModeApplication,
SingleModeAgent,
};

/**
Expand Down
14 changes: 12 additions & 2 deletions src/lib/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { readJSON } from 'utility';
import { importModule } from '@eggjs/utils';
import { Agent } from './agent.js';
import { Application } from './application.js';
import { EggPlugin } from './types.js';

export interface StartEggOptions {
/** specify framework that can be absolute path or npm package */
Expand All @@ -13,6 +14,15 @@ export interface StartEggOptions {
ignoreWarning?: boolean;
mode?: 'single';
env?: string;
plugins?: EggPlugin;
}

export interface SingleModeApplication extends Application {
agent: SingleModeAgent;
}

export interface SingleModeAgent extends Agent {
app: SingleModeApplication;
}

/**
Expand Down Expand Up @@ -41,11 +51,11 @@ export async function startEgg(options: StartEggOptions = {}) {

const agent = new AgentClass({
...options,
});
}) as SingleModeAgent;
await agent.ready();
const application = new ApplicationClass({
...options,
});
}) as SingleModeApplication;
application.agent = agent;
agent.application = application;
await application.ready();
Expand Down
11 changes: 11 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
LoggerLevel,
EggPlugin,
EggAppInfo,
start, SingleModeApplication, SingleModeAgent,
} from '../src/index.js';
import { HttpClient } from '../src/urllib.js';

Expand Down Expand Up @@ -113,3 +114,13 @@ expectType<EggAppInfo>({
scope: 'scope',
root: 'root',
});

const singleApp = await start({
baseDir: 'baseDir',
framework: 'egg',
plugins: plugin,
});

expectType<SingleModeApplication>(singleApp);
expectType<SingleModeAgent>(singleApp.agent);
expectType<SingleModeApplication>(singleApp.agent.app);

0 comments on commit 4b9276a

Please sign in to comment.