Skip to content

Commit 615d660

Browse files
authored
feat: runInAnonymousContextScope support req (#5134)
1 parent 1c0c7f1 commit 615d660

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

index.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,9 @@ declare module 'egg' {
729729
* Run async function in the anonymous context scope
730730
* @see Context#runInAnonymousContextScope
731731
* @param {Function} scope - the first args is an anonymous ctx, scope should be async function
732+
* @param {Request} req - if you want to mock request like querystring, you can pass an object to this function.
732733
*/
733-
runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>): Promise<void>;
734+
runInAnonymousContextScope<R>(scope: (ctx: Context) => Promise<R>, req?: Request): Promise<R>;
734735

735736
/**
736737
* Get current execute ctx async local storage

lib/application.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,13 @@ class Application extends EggApplication {
260260
* Run async function in the anonymous context scope
261261
* @see Context#runInAnonymousContextScope
262262
* @param {Function} scope - the first args is an anonymous ctx, scope should be async function
263+
* @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
263264
*/
264-
async runInAnonymousContextScope(scope) {
265-
const ctx = this.createAnonymousContext();
265+
async runInAnonymousContextScope(scope, req) {
266+
const ctx = this.createAnonymousContext(req);
266267
if (!scope.name) scope._name = eggUtils.getCalleeFromStack(true);
267-
await this.ctxStorage.run(ctx, async () => {
268-
await scope(ctx);
268+
return await this.ctxStorage.run(ctx, async () => {
269+
return await scope(ctx);
269270
});
270271
}
271272

lib/egg.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ class EggApplication extends EggCore {
510510
* Create an anonymous context, the context isn't request level, so the request is mocked.
511511
* then you can use context level API like `ctx.service`
512512
* @member {String} EggApplication#createAnonymousContext
513-
* @param {Request} req - if you want to mock request like querystring, you can pass an object to this function.
513+
* @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
514514
* @return {Context} context
515515
*/
516516
createAnonymousContext(req) {

0 commit comments

Comments
 (0)