From 16274e744007bcbeef9a99210fa00c8812d05718 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Fri, 13 Dec 2024 11:22:45 +0800 Subject: [PATCH] fix: dont import default value (#273) ## Summary by CodeRabbit - **New Features** - Updated the dependency version for `@eggjs/koa` and added an ECMAScript module field in `package.json`. - **Bug Fixes** - Enhanced error handling in lifecycle management methods. - Improved type safety in the error handling function of the `Application` class. - **Documentation** - Corrected minor typos in comments for clarity. - **Chores** - Updated `.gitignore` to exclude `.package-lock.json`. - Modified import statements for clarity and consistency across files. --- .gitignore | 1 + package.json | 9 ++++----- src/egg.ts | 9 +++------ src/lifecycle.ts | 6 +++--- src/loader/egg_loader.ts | 2 +- test/fixtures/egg-esm/index.ts | 2 +- 6 files changed, 13 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 49d05c7e..73a57841 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ test/fixtures/*/timing.json lib/ .tshy* dist +.package-lock.json diff --git a/package.json b/package.json index 48b6808e..186f9a30 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ }, "homepage": "https://github.com/eggjs/egg-core#readme", "dependencies": { - "@eggjs/koa": "^2.18.2", + "@eggjs/koa": "^2.19.2", "@eggjs/router": "^3.0.5", "@eggjs/utils": "^4.0.2", "egg-logger": "^3.5.0", @@ -66,7 +66,7 @@ "mm": "3", "supertest": "7", "ts-node": "10", - "tshy": "1", + "tshy": "3", "tshy-after": "1", "typescript": "5", "urllib": "4" @@ -86,17 +86,16 @@ "./package.json": "./package.json", ".": { "import": { - "source": "./src/index.ts", "types": "./dist/esm/index.d.ts", "default": "./dist/esm/index.js" }, "require": { - "source": "./src/index.ts", "types": "./dist/commonjs/index.d.ts", "default": "./dist/commonjs/index.js" } } }, "main": "./dist/commonjs/index.js", - "types": "./dist/commonjs/index.d.ts" + "types": "./dist/commonjs/index.d.ts", + "module": "./dist/esm/index.js" } diff --git a/src/egg.ts b/src/egg.ts index 08976ca7..5bf63755 100644 --- a/src/egg.ts +++ b/src/egg.ts @@ -1,8 +1,7 @@ /* eslint-disable prefer-spread */ import assert from 'node:assert'; import { debuglog } from 'node:util'; -import is from 'is-type-of'; -import KoaApplication from '@eggjs/koa'; +import { Application as KoaApplication } from '@eggjs/koa'; import type { ContextDelegation, MiddlewareFunc } from '@eggjs/koa'; import { EggConsoleLogger } from 'egg-logger'; import { RegisterOptions, ResourcesController, EggRouter as Router } from '@eggjs/router'; @@ -157,10 +156,8 @@ export class EggCore extends KoaApplication { * override koa's app.use, support generator function * @since 1.0.0 */ - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore use(fn: MiddlewareFunc) { - assert(is.function(fn), 'app.use() requires a function'); + assert(typeof fn === 'function', 'app.use() requires a function'); debug('[use] add middleware: %o', fn._name || fn.name || '-'); this.middleware.push(fn); return this; @@ -226,7 +223,7 @@ export class EggCore extends KoaApplication { * Execute scope after loaded and before app start. * * Notice: - * This method is now NOT recommanded and reguarded as a deprecated one, + * This method is now NOT recommended and regarded as a deprecated one, * For plugin development, we should use `didLoad` instead. * For application development, we should use `willReady` instead. * diff --git a/src/lifecycle.ts b/src/lifecycle.ts index e1b6177b..ab957d21 100644 --- a/src/lifecycle.ts +++ b/src/lifecycle.ts @@ -1,8 +1,8 @@ import assert from 'node:assert'; import { EventEmitter } from 'node:events'; import { debuglog } from 'node:util'; -import is, { isClass } from 'is-type-of'; -import ReadyObject from 'get-ready'; +import { isClass } from 'is-type-of'; +import { Ready as ReadyObject } from 'get-ready'; import type { ReadyFunctionArg } from 'get-ready'; import { Ready } from 'ready-callback'; import { EggConsoleLogger } from 'egg-logger'; @@ -184,7 +184,7 @@ export class Lifecycle extends EventEmitter { } registerBeforeClose(fn: Fun) { - assert(is.function(fn), 'argument should be function'); + assert(typeof fn === 'function', 'argument should be function'); assert(this.#isClosed === false, 'app has been closed'); this.#closeFunctionSet.add(fn); } diff --git a/src/loader/egg_loader.ts b/src/loader/egg_loader.ts index 8edb1558..7c38254c 100644 --- a/src/loader/egg_loader.ts +++ b/src/loader/egg_loader.ts @@ -3,7 +3,7 @@ import path from 'node:path'; import assert from 'node:assert'; import { debuglog, inspect } from 'node:util'; import { isAsyncFunction, isClass, isGeneratorFunction, isObject } from 'is-type-of'; -import homedir from 'node-homedir'; +import { homedir } from 'node-homedir'; import type { Logger } from 'egg-logger'; import { getParamNames, readJSONSync } from 'utility'; import { extend } from 'extend2'; diff --git a/test/fixtures/egg-esm/index.ts b/test/fixtures/egg-esm/index.ts index dd152a67..a47efb03 100644 --- a/test/fixtures/egg-esm/index.ts +++ b/test/fixtures/egg-esm/index.ts @@ -26,7 +26,7 @@ export class Application extends EggCore { constructor(options: EggCoreInitOptions = {}) { super(options); - this.on('error', err => { + this.on('error', (err: any) => { console.error(err); }); }