Skip to content
This repository was archived by the owner on Nov 28, 2023. It is now read-only.

Commit 5dafe47

Browse files
committed
add: npm part
1 parent 3d55655 commit 5dafe47

File tree

8 files changed

+135
-14
lines changed

8 files changed

+135
-14
lines changed

Diff for: README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@
3030

3131
参数二选一,GET方法只支持提交图片的URL。
3232

33+
## npm
34+
35+
提供查询npm包信息和下载量的服务。
36+
37+
### API
38+
39+
/npm/registry
40+
41+
支持方法:GET
42+
43+
提交参数:package:string
44+
45+
/npm/downloads
46+
47+
支持方法:GET
48+
49+
提交参数:start:string(YYYY-MM-DD), end:string(YYYY-MM-DD), package:string
50+
3351
## 部署说明
3452

3553
参考egg.js服务器通用的部署方法,在克隆本仓库代码后,使用npm install安装依赖,并安装egg-scripts,即可使用npm run start启动服务器。
@@ -46,4 +64,4 @@
4664

4765
cookie: 用于加密Cookie的密钥
4866

49-
redis:redis访问密钥,请确保你的redis是有访问密码的。
67+
redis:redis访问密钥,请确保你的redis是有访问密码的。

Diff for: app/controller/v1/npm.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const Controller = require('egg').Controller;
4+
const R = require('../../utils/common/response');
5+
6+
class NpmController extends Controller {
7+
async registry() {
8+
const { ctx } = this;
9+
ctx.validate({ package: 'string' }, ctx.query);
10+
const res = await this.service.npm.getRegistry(ctx.query.package);
11+
if (res) {
12+
R.success(ctx, res, null);
13+
return;
14+
}
15+
R.error(ctx, 500, '无法获取数据');
16+
}
17+
async downloads() {
18+
const { ctx } = this;
19+
ctx.validate({ package: 'string', start: 'date', end: 'date' }, ctx.query);
20+
const res = await this.service.npm.getDownloads(ctx.query.start, ctx.query.end, ctx.query.package);
21+
if (res) {
22+
R.success(ctx, res, null);
23+
return;
24+
}
25+
R.error(ctx, 500, '无法获取数据');
26+
}
27+
}
28+
29+
module.exports = NpmController;

Diff for: app/service/npm.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const Service = require('egg').Service;
4+
const axios = require('../utils/common/axios');
5+
6+
const REGISTRY_URL = 'https://registry.npmjs.org';
7+
const API_URL = 'https://api.npmjs.org';
8+
9+
class NpmService extends Service {
10+
async getRegistry(name) {
11+
const cached = this.service.redis.get(`npm_registry_${name}`);
12+
if (cached) {
13+
return cached;
14+
}
15+
try {
16+
const res = await axios.get(`${REGISTRY_URL}/${name}`);
17+
if (res.data) {
18+
this.service.redis.set(`npm_registry_${name}`, res.data, 1800);
19+
return res.data;
20+
}
21+
return null;
22+
} catch (err) {
23+
throw err.message;
24+
}
25+
}
26+
async getDownloads(start, end, name) {
27+
const cached = this.service.redis.get(`npm_downloads_${start}_${end}_${name}`);
28+
if (cached) {
29+
return cached;
30+
}
31+
try {
32+
const res = await axios.get(`${API_URL}/downloads/point/${start}:${end}/${name}`);
33+
if (res.data) {
34+
this.service.redis.set(`npm_downloads_${start}_${end}_${name}`, res.data, 1800);
35+
return res.data;
36+
}
37+
return null;
38+
} catch (err) {
39+
throw err.message;
40+
}
41+
}
42+
}
43+
44+
module.exports = NpmService;

Diff for: app/utils/common/axios.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const axios = require('axios');
4+
const qs = require('qs');
5+
6+
axios.defaults.timeout = 5000;
7+
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
8+
axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded';
9+
axios.defaults.transformRequest = [
10+
function(data) {
11+
return qs.stringify(data, {
12+
arrayFormat: 'brackets',
13+
});
14+
},
15+
];
16+
17+
module.exports = axios;

Diff for: app/utils/common/error_response.js

-10
This file was deleted.

Diff for: app/utils/common/response.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
module.exports = {
4+
success(ctx, data, message) {
5+
ctx.status = 200;
6+
ctx.body = {
7+
code: 200,
8+
status: 'success',
9+
message,
10+
data,
11+
};
12+
},
13+
error(ctx, code, message) {
14+
ctx.status = 200;
15+
ctx.body = {
16+
code,
17+
status: 'error',
18+
message,
19+
};
20+
},
21+
};

Diff for: config/config.default.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ module.exports = () => {
2626
csrf: {
2727
enable: false,
2828
},
29+
domainWhiteList: ['http://localhost:8080', 'http://packages.pwp.app'],
2930
};
3031

3132
config.cors = {
32-
origin: '*',
3333
allowMethods: 'GET,POST',
3434
};
3535

Diff for: package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "tools-server",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "",
55
"private": true,
66
"egg": {
77
"declarations": true
88
},
99
"dependencies": {
10+
"axios": "^0.19.2",
1011
"egg": "^2.26.1",
1112
"egg-cors": "^2.2.3",
1213
"egg-onerror": "^2.1.0",
@@ -16,7 +17,8 @@
1617
"egg-validate": "^2.0.2",
1718
"jimp": "^0.10.1",
1819
"jsqr": "^1.3.1",
19-
"qrcode": "^1.4.4"
20+
"qrcode": "^1.4.4",
21+
"qs": "^6.9.4"
2022
},
2123
"devDependencies": {
2224
"autod": "^3.0.1",

0 commit comments

Comments
 (0)