8000 refactor: plugin/config, plugin/ajv by fengmk2 · Pull Request #283 · eggjs/tegg · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: plugin/config, plugin/ajv #283

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

Merged
merged 3 commits into from
Feb 21, 2025
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
8 changes: 5 additions & 3 deletions .mocharc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ spec:
recursive: true
extension:
- ts
require:
- ts-node/register
- source-map-support/register
# require:
# - ts-node/register.js
# - source-map-support/register
import:
- tsx/esm
full-trace: true
exit: true
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"lint": "npm run oxlint && eslint . --ext .ts --cache",
"oxlint": "oxlint",
"test": "vitest",
"cov": "nyc npm run test",
"bump": "lerna version --conventional-commits --sign-git-commit --sign-git-tag --force-publish",
"pub": "lerna publish from-git",
"pub-canary": "lerna publish --canary",
Expand All @@ -31,7 +30,7 @@
"tsc-workspaces": "npm run tsc --workspaces --if-present",
"tsc:pub": "lerna run tsc:pub",
"tsc:pub-workspaces": "npm run tsc:pub --workspaces --if-present",
"coverage": "vitest run --coverage core/*",
"coverage": "vitest run --coverage core/* plugin/common plugin/config",
"preci": "npm run clean && npm run lint",
"ci": "npm run coverage",
"postci-skip": "npm run prepublishOnly --workspaces --if-present"
Expand All @@ -53,6 +52,7 @@
"rimraf": "^6.0.1",
"source-map-support": "^0.5.16",
"test-exclude": "^6.0.0",
"tsx": "^4.19.2",
"vite-plugin-swc-transform": "^1.1.0",
"vitest": "^3.0.5"
},
Expand Down
4 changes: 3 additions & 1 deletion plugin/ajv/lib/Ajv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Ajv2019, { type Schema } from 'ajv/dist/2019';
import { type Schema, Ajv2019 } from 'ajv/dist/2019.js';

Check warning on line 1 in plugin/ajv/lib/Ajv.ts

View check run for this annotation

Codecov / codecov/patch

plugin/ajv/lib/Ajv.ts#L1

