Skip to content

Commit 5d15623

Browse files
authored
fix: export createAnonymousContext define (#5388)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enabled dynamic reading and updating of the response status via the context interface. - Consolidated asynchronous execution by introducing a unified method for running operations within a controlled context. - **Tests** - Strengthened type validations to ensure consistent and reliable context management. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 2959d79 commit 5d15623

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

src/app/extend/context.ts

+2
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,7 @@ declare module '@eggjs/core' {
318318
get logger(): EggLogger;
319319
get coreLogger(): EggLogger;
320320
get locals(): Record<string, any>;
321+
get realStatus(): number;
322+
set realStatus(val: number);
321323
}
322324
}

src/lib/application.ts

-16
Original file line numberDiff line numberDiff line change
@@ -233,22 +233,6 @@ export class Application extends EggApplicationCore {
233233
});
234234
}
235235

236-
/**
237-
* Run async function in the anonymous context scope
238-
* @see Context#runInAnonymousContextScope
239-
* @param {Function} scope - the first args is an anonymous ctx, scope should be async function
240-
* @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
241-
*/
242-
async runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>, req?: unknown) {
243-
const ctx = this.createAnonymousContext(req);
244-
if (!scope.name) {
245-
Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
246-
}
247-
return await this.ctxStorage.run(ctx, async () => {
248-
return await scope(ctx);
249-
});
250-
}
251-
252236
/**
253237
* secret key for Application
254238
* @member {String} Application#keys

src/lib/egg.ts

+19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
Next, MiddlewareFunc as EggCoreMiddlewareFunc,
1616
ILifecycleBoot,
1717
} from '@eggjs/core';
18+
import { utils as eggUtils } from '@eggjs/core';
1819
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
1920
// @ts-ignore
2021
import createClusterClient, { close as closeClusterClient } from 'cluster-client';
@@ -641,6 +642,22 @@ export class EggApplicationCore extends EggCore {
641642
return this.createContext(request, response);
642643
}
643644

645+
/**
646+
* Run async function in the anonymous context scope
647+
* @see Context#runInAnonymousContextScope
648+
* @param {Function} scope - the first args is an anonymous ctx, scope should be async function
649+
* @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
650+
*/
651+
async runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>, req?: unknown) {
652+
const ctx = this.createAnonymousContext(req);
653+
if (!scope.name) {
654+
Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
655+
}
656+
return await this.ctxStorage.run(ctx, async () => {
657+
return await scope(ctx);
658+
});
659+
}
660+
644661
/**
645662
* Create egg context
646663
* @function EggApplication#createContext
@@ -677,5 +694,7 @@ declare module '@eggjs/core' {
677694
HttpClient: typeof HttpClient;
678695
get httpClient(): HttpClient;
679696
curl<T = any>(url: HttpClientRequestURL, options?: HttpClientRequestOptions): Promise<HttpClientResponse<T>>;
697+
createAnonymousContext(req?: any): EggContext;
698+
runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>, req?: unknown): Promise<void>;
680699
}
681700
}

test/index.test-d.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expectType } from 'tsd';
2+
import { EggCore, Context } from '@eggjs/core';
23
import {
3-
Context, Application, IBoot, ILifecycleBoot,
4+
Application, IBoot, ILifecycleBoot,
45
LoggerLevel,
56
EggPlugin,
67
EggAppInfo,
@@ -10,7 +11,7 @@ import {
1011
} from '../src/index.js';
1112
import { HttpClient } from '../src/urllib.js';
1213

13-
const app = {} as Application;
14+
const app = {} as EggCore;
1415
const ctx = app.createAnonymousContext();
1516

1617
expectType<Promise<void>>(app.runInAnonymousContextScope(async ctx => {
@@ -20,6 +21,8 @@ expectType<Promise<void>>(app.runInAnonymousContextScope(async ctx => {
2021
expectType<Context>(ctx);
2122
expectType<HttpClient>(ctx.httpClient);
2223
expectType<any>(ctx.request.body);
24+
expectType<number>(ctx.realStatus);
25+
expectType<number>(ctx.realStatus = 200);
2326

2427
// watcher plugin types
2528
expectType<object>(app.watcher);

0 commit comments

Comments
 (0)