diff --git a/.eslintrc.yml b/.eslintrc.yml index 6199b7a6..7defee45 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -11,5 +11,6 @@ rules: "@typescript-eslint/no-unused-vars": 0 "node/no-unpublished-import": 0 "@typescript-eslint/prefer-ts-expect-error": 0 + "@typescript-eslint/no-extraneous-class": 0 "unicorn/filename-case": 0 diff --git a/.githooks/commit-msg b/.githooks/commit-msg index 4251521e..31246f91 100755 --- a/.githooks/commit-msg +++ b/.githooks/commit-msg @@ -6,8 +6,10 @@ # Date: 2023.07.21 # -if [[ -f $HOME/.nvm/nvm.sh ]]; then - . $HOME/.nvm/nvm.sh +if [[ -z $NVM_DIR ]]; then + if [[ -f $HOME/.nvm/nvm.sh ]]; then + . $HOME/.nvm/nvm.sh + fi fi branch=$( git branch | grep \* | cut -d ' ' -f2 ) diff --git a/.githooks/pre-commit.sample b/.githooks/pre-commit.sample index 323b36af..a68f4bf5 100755 --- a/.githooks/pre-commit.sample +++ b/.githooks/pre-commit.sample @@ -8,8 +8,10 @@ # Date: 2017.02.09 # -if [[ -f $HOME/.nvm/nvm.sh ]]; then - . $HOME/.nvm/nvm.sh +if [[ -z $NVM_DIR ]]; then + if [[ -f $HOME/.nvm/nvm.sh ]]; then + . $HOME/.nvm/nvm.sh + fi fi if git rev-parse --verify HEAD >/dev/null 2>&1 @@ -28,7 +30,7 @@ fi # ------------- parse ts files=$(git diff --cached --name-only --diff-filter=ACMR $against|grep '\.ts$') if [[ $files != "" ]] ; then - # echo $files | xargs eslint --fix + # echo $files | xargs eslint --fix npm run lint rc=$? if [[ $rc != 0 ]] ; then diff --git a/.githooks/pre-push.sample b/.githooks/pre-push.sample index 1ccb92a6..2545298f 100755 --- a/.githooks/pre-push.sample +++ b/.githooks/pre-push.sample @@ -10,9 +10,10 @@ remote="$1" url="$2" - -if [[ -f $HOME/.nvm/nvm.sh ]]; then - . $HOME/.nvm/nvm.sh +if [[ -z $NVM_DIR ]]; then + if [[ -f $HOME/.nvm/nvm.sh ]]; then + . $HOME/.nvm/nvm.sh + fi fi z40=0000000000000000000000000000000000000000 diff --git a/.github/workflows/benchmark.yml.example b/.github/workflows/benchmark.yml.example index 89234357..0ff61021 100644 --- a/.github/workflows/benchmark.yml.example +++ b/.github/workflows/benchmark.yml.example @@ -20,14 +20,14 @@ jobs: os: [ubuntu-latest] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node }} - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node }} - name: Cache - uses: actions/cache@v3 + uses: actions/cache@v4 id: cache-lerna env: cache-name: cache-lerna diff --git a/.github/workflows/nodejs-win.yml.example b/.github/workflows/nodejs-win.yml.example index 6abb0350..31a09c33 100644 --- a/.github/workflows/nodejs-win.yml.example +++ b/.github/workflows/nodejs-win.yml.example @@ -17,14 +17,14 @@ jobs: os: [windows-latest] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node }} - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node }} - name: Cache - uses: actions/cache@v3 + uses: actions/cache@v4 id: cache-lerna-win env: cache-name: cache-lerna-win diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index f44a6f34..553a6d26 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -17,14 +17,14 @@ jobs: os: [ubuntu-latest] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node }} - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node }} - name: Cache - uses: actions/cache@v3 + uses: actions/cache@v4 id: cache-lerna env: cache-name: cache-lerna diff --git a/.github/workflows/npm-manual-publish.yml b/.github/workflows/npm-manual-publish.yml index f03d58a3..a5dbb097 100644 --- a/.github/workflows/npm-manual-publish.yml +++ b/.github/workflows/npm-manual-publish.yml @@ -22,7 +22,7 @@ jobs: os: [ubuntu-latest] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: # pulls all commits (needed for lerna / semantic release to correctly version) fetch-depth: "0" @@ -34,13 +34,13 @@ jobs: git config --global --add safe.directory $cwd git fetch --depth=1 origin +refs/tags/*:refs/tags/* - name: Use Node.js ${{ matrix.node }} - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node }} registry-url: 'https://registry.npmjs.org' - name: Cache - uses: actions/cache@v3 + uses: actions/cache@v4 id: cache-lerna env: cache-name: cache-lerna diff --git a/.github/workflows/npm-publish-wo-test.yml b/.github/workflows/npm-publish-wo-test.yml index 3bdffc2b..355267bb 100644 --- a/.github/workflows/npm-publish-wo-test.yml +++ b/.github/workflows/npm-publish-wo-test.yml @@ -22,7 +22,7 @@ jobs: os: [ubuntu-latest] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: # pulls all commits (needed for lerna / semantic release to correctly version) fetch-depth: "0" @@ -34,13 +34,13 @@ jobs: git config --global --add safe.directory $cwd git fetch --depth=1 origin +refs/tags/*:refs/tags/* - name: Use Node.js ${{ matrix.node }} - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node }} registry-url: 'https://registry.npmjs.org' - name: Cache - uses: actions/cache@v3 + uses: actions/cache@v4 id: cache-lerna env: cache-name: cache-lerna diff --git a/.scripts/set-env-local.sh.example b/.scripts/set-env-local.sh.example new file mode 100755 index 00000000..5bb0e92d --- /dev/null +++ b/.scripts/set-env-local.sh.example @@ -0,0 +1,23 @@ +#!/bin/bash + + +export MIDWAY_SERVER_ENV=local +export NODE_ENV=local +export OTEL_EXPORTER_OTLP_ENDPOINT="http://127.0.0.1:4317" +export TS_NODE_PROJECT="test/tsconfig.json" +export POSTGRES_HOST="127.0.0.1" +export POSTGRES_PORT="5432" +export POSTGRES_DB="" +export POSTGRES_USER="postgres" +export POSTGRES_PASSWORD="postgres" +export ALI_ALB_IPS="120.55" +export ALI_ALB_GROUPID="sgp-7z" +export ALI_ECS_AID="LTAI5" +export ALI_ECS_ASECRET="Dr" +export ALI_ECS_IDS="" +export ALI_ECS_IPS="" +export ALI_OSS_AID="LTAI4" +export ALI_OSS_ASECRET="FRK" +export ALI_OSS_ENDPOINT="https://oss-cn-hangzhou.aliyuncs.com" +export ALI_OSS_BUCKET="" + diff --git a/.scripts/set-env-test.sh.example b/.scripts/set-env-test.sh.example new file mode 100755 index 00000000..a03a38ae --- /dev/null +++ b/.scripts/set-env-test.sh.example @@ -0,0 +1,23 @@ +#!/bin/bash + + +export MIDWAY_SERVER_ENV=unittest +export NODE_ENV=unittest +export OTEL_EXPORTER_OTLP_ENDPOINT="http://127.0.0.1:4317" +export TS_NODE_PROJECT="test/tsconfig.json" +export POSTGRES_HOST="127.0.0.1" +export POSTGRES_PORT="5432" +export POSTGRES_DB="" +export POSTGRES_USER="postgres" +export POSTGRES_PASSWORD="postgres" +export ALI_ALB_IPS="120.55" +export ALI_ALB_GROUPID="sgp-7z" +export ALI_ECS_AID="LTAI5" +export ALI_ECS_ASECRET="Dr" +export ALI_ECS_IDS="" +export ALI_ECS_IPS="" +export ALI_OSS_AID="LTAI4" +export ALI_OSS_ASECRET="FRK" +export ALI_OSS_ENDPOINT="https://oss-cn-hangzhou.aliyuncs.com" +export ALI_OSS_BUCKET="" + diff --git a/.vscode/launch.json.example b/.vscode/launch.json.example index 78dd7378..bbd15c5d 100644 --- a/.vscode/launch.json.example +++ b/.vscode/launch.json.example @@ -83,7 +83,6 @@ ], "runtimeExecutable": "npm", "skipFiles": [ - "**/node_modules/**", "**/node_modules/rxjs/**", "/**", "/**/*.js" diff --git a/.vscode/settings.json b/.vscode/settings.json index 6a558476..e001b3c8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,7 +7,7 @@ "editor.tabSize": 2, "editor.wordWrap": "on", "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" }, "eslint.run": "onSave", "eslint.validate": [ diff --git a/package.json b/package.json index 27025d83..84f96ed6 100644 --- a/package.json +++ b/package.json @@ -7,12 +7,13 @@ "devDependencies": { "@commitlint/cli": "18", "@commitlint/config-conventional": "18", - "@midwayjs/mock": "^3.14.0", - "@midwayjs/swagger": "^3.14.0", + "@midwayjs/mock": "^3.15.0", + "@midwayjs/swagger": "^3.15.0", + "@midwayjs/validate": "^3.15.0", "@types/koa": "2", "@types/koa": "2", "@types/mocha": "10", - "@types/node": "20", + "@types/node": "20.11", "@waiting/eslint-config": "^9.1.1", "@waiting/shared-core": "^23.0.0", "@waiting/shared-types": "^23.0.0", @@ -25,8 +26,9 @@ "madge": "^6.1.0", "mocha": "10", "mocha-lcov-reporter": "1", - "mwtsc": "^1.4.0", - "nx": "17", + "mwtsc": "^1.7.0", + "nx": "18", + "swagger-ui-dist": "^5.11.0", "ts-node": "^10.9.2", "tsc-alias": "^1.8.8", "tsconfig-paths": "4", @@ -44,6 +46,7 @@ "add:pkg": "sh .scripts/add-pkg.sh", "bp:add": "git remote add bp https://github.com/waitingsong/npm-mono-base", "bp:sync": "git fetch --all -v && git pull origin && git merge bp/main -m \"Merge remote-tracking branch 'bp/main'\" ", + "bp:sync-force": "git fetch --all -v && git pull origin && git merge bp/main --allow-unrelated-histories -m \"Merge remote-tracking branch 'bp/main'\" ", "bp:syncxo": "git fetch --all -v && git pull origin && git merge bp/main -m \"Merge remote-tracking branch 'bp/main'\" -Xours", "bootstrap": "npm i --loglevel info --disturl=https://npmmirror.com/dist/", "build": "sh .scripts/build.sh", diff --git a/packages/mw-demo/.eslintrc.yml b/packages/mw-demo/.eslintrc.yml index 3d2c0935..cda1a4a8 100644 --- a/packages/mw-demo/.eslintrc.yml +++ b/packages/mw-demo/.eslintrc.yml @@ -5,6 +5,7 @@ parserOptions: rules: "@typescript-eslint/no-unused-vars": 0 "@typescript-eslint/prefer-ts-expect-error": 0 + "@typescript-eslint/no-extraneous-class": 0 "import/no-extraneous-dependencies": - 2 - packageDir: diff --git a/packages/mw-demo/README.md b/packages/mw-demo/README.md index a14830d7..8d568134 100644 --- a/packages/mw-demo/README.md +++ b/packages/mw-demo/README.md @@ -20,11 +20,21 @@ import { ILifeCycle } from '@midwayjs/core' import { Configuration } from '@midwayjs/decorator' import * as demo from '@mw-components/demo' +import * as DefaultConfig from './config/config.default.js' +import * as LocalConfig from './config/config.local.js' +import * as UnittestConfig from './config/config.unittest.js' + @Configuration({ + importConfigs: [ + { + default: DefaultConfig, + local: LocalConfig, + unittest: UnittestConfig, + }, + ], imports: [ demo, ], - importConfigs: [join(__dirname, 'config')], }) export class ContainerConfiguration implements ILifeCycle { } @@ -32,6 +42,9 @@ export class ContainerConfiguration implements ILifeCycle { } ### Add Configurations +### Swagger API +- start `npm run build && npm run dev` +- open `http://localhost:7001/swagger-ui/index.html` in browser ## License [MIT](LICENSE) diff --git a/packages/mw-demo/package.json b/packages/mw-demo/package.json index 25556033..54f6e4b7 100644 --- a/packages/mw-demo/package.json +++ b/packages/mw-demo/package.json @@ -32,8 +32,8 @@ }, "license": "MIT", "dependencies": { - "@midwayjs/info": "^3.14.0", - "@mwcp/share": "*" + "@mwcp/otel": "20 - 22", + "@mwcp/share": "20 - 22" }, "devDependencies": { }, @@ -51,6 +51,7 @@ "bin", "database", "dist", + "src", "!**/*.spec.*", "!**/*.tsbuildinfo" ], diff --git a/packages/mw-demo/src/app/default.controller.ts b/packages/mw-demo/src/app/default.controller.ts index a587d89e..a38ee1c3 100644 --- a/packages/mw-demo/src/app/default.controller.ts +++ b/packages/mw-demo/src/app/default.controller.ts @@ -3,26 +3,27 @@ import { Controller, Get, } from '@midwayjs/core' +import { ApiResponse } from '@midwayjs/swagger' + +import { DefaultApi } from './default.types.js' import { Config, ConfigKey, Msg } from '##/lib/types.js' -@Controller(`/_${ConfigKey.namespace}`) +@Controller(DefaultApi.base) export class DefaultComponentController { - @_Config(ConfigKey.config) readonly config: Config + @_Config(ConfigKey.config) private readonly config: Config - @Get('/hello') - hello(): string { - this.valiateRoute() + @Get(DefaultApi.hello) + @ApiResponse({ + type: 'string', + description: Msg.hello, + }) + async hello(): Promise { + void this.config return Msg.hello } - valiateRoute(): void { - if (! this.config.enableDefaultRoute) { - throw new Error('route is not enabled') - } - } - } diff --git a/packages/mw-demo/src/app/default.types.ts b/packages/mw-demo/src/app/default.types.ts new file mode 100644 index 00000000..5ac43dc7 --- /dev/null +++ b/packages/mw-demo/src/app/default.types.ts @@ -0,0 +1,7 @@ +import { ConfigKey } from '##/lib/types.js' + + +export class DefaultApi { + static readonly base = `/_${ConfigKey.namespace}` + static readonly hello = '/hello' +} diff --git a/packages/mw-demo/src/configuration.ts b/packages/mw-demo/src/configuration.ts index 8bcee5ec..0d28d81f 100644 --- a/packages/mw-demo/src/configuration.ts +++ b/packages/mw-demo/src/configuration.ts @@ -3,22 +3,25 @@ import assert from 'node:assert' import { App, - Config, + Config as _Config, Configuration, - MidwayEnvironmentService, - MidwayInformationService, ILifeCycle, ILogger, Inject, Logger, + MidwayEnvironmentService, + MidwayInformationService, + MidwayWebRouterService, } from '@midwayjs/core' +import { TraceInit } from '@mwcp/otel' import { Application, IMidwayContainer, registerMiddleware, + deleteRouter, } from '@mwcp/share' -import * as DefulatConfig from './config/config.default.js' +import * as DefaultConfig from './config/config.default.js' import * as LocalConfig from './config/config.local.js' import * as UnittestConfig from './config/config.unittest.js' import { useComponents } from './imports.js' @@ -34,7 +37,7 @@ import { DemoMiddleware } from './middleware/index.middleware.js' namespace: ConfigKey.namespace, importConfigs: [ { - default: DefulatConfig, + default: DefaultConfig, local: LocalConfig, unittest: UnittestConfig, }, @@ -47,11 +50,23 @@ export class AutoConfiguration implements ILifeCycle { @Inject() protected readonly environmentService: MidwayEnvironmentService @Inject() protected readonly informationService: MidwayInformationService + @Inject() protected readonly webRouterService: MidwayWebRouterService + @Logger() protected readonly logger: ILogger - @Config(ConfigKey.config) protected readonly config: Conf - @Config(ConfigKey.middlewareConfig) protected readonly mwConfig: MiddlewareConfig + @_Config(ConfigKey.config) protected readonly config: Conf + @_Config(ConfigKey.middlewareConfig) protected readonly mwConfig: MiddlewareConfig + async onConfigLoad(): Promise { + if (! this.config.enableDefaultRoute) { + await deleteRouter(`/_${ConfigKey.namespace}`, this.webRouterService) + } + else if (this.mwConfig.ignore) { + this.mwConfig.ignore.push(new RegExp(`/_${ConfigKey.namespace}/.+`, 'u')) + } + } + + @TraceInit({ namespace: ConfigKey.namespace }) async onReady(container: IMidwayContainer): Promise { void container assert( @@ -59,10 +74,6 @@ export class AutoConfiguration implements ILifeCycle { 'this.app undefined. If start for development, please set env first like `export MIDWAY_SERVER_ENV=local`', ) - if (this.config.enableDefaultRoute && this.mwConfig.ignore) { - this.mwConfig.ignore.push(new RegExp(`/_${ConfigKey.namespace}/.+`, 'u')) - } - const isDevelopmentEnvironment = this.environmentService.isDevelopmentEnvironment() const { enableMiddleware } = this.mwConfig diff --git a/packages/mw-demo/src/imports.ts b/packages/mw-demo/src/imports.ts index c71038c3..f221ffeb 100644 --- a/packages/mw-demo/src/imports.ts +++ b/packages/mw-demo/src/imports.ts @@ -1,5 +1,7 @@ import * as info from '@midwayjs/info' import * as koa from '@midwayjs/koa' +import * as swagger from '@midwayjs/swagger' +import * as otel from '@mwcp/otel' /* c8 ignore next 4 */ @@ -10,9 +12,11 @@ const CI = !! (process.env['MIDWAY_SERVER_ENV'] === 'unittest' ) export const useComponents: IComponentInfo[] = [] -if (CI && ! useComponents.includes(koa)) { +if (CI) { + useComponents.push(otel) useComponents.push(koa) useComponents.push(info) + useComponents.push(swagger) } export interface IComponentInfo {