Skip to content

Commit 4ae8be2

Browse files
authored
feat: upgrade egg v3 (#121)
1 parent 819d349 commit 4ae8be2

File tree

18 files changed

+51
-202
lines changed

18 files changed

+51
-202
lines changed

hackernews-async-ts/.autod.conf.js

-30
This file was deleted.

hackernews-async-ts/.eslintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"root": true,
3+
"extends": "eslint-config-egg/typescript"
4+
}
5+

hackernews-async-ts/.vscode/settings.json

-3
This file was deleted.

hackernews-async-ts/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ $ npm start
2929

3030
### Requirement
3131

32-
- Node.js 8.x
33-
- Typescript 2.8+
32+
- Node.js 16.x
33+
- Typescript 4.x

hackernews-async-ts/app/controller/news.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default class NewsController extends Controller {
99
const idList = await ctx.service.news.getTopStories(page);
1010

1111
// get itemInfo parallel
12-
const newsList = await Promise.all(idList.map((id) => ctx.service.news.getItem(id)));
12+
const newsList = await Promise.all(idList.map(id => ctx.service.news.getItem(id)));
1313
await ctx.render('news/list.tpl', { list: newsList, page, pageSize });
1414
}
1515

@@ -18,7 +18,7 @@ export default class NewsController extends Controller {
1818
const id = ctx.params.id;
1919
const newsInfo = await ctx.service.news.getItem(id);
2020
// get comment parallel
21-
const commentList = await Promise.all(newsInfo.kids.map((_id) => ctx.service.news.getItem(_id)));
21+
const commentList = await Promise.all(newsInfo.kids.map(_id => ctx.service.news.getItem(_id)));
2222
await ctx.render('news/detail.tpl', { item: newsInfo, comments: commentList });
2323
}
2424

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
'use strict';
2-
3-
import * as moment from 'moment';
1+
import moment from 'moment';
42

53
export function relativeTime(time) {
64
return moment(new Date(time * 1000)).fromNow();
7-
};
5+
}
86

9-
export function domain (url) {
7+
export function domain(url) {
108
return url && url.split('/')[2];
11-
};
9+
}

