@cloudbase/storage 2.0.4-alpha.0 → 2.5.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { constants, utils, helpers } from '@cloudbase/utilities';
2
- import { ICloudbase } from '@cloudbase/types';
3
- import { ICloudbaseComponent } from '@cloudbase/types/component';
1
+ import { constants, utils, helpers } from '@cloudbase/utilities'
2
+ import { ICloudbase } from '@cloudbase/types'
3
+ import { ICloudbaseComponent } from '@cloudbase/types/component'
4
4
  import {
5
5
  ICloudbaseFileMetaDataRes,
6
6
  ICloudbaseFileInfo,
@@ -12,27 +12,27 @@ import {
12
12
  ICloudbaseGetTempFileURLResult,
13
13
  ICloudbaseGetTempFileURLParams,
14
14
  ICloudbaseDownloadFileResult,
15
- ICloudbaseDownloadFileParams
16
- } from '@cloudbase/types/storage';
15
+ ICloudbaseDownloadFileParams,
16
+ } from '@cloudbase/types/storage'
17
17
 
18
- declare const cloudbase: ICloudbase;
18
+ declare const cloudbase: ICloudbase
19
19
 
20
20
  enum EUploadMethod {
21
21
  put = 'put',
22
22
  post = 'post'
23
23
  }
24
24
 
25
- const { getSdkName, ERRORS, COMMUNITY_SITE_URL } = constants;
26
- const { isArray, isString, isPalinObject, execCallback } = utils;
27
- const { catchErrorsDecorator } = helpers;
25
+ const { getSdkName, ERRORS, COMMUNITY_SITE_URL } = constants
26
+ const { isArray, isString, isPalinObject, execCallback } = utils
27
+ const { catchErrorsDecorator } = helpers
28
28
 
29
- const COMPONENT_NAME = 'storage';
29
+ const COMPONENT_NAME = 'storage'
30
30
 
31
31
  class CloudbaseStorage {
32
32
  @catchErrorsDecorator({
33
33
  customInfo: {
34
34
  className: 'Cloudbase',
35
- methodName: 'uploadFile'
35
+ methodName: 'uploadFile',
36
36
  },
37
37
  title: '上传文件失败',
38
38
  messages: [
@@ -40,45 +40,46 @@ class CloudbaseStorage {
40
40
  ' 1 - 调用 uploadFile() 的语法或参数是否正确',
41
41
  ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety',
42
42
  ' 3 - 云存储安全规则是否限制了当前登录状态访问',
43
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
44
- ]
43
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
44
+ ],
45
45
  })
46
46
  public async uploadFile(
47
47
  params: ICloudbaseUploadFileParams,
48
48
  callback?: Function
49
49
  ): Promise<ICloudbaseUploadFileResult> {
50
- const { cloudPath, filePath, onUploadProgress, method = 'put', headers = {} } = params;
50
+ const { cloudPath, filePath, onUploadProgress, method = 'put', headers = {} } = params
51
51
  if (!isString(cloudPath) || !filePath) {
52
52
  throw new Error(JSON.stringify({
53
53
  code: ERRORS.INVALID_PARAMS,
54
- msg: `[${COMPONENT_NAME}.uploadFile] invalid params`
55
- }));
54
+ msg: `[${COMPONENT_NAME}.uploadFile] invalid params`,
55
+ }))
56
56
  }
57
- const uploadMethod = { put: EUploadMethod.put, post: EUploadMethod.post }[method.toLocaleLowerCase()] || EUploadMethod.put;
57
+ const uploadMethod = { put: EUploadMethod.put, post: EUploadMethod.post }[method.toLocaleLowerCase()] || EUploadMethod.put
58
58
 
59
- const action = 'storage.getUploadMetadata';
59
+ const action = 'storage.getUploadMetadata'
60
60
  // @ts-ignore
61
- const request = this.request;
61
+ const { request } = this
62
62
  const metaDataParam = {
63
63
  path: cloudPath,
64
64
  method: uploadMethod,
65
65
  }
