Skip to content

Commit b609224

Browse files
committed
修复 eslint & 为 QiniuRequestError 添加 data 字段
1 parent a39495b commit b609224

File tree

16 files changed

+107
-67
lines changed

16 files changed

+107
-67
lines changed

.travis.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
language: node_js
2+
23
node_js:
34
- '12'
5+
46
cache:
57
directories:
68
- node_modules
9+
710
install:
811
- npm install
12+
913
script:
14+
- npm run lint
15+
- npm run test
1016
- npm run build
11-
- jest --coverage
12-
- npx codecov
17+
1318
before_deploy: npm run build
19+
1420
deploy:
1521
provider: npm
1622

package.json

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
"module": "esm/index.js",
1010
"scripts": {
1111
"clean": "del \"./(lib|dist|esm)\"",
12-
"test": "jest --coverage",
12+
"test": "jest --coverage && npx codecov",
1313
"build": "npm run clean && tsc && babel esm --out-dir lib && webpack --optimize-minimize --config webpack.prod.js",
1414
"dev": "npm run lint && webpack-dev-server --open --config webpack.dev.js",
15-
"lint": "tsc --noEmit && eslint --ext .ts",
15+
"lint": "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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import * as api from '.'
44
export const errorMap = {
55
networkError: new QiniuNetworkError('mock', 'message'), // 网络错误
66

7-
invalidParams: new QiniuRequestError(400, 'mock', 'message'), // 无效的参数
8-
expiredToken: new QiniuRequestError(401, 'mock', 'message'), // token 过期
7+
invalidParams: new QiniuRequestError(400, 'mock', 'message', {}), // 无效的参数
8+
expiredToken: new QiniuRequestError(401, 'mock', 'message', {}), // token 过期
99

10-
gatewayUnavailable: new QiniuRequestError(502, 'mock', 'message'), // 网关不可用
11-
serviceUnavailable: new QiniuRequestError(503, 'mock', 'message'), // 服务不可用
12-
serviceTimeout: new QiniuRequestError(504, 'mock', 'message'), // 服务超时
13-
serviceError: new QiniuRequestError(599, 'mock', 'message'), // 服务错误
10+
gatewayUnavailable: new QiniuRequestError(502, 'mock', 'message', {}), // 网关不可用
11+
serviceUnavailable: new QiniuRequestError(503, 'mock', 'message', {}), // 服务不可用
12+
serviceTimeout: new QiniuRequestError(504, 'mock', 'message', {}), // 服务超时
13+
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,8 +42,14 @@ 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 = undefined
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

@@ -52,6 +58,6 @@ export class QiniuRequestError extends QiniuError {
5258
*/
5359
export class QiniuNetworkError extends QiniuRequestError {
5460
constructor(reqId = '', message: string) {
55-
super(0, reqId, message)
61+
super(0, reqId, message, undefined)
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/hosts.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ export class HostPool {
119119
// 无可用的,去取离解冻最近的 host
120120
const priorityQueue = cachedHostList
121121
.slice().sort((hostA, hostB) => (
122-
hostA.getUnfreezeTime() || 0) - (hostB.getUnfreezeTime() || 0)
123-
)
122+
hostA.getUnfreezeTime() || 0) - (hostB.getUnfreezeTime() || 0))
124123

125124
return priorityQueue[0]
126125
}

src/upload/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ describe('test upload', () => {
5151
localStorage.clear()
5252
mockApi.clearInterceptor()
5353
mockApi.setInterceptor('direct', () => Promise.reject(error))
54+
// eslint-disable-next-line no-await-in-loop
5455
await expect(observablePromisify(upload(File3M, null, testToken)))
5556
.rejects.toStrictEqual(error)
5657
}
@@ -73,6 +74,7 @@ describe('test upload', () => {
7374
localStorage.clear()
7475
mockApi.clearInterceptor()
7576
mockApi.setInterceptor(apiName, (..._: any[]) => Promise.reject(error))
77+
// eslint-disable-next-line no-await-in-loop
7678
await expect(observablePromisify(upload(File5M, null, testToken)))
7779
.rejects.toStrictEqual(error)
7880
}

src/upload/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ export default function upload(
6060
// 创建 host 池
6161
const hostPool = new HostPool(options.config.uphost)
6262

63-
return new Observable((observer: IObserver<UploadProgress, QiniuError | QiniuRequestError | QiniuNetworkError, UploadCompleteData>) => {
63+
return new Observable((observer: IObserver<
64+
UploadProgress,
65+
QiniuError | QiniuRequestError | QiniuNetworkError,
66+
UploadCompleteData
67+
>) => {
6468
const manager = createUploadManager(options, {
6569
onData: (data: UploadProgress) => observer.next(data),
6670
onError: (err: QiniuError) => observer.error(err),

src/upload/resume.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import Base, { Progress, UploadInfo, Extra } from './base'
66

77
export interface UploadedChunkStorage extends UploadChunkData {
88
size: number
9+
10+
// 标记该 chunk 是否来自缓存
11+
isLocalCache?: true
912
}
1013

1114
export interface ChunkLoaded {
@@ -208,6 +211,12 @@ export default class Resume extends Base {
208211

209212
this.logger.info(infoMessage.join(', '))
210213
this.uploadedList = localInfo.data
214+
if (Array.isArray(this.uploadedList)) {
215+
this.uploadedList.forEach(chunk => {
216+
chunk.isLocalCache = true
217+
})
218+
}
219+
211220
this.uploadId = localInfo.id
212221
}
213222

src/utils/compress.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { EXIF } from 'exif-js'
2+
23
import { QiniuErrorName, QiniuError } from '../errors'
34

45
import { createObjectURL, getTransform } from './helper'

src/utils/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Logger from '../logger'
22
import { regionUphostMap } from '../config'
33
import { Config, DEFAULT_CHUNK_SIZE, InternalConfig } from '../upload'
44

5-
export function normalizeUploadConfig(config?: Partial<Config>, logger?: Logger,): InternalConfig {
5+
export function normalizeUploadConfig(config?: Partial<Config>, logger?: Logger): InternalConfig {
66
const { upprotocol, uphost, ...otherConfig } = { ...config }
77

88
const normalizeConfig: InternalConfig = {

src/utils/helper.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('api function test', () => {
1515

1616
for (const [input, expected] of testData) {
1717
const testBlob = new Blob([input], { type: 'text/plain;charset=utf-8' })
18+
// eslint-disable-next-line no-await-in-loop
1819
const actual = await computeMd5(testBlob)
1920
expect(actual).toStrictEqual(expected)
2021
}

src/utils/helper.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,15 @@ export function request<T>(url: string, options: RequestOptions): Response<T> {
234234
if (responseText) {
235235
message += ` response: ${responseText}`
236236
}
237-
reject(new QiniuRequestError(xhr.status, reqId, message))
237+
238+
let data
239+
try {
240+
data = JSON.parse(responseText)
241+
} catch {
242+
// 无需处理该错误、可能拿到非 json 格式的响应是预期的
243+
}
244+
245+
reject(new QiniuRequestError(xhr.status, reqId, message, data))
238246
return
239247
}
240248

0 commit comments

Comments
 (0)