@alipay/faas-web-sdk 1.1.0-alpha.15

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/README.md ADDED
@@ -0,0 +1 @@
1
+ # @alipay/faas-web-sdk
@@ -0,0 +1,10 @@
1
+ import { QueryChainCommand, SampleAggregateParam } from '@alipay/faas-db-builder';
2
+ import { CreateDatabaseOptions } from '../types';
3
+ export declare class Aggregate {
4
+ #private;
5
+ constructor(collectionName: string, options: CreateDatabaseOptions);
6
+ match(match: QueryChainCommand): Aggregate;
7
+ group(group: object): Aggregate;
8
+ sample(sample: SampleAggregateParam): Aggregate;
9
+ end<T = any>(): Promise<T[]>;
10
+ }
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Aggregate=void 0;const faas_db_builder_1=require("@alipay/faas-db-builder"),utils_1=require("../utils");class Aggregate{#e;#s;#t;constructor(t,e){this.#s=t,this.#e=e,this.#t=new faas_db_builder_1.AggregateBuilder(t)}match(t){return this.#t.match(t),this}group(t){return this.#t.group(t),this}sample(t){return this.#t.sample(t),this}async end(){const{options:t}=this.#t.end(),e="POST",s=`${this.#r()}?aggregate`,r=await this.#e.httpclient.request({method:e,path:s,data:t,headers:{}});return utils_1.ResponseUtil.handleResponse(r)}#r(){return`/${this.#e.databaseName||"faas"}/${this.#s}`}}exports.Aggregate=Aggregate;
@@ -0,0 +1,21 @@
1
+ import { QueryChainCommand, Sort } from '@alipay/faas-db-builder';
2
+ import { AddDocumentParam, AddDocumentResult, CountDocumentResult, CreateDatabaseOptions } from '../types';
3
+ import { Aggregate } from './Aggregate';
4
+ import { Document } from './Document';
5
+ import { Query } from './Query';
6
+ export declare class Collection {
7
+ #private;
8
+ private readonly builder;
9
+ constructor(collectionName: string, options: CreateDatabaseOptions);
10
+ add(param: AddDocumentParam): Promise<AddDocumentResult>;
11
+ doc(_id: string): Document;
12
+ limit(limit: number): Query;
13
+ skip(skip: number): Query;
14
+ where(match: QueryChainCommand): Query;
15
+ projection(projection: object): Query;
16
+ field(projection: object): Query;
17
+ orderBy(field: string, sort: Sort): Query;
18
+ aggregate(): Aggregate;
19
+ get<T = any>(): Promise<Array<T>>;
20
+ count(): Promise<CountDocumentResult>;
21
+ }
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Collection=void 0;const faas_db_builder_1=require("@alipay/faas-db-builder"),utils_1=require("../utils"),Aggregate_1=require("./Aggregate"),Document_1=require("./Document"),Query_1=require("./Query");class Collection{#e;#t;builder;constructor(e,t){this.#t=e,this.#e=t,this.builder=new faas_db_builder_1.CollectionBuilder(e)}async add(e){const{options:t}=this.builder.add(e),r="POST",i=this.#r(e.data._id),s=await this.#e.httpclient.request({method:r,path:i,data:t,headers:{}});return utils_1.ResponseUtil.handleResponse(s)}doc(e){return new Document_1.Document(this.#t,e,this.#e)}limit(e){return new Query_1.Query(this.#t,this.#e).limit(e)}skip(e){return new Query_1.Query(this.#t,this.#e).skip(e)}where(e){return new Query_1.Query(this.#t,this.#e).where(e)}projection(e){return new Query_1.Query(this.#t,this.#e).projection(e)}field(e){return new Query_1.Query(this.#t,this.#e).field(e)}orderBy(e,t){return new Query_1.Query(this.#t,this.#e).orderBy(e,t)}aggregate(){return new Aggregate_1.Aggregate(this.#t,this.#e)}async get(){return await new Query_1.Query(this.#t,this.#e).get()}async count(){return await new Query_1.Query(this.#t,this.#e).count()}#r(e){return`/${this.#e.databaseName||"faas"}/${this.#t}?_id=${e||""}`}}exports.Collection=Collection;
@@ -0,0 +1,10 @@
1
+ import { UpdateData } from '@alipay/faas-db-builder';
2
+ import { CreateDatabaseOptions, DeleteDocumentResult, UpdateDocumentResult } from '../types';
3
+ export declare class Document {
4
+ #private;
5
+ constructor(collectionName: string, id: string, options: CreateDatabaseOptions);
6
+ update(data: UpdateData): Promise<UpdateDocumentResult>;
7
+ set(data: UpdateData): Promise<UpdateDocumentResult>;
8
+ remove(): Promise<DeleteDocumentResult>;
9
+ get<T = any>(): Promise<T | null>;
10
+ }
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Document=void 0;const error_1=require("../error"),Query_1=require("./Query");class Document{#e;#s;#r;constructor(t,e,r){this.#s=t,this.#r=e,this.#e=r}async update(t){return await this.#t.update(t)}async set(t){return await this.#t.set(t)}async remove(){return await this.#t.remove()}async get(){const e=(await this.#t.get())[0];if(!e){if(this.#e.throwOnNotFound)throw error_1.FaasError.NOT_FOUND_ERR(`\u4E0D\u5B58\u5728_id \u4E3A ${this.#r} \u7684\u6587\u6863`);return null}return e}get#t(){return new Query_1.Query(this.#s,this.#e).where({_id:String(this.#r)}).limit(1)}}exports.Document=Document;
@@ -0,0 +1,17 @@
1
+ import { QueryChainCommand, Sort, UpdateData } from '@alipay/faas-db-builder';
2
+ import { CountDocumentResult, CreateDatabaseOptions, DeleteDocumentResult, UpdateDocumentResult } from '../types';
3
+ export declare class Query {
4
+ #private;
5
+ constructor(collectionName: string, options: CreateDatabaseOptions);
6
+ limit(limit: number): Query;
7
+ skip(skip: number): Query;
8
+ where(match: QueryChainCommand): Query;
9
+ projection(projection: object): Query;
10
+ field(projection: object): Query;
11
+ orderBy(field: string, sort: Sort | string): Query;
12
+ update(data: UpdateData): Promise<UpdateDocumentResult>;
13
+ set(data: UpdateData): Promise<UpdateDocumentResult>;
14
+ remove(): Promise<DeleteDocumentResult>;
15
+ get<T = any>(): Promise<Array<T>>;
16
+ count(): Promise<CountDocumentResult>;
17
+ }
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Query=void 0;const faas_db_builder_1=require("@alipay/faas-db-builder"),utils_1=require("../utils");class Query{#e;#n;#t;constructor(t,e){this.#n=t,this.#e=e,this.#t=new faas_db_builder_1.QueryBuilder(t)}limit(t){return this.#t.limit(t),this}skip(t){return this.#t.skip(t),this}where(t){return this.#t.where(t),this}projection(t){return this.#t.projection(t),this}field(t){return this.#t.projection(t),this}orderBy(t,e){return this.#t.orderBy(t,e),this}async update(t){const e="POST",s=`${this.#s()}?update`,{options:n}=this.#t.update(t),i=await this.#e.httpclient.request({method:e,path:s,data:n,headers:{}});return utils_1.ResponseUtil.handleResponse(i)}async set(t){const e="POST",s=`${this.#s()}?replace`,{options:n}=this.#t.set(t),i=await this.#e.httpclient.request({method:e,path:s,data:n,headers:{}});return utils_1.ResponseUtil.handleResponse(i)}async remove(){const t="POST",e=`${this.#s()}?delete`,{options:s}=this.#t.remove(),n=await this.#e.httpclient.request({method:t,path:e,data:s,headers:{}});return utils_1.ResponseUtil.handleResponse(n)}async get(){const t="POST",e=`${this.#s()}?query`,{options:s}=this.#t.get(),n=await this.#e.httpclient.request({method:t,path:e,data:s,headers:{}});return utils_1.ResponseUtil.handleResponse(n)}async count(){const t="POST",e=`${this.#s()}?count`,{options:s}=this.#t.count(),n=await this.#e.httpclient.request({method:t,path:e,data:s,headers:{}});return{total:utils_1.ResponseUtil.handleResponse(n).count}}#s(){return`/${this.#e.databaseName||"faas"}/${this.#n}`}}exports.Query=Query;
@@ -0,0 +1,9 @@
1
+ import { Command } from '@alipay/faas-db-builder';
2
+ import { CreateDatabaseOptions } from '../types';
3
+ import { Collection } from './Collection';
4
+ export declare class Database {
5
+ #private;
6
+ readonly command: Command;
7
+ constructor(options: CreateDatabaseOptions);
8
+ collection(collectionName: string): Collection;
9
+ }
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Database=void 0;const Collection_1=require("./Collection");class DatabaseHttpclient{#t;constructor(t){this.#t=t}async request({method:t,path:a,data:o,headers:s}){return await this.#t.request({method:t,path:a,data:o,headers:{"x-alipay-cloud-mode":"mongo","x-data-api-type":"mongo","x-expire-timestamp":`${Date.now()+6e4}`,...s}})}}class Database{#t;command;constructor(t){this.#t={...t,httpclient:new DatabaseHttpclient(t.httpclient)}}collection(t){return new Collection_1.Collection(t,this.#t)}}exports.Database=Database;
@@ -0,0 +1,25 @@
1
+ import { CreateStorageOptions, DeleteFileParam, DeleteFileResult, DownloadFileParam, FileUploaderResult, GetTempFileURLParam, GetTempFileURLResult, UploadFileParam, UploadFileResult } from './types';
2
+ export interface GetDownloadFileRawResult {
3
+ file_list: Array<{
4
+ file_id: string;
5
+ expire: number;
6
+ status: boolean;
7
+ result_message: string;
8
+ download_url: string;
9
+ }>;
10
+ }
11
+ export interface DeleteFileRawResult {
12
+ file_list: Array<{
13
+ file_id: string;
14
+ status: boolean;
15
+ result_message: string;
16
+ }>;
17
+ }
18
+ export declare class Storage<FileContent, DownloadFileResult extends FileUploaderResult> {
19
+ #private;
20
+ constructor(options: CreateStorageOptions<FileContent, DownloadFileResult>);
21
+ uploadFile(param: UploadFileParam<FileContent>): Promise<UploadFileResult>;
22
+ downloadFile(param: DownloadFileParam): Promise<DownloadFileResult>;
23
+ deleteFile(param: DeleteFileParam): Promise<DeleteFileResult>;
24
+ getTempFileURL(param: GetTempFileURLParam): Promise<GetTempFileURLResult>;
25
+ }
package/lib/Storage.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Storage=void 0;const error_1=require("./error"),utils_1=require("./utils");class Storage{#t;constructor(t){this.#t=t}#i(t){return`/${t}?upload_url`}#a(t){return`/${t}?download_url&expire=3600`}#n(){return"/?delete"}#l(){return"/?download_url"}#h(t){if(typeof t!="string")return"\u5FC5\u987B\u4E3A\u5B57\u7B26\u4E32";if(t=t.trim(),!t)return"\u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32";if(t.startsWith("/"))return"\u4E0D\u80FD\u4EE5 / \u5F00\u5934";if(t.includes("//"))return"\u4E0D\u80FD\u5305\u542B\u8FDE\u7EED\u7684 /";if(t.length>850)return"\u957F\u5EA6\u4E0D\u80FD\u8D85\u8FC7 850 \u5B57\u8282"}#s(t){if(typeof t!="string")return"\u5FC5\u987B\u4E3A\u5B57\u7B26\u4E32";if(t=t.trim(),!t)return"\u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32"}#r(t){return`cloud://${this.#t.toEnvId}/${t.replace(/^\/+/,"")}`}#o(t){const s=t.trim().replace(/^cloud:\/\//,""),r=s.indexOf("/");if(r<=0)throw error_1.FaasError.INVALID_PARAM_ERR("fileID","\u4E0D\u5408\u6CD5");const o=s.substring(0,r),i=s.substring(r+1);return o!==this.#t.toEnvId&&console.warn(`file ${t} does not belong to env ${this.#t.toEnvId}`),i}async uploadFile(t){const s=this.#h(t.cloudPath);if(s)throw error_1.FaasError.INVALID_PARAM_ERR("cloudPath",s);const r=this.#i(t.cloudPath.trim()),o=await this.#e({method:"GET",path:r,data:{},headers:{}}),i=utils_1.ResponseUtil.handleResponse(o),a=this.#r(i.file_id),e=await this.#t.fileUploader.upload({fileID:a,uploadURL:i.upload_url,fileContent:t.fileContent});if(e.statusCode!==200)throw error_1.FaasError.STORAGE_ERR(-1,"\u6587\u4EF6\u4E0B\u8F7D\u5931\u8D25",o.requestID);return{fileID:a,statusCode:e.statusCode}}async downloadFile(t){const s=this.#s(t.fileID);if(s)throw error_1.FaasError.INVALID_PARAM_ERR("fileID",s);const r=this.#o(t.fileID),o=this.#a(r),i=await this.#e({method:"GET",path:o,data:{},headers:{}}),a=utils_1.ResponseUtil.handleResponse(i),e=await this.#t.fileUploader.download({fileID:t.fileID,downloadURL:a.download_url});if(e.statusCode!==200)throw error_1.FaasError.STORAGE_ERR(-1,"\u6587\u4EF6\u4E0B\u8F7D\u5931\u8D25",i.requestID);return e}async deleteFile(t){const s=Array.isArray(t)?t:t.fileList;if(!Array.isArray(s)||s.length===0)throw error_1.FaasError.INVALID_PARAM_ERR("fileList","\u4E0D\u80FD\u4E3A\u7A7A\u6570\u7EC4");const r=[];for(const e of s){const n=this.#s(e);if(n)throw error_1.FaasError.INVALID_PARAM_ERR("fileID",n);const l=this.#o(e);r.push(l)}const o=this.#n(),i=await this.#e({method:"POST",path:o,data:{file_list:r},headers:{}});return{fileList:utils_1.ResponseUtil.handleResponse(i).file_list.map(e=>({fileID:this.#r(e.file_id),status:e.status?0:-1,errMsg:e.status?"ok":e.result_message}))}}async getTempFileURL(t){const s=Array.isArray(t)?t:t.fileList;if(!Array.isArray(s)||s.length===0)throw error_1.FaasError.INVALID_PARAM_ERR("fileList","\u4E0D\u80FD\u4E3A\u7A7A\u6570\u7EC4");if(s.length>50)throw error_1.FaasError.INVALID_PARAM_ERR("fileList","\u6700\u5927\u957F\u5EA6 50");const r=[];for(const e of s){const n=this.#s(e);if(n)throw error_1.FaasError.INVALID_PARAM_ERR("fileID",n);const l=this.#o(e);r.push({file_id:l,expire:600})}const o=this.#l(),i=await this.#e({method:"POST",path:o,data:{file_list:r},headers:{}});return{fileList:utils_1.ResponseUtil.handleResponse(i).file_list.map(e=>({fileID:this.#r(e.file_id),tempFileURL:e.download_url,status:e.status?0:-1,errMsg:e.status?"ok":e.result_message}))}}async#e({method:t,path:s,data:r,headers:o}){return await this.#t.httpclient.request({method:t,path:s,data:r,headers:{"x-alipay-cloud-mode":"oss","x-data-api-type":"oss","x-expire-timestamp":`${Date.now()+6e4}`,...o}})}}exports.Storage=Storage;
@@ -0,0 +1,9 @@
1
+ import { Database } from './Database';
2
+ import { Storage } from './Storage';
3
+ import { CallContainer, CallFunction, CreateDatabaseOptions, CreateOptions, CreateStorageOptions, FileUploaderResult } from './types';
4
+ export declare class WebSDK {
5
+ static createCallFunction<T = any>(options: CreateOptions): CallFunction<T>;
6
+ static createCallContainer<T = any>(options: CreateOptions): CallContainer<T>;
7
+ static createDatabase(options: CreateDatabaseOptions): Database;
8
+ static createStorage<FileContent, UploadResult extends FileUploaderResult>(options: CreateStorageOptions<FileContent, UploadResult>): Storage<FileContent, UploadResult>;
9
+ }
package/lib/WebSDK.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.WebSDK=void 0;const callContainer_1=require("./callContainer"),callFunction_1=require("./callFunction"),Database_1=require("./Database"),Storage_1=require("./Storage");class WebSDK{static createCallFunction(e){return(0,callFunction_1.createCallFunction)(e)}static createCallContainer(e){return(0,callContainer_1.createCallContainer)(e)}static createDatabase(e){return new Database_1.Database(e)}static createStorage(e){return new Storage_1.Storage(e)}}exports.WebSDK=WebSDK;
@@ -0,0 +1,2 @@
1
+ import { CallContainerParam, CallContainerResult, CreateOptions } from './types';
2
+ export declare const createCallContainer: <Result>(options: CreateOptions) => import("./types").FunctionCall<CallContainerParam, CallContainerResult<Result>, Error>;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.createCallContainer=void 0;const utils_1=require("./utils"),createCallContainer=e=>{const t=async r=>{throw new Error("not implemented in "+e.type+" with "+r)};return(0,utils_1.wrapCallback)(t)};exports.createCallContainer=createCallContainer;
@@ -0,0 +1,2 @@
1
+ import { CallFunctionParam, CallFunctionResult, CreateOptions } from './types';
2
+ export declare const createCallFunction: <Result>(options: CreateOptions) => import("./types").FunctionCall<CallFunctionParam, CallFunctionResult<Result>, Error>;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.createCallFunction=void 0;const error_1=require("./error"),utils_1=require("./utils"),createCallFunction=o=>{const{httpclient:s}=o,c=async r=>{if(!r.name)throw error_1.FaasError.INVALID_PARAM_ERR("name","\u7F3A\u5931");const u={"x-to-function-name":r.name,"x-alipay-cloud-mode":"function"},i="POST",l="/functions/invokeFunction",t=await s.request({method:i,path:l,data:r.data,headers:u});if(t.status!==200){let n=-1,a="";const e=t.data;throw e&&({errCode:n,errMsg:a}=e,e.errDetail&&(a+=`\uFF0C\u8BE6\u60C5\uFF1A${e.errDetail}`)),error_1.FaasError.SERVER_ERR(n,a,t.requestID,t.status)}return{requestID:t.requestID,result:t.data}};return(0,utils_1.wrapCallback)(c)};exports.createCallFunction=createCallFunction;
package/lib/error.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ export declare class FaasError extends Error {
2
+ httpStatus: number;
3
+ error: number;
4
+ errorMessage: string;
5
+ requestID: string;
6
+ get errCode(): number;
7
+ get errMsg(): string;
8
+ constructor(code: number, message: string);
9
+ static INVALID_PARAM_ERR(paramName: string, message: string): FaasError;
10
+ static NOT_FOUND_ERR(message: string): FaasError;
11
+ static SERVER_ERR(code: number, message: string, requestID: string, httpStatus: number): FaasError;
12
+ static STORAGE_ERR(code: number, message: string, requestID: string): FaasError;
13
+ }
package/lib/error.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.FaasError=void 0;class FaasError extends Error{httpStatus;error;errorMessage;requestID;get errCode(){return this.error}get errMsg(){return this.message}constructor(e,r){super(r),this.error=e,this.errorMessage=r}static INVALID_PARAM_ERR(e,r){return new FaasError(60001,`\u65E0\u6548\u5165\u53C2\uFF0C\u53C2\u6570 ${e} ${r}`)}static NOT_FOUND_ERR(e){return new FaasError(70001,e)}static SERVER_ERR(e,r,s,t){const u=new FaasError(e,r);return u.requestID=s,u.httpStatus=t,u}static STORAGE_ERR(e,r,s){const t=new FaasError(e,r);return t.requestID=s,t}}exports.FaasError=FaasError;
package/lib/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export { WebSDK } from './WebSDK';
2
+ export { Storage } from './Storage';
3
+ export { Database } from './Database';
4
+ export { Document } from './Database/Document';
5
+ export { Collection } from './Database/Collection';
6
+ export { Aggregate } from './Database/Aggregate';
7
+ export { Query } from './Database/Query';
8
+ export { FaasError } from './error';
9
+ export { ResponseUtil } from './utils';
10
+ export * from './types';
package/lib/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(n,r,e,t){t===void 0&&(t=e);var o=Object.getOwnPropertyDescriptor(r,e);(!o||("get"in o?!r.__esModule:o.writable||o.configurable))&&(o={enumerable:!0,get:function(){return r[e]}}),Object.defineProperty(n,t,o)}:function(n,r,e,t){t===void 0&&(t=e),n[t]=r[e]}),__exportStar=this&&this.__exportStar||function(n,r){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(r,e)&&__createBinding(r,n,e)};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ResponseUtil=exports.FaasError=exports.Query=exports.Aggregate=exports.Collection=exports.Document=exports.Database=exports.Storage=exports.WebSDK=void 0;var WebSDK_1=require("./WebSDK");Object.defineProperty(exports,"WebSDK",{enumerable:!0,get:function(){return WebSDK_1.WebSDK}});var Storage_1=require("./Storage");Object.defineProperty(exports,"Storage",{enumerable:!0,get:function(){return Storage_1.Storage}});var Database_1=require("./Database");Object.defineProperty(exports,"Database",{enumerable:!0,get:function(){return Database_1.Database}});var Document_1=require("./Database/Document");Object.defineProperty(exports,"Document",{enumerable:!0,get:function(){return Document_1.Document}});var Collection_1=require("./Database/Collection");Object.defineProperty(exports,"Collection",{enumerable:!0,get:function(){return Collection_1.Collection}});var Aggregate_1=require("./Database/Aggregate");Object.defineProperty(exports,"Aggregate",{enumerable:!0,get:function(){return Aggregate_1.Aggregate}});var Query_1=require("./Database/Query");Object.defineProperty(exports,"Query",{enumerable:!0,get:function(){return Query_1.Query}});var error_1=require("./error");Object.defineProperty(exports,"FaasError",{enumerable:!0,get:function(){return error_1.FaasError}});var utils_1=require("./utils");Object.defineProperty(exports,"ResponseUtil",{enumerable:!0,get:function(){return utils_1.ResponseUtil}}),__exportStar(require("./types"),exports);
package/lib/types.d.ts ADDED
@@ -0,0 +1,143 @@
1
+ import { DocumentData } from '@alipay/faas-db-builder';
2
+ export interface CallbackParam<T, V = Error> {
3
+ success?: (res: T) => any;
4
+ fail?: (e: V) => any;
5
+ complete?: (e: T | V) => any;
6
+ }
7
+ export interface FunctionCall<Param, Result, E = Error> {
8
+ (param: Param): Promise<Result>;
9
+ (param: Param & CallbackParam<Result, E>): void;
10
+ }
11
+ export interface CallFunctionFailResponse {
12
+ errCode: number;
13
+ errMsg: string;
14
+ errDetail?: string;
15
+ }
16
+ export interface CommonCallResponse<T = object> {
17
+ code: number;
18
+ message: string;
19
+ data: T;
20
+ }
21
+ export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';
22
+ export interface HttpRequestOptions {
23
+ method: HttpMethod;
24
+ path: string;
25
+ data: any;
26
+ headers: Record<string, string | string[]>;
27
+ }
28
+ export interface HttpResponse<T> {
29
+ status: number;
30
+ requestID: string;
31
+ headers: Record<string, string | string[]>;
32
+ data: T;
33
+ }
34
+ export interface Httpclient {
35
+ request<T>(options: HttpRequestOptions): Promise<HttpResponse<T>>;
36
+ }
37
+ export interface CreateOptions {
38
+ httpclient: Httpclient;
39
+ type: 'web' | 'miniprogram' | 'function';
40
+ }
41
+ export interface CreateDatabaseOptions extends CreateOptions {
42
+ databaseName?: string;
43
+ throwOnNotFound?: boolean;
44
+ }
45
+ export interface CallFunctionResult<T = any> {
46
+ requestID: string;
47
+ result?: T;
48
+ }
49
+ export interface CallFunctionParam {
50
+ name: string;
51
+ data: object | string | number | boolean;
52
+ }
53
+ export type CallFunction<T> = FunctionCall<CallFunctionParam, CallFunctionResult<T>>;
54
+ export interface CallContainerResult<T> {
55
+ data: T;
56
+ statusCode: number;
57
+ header: Record<string, string | string[]>;
58
+ }
59
+ export interface CallContainerParam {
60
+ path: string;
61
+ method?: string;
62
+ header?: Record<string, string | string[]>;
63
+ data?: string | object;
64
+ timeout?: number;
65
+ dataType?: 'json' | 'text';
66
+ }
67
+ export type CallContainer<T> = FunctionCall<CallContainerParam, CallContainerResult<T>>;
68
+ export interface AddDocumentParam {
69
+ data: DocumentData;
70
+ }
71
+ export interface AddDocumentResult {
72
+ _id: string;
73
+ }
74
+ export interface CountDocumentResult {
75
+ total: number;
76
+ }
77
+ export interface DeleteDocumentResult {
78
+ count: number;
79
+ }
80
+ export interface UpdateDocumentResult {
81
+ count: number;
82
+ }
83
+ export interface FileUploaderUploadParam<T> {
84
+ uploadURL: string;
85
+ fileID: string;
86
+ fileContent: T;
87
+ }
88
+ export interface FileUploaderDownloadParam {
89
+ fileID: string;
90
+ downloadURL: string;
91
+ }
92
+ export interface FileUploaderResult {
93
+ statusCode: number;
94
+ }
95
+ export interface FileUploader<FileContent, DownloadResult extends FileUploaderResult> {
96
+ upload(param: FileUploaderUploadParam<FileContent>): Promise<FileUploaderResult>;
97
+ download(param: FileUploaderDownloadParam): Promise<DownloadResult>;
98
+ }
99
+ export interface CreateStorageOptions<FileContent, DownloadResult extends FileUploaderResult> extends CreateOptions {
100
+ fileUploader: FileUploader<FileContent, DownloadResult>;
101
+ toEnvId: string;
102
+ }
103
+ export interface FileInfo {
104
+ fileID: string;
105
+ status: number;
106
+ errMsg: string;
107
+ }
108
+ export interface TempFileInfo extends FileInfo {
109
+ tempFileURL: string;
110
+ }
111
+ export interface UploadFileParam<T> {
112
+ cloudPath: string;
113
+ fileContent: T;
114
+ }
115
+ export interface UploadFileResult {
116
+ fileID: string;
117
+ statusCode: number;
118
+ }
119
+ export interface DownloadFileParam {
120
+ fileID: string;
121
+ }
122
+ export interface DownloadFileResult {
123
+ statusCode: number;
124
+ }
125
+ export interface DeleteFileParam {
126
+ fileList: string[];
127
+ }
128
+ export interface DeleteFileResult {
129
+ fileList: FileInfo[];
130
+ }
131
+ export interface GetTempFileURLParam {
132
+ fileList: string[];
133
+ }
134
+ export interface GetTempFileURLResult {
135
+ fileList: TempFileInfo[];
136
+ }
137
+ export interface GetUploadUrlResult {
138
+ file_id: string;
139
+ upload_url: string;
140
+ }
141
+ export interface GetDownloadURLResult {
142
+ download_url: string;
143
+ }
package/lib/types.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});
package/lib/utils.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { CommonCallResponse, FunctionCall, HttpResponse } from './types';
2
+ export declare const wrapCallback: <Arg, Res, E = Error>(func: (arg: Arg) => Promise<Res>) => FunctionCall<Arg, Res, E>;
3
+ export declare class ResponseUtil {
4
+ static handleResponse<T = object>(res: HttpResponse<CommonCallResponse<T>>): T;
5
+ }
package/lib/utils.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ResponseUtil=exports.wrapCallback=void 0;const error_1=require("./error"),wrapCallback=s=>function(e){if(e?.complete||e?.success||e?.fail){const{complete:r,success:o,fail:n,...a}=e;s(a).then(t=>(o?.(t),t),t=>(n?.(t),t)).then(t=>{r?.(t)});return}return s(e)};exports.wrapCallback=wrapCallback;class ResponseUtil{static handleResponse(e){if(e.status!==200){const{code:r,message:o}=e.data||{code:-1,message:"\u672A\u77E5\u5F02\u5E38"};throw error_1.FaasError.SERVER_ERR(r,o,e.requestID,e.status)}return e.data.data}}exports.ResponseUtil=ResponseUtil;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@alipay/faas-web-sdk",
3
+ "version": "1.1.0-alpha.15",
4
+ "description": "云函数服务 SDK",
5
+ "main": "lib/index.js",
6
+ "files": [
7
+ "lib"
8
+ ],
9
+ "scripts": {
10
+ "lint": "eslint src test --cache --fix --ext ts",
11
+ "build": "tsc -p ./tsconfig.json && minimize-js lib",
12
+ "clean": "tsc --build --clean",
13
+ "test": "egg-bin test",
14
+ "cov": "egg-bin cov",
15
+ "ci": "npm run cov && npm run build",
16
+ "prepublishOnly": "npm run clean && npm run build"
17
+ },
18
+ "license": "MIT",
19
+ "devDependencies": {
20
+ "@eggjs/tsconfig": "^1.3.2",
21
+ "eslint": "^8.34.0",
22
+ "eslint-config-egg": "^12.1.0",
23
+ "minimize-js": "^1.3.1",
24
+ "mm": "^3.2.1",
25
+ "typescript": "^5.0.4"
26
+ },
27
+ "dependencies": {
28
+ "@alipay/faas-db-builder": "^1.1.0-alpha.15"
29
+ },
30
+ "engines": {
31
+ "node": ">= 16.0.0"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public"
35
+ }
36
+ }