|
| 1 | +import { QiniuNetworkError, QiniuRequestError } from '../errors' |
| 2 | +import * as api from '.' |
| 3 | + |
| 4 | +export const errorMap = { |
| 5 | + networkError: new QiniuNetworkError('mock', 'message'), // 网络错误 |
| 6 | + |
| 7 | + invalidParams: new QiniuRequestError(400, 'mock', 'message'), // 无效的参数 |
| 8 | + expiredToken: new QiniuRequestError(401, 'mock', 'message'), // token 过期 |
| 9 | + |
| 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'), // 服务错误 |
| 14 | + |
| 15 | + invalidUploadId: new QiniuRequestError(612, 'mock', 'message'), // 无效的 upload id |
| 16 | +} |
| 17 | + |
| 18 | +export type ApiName = |
| 19 | + | 'direct' |
| 20 | + | 'getUpHosts' |
| 21 | + | 'uploadChunk' |
| 22 | + | 'uploadComplete' |
| 23 | + | 'initUploadParts' |
| 24 | + | 'deleteUploadedChunks' |
| 25 | + |
| 26 | +export class MockApi { |
| 27 | + constructor() { |
| 28 | + this.direct = this.direct.bind(this) |
| 29 | + this.getUpHosts = this.getUpHosts.bind(this) |
| 30 | + this.uploadChunk = this.uploadChunk.bind(this) |
| 31 | + this.uploadComplete = this.uploadComplete.bind(this) |
| 32 | + this.initUploadParts = this.initUploadParts.bind(this) |
| 33 | + this.deleteUploadedChunks = this.deleteUploadedChunks.bind(this) |
| 34 | + } |
| 35 | + |
| 36 | + private interceptorMap = new Map<ApiName, any>() |
| 37 | + public clearInterceptor() { |
| 38 | + this.interceptorMap.clear() |
| 39 | + } |
| 40 | + |
| 41 | + public setInterceptor(name: 'direct', interceptor: typeof api.direct): void |
| 42 | + public setInterceptor(name: 'getUpHosts', interceptor: typeof api.getUpHosts): void |
| 43 | + public setInterceptor(name: 'uploadChunk', interceptor: typeof api.uploadChunk): void |
| 44 | + public setInterceptor(name: 'uploadComplete', interceptor: typeof api.uploadComplete): void |
| 45 | + public setInterceptor(name: 'initUploadParts', interceptor: typeof api.initUploadParts): void |
| 46 | + public setInterceptor(name: 'deleteUploadedChunks', interceptor: typeof api.deleteUploadedChunks): void |
| 47 | + public setInterceptor(name: ApiName, interceptor: any): void |
| 48 | + public setInterceptor(name: any, interceptor: any): void { |
| 49 | + this.interceptorMap.set(name, interceptor) |
| 50 | + } |
| 51 | + |
| 52 | + private callInterceptor(name: ApiName, defaultValue: any): any { |
| 53 | + const interceptor = this.interceptorMap.get(name) |
| 54 | + if (interceptor != null) { |
| 55 | + return interceptor() |
| 56 | + } |
| 57 | + |
| 58 | + return defaultValue |
| 59 | + } |
| 60 | + |
| 61 | + public direct(): ReturnType<typeof api.direct> { |
| 62 | + const defaultData: ReturnType<typeof api.direct> = Promise.resolve({ |
| 63 | + reqId: 'req-id', |
| 64 | + data: { |
| 65 | + fsize: 270316, |
| 66 | + bucket: 'test2222222222', |
| 67 | + hash: 'Fs_k3kh7tT5RaFXVx3z1sfCyoa2Y', |
| 68 | + name: '84575bc9e34412d47cf3367b46b23bc7e394912a', |
| 69 | + key: '84575bc9e34412d47cf3367b46b23bc7e394912a.html' |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + return this.callInterceptor('direct', defaultData) |
| 74 | + } |
| 75 | + |
| 76 | + public getUpHosts(): ReturnType<typeof api.getUpHosts> { |
| 77 | + const defaultData: ReturnType<typeof api.getUpHosts> = Promise.resolve({ |
| 78 | + reqId: 'req-id', |
| 79 | + data: { |
| 80 | + ttl: 86400, |
| 81 | + io: { src: { main: ['iovip-z2.qbox.me'] } }, |
| 82 | + up: { |
| 83 | + acc: { |
| 84 | + main: ['upload-z2.qiniup.com'], |
| 85 | + backup: ['upload-dg.qiniup.com', 'upload-fs.qiniup.com'] |
| 86 | + }, |
| 87 | + old_acc: { main: ['upload-z2.qbox.me'], info: 'compatible to non-SNI device' }, |
| 88 | + old_src: { main: ['up-z2.qbox.me'], info: 'compatible to non-SNI device' }, |
| 89 | + src: { main: ['up-z2.qiniup.com'], backup: ['up-dg.qiniup.com', 'up-fs.qiniup.com'] } |
| 90 | + }, |
| 91 | + uc: { acc: { main: ['uc.qbox.me'] } }, |
| 92 | + rs: { acc: { main: ['rs-z2.qbox.me'] } }, |
| 93 | + rsf: { acc: { main: ['rsf-z2.qbox.me'] } }, |
| 94 | + api: { acc: { main: ['api-z2.qiniu.com'] } } |
| 95 | + } |
| 96 | + }) |
| 97 | + |
| 98 | + return this.callInterceptor('getUpHosts', defaultData) |
| 99 | + } |
| 100 | + |
| 101 | + public uploadChunk(): ReturnType<typeof api.uploadChunk> { |
| 102 | + const defaultData: ReturnType<typeof api.uploadChunk> = Promise.resolve({ |
| 103 | + reqId: 'req-id', |
| 104 | + data: { |
| 105 | + etag: 'FuYYVJ1gmVCoGk5C5r5ftrLXxE6m', |
| 106 | + md5: '491309eddd8e7233e14eaa25216594b4' |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + return this.callInterceptor('uploadChunk', defaultData) |
| 111 | + } |
| 112 | + |
| 113 | + public uploadComplete(): ReturnType<typeof api.uploadComplete> { |
| 114 | + const defaultData: ReturnType<typeof api.uploadComplete> = Promise.resolve({ |
| 115 | + reqId: 'req-id', |
| 116 | + data: { |
| 117 | + key: 'test.zip', |
| 118 | + hash: 'lsril688bAmXn7kiiOe9fL4mpc39', |
| 119 | + fsize: 11009649, |
| 120 | + bucket: 'test', |
| 121 | + name: 'test' |
| 122 | + } |
| 123 | + }) |
| 124 | + |
| 125 | + return this.callInterceptor('uploadComplete', defaultData) |
| 126 | + } |
| 127 | + |
| 128 | + public initUploadParts(): ReturnType<typeof api.initUploadParts> { |
| 129 | + const defaultData: ReturnType<typeof api.initUploadParts> = Promise.resolve({ |
| 130 | + reqId: 'req-id', |
| 131 | + data: { uploadId: '60878b9408bc044043f5d74f', expireAt: 1620100628 } |
| 132 | + }) |
| 133 | + |
| 134 | + return this.callInterceptor('initUploadParts', defaultData) |
| 135 | + } |
| 136 | + |
| 137 | + public deleteUploadedChunks(): ReturnType<typeof api.deleteUploadedChunks> { |
| 138 | + const defaultData: ReturnType<typeof api.deleteUploadedChunks> = Promise.resolve({ |
| 139 | + reqId: 'req-id', |
| 140 | + data: undefined |
| 141 | + }) |
| 142 | + |
| 143 | + return this.callInterceptor('deleteUploadedChunks', defaultData) |
| 144 | + } |
| 145 | +} |
0 commit comments