From 4b9276af5b40f2bdb05175381b4e9cb61ed916cf Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 9 Jan 2025 21:42:15 +0800 Subject: [PATCH] add SingleModeApplication type --- src/index.ts | 8 +++++++- src/lib/start.ts | 14 ++++++++++++-- test/index.test-d.ts | 11 +++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index c4b71b0e6a..06b1196fd0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -45,6 +49,8 @@ export * from '@eggjs/cluster'; */ export { startEgg as start, + SingleModeApplication, + SingleModeAgent, }; /** diff --git a/src/lib/start.ts b/src/lib/start.ts index 9a94a10573..60abf4dd9a 100644 --- a/src/lib/start.ts +++ b/src/lib/start.ts @@ -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 */ @@ -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; } /** @@ -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(); diff --git a/test/index.test-d.ts b/test/index.test-d.ts index 052473eb5c..2938304bde 100644 --- a/test/index.test-d.ts +++ b/test/index.test-d.ts @@ -4,6 +4,7 @@ import { LoggerLevel, EggPlugin, EggAppInfo, + start, SingleModeApplication, SingleModeAgent, } from '../src/index.js'; import { HttpClient } from '../src/urllib.js'; @@ -113,3 +114,13 @@ expectType({ scope: 'scope', root: 'root', }); + +const singleApp = await start({ + baseDir: 'baseDir', + framework: 'egg', + plugins: plugin, +}); + +expectType(singleApp); +expectType(singleApp.agent); +expectType(singleApp.agent.app);