66
66
  if (uploadMethod === EUploadMethod.put) {
67
- metaDataParam["headers"] = headers;
67
+ /* eslint-disable-next-line */
68
+ metaDataParam["headers"] = headers
68
69
  }
69
- const metaData: ICloudbaseFileMetaDataRes = await request.send(action, metaDataParam);
70
+ const metaData: ICloudbaseFileMetaDataRes = await request.send(action, metaDataParam)
70
71
 
71
72
  const {
72
- data: { url, authorization, token, fileId, cosFileId, download_url },
73
- requestId
74
- } = metaData;
73
+ data: { url, authorization, token, fileId, cosFileId, download_url: downloadUrl },
74
+ requestId,
75
+ } = metaData
75
76
 
76
77
  const commonParams = {
77
78
  url,
78
79
  file: filePath,
79
80
  name: cloudPath,
80
- onUploadProgress
81
- };
81
+ onUploadProgress,
82
+ }
82
83
 
83
84
  const putParams = {
84
85
  ...commonParams,
@@ -88,8 +89,8 @@ class CloudbaseStorage {
88
89
  authorization,
89
90
  'x-cos-meta-fileid': cosFileId,
90
91
  'x-cos-security-token': token,
91
- }
92
- };
92
+ },
93
+ }
93
94
 
94
95
  const postParams = {
95
96
  ...commonParams,
@@ -98,38 +99,37 @@ class CloudbaseStorage {
98
99
  key: cloudPath,
99
100
  signature: authorization,
100
101
  'x-cos-meta-fileid': cosFileId,
101
- 'success_action_status': '201',
102
- 'x-cos-security-token': token
103
- }
104
- };
102
+ success_action_status: '201',
103
+ 'x-cos-security-token': token,
104
+ },
105
+ }
105
106
 
106
107
  const uploadConfig = {
107
108
  [EUploadMethod.put]: {
108
109
  params: putParams,
109
- successCode: 200,
110
+ isSuccess: (code: number) => code >= 200 && code < 300,
110
111
  },
111
112
  [EUploadMethod.post]: {
112
113
  params: postParams,
113
- successCode: 201,
114
- }
114
+ isSuccess: (code: number) => code === 201,
115
+ },
115
116
  }
116
117
 
117
- const res = await request.upload(uploadConfig[uploadMethod].params);
118
+ const res = await request.upload(uploadConfig[uploadMethod].params)
118
119
 
119
- if (res.statusCode === uploadConfig[uploadMethod].successCode) {
120
+ if (uploadConfig[uploadMethod].isSuccess(res.statusCode)) {
120
121
  return execCallback(callback, null, {
121
122
  fileID: fileId,
122
- download_url,
123
- requestId
124
- });
125
- } else {
126
- return execCallback(callback, new Error(`[${getSdkName()}][${ERRORS.OPERATION_FAIL}][${COMPONENT_NAME}]:${res.data}`));
123
+ download_url: downloadUrl,
124
+ requestId,
125
+ })
127
126
  }
127
+ return execCallback(callback, new Error(`[${getSdkName()}][${ERRORS.OPERATION_FAIL}][${COMPONENT_NAME}]:${res.data}`))
128
128
  }
129
129
  @catchErrorsDecorator({
130
130
  customInfo: {
131
131
  className: 'Cloudbase',
132
- methodName: 'getUploadMetadata'
132
+ methodName: 'getUploadMetadata',
133
133
  },
134
134
  title: '获取上传元信息失败',
135
135
  messages: [
@@ -137,37 +137,37 @@ class CloudbaseStorage {
137
137
  ' 1 - 调用 getUploadMetadata() 的语法或参数是否正确',
138
138
  ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety',
139
139
  ' 3 - 云存储安全规则是否限制了当前登录状态访问',
140
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
141
- ]
140
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
141
+ ],
142
142
  })
