Skip to content

Commit 1facf82

Browse files
authored
fix: don't override koa Middleware type (#271)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a GitHub Actions workflow "Publish Any Commit" to automate build and publish tasks on commit. - **Updates** - Updated `eslint-config-egg` to version 14. - Updated `urllib` to version 4. - **Refactor** - Streamlined import and export statements to improve maintainability and clarity. - Standardized middleware function type declarations. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 302d076 commit 1facf82

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

.github/workflows/pkg.pr.new.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Publish Any Commit
2+
on: [push, pull_request]
3+
4+
jobs:
5+
build:
6+
runs-on: ubuntu-latest
7+
8+
steps:
9+
- name: Checkout code
10+
uses: actions/checkout@v4
11+
12+
- run: corepack enable
13+
- uses: actions/setup-node@v4
14+
with:
15+
node-version: 20
16+
17+
- name: Install dependencies
18+
run: npm install
19+
20+
- name: Build
21+
run: npm run prepublishOnly
22+
23+
- run: npx pkg-pr-new publish

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"coffee": "5",
6161
"egg-bin": "6",
6262
"eslint": "8",
63-
"eslint-config-egg": "13",
63+
"eslint-config-egg": "14",
6464
"gals": "1",
6565
"js-yaml": "3",
6666
"mm": "3",
@@ -69,7 +69,7 @@
6969
"tshy": "1",
7070
"tshy-after": "1",
7171
"typescript": "5",
72-
"urllib": "3"
72+
"urllib": "4"
7373
},
7474
"files": [
7575
"dist",

src/egg.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import assert from 'node:assert';
33
import { debuglog } from 'node:util';
44
import is from 'is-type-of';
55
import KoaApplication from '@eggjs/koa';
6-
import type { ContextDelegation, Next } from '@eggjs/koa';
6+
import type { ContextDelegation, MiddlewareFunc } from '@eggjs/koa';
77
import { EggConsoleLogger } from 'egg-logger';
88
import { RegisterOptions, ResourcesController, EggRouter as Router } from '@eggjs/router';
99
import type { ReadyFunctionArg } from 'get-ready';
@@ -28,13 +28,8 @@ export interface EggCoreOptions {
2828

2929
export type EggCoreInitOptions = Partial<EggCoreOptions>;
3030

31-
type Middleware = (ctx: EggCoreContext, next: Next) => Promise<void> | void;
32-
export type MiddlewareFunc = Middleware & {
33-
_name?: string;
34-
};
31+
export type { ContextDelegation, MiddlewareFunc, Next } from '@eggjs/koa';
3532

36-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
37-
// @ts-ignore
3833
export interface EggCoreContext extends ContextDelegation {
3934
app: EggCore;
4035
}
@@ -58,9 +53,6 @@ export class EggCore extends KoaApplication {
5853
readonly controller: Record<string, any> = {};
5954
/** auto inject on loadMiddleware() */
6055
readonly middlewares: Record<string, (opt: any, app: EggCore) => MiddlewareFunc> = {};
61-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
62-
// @ts-ignore
63-
declare middleware: MiddlewareFunc[];
6456

6557
/**
6658
* @class

src/loader/egg_loader.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import homedir from 'node-homedir';
77
import type { Logger } from 'egg-logger';
88
import { getParamNames, readJSONSync } from 'utility';
99
import { extend } from 'extend2';
10-
import { Request, Response, Context, Application, Next } from '@eggjs/koa';
10+
import { Request, Response, Context, Application } from '@eggjs/koa';
1111
import { pathMatching, type PathMatchingOptions } from 'egg-path-matching';
1212
import { now, diff } from 'performance-ms';
1313
import { FULLPATH, FileLoader, FileLoaderOptions } from './file_loader.js';
@@ -1601,7 +1601,7 @@ function wrapMiddleware(mw: MiddlewareFunc,
16011601
}
16021602
const match = pathMatching(options);
16031603

1604-
const fn = (ctx: EggCoreContext, next: Next) => {
1604+
const fn: MiddlewareFunc = (ctx, next) => {
16051605
if (!match(ctx)) return next();
16061606
return mw(ctx, next);
16071607
};
@@ -1610,7 +1610,7 @@ function wrapMiddleware(mw: MiddlewareFunc,
16101610
}
16111611

16121612
function debugMiddlewareWrapper(mw: MiddlewareFunc): MiddlewareFunc {
1613-
const fn = async (ctx: EggCoreContext, next: Next) => {
1613+
const fn: MiddlewareFunc = async (ctx, next) => {
16141614
const startTime = now();
16151615
debug('[debugMiddlewareWrapper] [%s %s] enter middleware: %s', ctx.method, ctx.url, mw._name);
16161616
await mw(ctx, next);

0 commit comments

Comments
 (0)