@alipay/faas-common-sdk 1.1.17 → 1.1.18-alpha.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/esm/Storage.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CreateStorageOptions, DeleteFileParam, DeleteFileResult, DownloadFileParam, FileUploaderResult, GetTempFileURLParam, GetTempFileURLResult, UploadFileParam, UploadFileResult } from './types';
1
+ import { CreateStorageOptions, DeleteFileParam, DeleteFileResult, DownloadFileParam, FileUploaderResult, GetTempFileURLParam, GetTempFileURLResult, GetUploadFileURLParam, GetUploadFileURLResult, UploadFileParam, UploadFileResult } from './types';
2
2
  export interface GetDownloadFileRawResult {
3
3
  file_list: Array<{
4
4
  file_id: string;
@@ -26,6 +26,7 @@ export declare class Storage<FileContent, DownloadFileResult extends FileUploade
26
26
  private checkFileID;
27
27
  private formatFileID;
28
28
  private getCloudPathFromFileID;
29
+ getUploadFileURL(param: string | GetUploadFileURLParam): Promise<GetUploadFileURLResult>;
29
30
  uploadFile(param: UploadFileParam<FileContent>): Promise<UploadFileResult>;
30
31
  downloadFile(param: DownloadFileParam): Promise<DownloadFileResult>;
31
32
  deleteFile(param: DeleteFileParam): Promise<DeleteFileResult>;
package/esm/Storage.js CHANGED
@@ -1 +1 @@
1
- var w=function(){w=Object.assign||function(a){for(var e,r=1,o=arguments.length;r<o;r++){e=arguments[r];for(var n in e)if(Object.prototype.hasOwnProperty.call(e,n))a[n]=e[n]}return a};return w.apply(this,arguments)};var I=function(a,e,r,o){function n(t){return t instanceof r?t:new r(function(s){s(t)})}return new(r||(r=Promise))(function(t,s){function l(h){try{i(o.next(h))}catch(d){s(d)}}function u(h){try{i(o["throw"](h))}catch(d){s(d)}}function i(h){h.done?t(h.value):n(h.value).then(l,u)}i((o=o.apply(a,e||[])).next())})};var R=function(a,e){var r={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},o,n,t,s;return s={next:l(0),"throw":l(1),"return":l(2)},typeof Symbol==="function"&&(s[Symbol.iterator]=function(){return this}),s;function l(i){return function(h){return u([i,h])}}function u(i){if(o)throw new TypeError("Generator is already executing.");while(s&&(s=0,i[0]&&(r=0)),r)try{if(o=1,n&&(t=i[0]&2?n["return"]:i[0]?n["throw"]||((t=n["return"])&&t.call(n),0):n.next)&&!(t=t.call(n,i[1])).done)return t;if(n=0,t)i=[i[0]&2,t.value];switch(i[0]){case 0:case 1:t=i;break;case 4:r.label++;return{value:i[1],done:false};case 5:r.label++;n=i[1];i=[0];continue;case 7:i=r.ops.pop();r.trys.pop();continue;default:if(!(t=r.trys,t=t.length>0&&t[t.length-1])&&(i[0]===6||i[0]===2)){r=0;continue}if(i[0]===3&&(!t||i[1]>t[0]&&i[1]<t[3])){r.label=i[1];break}if(i[0]===6&&r.label<t[1]){r.label=t[1];t=i;break}if(t&&r.label<t[2]){r.label=t[2];r.ops.push(i);break}if(t[2])r.ops.pop();r.trys.pop();continue}i=e.call(a,r)}catch(h){i=[6,h];n=0}finally{o=t=0}if(i[0]&5)throw i[1];return{value:i[0]?i[1]:void 0,done:true}}};import{FaasError as f}from"./error";import{ResponseUtil as _}from"./utils";var y=function(){function a(e){this.options=e}a.prototype.getUploadPath=function(e){return"/".concat(e,"?upload_url")};a.prototype.getDownloadPath=function(e){return"/".concat(e,"?download_url&expire=3600")};a.prototype.getBatchDeletePath=function(){return"/?delete"};a.prototype.getBatchDownloadPath=function(){return"/?download_url"};a.prototype.checkCloudPath=function(e){if(typeof e!=="string")return"\u5FC5\u987B\u4E3A\u5B57\u7B26\u4E32";e=e.trim();if(!e)return"\u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32";if(e.startsWith("/"))return"\u4E0D\u80FD\u4EE5 / \u5F00\u5934";if(e.includes("//"))return"\u4E0D\u80FD\u5305\u542B\u8FDE\u7EED\u7684 /";if(e.length>850)return"\u957F\u5EA6\u4E0D\u80FD\u8D85\u8FC7 850 \u5B57\u8282"};a.prototype.checkFileID=function(e){if(typeof e!=="string")return"\u5FC5\u987B\u4E3A\u5B57\u7B26\u4E32";e=e.trim();if(!e)return"\u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32"};a.prototype.formatFileID=function(e){return"cloud://".concat(this.options.toEnvId,"/").concat(e.replace(/^\/+/,""))};a.prototype.getCloudPathFromFileID=function(e){var r=e.trim().replace(/^cloud:\/\//,"");var o=r.indexOf("/");if(o<=0){throw f.INVALID_PARAM_ERR("fileID","\u4E0D\u5408\u6CD5")}var n=r.substring(0,o);var t=r.substring(o+1);if(n!==this.options.toEnvId){console.warn("file ".concat(e," does not belong to env ").concat(this.options.toEnvId))}return t};a.prototype.uploadFile=function(e){return I(this,void 0,void 0,function(){var r,o,n,t,s,l;return R(this,function(u){switch(u.label){case 0:r=this.checkCloudPath(e.cloudPath);if(r){throw f.INVALID_PARAM_ERR("cloudPath",r)}o=this.getUploadPath(e.cloudPath.trim());return[4,this.request({method:"GET",path:o})];case 1:n=u.sent();t=_.handleResponse(n);s=this.formatFileID(t.file_id);return[4,this.options.fileUploader.upload({fileID:s,uploadURL:t.upload_url,fileContent:e.fileContent})];case 2:l=u.sent();if(l.statusCode!==200){throw f.STORAGE_ERR(-1,"\u4E0A\u4F20\u6587\u4EF6 ".concat(t.upload_url," \u5931\u8D25"),n.requestID)}return[2,{fileID:s,statusCode:l.statusCode}]}})})};a.prototype.downloadFile=function(e){return I(this,void 0,void 0,function(){var r,o,n,t,s,l;return R(this,function(u){switch(u.label){case 0:r=this.checkFileID(e.fileID);if(r){throw f.INVALID_PARAM_ERR("fileID",r)}o=this.getCloudPathFromFileID(e.fileID);n=this.getDownloadPath(o);return[4,this.request({method:"GET",path:n})];case 1:t=u.sent();s=_.handleResponse(t);return[4,this.options.fileUploader.download({fileID:e.fileID,downloadURL:s.download_url})];case 2:l=u.sent();if(l.statusCode!==200){throw f.STORAGE_ERR(-1,"\u4E0B\u8F7D\u6587\u4EF6 ".concat(s.download_url," \u5931\u8D25"),t.requestID)}return[2,l]}})})};a.prototype.deleteFile=function(e){return I(this,void 0,void 0,function(){var r,o,n,t,s,l,u,i,h,d;var g=this;return R(this,function(p){switch(p.label){case 0:r=Array.isArray(e)?e:e.fileList;if(!Array.isArray(r)||r.length===0){throw f.INVALID_PARAM_ERR("fileList","\u4E0D\u80FD\u4E3A\u7A7A\u6570\u7EC4")}o=[];for(n=0,t=r;n<t.length;n++){s=t[n];l=this.checkFileID(s);if(l){throw f.INVALID_PARAM_ERR("fileID",l)}u=this.getCloudPathFromFileID(s);o.push(u)}i=this.getBatchDeletePath();return[4,this.request({method:"POST",path:i,data:{file_list:o}})];case 1:h=p.sent();d=_.handleResponse(h);return[2,{fileList:d.file_list.map(function(c){return{fileID:g.formatFileID(c.file_id),status:c.status?0:-1,errMsg:c.status?"ok":c.result_message}})}]}})})};a.prototype.getTempFileURL=function(e){return I(this,void 0,void 0,function(){var r,o,n,t,s,l,u,i,h,d;var g=this;return R(this,function(p){switch(p.label){case 0:r=Array.isArray(e)?e:e.fileList;if(!Array.isArray(r)||r.length===0){throw f.INVALID_PARAM_ERR("fileList","\u4E0D\u80FD\u4E3A\u7A7A\u6570\u7EC4")}if(r.length>50){throw f.INVALID_PARAM_ERR("fileList","\u6700\u5927\u957F\u5EA6 50")}o=[];for(n=0,t=r;n<t.length;n++){s=t[n];l=this.checkFileID(s);if(l){throw f.INVALID_PARAM_ERR("fileID",l)}u=this.getCloudPathFromFileID(s);o.push({file_id:u,expire:600})}i=this.getBatchDownloadPath();return[4,this.request({method:"POST",path:i,data:{file_list:o}})];case 1:h=p.sent();d=_.handleResponse(h);return[2,{fileList:d.file_list.map(function(c){return{fileID:g.formatFileID(c.file_id),tempFileURL:c.download_url,status:c.status?0:-1,errMsg:c.status?"ok":c.result_message}})}]}})})};a.prototype.request=function(e){var r=e.method,o=e.path,n=e.data,t=e.headers;return I(this,void 0,void 0,function(){return R(this,function(s){switch(s.label){case 0:return[4,this.options.httpclient.request({method:r,path:o,data:n,headers:w({"x-alipay-cloud-mode":"oss","x-data-api-type":"oss","x-expire-timestamp":"".concat(Date.now()+6e4)},t)})];case 1:return[2,s.sent()]}})})};return a}();export{y as Storage};
1
+ var w=function(){w=Object.assign||function(l){for(var e,r=1,o=arguments.length;r<o;r++){e=arguments[r];for(var i in e)if(Object.prototype.hasOwnProperty.call(e,i))l[i]=e[i]}return l};return w.apply(this,arguments)};var p=function(l,e,r,o){function i(t){return t instanceof r?t:new r(function(s){s(t)})}return new(r||(r=Promise))(function(t,s){function a(u){try{n(o.next(u))}catch(d){s(d)}}function h(u){try{n(o["throw"](u))}catch(d){s(d)}}function n(u){u.done?t(u.value):i(u.value).then(a,h)}n((o=o.apply(l,e||[])).next())})};var I=function(l,e){var r={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},o,i,t,s;return s={next:a(0),"throw":a(1),"return":a(2)},typeof Symbol==="function"&&(s[Symbol.iterator]=function(){return this}),s;function a(n){return function(u){return h([n,u])}}function h(n){if(o)throw new TypeError("Generator is already executing.");while(s&&(s=0,n[0]&&(r=0)),r)try{if(o=1,i&&(t=n[0]&2?i["return"]:n[0]?i["throw"]||((t=i["return"])&&t.call(i),0):i.next)&&!(t=t.call(i,n[1])).done)return t;if(i=0,t)n=[n[0]&2,t.value];switch(n[0]){case 0:case 1:t=n;break;case 4:r.label++;return{value:n[1],done:false};case 5:r.label++;i=n[1];n=[0];continue;case 7:n=r.ops.pop();r.trys.pop();continue;default:if(!(t=r.trys,t=t.length>0&&t[t.length-1])&&(n[0]===6||n[0]===2)){r=0;continue}if(n[0]===3&&(!t||n[1]>t[0]&&n[1]<t[3])){r.label=n[1];break}if(n[0]===6&&r.label<t[1]){r.label=t[1];t=n;break}if(t&&r.label<t[2]){r.label=t[2];r.ops.push(n);break}if(t[2])r.ops.pop();r.trys.pop();continue}n=e.call(l,r)}catch(u){n=[6,u];i=0}finally{o=t=0}if(n[0]&5)throw n[1];return{value:n[0]?n[1]:void 0,done:true}}};import{FaasError as c}from"./error";import{ResponseUtil as D}from"./utils";var y=function(){function l(e){this.options=e}l.prototype.getUploadPath=function(e){return"/".concat(e,"?upload_url")};l.prototype.getDownloadPath=function(e){return"/".concat(e,"?download_url&expire=3600")};l.prototype.getBatchDeletePath=function(){return"/?delete"};l.prototype.getBatchDownloadPath=function(){return"/?download_url"};l.prototype.checkCloudPath=function(e){if(typeof e!=="string")return"\u5FC5\u987B\u4E3A\u5B57\u7B26\u4E32";e=e.trim();if(!e)return"\u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32";if(e.startsWith("/"))return"\u4E0D\u80FD\u4EE5 / \u5F00\u5934";if(e.includes("//"))return"\u4E0D\u80FD\u5305\u542B\u8FDE\u7EED\u7684 /";if(e.length>850)return"\u957F\u5EA6\u4E0D\u80FD\u8D85\u8FC7 850 \u5B57\u8282"};l.prototype.checkFileID=function(e){if(typeof e!=="string")return"\u5FC5\u987B\u4E3A\u5B57\u7B26\u4E32";e=e.trim();if(!e)return"\u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32"};l.prototype.formatFileID=function(e){return"cloud://".concat(this.options.toEnvId,"/").concat(e.replace(/^\/+/,""))};l.prototype.getCloudPathFromFileID=function(e){var r=e.trim().replace(/^cloud:\/\//,"");var o=r.indexOf("/");if(o<=0){throw c.INVALID_PARAM_ERR("fileID","\u4E0D\u5408\u6CD5")}var i=r.substring(0,o);var t=r.substring(o+1);if(i!==this.options.toEnvId){console.warn("file ".concat(e," does not belong to env ").concat(this.options.toEnvId))}return t};l.prototype.getUploadFileURL=function(e){return p(this,void 0,void 0,function(){var r,o,i,t,s,a;return I(this,function(h){switch(h.label){case 0:r=typeof e==="string"?e:e.cloudPath;o=this.checkCloudPath(r);if(o){throw c.INVALID_PARAM_ERR("cloudPath",o)}i=this.getUploadPath(r.trim());return[4,this.request({method:"GET",path:i})];case 1:t=h.sent();s=D.handleResponse(t);a=this.formatFileID(s.file_id);return[2,{fileID:a,uploadURL:s.upload_url,requestID:t.requestID}]}})})};l.prototype.uploadFile=function(e){return p(this,void 0,void 0,function(){var r,o,i,t,s;return I(this,function(a){switch(a.label){case 0:return[4,this.getUploadFileURL(e)];case 1:r=a.sent(),o=r.fileID,i=r.uploadURL,t=r.requestID;return[4,this.options.fileUploader.upload({fileID:o,uploadURL:i,fileContent:e.fileContent})];case 2:s=a.sent();if(s.statusCode!==200){throw c.STORAGE_ERR(60004,"\u4E0A\u4F20\u6587\u4EF6 ".concat(i," \u5931\u8D25"),t)}return[2,{fileID:o,statusCode:s.statusCode}]}})})};l.prototype.downloadFile=function(e){return p(this,void 0,void 0,function(){var r,o,i,t,s,a;return I(this,function(h){switch(h.label){case 0:r=this.checkFileID(e.fileID);if(r){throw c.INVALID_PARAM_ERR("fileID",r)}o=this.getCloudPathFromFileID(e.fileID);i=this.getDownloadPath(o);return[4,this.request({method:"GET",path:i})];case 1:t=h.sent();s=D.handleResponse(t);return[4,this.options.fileUploader.download({fileID:e.fileID,downloadURL:s.download_url})];case 2:a=h.sent();if(a.statusCode!==200){throw c.STORAGE_ERR(60004,"\u4E0B\u8F7D\u6587\u4EF6 ".concat(s.download_url," \u5931\u8D25"),t.requestID)}return[2,a]}})})};l.prototype.deleteFile=function(e){return p(this,void 0,void 0,function(){var r,o,i,t,s,a,h,n,u,d;var g=this;return I(this,function(R){switch(R.label){case 0:r=Array.isArray(e)?e:e.fileList;if(!Array.isArray(r)||r.length===0){throw c.INVALID_PARAM_ERR("fileList","\u4E0D\u80FD\u4E3A\u7A7A\u6570\u7EC4")}o=[];for(i=0,t=r;i<t.length;i++){s=t[i];a=this.checkFileID(s);if(a){throw c.INVALID_PARAM_ERR("fileID",a)}h=this.getCloudPathFromFileID(s);o.push(h)}n=this.getBatchDeletePath();return[4,this.request({method:"POST",path:n,data:{file_list:o}})];case 1:u=R.sent();d=D.handleResponse(u);return[2,{fileList:d.file_list.map(function(f){return{fileID:g.formatFileID(f.file_id),status:f.status?0:-1,errMsg:f.status?"ok":f.result_message}})}]}})})};l.prototype.getTempFileURL=function(e){return p(this,void 0,void 0,function(){var r,o,i,t,s,a,h,n,u,d;var g=this;return I(this,function(R){switch(R.label){case 0:r=Array.isArray(e)?e:e.fileList;if(!Array.isArray(r)||r.length===0){throw c.INVALID_PARAM_ERR("fileList","\u4E0D\u80FD\u4E3A\u7A7A\u6570\u7EC4")}if(r.length>50){throw c.INVALID_PARAM_ERR("fileList","\u6700\u5927\u957F\u5EA6 50")}o=[];for(i=0,t=r;i<t.length;i++){s=t[i];a=this.checkFileID(s);if(a){throw c.INVALID_PARAM_ERR("fileID",a)}h=this.getCloudPathFromFileID(s);o.push({file_id:h,expire:600})}n=this.getBatchDownloadPath();return[4,this.request({method:"POST",path:n,data:{file_list:o}})];case 1:u=R.sent();d=D.handleResponse(u);return[2,{fileList:d.file_list.map(function(f){return{fileID:g.formatFileID(f.file_id),tempFileURL:f.download_url,status:f.status?0:-1,errMsg:f.status?"ok":f.result_message}})}]}})})};l.prototype.request=function(e){var r=e.method,o=e.path,i=e.data,t=e.headers;return p(this,void 0,void 0,function(){return I(this,function(s){switch(s.label){case 0:return[4,this.options.httpclient.request({method:r,path:o,data:i,headers:w({"x-alipay-cloud-mode":"oss","x-data-api-type":"oss","x-expire-timestamp":"".concat(Date.now()+6e4)},t)})];case 1:return[2,s.sent()]}})})};return l}();export{y as Storage};
package/esm/types.d.ts CHANGED
@@ -115,6 +115,14 @@ export interface FileInfo {
115
115
  export interface TempFileInfo extends FileInfo {
116
116
  tempFileURL: string;
117
117
  }
118
+ export interface GetUploadFileURLParam {
119
+ cloudPath: string;
120
+ }
121
+ export interface GetUploadFileURLResult {
122
+ fileID: string;
123
+ uploadURL: string;
124
+ requestID: string;
125
+ }
118
126
  export interface UploadFileParam<T> {
119
127
  cloudPath: string;
120
128
  fileContent: T;
package/lib/Storage.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CreateStorageOptions, DeleteFileParam, DeleteFileResult, DownloadFileParam, FileUploaderResult, GetTempFileURLParam, GetTempFileURLResult, UploadFileParam, UploadFileResult } from './types';
1
+ import { CreateStorageOptions, DeleteFileParam, DeleteFileResult, DownloadFileParam, FileUploaderResult, GetTempFileURLParam, GetTempFileURLResult, GetUploadFileURLParam, GetUploadFileURLResult, UploadFileParam, UploadFileResult } from './types';
2
2
  export interface GetDownloadFileRawResult {
3
3
  file_list: Array<{
4
4
  file_id: string;
@@ -26,6 +26,7 @@ export declare class Storage<FileContent, DownloadFileResult extends FileUploade
26
26
  private checkFileID;
27
27
  private formatFileID;
28
28
  private getCloudPathFromFileID;
29
+ getUploadFileURL(param: string | GetUploadFileURLParam): Promise<GetUploadFileURLResult>;
29
30
  uploadFile(param: UploadFileParam<FileContent>): Promise<UploadFileResult>;
30
31
  downloadFile(param: DownloadFileParam): Promise<DownloadFileResult>;
31
32
  deleteFile(param: DeleteFileParam): Promise<DeleteFileResult>;
package/lib/Storage.js CHANGED
@@ -1 +1 @@
1
- "use strict";var __assign=this&&this.__assign||function(){__assign=Object.assign||function(a){for(var e,r=1,o=arguments.length;r<o;r++){e=arguments[r];for(var n in e)if(Object.prototype.hasOwnProperty.call(e,n))a[n]=e[n]}return a};return __assign.apply(this,arguments)};var __awaiter=this&&this.__awaiter||function(a,e,r,o){function n(t){return t instanceof r?t:new r(function(s){s(t)})}return new(r||(r=Promise))(function(t,s){function l(h){try{i(o.next(h))}catch(f){s(f)}}function u(h){try{i(o["throw"](h))}catch(f){s(f)}}function i(h){h.done?t(h.value):n(h.value).then(l,u)}i((o=o.apply(a,e||[])).next())})};var __generator=this&&this.__generator||function(a,e){var r={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},o,n,t,s;return s={next:l(0),"throw":l(1),"return":l(2)},typeof Symbol==="function"&&(s[Symbol.iterator]=function(){return this}),s;function l(i){return function(h){return u([i,h])}}function u(i){if(o)throw new TypeError("Generator is already executing.");while(s&&(s=0,i[0]&&(r=0)),r)try{if(o=1,n&&(t=i[0]&2?n["return"]:i[0]?n["throw"]||((t=n["return"])&&t.call(n),0):n.next)&&!(t=t.call(n,i[1])).done)return t;if(n=0,t)i=[i[0]&2,t.value];switch(i[0]){case 0:case 1:t=i;break;case 4:r.label++;return{value:i[1],done:false};case 5:r.label++;n=i[1];i=[0];continue;case 7:i=r.ops.pop();r.trys.pop();continue;default:if(!(t=r.trys,t=t.length>0&&t[t.length-1])&&(i[0]===6||i[0]===2)){r=0;continue}if(i[0]===3&&(!t||i[1]>t[0]&&i[1]<t[3])){r.label=i[1];break}if(i[0]===6&&r.label<t[1]){r.label=t[1];t=i;break}if(t&&r.label<t[2]){r.label=t[2];r.ops.push(i);break}if(t[2])r.ops.pop();r.trys.pop();continue}i=e.call(a,r)}catch(h){i=[6,h];n=0}finally{o=t=0}if(i[0]&5)throw i[1];return{value:i[0]?i[1]:void 0,done:true}}};Object.defineProperty(exports,"__esModule",{value:true});exports.Storage=void 0;var error_1=require("./error");var utils_1=require("./utils");var Storage=function(){function a(e){this.options=e}a.prototype.getUploadPath=function(e){return"/".concat(e,"?upload_url")};a.prototype.getDownloadPath=function(e){return"/".concat(e,"?download_url&expire=3600")};a.prototype.getBatchDeletePath=function(){return"/?delete"};a.prototype.getBatchDownloadPath=function(){return"/?download_url"};a.prototype.checkCloudPath=function(e){if(typeof e!=="string")return"\u5FC5\u987B\u4E3A\u5B57\u7B26\u4E32";e=e.trim();if(!e)return"\u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32";if(e.startsWith("/"))return"\u4E0D\u80FD\u4EE5 / \u5F00\u5934";if(e.includes("//"))return"\u4E0D\u80FD\u5305\u542B\u8FDE\u7EED\u7684 /";if(e.length>850)return"\u957F\u5EA6\u4E0D\u80FD\u8D85\u8FC7 850 \u5B57\u8282"};a.prototype.checkFileID=function(e){if(typeof e!=="string")return"\u5FC5\u987B\u4E3A\u5B57\u7B26\u4E32";e=e.trim();if(!e)return"\u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32"};a.prototype.formatFileID=function(e){return"cloud://".concat(this.options.toEnvId,"/").concat(e.replace(/^\/+/,""))};a.prototype.getCloudPathFromFileID=function(e){var r=e.trim().replace(/^cloud:\/\//,"");var o=r.indexOf("/");if(o<=0){throw error_1.FaasError.INVALID_PARAM_ERR("fileID","\u4E0D\u5408\u6CD5")}var n=r.substring(0,o);var t=r.substring(o+1);if(n!==this.options.toEnvId){console.warn("file ".concat(e," does not belong to env ").concat(this.options.toEnvId))}return t};a.prototype.uploadFile=function(e){return __awaiter(this,void 0,void 0,function(){var r,o,n,t,s,l;return __generator(this,function(u){switch(u.label){case 0:r=this.checkCloudPath(e.cloudPath);if(r){throw error_1.FaasError.INVALID_PARAM_ERR("cloudPath",r)}o=this.getUploadPath(e.cloudPath.trim());return[4,this.request({method:"GET",path:o})];case 1:n=u.sent();t=utils_1.ResponseUtil.handleResponse(n);s=this.formatFileID(t.file_id);return[4,this.options.fileUploader.upload({fileID:s,uploadURL:t.upload_url,fileContent:e.fileContent})];case 2:l=u.sent();if(l.statusCode!==200){throw error_1.FaasError.STORAGE_ERR(-1,"\u4E0A\u4F20\u6587\u4EF6 ".concat(t.upload_url," \u5931\u8D25"),n.requestID)}return[2,{fileID:s,statusCode:l.statusCode}]}})})};a.prototype.downloadFile=function(e){return __awaiter(this,void 0,void 0,function(){var r,o,n,t,s,l;return __generator(this,function(u){switch(u.label){case 0:r=this.checkFileID(e.fileID);if(r){throw error_1.FaasError.INVALID_PARAM_ERR("fileID",r)}o=this.getCloudPathFromFileID(e.fileID);n=this.getDownloadPath(o);return[4,this.request({method:"GET",path:n})];case 1:t=u.sent();s=utils_1.ResponseUtil.handleResponse(t);return[4,this.options.fileUploader.download({fileID:e.fileID,downloadURL:s.download_url})];case 2:l=u.sent();if(l.statusCode!==200){throw error_1.FaasError.STORAGE_ERR(-1,"\u4E0B\u8F7D\u6587\u4EF6 ".concat(s.download_url," \u5931\u8D25"),t.requestID)}return[2,l]}})})};a.prototype.deleteFile=function(e){return __awaiter(this,void 0,void 0,function(){var r,o,n,t,s,l,u,i,h,f;var p=this;return __generator(this,function(d){switch(d.label){case 0:r=Array.isArray(e)?e:e.fileList;if(!Array.isArray(r)||r.length===0){throw error_1.FaasError.INVALID_PARAM_ERR("fileList","\u4E0D\u80FD\u4E3A\u7A7A\u6570\u7EC4")}o=[];for(n=0,t=r;n<t.length;n++){s=t[n];l=this.checkFileID(s);if(l){throw error_1.FaasError.INVALID_PARAM_ERR("fileID",l)}u=this.getCloudPathFromFileID(s);o.push(u)}i=this.getBatchDeletePath();return[4,this.request({method:"POST",path:i,data:{file_list:o}})];case 1:h=d.sent();f=utils_1.ResponseUtil.handleResponse(h);return[2,{fileList:f.file_list.map(function(c){return{fileID:p.formatFileID(c.file_id),status:c.status?0:-1,errMsg:c.status?"ok":c.result_message}})}]}})})};a.prototype.getTempFileURL=function(e){return __awaiter(this,void 0,void 0,function(){var r,o,n,t,s,l,u,i,h,f;var p=this;return __generator(this,function(d){switch(d.label){case 0:r=Array.isArray(e)?e:e.fileList;if(!Array.isArray(r)||r.length===0){throw error_1.FaasError.INVALID_PARAM_ERR("fileList","\u4E0D\u80FD\u4E3A\u7A7A\u6570\u7EC4")}if(r.length>50){throw error_1.FaasError.INVALID_PARAM_ERR("fileList","\u6700\u5927\u957F\u5EA6 50")}o=[];for(n=0,t=r;n<t.length;n++){s=t[n];l=this.checkFileID(s);if(l){throw error_1.FaasError.INVALID_PARAM_ERR("fileID",l)}u=this.getCloudPathFromFileID(s);o.push({file_id:u,expire:600})}i=this.getBatchDownloadPath();return[4,this.request({method:"POST",path:i,data:{file_list:o}})];case 1:h=d.sent();f=utils_1.ResponseUtil.handleResponse(h);return[2,{fileList:f.file_list.map(function(c){return{fileID:p.formatFileID(c.file_id),tempFileURL:c.download_url,status:c.status?0:-1,errMsg:c.status?"ok":c.result_message}})}]}})})};a.prototype.request=function(e){var r=e.method,o=e.path,n=e.data,t=e.headers;return __awaiter(this,void 0,void 0,function(){return __generator(this,function(s){switch(s.label){case 0:return[4,this.options.httpclient.request({method:r,path:o,data:n,headers:__assign({"x-alipay-cloud-mode":"oss","x-data-api-type":"oss","x-expire-timestamp":"".concat(Date.now()+6e4)},t)})];case 1:return[2,s.sent()]}})})};return a}();exports.Storage=Storage;
1
+ "use strict";var __assign=this&&this.__assign||function(){__assign=Object.assign||function(l){for(var e,r=1,o=arguments.length;r<o;r++){e=arguments[r];for(var i in e)if(Object.prototype.hasOwnProperty.call(e,i))l[i]=e[i]}return l};return __assign.apply(this,arguments)};var __awaiter=this&&this.__awaiter||function(l,e,r,o){function i(t){return t instanceof r?t:new r(function(s){s(t)})}return new(r||(r=Promise))(function(t,s){function a(u){try{n(o.next(u))}catch(f){s(f)}}function h(u){try{n(o["throw"](u))}catch(f){s(f)}}function n(u){u.done?t(u.value):i(u.value).then(a,h)}n((o=o.apply(l,e||[])).next())})};var __generator=this&&this.__generator||function(l,e){var r={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},o,i,t,s;return s={next:a(0),"throw":a(1),"return":a(2)},typeof Symbol==="function"&&(s[Symbol.iterator]=function(){return this}),s;function a(n){return function(u){return h([n,u])}}function h(n){if(o)throw new TypeError("Generator is already executing.");while(s&&(s=0,n[0]&&(r=0)),r)try{if(o=1,i&&(t=n[0]&2?i["return"]:n[0]?i["throw"]||((t=i["return"])&&t.call(i),0):i.next)&&!(t=t.call(i,n[1])).done)return t;if(i=0,t)n=[n[0]&2,t.value];switch(n[0]){case 0:case 1:t=n;break;case 4:r.label++;return{value:n[1],done:false};case 5:r.label++;i=n[1];n=[0];continue;case 7:n=r.ops.pop();r.trys.pop();continue;default:if(!(t=r.trys,t=t.length>0&&t[t.length-1])&&(n[0]===6||n[0]===2)){r=0;continue}if(n[0]===3&&(!t||n[1]>t[0]&&n[1]<t[3])){r.label=n[1];break}if(n[0]===6&&r.label<t[1]){r.label=t[1];t=n;break}if(t&&r.label<t[2]){r.label=t[2];r.ops.push(n);break}if(t[2])r.ops.pop();r.trys.pop();continue}n=e.call(l,r)}catch(u){n=[6,u];i=0}finally{o=t=0}if(n[0]&5)throw n[1];return{value:n[0]?n[1]:void 0,done:true}}};Object.defineProperty(exports,"__esModule",{value:true});exports.Storage=void 0;var error_1=require("./error");var utils_1=require("./utils");var Storage=function(){function l(e){this.options=e}l.prototype.getUploadPath=function(e){return"/".concat(e,"?upload_url")};l.prototype.getDownloadPath=function(e){return"/".concat(e,"?download_url&expire=3600")};l.prototype.getBatchDeletePath=function(){return"/?delete"};l.prototype.getBatchDownloadPath=function(){return"/?download_url"};l.prototype.checkCloudPath=function(e){if(typeof e!=="string")return"\u5FC5\u987B\u4E3A\u5B57\u7B26\u4E32";e=e.trim();if(!e)return"\u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32";if(e.startsWith("/"))return"\u4E0D\u80FD\u4EE5 / \u5F00\u5934";if(e.includes("//"))return"\u4E0D\u80FD\u5305\u542B\u8FDE\u7EED\u7684 /";if(e.length>850)return"\u957F\u5EA6\u4E0D\u80FD\u8D85\u8FC7 850 \u5B57\u8282"};l.prototype.checkFileID=function(e){if(typeof e!=="string")return"\u5FC5\u987B\u4E3A\u5B57\u7B26\u4E32";e=e.trim();if(!e)return"\u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32"};l.prototype.formatFileID=function(e){return"cloud://".concat(this.options.toEnvId,"/").concat(e.replace(/^\/+/,""))};l.prototype.getCloudPathFromFileID=function(e){var r=e.trim().replace(/^cloud:\/\//,"");var o=r.indexOf("/");if(o<=0){throw error_1.FaasError.INVALID_PARAM_ERR("fileID","\u4E0D\u5408\u6CD5")}var i=r.substring(0,o);var t=r.substring(o+1);if(i!==this.options.toEnvId){console.warn("file ".concat(e," does not belong to env ").concat(this.options.toEnvId))}return t};l.prototype.getUploadFileURL=function(e){return __awaiter(this,void 0,void 0,function(){var r,o,i,t,s,a;return __generator(this,function(h){switch(h.label){case 0:r=typeof e==="string"?e:e.cloudPath;o=this.checkCloudPath(r);if(o){throw error_1.FaasError.INVALID_PARAM_ERR("cloudPath",o)}i=this.getUploadPath(r.trim());return[4,this.request({method:"GET",path:i})];case 1:t=h.sent();s=utils_1.ResponseUtil.handleResponse(t);a=this.formatFileID(s.file_id);return[2,{fileID:a,uploadURL:s.upload_url,requestID:t.requestID}]}})})};l.prototype.uploadFile=function(e){return __awaiter(this,void 0,void 0,function(){var r,o,i,t,s;return __generator(this,function(a){switch(a.label){case 0:return[4,this.getUploadFileURL(e)];case 1:r=a.sent(),o=r.fileID,i=r.uploadURL,t=r.requestID;return[4,this.options.fileUploader.upload({fileID:o,uploadURL:i,fileContent:e.fileContent})];case 2:s=a.sent();if(s.statusCode!==200){throw error_1.FaasError.STORAGE_ERR(60004,"\u4E0A\u4F20\u6587\u4EF6 ".concat(i," \u5931\u8D25"),t)}return[2,{fileID:o,statusCode:s.statusCode}]}})})};l.prototype.downloadFile=function(e){return __awaiter(this,void 0,void 0,function(){var r,o,i,t,s,a;return __generator(this,function(h){switch(h.label){case 0:r=this.checkFileID(e.fileID);if(r){throw error_1.FaasError.INVALID_PARAM_ERR("fileID",r)}o=this.getCloudPathFromFileID(e.fileID);i=this.getDownloadPath(o);return[4,this.request({method:"GET",path:i})];case 1:t=h.sent();s=utils_1.ResponseUtil.handleResponse(t);return[4,this.options.fileUploader.download({fileID:e.fileID,downloadURL:s.download_url})];case 2:a=h.sent();if(a.statusCode!==200){throw error_1.FaasError.STORAGE_ERR(60004,"\u4E0B\u8F7D\u6587\u4EF6 ".concat(s.download_url," \u5931\u8D25"),t.requestID)}return[2,a]}})})};l.prototype.deleteFile=function(e){return __awaiter(this,void 0,void 0,function(){var r,o,i,t,s,a,h,n,u,f;var p=this;return __generator(this,function(d){switch(d.label){case 0:r=Array.isArray(e)?e:e.fileList;if(!Array.isArray(r)||r.length===0){throw error_1.FaasError.INVALID_PARAM_ERR("fileList","\u4E0D\u80FD\u4E3A\u7A7A\u6570\u7EC4")}o=[];for(i=0,t=r;i<t.length;i++){s=t[i];a=this.checkFileID(s);if(a){throw error_1.FaasError.INVALID_PARAM_ERR("fileID",a)}h=this.getCloudPathFromFileID(s);o.push(h)}n=this.getBatchDeletePath();return[4,this.request({method:"POST",path:n,data:{file_list:o}})];case 1:u=d.sent();f=utils_1.ResponseUtil.handleResponse(u);return[2,{fileList:f.file_list.map(function(c){return{fileID:p.formatFileID(c.file_id),status:c.status?0:-1,errMsg:c.status?"ok":c.result_message}})}]}})})};l.prototype.getTempFileURL=function(e){return __awaiter(this,void 0,void 0,function(){var r,o,i,t,s,a,h,n,u,f;var p=this;return __generator(this,function(d){switch(d.label){case 0:r=Array.isArray(e)?e:e.fileList;if(!Array.isArray(r)||r.length===0){throw error_1.FaasError.INVALID_PARAM_ERR("fileList","\u4E0D\u80FD\u4E3A\u7A7A\u6570\u7EC4")}if(r.length>50){throw error_1.FaasError.INVALID_PARAM_ERR("fileList","\u6700\u5927\u957F\u5EA6 50")}o=[];for(i=0,t=r;i<t.length;i++){s=t[i];a=this.checkFileID(s);if(a){throw error_1.FaasError.INVALID_PARAM_ERR("fileID",a)}h=this.getCloudPathFromFileID(s);o.push({file_id:h,expire:600})}n=this.getBatchDownloadPath();return[4,this.request({method:"POST",path:n,data:{file_list:o}})];case 1:u=d.sent();f=utils_1.ResponseUtil.handleResponse(u);return[2,{fileList:f.file_list.map(function(c){return{fileID:p.formatFileID(c.file_id),tempFileURL:c.download_url,status:c.status?0:-1,errMsg:c.status?"ok":c.result_message}})}]}})})};l.prototype.request=function(e){var r=e.method,o=e.path,i=e.data,t=e.headers;return __awaiter(this,void 0,void 0,function(){return __generator(this,function(s){switch(s.label){case 0:return[4,this.options.httpclient.request({method:r,path:o,data:i,headers:__assign({"x-alipay-cloud-mode":"oss","x-data-api-type":"oss","x-expire-timestamp":"".concat(Date.now()+6e4)},t)})];case 1:return[2,s.sent()]}})})};return l}();exports.Storage=Storage;
package/lib/types.d.ts CHANGED
@@ -115,6 +115,14 @@ export interface FileInfo {
115
115
  export interface TempFileInfo extends FileInfo {
116
116
  tempFileURL: string;
117
117
  }
118
+ export interface GetUploadFileURLParam {
119
+ cloudPath: string;
120
+ }
121
+ export interface GetUploadFileURLResult {
122
+ fileID: string;
123
+ uploadURL: string;
124
+ requestID: string;
125
+ }
118
126
  export interface UploadFileParam<T> {
119
127
  cloudPath: string;
120
128
  fileContent: T;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alipay/faas-common-sdk",
3
- "version": "1.1.17",
3
+ "version": "1.1.18-alpha.0",
4
4
  "description": "云函数服务通用 SDK",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -30,7 +30,7 @@
30
30
  "typescript": "^5.0.4"
31
31
  },
32
32
  "dependencies": {
33
- "@alipay/faas-db-builder": "^1.1.17"
33
+ "@alipay/faas-db-builder": "^1.1.18-alpha.0"
34
34
  },
35
35
  "engines": {
36
36
  "node": ">= 14.0.0"