143
143
  public async getUploadMetadata(
144
144
  params: ICloudbaseGetUploadMetadataParams,
145
145
  callback?: Function
146
146
  ): Promise<any> {
147
- const { cloudPath } = params;
147
+ const { cloudPath } = params
148
148
  if (!isString(cloudPath)) {
149
149
  throw new Error(JSON.stringify({
150
150
  code: ERRORS.INVALID_PARAMS,
151
- msg: `[${COMPONENT_NAME}.getUploadMetadata] invalid cloudPath`
152
- }));
151
+ msg: `[${COMPONENT_NAME}.getUploadMetadata] invalid cloudPath`,
152
+ }))
153
153
  }
154
154
  // @ts-ignore
155
- const request = this.request;
156
- const action = 'storage.getUploadMetadata';
155
+ const { request } = this
156
+ const action = 'storage.getUploadMetadata'
157
157
 
158
158
  try {
159
159
  const metaData = await request.send(action, {
160
- path: cloudPath
161
- });
162
- return execCallback(callback, null, metaData);
160
+ path: cloudPath,
161
+ })
162
+ return execCallback(callback, null, metaData)
163
163
  } catch (err) {
164
- return execCallback(callback, err);
164
+ return execCallback(callback, err)
165
165
  }
166
166
  }
167
167
  @catchErrorsDecorator({
168
168
  customInfo: {
169
169
  className: 'Cloudbase',
170
- methodName: 'deleteFile'
170
+ methodName: 'deleteFile',
171
171
  },
172
172
  title: '删除文件失败',
173
173
  messages: [
@@ -175,51 +175,51 @@ class CloudbaseStorage {
175
175
  ' 1 - 调用 deleteFile() 的语法或参数是否正确',
176
176
  ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety',
177
177
  ' 3 - 云存储安全规则是否限制了当前登录状态访问',
178
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
179
- ]
178
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
179
+ ],
180
180
  })
181
181
  public async deleteFile(
182
182
  params: ICloudbaseDeleteFileParams,
183
183
  callback?: Function
184
184
  ): Promise<ICloudbaseDeleteFileResult> {
185
- const { fileList } = params;
185
+ const { fileList } = params
186
186
 
187
187
  if (!fileList || !isArray(fileList) || fileList.length === 0) {
188
188
  throw new Error(JSON.stringify({
189
189
  code: ERRORS.INVALID_PARAMS,
190
- msg: `[${COMPONENT_NAME}.deleteFile] fileList must not be empty`
191
- }));
190
+ msg: `[${COMPONENT_NAME}.deleteFile] fileList must not be empty`,
191
+ }))
192
192
  }
193
193
 
194
194
  for (const fileId of fileList) {
195
195
  if (!fileId || !isString(fileId)) {
196
196
  throw new Error(JSON.stringify({
197
197
  code: ERRORS.INVALID_PARAMS,
198
- msg: `[${COMPONENT_NAME}.deleteFile] fileID must be string`
199
- }));
198
+ msg: `[${COMPONENT_NAME}.deleteFile] fileID must be string`,
199
+ }))
200
200
  }
201
201
  }
202
202
 
203
- const action = 'storage.batchDeleteFile';
203
+ const action = 'storage.batchDeleteFile'
204
204
  // @ts-ignore
205
- const request = this.request;
205
+ const { request } = this
206
206
  const res = await request.send(action, {
207
- fileid_list: fileList
208
- });
207
+ fileid_list: fileList,
208
+ })
209
209
 
210
210
  if (res.code) {
211
- return execCallback(callback, null, res);
211
+ return execCallback(callback, null, res)
212
212
  }
213
213
  const data = {
214
214
  fileList: res.data.delete_list,
215
- requestId: res.requestId
216
- };
217
- return execCallback(callback, null, data);;
215
+ requestId: res.requestId,
216
+ }
217
+ return execCallback(callback, null, data)
218
218
  }
219
219
  @catchErrorsDecorator({
220
220
  customInfo: {
221
221
  className: 'Cloudbase',
222
- methodName: 'getTempFileURL'
222
+ methodName: 'getTempFileURL',
223
223
  },
224
224
  title: '获取文件下载链接',
225
225
  messages: [
@@ -227,67 +227,68 @@ class CloudbaseStorage {
227
227
  ' 1 - 调用 getTempFileURL() 的语法或参数是否正确',
228
228
  ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety',
229
229
  ' 3 - 云存储安全规则是否限制了当前登录状态访问',
230
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
231
- ]
230
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
231
+ ],
232
232
  })
