Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't override koa Middleware type #271

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/pkg.pr.new.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish Any Commit
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm install

- name: Build
run: npm run prepublishOnly

- run: npx pkg-pr-new publish
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"coffee": "5",
"egg-bin": "6",
"eslint": "8",
"eslint-config-egg": "13",
"eslint-config-egg": "14",
"gals": "1",
"js-yaml": "3",
"mm": "3",
Expand All @@ -69,7 +69,7 @@
"tshy": "1",
"tshy-after": "1",
"typescript": "5",
"urllib": "3"
"urllib": "4"
},
"files": [
"dist",
Expand Down
12 changes: 2 additions & 10 deletions src/egg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assert from 'node:assert';
import { debuglog } from 'node:util';
import is from 'is-type-of';
import KoaApplication from '@eggjs/koa';
import type { ContextDelegation, Next } from '@eggjs/koa';
import type { ContextDelegation, MiddlewareFunc } from '@eggjs/koa';
import { EggConsoleLogger } from 'egg-logger';
import { RegisterOptions, ResourcesController, EggRouter as Router } from '@eggjs/router';
import type { ReadyFunctionArg } from 'get-ready';
Expand All @@ -28,13 +28,8 @@ export interface EggCoreOptions {

export type EggCoreInitOptions = Partial<EggCoreOptions>;

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

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
export interface EggCoreContext extends ContextDelegation {
app: EggCore;
}
Expand All @@ -58,9 +53,6 @@ export class EggCore extends KoaApplication {
readonly controller: Record<string, any> = {};
/** auto inject on loadMiddleware() */
readonly middlewares: Record<string, (opt: any, app: EggCore) => MiddlewareFunc> = {};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
declare middleware: MiddlewareFunc[];

/**
* @class
Expand Down
6 changes: 3 additions & 3 deletions src/loader/egg_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import type { Logger } from 'egg-logger';
import { getParamNames, readJSONSync } from 'utility';
import { extend } from 'extend2';
import { Request, Response, Context, Application, Next } from '@eggjs/koa';
import { Request, Response, Context, Application } from '@eggjs/koa';
import { pathMatching, type PathMatchingOptions } from 'egg-path-matching';
import { now, diff } from 'performance-ms';
import { FULLPATH, FileLoader, FileLoaderOptions } from './file_loader.js';
Expand Down Expand Up @@ -1601,7 +1601,7 @@
}
const match = pathMatching(options);

const fn = (ctx: EggCoreContext, next: Next) => {
const fn: MiddlewareFunc = (ctx, next) => {
if (!match(ctx)) return next();
return mw(ctx, next);
};
Expand All @@ -1610,7 +1610,7 @@
}

function debugMiddlewareWrapper(mw: MiddlewareFunc): MiddlewareFunc {
const fn = async (ctx: EggCoreContext, next: Next) => {
const fn: MiddlewareFunc = async (ctx, next) => {

Check warning on line 1613 in src/loader/egg_loader.ts

View check run for this annotation

Codecov / codecov/patch

src/loader/egg_loader.ts#L1613

Added line #L1613 was not covered by tests
const startTime = now();
debug('[debugMiddlewareWrapper] [%s %s] enter middleware: %s', ctx.method, ctx.url, mw._name);
await mw(ctx, next);
Expand Down
Loading