Added line #L1 was not covered by tests
import addFormats from '@eggjs/ajv-formats';
import keyWords from '@eggjs/ajv-keywords';
import { type Ajv as IAjv, AjvInvalidParamError } from '@eggjs/tegg/ajv';
Expand All @@ -15,7 +15,9 @@
@LifecycleInit()
protected _init() {
this.#ajvInstance = new Ajv2019();
// @ts-expect-error ajv-keywords is not typed

Check warning on line 18 in plugin/ajv/lib/Ajv.ts

View check run for this annotation

Codecov / codecov/patch

plugin/ajv/lib/Ajv.ts#L18

Added line #L18 was not covered by tests
keyWords(this.#ajvInstance, 'transform');
// @ts-expect-error ajv-formats is not typed

Check warning on line 20 in plugin/ajv/lib/Ajv.ts

View check run for this annotation

Codecov / codecov/patch

plugin/ajv/lib/Ajv.ts#L20

Added line #L20 was not covered by tests
addFormats(this.#ajvInstance, [
'date-time',
'time',
Expand Down
30 changes: 16 additions & 14 deletions plugin/ajv/package.json
10000
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,23 @@
"tegg",
"ajv"
],
"type": "module",
"exports": {
".": {
"types": "./src/index.d.ts",
"default": "./src/index.js"
}
},
"files": [
"lib/**/*.js",
"lib/**/*.d.ts",
"typings/*.d.ts"
"typings/**/*.d.ts"
],
"types": "typings/index.d.ts",
"scripts": {
"test": "cross-env NODE_ENV=test NODE_OPTIONS='--no-deprecation' mocha",
"clean": "tsc -b --clean",
"tsc": "npm run clean && tsc -p ./tsconfig.json",
"tsc:pub": "npm run clean && tsc -p ./tsconfig.pub.json",
"prepublishOnly": "npm run tsc:pub"
"tsc:pub": "npm run tsc",
"prepublishOnly": "npm run tsc"
},
"homepage": "https://github.com/eggjs/tegg",
"bugs": {
Expand All @@ -43,7 +48,7 @@
"directory": "plugin/ajv"
},
"engines": {
"node": ">=16.0.0"
"node": ">=20.0.0"
},
"dependencies": {
"@eggjs/ajv-formats": "^3.0.1",
Expand All @@ -56,14 +61,11 @@
"@eggjs/tegg-config": "^3.52.0",
"@eggjs/tegg-controller-plugin": "^3.52.0",
"@eggjs/tegg-plugin": "^3.52.0",
"@types/mocha": "^10.0.1",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"egg": "^3.9.1",
"egg-mock": "^5.5.0",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
"@types/node": "22",
"egg": "4",
"@eggjs/mock": "6",
"ts-node": "10",
"typescript": "5"
},
"publishConfig": {
"access": "public"
Expand Down
83 changes: 41 additions & 42 deletions plugin/ajv/test/ajv.test.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
import assert from 'node:assert';
import assert from 'node:assert/strict';
import path from 'node:path';
import mm, { MockApplication } from 'egg-mock';
import { afterEach, it, beforeAll, afterAll } from 'vitest';
import { mm, MockApplication } from '@eggjs/mock';

describe('plugin/ajv/test/ajv.test.ts', () => {
let app: MockApplication;
let app: MockApplication;

afterEach(async () => {
return mm.restore();
});
afterEach(() => {
return mm.restore();
});

before(async () => {
mm(process.env, 'EGG_TYPESCRIPT', true);
mm(process, 'cwd', () => {
return path.join(__dirname, '../');
});
app = mm.app({
baseDir: path.join(__dirname, './fixtures/apps/ajv-app'),
framework: require.resolve('egg'),
});
await app.ready();
beforeAll(async () => {
mm(process.env, 'EGG_TYPESCRIPT', true);
mm(process, 'cwd', () => {
return path.join(__dirname, '../');
});

after(() => {
return app.close();
app = mm.app({
baseDir: path.join(__dirname, './fixtures/apps/ajv-app'),
framework: require.resolve('egg'),
});
await app.ready();
});

it('should throw AjvInvalidParamError', async () => {
app.mockCsrf();
const res = await app.httpRequest()
.post('/foo')
.send({});
assert.equal(res.status, 500);
assert.match(res.text, /AjvInvalidParamError: Validation Failed/);
});
afterAll(() => {
return app.close();
});

it('should throw AjvInvalidParamError', async () => {
app.mockCsrf();
const res = await app.httpRequest()
.post('/foo')
.send({});
assert.equal(res.status, 500);
assert.match(res.text, /AjvInvalidParamError: Validation Failed/);
});

it('should pass', async () => {
app.mockCsrf();
const res = await app.httpRequest()
.post('/foo')
.send({
fullname: 'fullname ',
skipDependencies: false,
});
assert.equal(res.status, 200);
assert.deepEqual(res.body, {
body: {
fullname: 'fullname',
skipDependencies: false,
},
it('should pass', async () => {
app.mockCsrf();
const res = await app.httpRequest()
.post('/foo')
.send({
fullname: 'fullname ',
skipDependencies: false,
});
assert.equal(res.status, 200);
assert.deepEqual(res.body, {
body: {
fullname: 'fullname',
skipDependencies: false,
},
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = () => {
export default () => {
const config = {
keys: 'test key',
security: {
Expand Down
19 changes: 0 additions & 19 deletions plugin/ajv/test/fixtures/apps/ajv-app/config/plugin.js

This file was deleted.

21 changes: 21 additions & 0 deletions plugin/ajv/test/fixtures/apps/ajv-app/config/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default () => {
return {
tracer: {
package: 'egg-tracer',
enable: true,
},
tegg: {
package: '@eggjs/tegg-plugin',
enable: true,
},
teggConfig: {
package: '@eggjs/tegg-config',
enable: true,
},
teggController: {
package: '@eggjs/tegg-controller-plugin',
enable: true,
},
};
};

Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"name": "demo",
"eggModule": {
"name": "demo"
}
},
"type": "module"
}
3 changes: 2 additions & 1 deletion plugin/ajv/test/fixtures/apps/ajv-app/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"name": "ajv-app"
"name": "ajv-app",
"type": "module"
}
2 changes: 1 addition & 1 deletion plugin/ajv/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"baseUrl": "./"
},
"exclude": [
"node_modules"
"test"
]
}
10 changes: 0 additions & 10 deletions plugin/ajv/tsconfig.pub.json

This file was deleted.

2 changes: 1 addition & 1 deletion plugin/common/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# `@eggjs/egg-module-common`

# Usage
## Usage

This is an internal tegg library, you probably shouldn't use it directly.
23 changes: 13 additions & 10 deletions plugin/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@
"common",
"tegg"
],
"type": "module",
"exports": {
".": {
"types": "./index.d.ts",
"default": "./index.js"
}
},
"files": [
"index.js",
"index.d.ts"
],
"types": "index.d.ts",
"scripts": {
"clean": "tsc -b --clean",
"tsc": "npm run clean && tsc -p ./tsconfig.json",
"tsc:pub": "npm run clean && tsc -p ./tsconfig.pub.json",
"prepublishOnly": "npm run tsc:pub"
"tsc:pub": "npm run tsc",
"prepublishOnly": "npm run tsc"
},
"author": "killagu <killa123@126.com>",
"license": "MIT",
Expand All @@ -32,17 +38,14 @@
"directory": "plugin/common"
},
"engines": {
"node": ">=14.0.0"
"node": ">=20.0.0"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
"@types/node": "22",
"ts-node": "10",
"typescript": "5"
}
}
9 changes: 9 additions & 0 deletions plugin/common/test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`should export stable 1`] = `
{
"EGG_CONTEXT": Symbol(context#eggContext),
"ROOT_PROTO": Symbol(context#rootProto),
"TEGG_CONTEXT": Symbol(context#teggContext),
}
`;
6 changes: 6 additions & 0 deletions plugin/common/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { expect, it } from 'vitest';
import * as types from '../index.js';

it('should export stable', async () => {
expect(types).toMatchSnapshot();
});
1 change: 0 additions & 1 deletion plugin/common/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"baseUrl": "./"
},
"exclude": [
"node_modules",
"test"
]
}
10 changes: 0 additions & 10 deletions plugin/common/tsconfig.pub.json

This file was deleted.

2 changes: 1 addition & 1 deletion plugin/config/agent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import TeggConfigAppHook from './app';
import TeggConfigAppHook from './app.js';

export default TeggConfigAppHook;
2 changes: 1 addition & 1 deletion plugin/config/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Application, IBoot } from 'egg';
import { ModuleConfigUtil } from '@eggjs/tegg-common-util';
import type { ModuleReference } from '@eggjs/tegg-common-util';
import { ModuleScanner } from './lib/ModuleScanner';
import { ModuleScanner } from './lib/ModuleScanner.js';

export default class App implements IBoot {
private readonly app: Application;
Expand Down
Loading
Loading
0