Skip to content

feat: upgrade to egg v4 #126

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 0 additions & 24 deletions .github/PULL_REQUEST_TEMPLATE.md

This file was deleted.

7 changes: 2 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ name: CI
on:
push:
branches: [ master ]

pull_request:
branches: [ master ]

workflow_dispatch: {}

jobs:
Job:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-test.yml@master
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest'
version: '14, 16, 18'
version: '20, 22'
18 changes: 4 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# Examples for [egg](https://github.com/eggjs/egg)

---

[![build status][travis-image]][travis-url]
[![CI](https://github.com/eggjs/examples/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/eggjs/examples/actions/workflows/ci.yml)
[![node version][node-image]][node-url]
[![egg version][egg-image]][egg-url]

[travis-image]: https://img.shields.io/travis/eggjs/examples.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/examples
[node-image]: https://img.shields.io/badge/node.js-%3E=_8-green.svg?style=flat-square
[node-image]: https://img.shields.io/badge/node.js-%3E=_18-green.svg?style=flat-square
[node-url]: http://nodejs.org/download/
[egg-image]: https://img.shields.io/badge/egg-%3E=_1-green.svg?style=flat-square
[egg-image]: https://img.shields.io/badge/egg-%3E=_4-green.svg?style=flat-square
[egg-url]: https://github.com/eggjs/egg

## Usage
Expand All @@ -23,7 +19,7 @@ $ npm install
$ npm run dev
```

**Recommend to use Node >= 8**
**Recommend to use Node >= 18**

## List of examples

Expand Down Expand Up @@ -64,12 +60,6 @@ You can use `--verbose` to show more infomation
$ npm test -- --verbose
```

### Generate dependencies

```bash
$ npm run autod
```

### Show list of examples

```bash
Expand Down
17 changes: 17 additions & 0 deletions bin/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -eux -o pipefail

test() {
echo "Test $1"
cd "$1"
pwd
rm -rf node_modules package-lock.json
npm install --registry=https://registry.npmmirror.com
npm run ci
cd ..
Comment on lines +11 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for npm commands.

The script currently doesn't preserve exit codes from the npm commands. If npm run ci fails, the script will still continue to the next test. This could hide test failures.

- npm run ci
- cd ..
+ if ! npm run ci; then
+   echo "Tests failed in $1"
+   cd ..
+   return 1
+ fi
+ cd ..
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
npm run ci
cd ..
if ! npm run ci; then
echo "Tests failed in $1"
cd ..
return 1
fi
cd ..

}

test body-parser-example
test helloworld
test hello-tegg
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const Controller = require('egg').Controller;
const { Controller } = require('egg');
const { xml2js } = require('xml-js');

class HomeController extends Controller {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

exports.keys = '123456';

exports.bodyParser = {
Expand Down
10 changes: 5 additions & 5 deletions bodyParser/package.json → body-parser-example/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "bodyParser",
"name": "body-parser-example",
"version": "1.0.0",
"dependencies": {
"egg": "^2",
"egg": "4",
"xml-js": "^1.6.9"
},
"devDependencies": {
"egg-bin": "^4.3.5",
"egg-mock": "^3.13.1"
"@eggjs/bin": "7",
"@eggjs/mock": "6"
},
"scripts": {
"dev": "egg-bin dev",
"test": "egg-bin test",
"cov": "egg-bin cov"
"ci": "egg-bin cov"
},
"private": true
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict';

const { app } = require('egg-mock/bootstrap');

describe('test/app/controller/home.test.js', () => {
const { app } = require('@eggjs/mock/bootstrap');

describe('test/home.test.js', () => {
beforeEach(() => app.mockCsrf());

it('should parse form', () => {
Expand Down
1 change: 1 addition & 0 deletions hello-tegg/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ test/**/*.map
config/**/*.map
*.d.ts
*.tsbuildinfo
.egg/
3 changes: 2 additions & 1 deletion hello-tegg/app/biz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"name": "biz-module",
"eggModule": {
"name": "biz"
}
},
"type": "module"
}
4 changes: 2 additions & 2 deletions hello-tegg/app/controller/HelloController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
Inject,
} from '@eggjs/tegg';
import { EggLogger } from 'egg';
import { traceMethod } from 'app/middleware/trace_method';
import { HelloService } from 'app/biz/HelloService';
import { traceMethod } from '../middleware/trace_method.js';
import { HelloService } from '../biz/HelloService.js';

@HTTPController()
@Middleware(traceMethod)
Expand Down
4 changes: 2 additions & 2 deletions hello-tegg/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EggAppConfig, PowerPartial } from 'egg';
import { EggAppInfo, EggAppConfig, PowerPartial } from 'egg';

export default (appInfo: EggAppConfig) => {
export default (appInfo: EggAppInfo) => {
const config = {} as PowerPartial<EggAppConfig>;

// override config from framework / plugin
Expand Down
53 changes: 29 additions & 24 deletions hello-tegg/package.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
{
"name": "hello-tegg",
"private": true,
"type": "module",
"description": "tegg application example",
"scripts": {
"start": "egg-scripts start",
"stop": "egg-scripts stop",
"start": "eggctl start --daemon",
"stop": "eggctl stop",
"dev": "egg-bin dev",
"debug": "egg-bin debug",
"test-local": "egg-bin test",
"test": "npm run lint -- --fix && npm run test-local",
"cov": "egg-bin cov",
"tsc": "tsc -p tsconfig.json",
"ci": "npm run lint && npm run cov && npm run tsc",
"pretest": "npm run clean && npm run lint -- --fix",
"test": "egg-bin test",
"tsc": "tsc -p tsconfig.prod.json",
"preci": "npm run clean && npm run lint",
"ci": "egg-bin cov",
"postci": "npm run tsc && npm run clean",
"lint": "eslint .",
"clean": "tsc -b --clean"
"clean": "tsc -b --clean",
"prepublishOnly": "npm run clean && tsc"
},
"egg": {
"typescript": true
},
"license": "MIT",
"dependencies": {
"@eggjs/tegg": "^3.2.1",
"@eggjs/tegg-controller-plugin": "^3.2.1",
"@eggjs/tegg-plugin": "^3.2.1",
"@eggjs/tsconfig": "^1.2.0",
"@eggjs/tegg-config": "^3.1.0",
"egg": "^3.9.1",
"egg-scripts": "^2.17.0"
"@eggjs/scripts": "^4.0.0",
"@eggjs/tegg": "^4.0.0-beta.2",
"@eggjs/tegg-config": "^4.0.0-beta.2",
"@eggjs/tegg-controller-plugin": "^4.0.0-beta.2",
"@eggjs/tegg-plugin": "^4.0.0-beta.2",
"egg": "^4.0.10"
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^16.18.10",
"egg-bin": "^6.0.0",
"egg-mock": "^5.4.0",
"eslint": "^8.30.0",
"eslint-config-egg": "^12.1.0",
"typescript": "^4.9.4"
"@types/mocha": "10",
"@types/node": "22",
"@eggjs/tsconfig": "2",
"@eggjs/bin": "7",
"@eggjs/mock": "6",
"eslint": "8",
"eslint-config-egg": "14",
"typescript": "5"
},
"repository": "[email protected]:eggjs/examples.git"
"repository": "[email protected]:eggjs/examples.git",
"engines": {
"node": ">=20.0.0"
}
}
19 changes: 6 additions & 13 deletions hello-tegg/test/biz/HelloService.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import { Context } from 'egg';
import assert from 'assert';
import { app } from 'egg-mock/bootstrap';
import { HelloService } from '../../app/biz/HelloService';
import assert from 'node:assert/strict';
import { app } from '@eggjs/mock/bootstrap';
import { HelloService } from '../../app/biz/HelloService.js';

describe('test/biz/HelloService.test.ts', () => {
let ctx: Context;
let helloService: HelloService;

beforeEach(async () => {
ctx = await app.mockModuleContext();
helloService = await ctx.getEggObject(HelloService);
});

afterEach(async () => {
await app.destroyModuleContext(ctx);
before(async () => {
helloService = await app.getEggObject(HelloService);
});

it('should work', async () => {
const msg = await helloService.hello('killa');
assert(msg === 'hello, killa');
assert.equal(msg, 'hello, killa');
});
});
22 changes: 10 additions & 12 deletions hello-tegg/test/controller/HelloController.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { app } from 'egg-mock/bootstrap';
import assert from 'assert';
import assert from 'node:assert/strict';
import { app } from '@eggjs/mock/bootstrap';

describe('test/controller/HelloController.test.ts', () => {
it('should work', async () => {
await app.httpRequest()
const res = await app.httpRequest()
.get('/hello?name=killa')
.expect(200)
.expect(res => {
assert.deepStrictEqual(res.body, {
success: true,
data: {
message: 'hello, killa (GET)',
},
});
});
.expect(200);
assert.deepEqual(res.body, {
success: true,
data: {
message: 'hello, killa (GET)',
},
});
});
});
6 changes: 4 additions & 2 deletions hello-tegg/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"baseUrl": "./"
}
"baseUrl": ".",
"declaration": false
},
"exclude": []
}
8 changes: 8 additions & 0 deletions hello-tegg/tsconfig.prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"baseUrl": ".",
"declaration": false
},
"exclude": []
}
6 changes: 6 additions & 0 deletions helloworld/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
2 changes: 2 additions & 0 deletions helloworld/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
*.js
13 changes: 0 additions & 13 deletions helloworld/app/controller/foo.js

This file was deleted.

11 changes: 11 additions & 0 deletions helloworld/app/controller/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Controller } from 'egg';

export default class FooController extends Controller {
async render() {
const ctx = this.ctx;
ctx.status = 400;
ctx.body = {
foo: 'bar',
};
}
}
13 changes: 0 additions & 13 deletions helloworld/app/controller/home.js

This file was deleted.

8 changes: 8 additions & 0 deletions helloworld/app/controller/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Controller } from 'egg';

export default class HomeController extends Controller {
async render() {
const ctx = this.ctx;
ctx.body = 'Hello World';
}
}
Loading