233
233
  public async getTempFileURL(
234
234
  params: ICloudbaseGetTempFileURLParams,
235
235
  callback?: Function
236
236
  ): Promise<ICloudbaseGetTempFileURLResult> {
237
- const { fileList } = params;
237
+ const { fileList } = params
238
238
 
239
239
  if (!fileList || !isArray(fileList) || fileList.length === 0) {
240
240
  throw new Error(JSON.stringify({
241
241
  code: ERRORS.INVALID_PARAMS,
242
- msg: `[${COMPONENT_NAME}.getTempFileURL] fileList must not be empty`
243
- }));
242
+ msg: `[${COMPONENT_NAME}.getTempFileURL] fileList must not be empty`,
243
+ }))
244
244
  }
245
245
 
246
- const file_list = [];
246
+ const convertedFileList = []
247
247
  for (const file of fileList) {
248
248
  if (isPalinObject(file)) {
249
- if (!file.hasOwnProperty('fileID') || !file.hasOwnProperty('maxAge')) {
249
+ if (!Object.prototype.hasOwnProperty.call(file, 'fileID')
250
+ || !Object.prototype.hasOwnProperty.call(file, 'maxAge')) {
250
251
  throw new Error(JSON.stringify({
251
252
  code: ERRORS.INVALID_PARAMS,
252
- msg: `[${COMPONENT_NAME}.getTempFileURL] file info must include fileID and maxAge`
253
- }));
253
+ msg: `[${COMPONENT_NAME}.getTempFileURL] file info must include fileID and maxAge`,
254
+ }))
254
255
  }
255
256
 
256
- file_list.push({
257
+ convertedFileList.push({
257
258
  fileid: (file as ICloudbaseFileInfo).fileID,
258
- max_age: (file as ICloudbaseFileInfo).maxAge
259
- });
259
+ max_age: (file as ICloudbaseFileInfo).maxAge,
260
+ })
260
261
  } else if (isString(file)) {
261
- file_list.push({
262
- fileid: file
263
- });
262
+ convertedFileList.push({
263
+ fileid: file,
264
+ })
264
265
  } else {
265
266
  throw new Error(JSON.stringify({
266
267
  code: ERRORS.INVALID_PARAMS,
267
- msg: `[${COMPONENT_NAME}.getTempFileURL] invalid fileList`
268
- }));
268
+ msg: `[${COMPONENT_NAME}.getTempFileURL] invalid fileList`,
269
+ }))
269
270
  }
270
271
  }
271
272
 
272
- const action = 'storage.batchGetDownloadUrl';
273
+ const action = 'storage.batchGetDownloadUrl'
273
274
  // @ts-ignore
274
- const request = this.request;
275
+ const { request } = this
275
276
 
276
- const res = await request.send(action, { file_list });
277
+ const res = await request.send(action, { file_list: convertedFileList })
277
278
 
278
279
  if (res.code) {
279
- return execCallback(callback, null, res);
280
+ return execCallback(callback, null, res)
280
281
  }
281
282
 
282
283
  return execCallback(callback, null, {
283
284
  fileList: res.data.download_list,
284
- requestId: res.requestId
285
- });
285
+ requestId: res.requestId,
286
+ })
286
287
  }
287
288
  @catchErrorsDecorator({
288
289
  customInfo: {
289
290
  className: 'Cloudbase',
290
- methodName: 'downloadFile'
291
+ methodName: 'downloadFile',
291
292
  },
292
293
  title: '下载文件失败',
293
294
  messages: [
@@ -295,44 +296,44 @@ class CloudbaseStorage {
295
296
  ' 1 - 调用 downloadFile() 的语法或参数是否正确',
296
297
  ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety',
297
298
  ' 3 - 云存储安全规则是否限制了当前登录状态访问',
298
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
299
- ]
299
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
300
+ ],
300
301
  })
