Skip to content

Commit 71b1482

Browse files
authored
修复 eslint & 为 QiniuRequestError 添加 data 字段 & 为 chunk 添加 isLocalCached 字段 (#511)
* 修复 eslint & 为 QiniuRequestError 添加 data 字段
1 parent a39495b commit 71b1482

File tree

19 files changed

+135
-79
lines changed

19 files changed

+135
-79
lines changed

.travis.yml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
language: node_js
2+
23
node_js:
3-
- '12'
4+
- "12"
5+
46
cache:
57
directories:
6-
- node_modules
8+
- node_modules
9+
710
install:
8-
- npm install
11+
- npm install
12+
913
script:
10-
- npm run build
11-
- jest --coverage
12-
- npx codecov
13-
before_deploy: npm run build
14+
- npm run lint
15+
- npm run test
16+
- npx codecov
17+
- npm run build
18+
19+
before_deploy:
20+
- npm run build
21+
1422
deploy:
1523
provider: npm
1624

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@ qiniu.compressImage(file, options).then(data => {
167167
* uploadInfo: `object`,只有分片上传时才返回该字段
168168
* uploadInfo.id: 上传任务的唯一标识。
169169
* uploadInfo.url: 上传地址。
170-
* total: 包含`loaded``total``percent`三个属性:
170+
* chunks: `Array<ProgressCompose>` 每个 `chunk` 的上传信息,只有分片上传有此字段
171+
* ProgressCompose 的信息如下
172+
* size: `number` chunk 的尺寸
173+
* loaded: `number` 已经发送完毕的尺寸
174+
* percent: `number` 进度比例,范围在 0 - 100
175+
* fromCache?: `boolean` 是否是来自缓存
176+
* total: 包含 `loaded``total``percent` 三个属性:
171177
* total.loaded: `number`,已上传大小,单位为字节。
172178
* total.total: `number`,本次上传的总量控制信息,单位为字节,注意这里的 total 跟文件大小并不一致。
173179
* total.percent: `number`,当前上传进度,范围:0100
@@ -178,6 +184,7 @@ qiniu.compressImage(file, options).then(data => {
178184
* message: `string` 错误的信息。
179185
* stack: `string` 调用堆栈信息。
180186
* `QiniuRequestError` 继承自 `QiniuError`
187+
* data: `any` 服务端返回的错误信息,如果不是标准的 `json` 格式,则该字段为 `undefined`
181188
* reqId: `string` xhr 请求错误的 `X-Reqid`
182189
* code: `number` 请求错误状态码,可查阅码值对应 [说明](https://developer.qiniu.com/kodo/api/3928/error-responses)。
183190
* isRequestError: 固定为 `true`*不推荐使用,即将废弃*

package.json

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
"types": "esm/index.d.ts",
99
"module": "esm/index.js",
1010
"scripts": {
11-
"clean": "del \"./(lib|dist|esm)\"",
1211
"test": "jest --coverage",
12+
"clean": "del \"./(lib|dist|esm)\"",
1313
"build": "npm run clean && tsc && babel esm --out-dir lib && webpack --optimize-minimize --config webpack.prod.js",
14-
"dev": "npm run lint && webpack-dev-server --open --config webpack.dev.js",
15-
"lint": "tsc --noEmit && eslint --ext .ts",
14+
"dev": "webpack-dev-server --open --config webpack.dev.js",
15+
"lint": "tsc --noEmit && eslint --ext .ts src/",
1616
"server": "node test/server.js"
1717
},
1818
"repository": {
@@ -43,19 +43,22 @@
4343
"@babel/plugin-proposal-object-rest-spread": "^7.10.1",
4444
"@babel/plugin-transform-runtime": "^7.10.1",
4545
"@babel/preset-env": "^7.10.2",
46-
"@qiniu/eslint-config": "0.0.4",
47-
"@types/jest": "^25.2.3",
48-
"@types/node": "^13.1.4",
46+
"@qiniu/eslint-config": "0.0.6-beta.7",
47+
"@types/jest": "^26.0.23",
48+
"@types/node": "^15.3.1",
4949
"@types/spark-md5": "^3.0.2",
50-
"@typescript-eslint/eslint-plugin": "^2.34.0",
51-
"@typescript-eslint/parser": "^2.14.0",
50+
"@typescript-eslint/eslint-plugin": "~4.10.0",
5251
"babel-loader": "^8.1.0",
5352
"babel-plugin-syntax-flow": "^6.18.0",
5453
"body-parser": "^1.18.2",
5554
"connect-multiparty": "^2.1.0",
5655
"del-cli": "^3.0.1",
57-
"eslint": "^6.8.0",
58-
"eslint-plugin-import": "^2.20.2",
56+
"eslint": "~7.2.0",
57+
"eslint-import-resolver-typescript": "~2.3.0",
58+
"eslint-plugin-import": "~2.22.1",
59+
"eslint-plugin-jsx-a11y": "~6.3.0",
60+
"eslint-plugin-react": "~7.20.0",
61+
"eslint-plugin-react-hooks": "~4.2.0",
5962
"express": "^4.16.2",
6063
"jest": "^26.0.1",
6164
"multiparty": "^4.1.3",

src/api/index.mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const errorMap = {
1212
serviceTimeout: new QiniuRequestError(504, 'mock', 'message'), // 服务超时
1313
serviceError: new QiniuRequestError(599, 'mock', 'message'), // 服务错误
1414

15-
invalidUploadId: new QiniuRequestError(612, 'mock', 'message'), // 无效的 upload id
15+
invalidUploadId: new QiniuRequestError(612, 'mock', 'message') // 无效的 upload id
1616
}
1717

1818
export type ApiName =

src/api/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jest.mock('../utils', () => ({
2323

2424
describe('api function test', () => {
2525
test('getUploadUrl', async () => {
26-
let config: Config = {
26+
const config: Config = {
2727
useCdnDomain: true,
2828
disableStatisticsReport: false,
2929
retryCount: 3,

src/errors/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,22 @@ export class QiniuRequestError extends QiniuError {
4242
*/
4343
public isRequestError = true
4444

45-
constructor(public code: number, public reqId: string, message: string) {
45+
/**
46+
* @description 发生错误时服务端返回的错误信息,如果返回不是一个合法的 json、则该字段为 undefined
47+
*/
48+
public data?: any
49+
50+
constructor(public code: number, public reqId: string, message: string, data?: any) {
4651
super(QiniuErrorName.RequestError, message)
52+
this.data = data
4753
}
4854
}
4955

5056
/**
5157
* @description 由于跨域、证书错误、断网、host 解析失败、系统拦截等原因导致的错误
5258
*/
5359
export class QiniuNetworkError extends QiniuRequestError {
54-
constructor(reqId = '', message: string) {
60+
constructor(message: string, reqId = '') {
5561
super(0, reqId, message)
5662
}
5763
}

src/image/index.test.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,26 @@ describe('image func test', () => {
77
const key = 'test.png'
88

99
test('imageView2', () => {
10-
let m = {
11-
'fop': 'imageView2',
12-
'mode': 2,
13-
'h': 450,
14-
'q': 100
10+
const m = {
11+
fop: 'imageView2',
12+
mode: 2,
13+
h: 450,
14+
q: 100
1515
}
16-
let url = imageView2(m, key, domain)
16+
const url = imageView2(m, key, domain)
1717
expect(url).toBe(
18-
'http://otxza7yo2.bkt.clouddn.com/' + key + '?' +
19-
'imageView2/' + encodeURIComponent(m.mode) +
20-
'/h' + '/' + encodeURIComponent(m.h) +
21-
'/q' + '/' + encodeURIComponent(m.q)
18+
'http://otxza7yo2.bkt.clouddn.com/' + key + '?'
19+
+ 'imageView2/' + encodeURIComponent(m.mode)
20+
+ '/h'
21+
+ '/'
22+
+ encodeURIComponent(m.h)
23+
+ '/q'
24+
+ '/' + encodeURIComponent(m.q)
2225
)
2326
})
2427

2528
test('imageMogr2', () => {
26-
let m = {
29+
const m = {
2730
thumbnail: 1,
2831
strip: true,
2932
gravity: 1,
@@ -34,27 +37,27 @@ describe('image func test', () => {
3437
blur: 1
3538
}
3639

37-
let url = imageMogr2(m, key, domain)
40+
const url = imageMogr2(m, key, domain)
3841
expect(url).toBe(
39-
'http://otxza7yo2.bkt.clouddn.com/' + key + '?imageMogr2/' +
40-
'thumbnail/1/strip/gravity/1/quality/1/crop/1/rotate/1/format/1/blur/1'
42+
'http://otxza7yo2.bkt.clouddn.com/' + key + '?imageMogr2/'
43+
+ 'thumbnail/1/strip/gravity/1/quality/1/crop/1/rotate/1/format/1/blur/1'
4144
)
4245
})
4346

4447
test('watermark', () => {
45-
let m = {
48+
const m = {
4649
fop: 'watermark',
4750
mode: 1,
4851
image: 'http://www.b1.qiniudn.com/images/logo-2.png',
4952
dissolve: 100,
5053
dx: 100,
5154
dy: 100
5255
}
53-
let url = watermark(m, key, domain)
56+
const url = watermark(m, key, domain)
5457
expect(url).toBe(
55-
'http://otxza7yo2.bkt.clouddn.com/' + key + '?' +
56-
'watermark/' + m.mode + '/image/' + urlSafeBase64Encode(m.image) +
57-
'/dissolve/100/dx/100/dy/100'
58+
'http://otxza7yo2.bkt.clouddn.com/' + key + '?'
59+
+ 'watermark/' + m.mode + '/image/' + urlSafeBase64Encode(m.image)
60+
+ '/dissolve/100/dx/100/dy/100'
5861
)
5962
m.mode = 3
6063
expect(() => {

src/logger/index.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ describe('test logger', () => {
4141
test('level', () => {
4242
const infoLogger = new Logger('', true, 'INFO')
4343
infoLogger.info('test1')
44-
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
44+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
4545
// @ts-ignore
4646
expect(logMessage).toStrictEqual([infoLogger.getPrintPrefix('INFO'), 'test1'])
4747
infoLogger.warn('test2')
48-
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
48+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
4949
// @ts-ignore
5050
expect(warnMessage).toStrictEqual([infoLogger.getPrintPrefix('WARN'), 'test2'])
5151
infoLogger.error('test3')
52-
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
52+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5353
// @ts-ignore
5454
expect(errorMessage).toStrictEqual([infoLogger.getPrintPrefix('ERROR'), 'test3'])
5555

@@ -62,11 +62,11 @@ describe('test logger', () => {
6262
warnLogger.info('test1')
6363
expect(logMessage).toStrictEqual([])
6464
warnLogger.warn('test2')
65-
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
65+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
6666
// @ts-ignore
6767
expect(warnMessage).toStrictEqual([warnLogger.getPrintPrefix('WARN'), 'test2'])
6868
warnLogger.error('test3')
69-
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
69+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
7070
// @ts-ignore
7171
expect(errorMessage).toStrictEqual([warnLogger.getPrintPrefix('ERROR'), 'test3'])
7272

@@ -81,7 +81,7 @@ describe('test logger', () => {
8181
errorLogger.warn('test2')
8282
expect(warnMessage).toStrictEqual([])
8383
errorLogger.error('test3')
84-
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
84+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
8585
// @ts-ignore
8686
expect(errorMessage).toStrictEqual([errorLogger.getPrintPrefix('ERROR'), 'test3'])
8787

@@ -100,15 +100,15 @@ describe('test logger', () => {
100100
})
101101

102102
test('unique id', () => {
103-
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
103+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
104104
// @ts-ignore
105105
const startId = Logger.id
106106
// eslint-disable-next-line no-new
107107
new Logger('', true, 'OFF')
108108
// eslint-disable-next-line no-new
109109
new Logger('', true, 'OFF')
110110
const last = new Logger('', true, 'OFF')
111-
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
111+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
112112
// @ts-ignore
113113
expect(last.id).toStrictEqual(startId + 3)
114114
})

src/upload/base.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ export interface InternalConfig {
4949
}
5050

5151
/** 上传任务的配置信息 */
52-
export interface Config extends Omit<InternalConfig, 'upprotocol' | 'uphost'> {
52+
export interface Config extends Partial<Omit<InternalConfig, 'upprotocol' | 'uphost'>> {
5353
/** 上传域名协议 */
54-
upprotocol: InternalConfig['upprotocol'] | 'https:' | 'http:'
54+
upprotocol?: InternalConfig['upprotocol'] | 'https:' | 'http:'
5555
/** 自定义上传域名 */
56-
uphost: InternalConfig['uphost'] | string
56+
uphost?: InternalConfig['uphost'] | string
5757
}
5858

5959
export interface UploadOptions {
@@ -83,14 +83,15 @@ export interface UploadHandlers {
8383
}
8484

8585
export interface Progress {
86-
loaded: number
8786
total: number
87+
loaded: number
8888
}
8989

9090
export interface ProgressCompose {
91-
loaded: number
9291
size: number
92+
loaded: number
9393
percent: number
94+
fromCache?: boolean
9495
}
9596

9697
export type XHRHandler = (xhr: XMLHttpRequest) => void
@@ -325,10 +326,11 @@ export default abstract class Base {
325326
})
326327
}
327328

328-
public getProgressInfoItem(loaded: number, size: number) {
329+
public getProgressInfoItem(loaded: number, size: number, fromCache?: boolean): ProgressCompose {
329330
return {
330-
loaded,
331331
size,
332+
loaded,
333+
fromCache,
332334
percent: loaded / size * 100
333335
}
334336
}

src/upload/hosts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ export class HostPool {
118118

119119
// 无可用的,去取离解冻最近的 host
120120
const priorityQueue = cachedHostList
121-
.slice().sort((hostA, hostB) => (
122-
hostA.getUnfreezeTime() || 0) - (hostB.getUnfreezeTime() || 0)
121+
.slice().sort(
122+
(hostA, hostB) => (hostA.getUnfreezeTime() || 0) - (hostB.getUnfreezeTime() || 0)
123123
)
124124

125125
return priorityQueue[0]

0 commit comments

Comments
 (0)