Skip to content

Commit

Permalink
fix: export createAnonymousContext define
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Feb 2, 2025
1 parent 2959d79 commit 17fbb4e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/app/extend/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,5 +318,7 @@ declare module '@eggjs/core' {
get logger(): EggLogger;
get coreLogger(): EggLogger;
get locals(): Record<string, any>;
get realStatus(): number;
set realStatus(val: number);
}
}
16 changes: 0 additions & 16 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,22 +233,6 @@ export class Application extends EggApplicationCore {
});
}

/**
* Run async function in the anonymous context scope
* @see Context#runInAnonymousContextScope
* @param {Function} scope - the first args is an anonymous ctx, scope should be async function
* @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
*/
async runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>, req?: unknown) {
const ctx = this.createAnonymousContext(req);
if (!scope.name) {
Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
}
return await this.ctxStorage.run(ctx, async () => {
return await scope(ctx);
});
}

/**
* secret key for Application
* @member {String} Application#keys
Expand Down
19 changes: 19 additions & 0 deletions src/lib/egg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
Next, MiddlewareFunc as EggCoreMiddlewareFunc,
ILifecycleBoot,
} from '@eggjs/core';
import { utils as eggUtils } from '@eggjs/core';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import createClusterClient, { close as closeClusterClient } from 'cluster-client';
Expand Down Expand Up @@ -641,6 +642,22 @@ export class EggApplicationCore extends EggCore {
return this.createContext(request, response);
}

/**
* Run async function in the anonymous context scope
* @see Context#runInAnonymousContextScope
* @param {Function} scope - the first args is an anonymous ctx, scope should be async function
* @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
*/
async runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>, req?: unknown) {
const ctx = this.createAnonymousContext(req);
if (!scope.name) {
Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
}
return await this.ctxStorage.run(ctx, async () => {
return await scope(ctx);
});
}

/**
* Create egg context
* @function EggApplication#createContext
Expand Down Expand Up @@ -677,5 +694,7 @@ declare module '@eggjs/core' {
HttpClient: typeof HttpClient;
get httpClient(): HttpClient;
curl<T = any>(url: HttpClientRequestURL, options?: HttpClientRequestOptions): Promise<HttpClientResponse<T>>;
createAnonymousContext(req?: any): EggContext;
runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>, req?: unknown): Promise<void>;
}
}
7 changes: 5 additions & 2 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expectType } from 'tsd';
import { EggCore, Context } from '@eggjs/core';
import {
Context, Application, IBoot, ILifecycleBoot,
Application, IBoot, ILifecycleBoot,
LoggerLevel,
EggPlugin,
EggAppInfo,
Expand All @@ -10,7 +11,7 @@ import {
} from '../src/index.js';
import { HttpClient } from '../src/urllib.js';

const app = {} as Application;
const app = {} as EggCore;
const ctx = app.createAnonymousContext();

expectType<Promise<void>>(app.runInAnonymousContextScope(async ctx => {
Expand All @@ -20,6 +21,8 @@ expectType<Promise<void>>(app.runInAnonymousContextScope(async ctx => {
expectType<Context>(ctx);
expectType<HttpClient>(ctx.httpClient);
expectType<any>(ctx.request.body);
expectType<number>(ctx.realStatus);
expectType<number>(ctx.realStatus = 200);

// watcher plugin types
expectType<object>(app.watcher);
Expand Down

0 comments on commit 17fbb4e

Please sign in to comment.