hackernews-async-ts/app/service/News.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ export class HackerNews extends Service {
2121
* @param api - Api name
2222
* @param opts - urllib options
2323
*/
24-
public async request(api: string, opts?: object) {
25-
const options = Object.assign({
24+
public async request(api: string, opts?: any) {
25+
const options = {
2626
dataType: 'json',
27-
timeout: ['30s', '30s'],
28-
}, opts);
27+
timeout: '30s',
28+
...opts,
29+
};
2930

3031
const result = await this.ctx.curl(`${this.config.news.serverUrl}/${api}`, options);
3132
return result.data;
@@ -38,17 +39,17 @@ export class HackerNews extends Service {
3839
*/
3940
public async getTopStories(page?: number, pageSize?: number): Promise<number[]> {
4041
page = page || 1;
41-
pageSize = pageSize || this.config.news.pageSize;
42+
const requestPageSize = pageSize ?? this.config.news.pageSize;
4243

4344
try {
4445
const result = await this.request('topstories.json', {
4546
data: {
4647
orderBy: '"$key"',
47-
startAt: `"${pageSize * (page - 1)}"`,
48-
endAt: `"${pageSize * page - 1}"`,
48+
startAt: `"${requestPageSize * (page - 1)}"`,
49+
endAt: `"${requestPageSize * page - 1}"`,
4950
},
5051
});
51-
return Object.keys(result).map((key) => result[key]);
52+
return Object.keys(result).map(key => result[key]);
5253
} catch (e) {
5354
this.ctx.logger.error(e);
5455
return [];

hackernews-async-ts/config/config.default.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
import { EggAppConfig, PowerPartial } from 'egg';
42
import * as fs from 'fs';
53
import * as path from 'path';

hackernews-async-ts/config/plugin.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
export default {
42
nunjucks: {
53
enable: true,

hackernews-async-ts/package.json

+18-23
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,34 @@
88
},
99
"scripts": {
1010
"start": "egg-scripts start",
11-
"dev": "egg-bin dev -r egg-ts-helper/register",
12-
"debug": "egg-bin debug -r egg-ts-helper/register",
13-
"test-local": "egg-bin test -r egg-ts-helper/register",
11+
"dev": "egg-bin dev",
12+
"debug": "egg-bin debug",
13+
"test-local": "egg-bin test",
1414
"test": "npm run lint -- --fix && npm run test-local",
15-
"cov": "egg-bin cov -r egg-ts-helper/register",
16-
"tsc": "ets && tsc -p tsconfig.json",
15+
"cov": "egg-bin cov",
16+
"tsc": "tsc -p tsconfig.json",
1717
"ci": "npm run lint && npm run cov && npm run tsc",
18-
"autod": "autod",
19-
"lint": "tslint .",
20-
"clean": "ets clean"
18+
"lint": "eslint .",
19+
"clean": "tsc -b --clean"
2120
},
2221
"dependencies": {
23-
"egg": "^2.6.0",
24-
"egg-scripts": "^2.6.0",
25-
"egg-view-nunjucks": "^2.2.0",
22+
"egg": "^3.11.0",
23+
"egg-scripts": "^2.17.0",
24+
"egg-view-nunjucks": "^2.3.0",
2625
"moment": "^2.22.0"
2726
},
2827
"devDependencies": {
28+
"@eggjs/tsconfig": "^1.1.0",
2929
"@types/cheerio": "^0.22.1",
30-
"@types/mocha": "^2.2.40",
31-
"@types/node": "^7.0.12",
32-
"@types/supertest": "^2.0.0",
33-
"autod": "^3.0.1",
34-
"autod-egg": "^1.1.0",
30+
"@types/mocha": "^10.0.1",
3531
"cheerio": "^1.0.0-rc.2",
36-
"egg-bin": "^4.6.2",
37-
"egg-mock": "^3.16.0",
38-
"egg-ts-helper": "^1.4.2",
39-
"tslib": "^1.9.0",
40-
"tslint": "^4.0.0",
41-
"typescript": "^2.8.1"
32+
"egg-bin": "^5.9.0",
33+
"egg-mock": "^5.5.0",
34+
"eslint": "^8.31.0",
35+
"eslint-config-egg": "^12.1.0",
36+
"typescript": "^4.9.4"
4237
},
4338
"engines": {
44-
"node": ">=8.9.0"
39+
"node": ">=16.0.0"
4540
}
4641
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
'use strict';
2-
1+
import { strict as assert } from 'assert';
32
import * as cheerio from 'cheerio';
4-
import { app, assert } from 'egg-mock/bootstrap';
3+
import { app } from 'egg-mock/bootstrap';
54

65
describe('test/app/controller/news.test.ts', () => {
76
it('should GET /news', async () => {
@@ -13,17 +12,17 @@ describe('test/app/controller/news.test.ts', () => {
1312

1413
it('should GET /news/item/:id', async () => {
1514
await app.httpRequest()
16-
.get('/news/item/1')
15+
.get('/news/item/1')
1716
// just a example, use regex to test part of dom string, but should be strong characteristic
18-
.expect(/\/news\/item\/1/)
19-
.expect(200);
17+
.expect(/\/news\/item\/1/)
18+
.expect(200);
2019
});
2120

2221
it('should GET /news/user/:id', async () => {
2322
await app.httpRequest()
24-
.get('/news/user/activatedgeek')
23+
.get('/news/user/activatedgeek')
2524
// just a example, use regex to test part of dom string, but should be strong characteristic
26-
.expect(/<span class="label">user:<\/span> activatedgeek/)
27-
.expect(200);
25+
.expect(/<span class="label">user:<\/span> activatedgeek/)
26+
.expect(200);
2827
});
2928
});

hackernews-async-ts/test/app/service/News.test.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
import * as assert from 'assert';
1+
import { strict as assert } from 'assert';
42
import { Context } from 'egg';
53
import { app } from 'egg-mock/bootstrap';
64

hackernews-async-ts/tsconfig.json

+2-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
{
2-
"compileOnSave": true,
2+
"extends": "@eggjs/tsconfig",
33
"compilerOptions": {
4-
"target": "es2017",
5-
"module": "commonjs",
6-
"strict": true,
7-
"noImplicitAny": false,
8-
"experimentalDecorators": true,
9-
"emitDecoratorMetadata": true,
10-
"charset": "utf8",
11-
"allowJs": false,
12-
"pretty": true,
13-
"noEmitOnError": false,
14-
"noUnusedLocals": true,
15-
"noUnusedParameters": true,
16-
"allowUnreachableCode": false,
17-
"allowUnusedLabels": false,
18-
"strictPropertyInitialization": false,
19-
"noFallthroughCasesInSwitch": true,
20-
"skipLibCheck": true,
21-
"skipDefaultLibCheck": true,
22-
"inlineSourceMap": true,
23-
"importHelpers": true
4+
"declaration": false
245
},
256
"exclude": [
267
"app/public",

hackernews-async-ts/tslint.json

-43
This file was deleted.

hackernews-async-ts/typings/app/controller/index.d.ts

-10
This file was deleted.

hackernews-async-ts/typings/app/service/index.d.ts

-10
This file was deleted.

hackernews-async-ts/typings/config/index.d.ts

-25
This file was deleted.

hackernews-async-ts/typings/index.d.ts

-3
This file was deleted.

0 commit comments

Comments
 (0)