301
302
  public async downloadFile(
302
303
  params: ICloudbaseDownloadFileParams,
303
304
  callback?: Function
304
305
  ): Promise<ICloudbaseDownloadFileResult> {
305
- const { fileID } = params;
306
+ const { fileID } = params
306
307
  if (!isString(fileID)) {
307
308
  throw new Error(JSON.stringify({
308
309
  code: ERRORS.INVALID_PARAMS,
309
- msg: `[${COMPONENT_NAME}.getTempFileURL] fileID must be string`
310
- }));
310
+ msg: `[${COMPONENT_NAME}.getTempFileURL] fileID must be string`,
311
+ }))
311
312
  }
312
313
 
313
314
  const tmpUrlRes = await this.getTempFileURL.call(this, {
314
315
  fileList: [{
315
316
  fileID,
316
- maxAge: 600
317
- }]
318
- });
317
+ maxAge: 600,
318
+ }],
319
+ })
319
320
 
320
- const res = tmpUrlRes.fileList[0];
321
+ const res = tmpUrlRes.fileList[0]
321
322
 
322
323
  if (res.code !== 'SUCCESS') {
323
- return execCallback(callback, res);
324
+ return execCallback(callback, res)
324
325
  }
325
326
  // @ts-ignore
326
- const request = this.request;
327
+ const { request } = this
327
328
 
328
- const tmpUrl = encodeURI(res.download_url);
329
+ const tmpUrl = encodeURI(res.download_url)
329
330
 
330
- const result = await request.download({ url: tmpUrl });
331
- return execCallback(callback, null, result);
331
+ const result = await request.download({ url: tmpUrl })
332
+ return execCallback(callback, null, result)
332
333
  }
333
334
  }
334
335
 
335
- const cloudbaseStorage = new CloudbaseStorage();
336
+ const cloudbaseStorage = new CloudbaseStorage()
336
337
  const component: ICloudbaseComponent = {
337
338
  name: COMPONENT_NAME,
338
339
  entity: {
@@ -340,18 +341,18 @@ const component: ICloudbaseComponent = {
340
341
  deleteFile: cloudbaseStorage.deleteFile,
341
342
  getTempFileURL: cloudbaseStorage.getTempFileURL,
342
343
  downloadFile: cloudbaseStorage.downloadFile,
343
- getUploadMetadata: cloudbaseStorage.getUploadMetadata
344
- }
344
+ getUploadMetadata: cloudbaseStorage.getUploadMetadata,
345
+ },
345
346
  }
346
347
 
347
348
  try {
348
- cloudbase.registerComponent(component);
349
+ cloudbase.registerComponent(component)
349
350
  } catch (e) { }
350
351
 
351
352
  export function registerStorage(app: Pick<ICloudbase, 'registerComponent'>) {
352
353
  try {
353
- app.registerComponent(component);
354
+ app.registerComponent(component)
354
355
  } catch (e) {
355
- console.warn(e);
356
+ console.warn(e)
356
357
  }
357
- }
358
+ }
package/.eslintrc DELETED
@@ -1,29 +0,0 @@
1
- {
2
- "extends": [
3
- "eslint-config-alloy/typescript"
4
- ],
5
- "rules": {
6
- "indent": [
7
- "error",
8
- 2,
9
- {
10
- "SwitchCase": 1,
11
- "flatTernaryExpressions": true
12
- }
13
- ],
14
- "guard-for-in": 0,
15
- "no-param-reassign": 0,
16
- "no-undefined": 0,
17
- "@typescript-eslint/explicit-member-accessibility": 0,
18
- "@typescript-eslint/no-invalid-this": 0,
19
- "@typescript-eslint/no-loss-of-precision": 0,
20
- "@typescript-eslint/no-duplicate-imports": 0
21
- },
22
- "parserOptions": {
23
- "ecmaVersion": 6,
24
- "sourceType": "module",
25
- "ecmaFeatures": {
26
- "modules": true
27
- }
28
- }
29
- }