@cloudbase/adapter-interface
tcb-js-sdk适配器接口,各平台适配器按照此接口规范进行开发。
安装
npm i @cloudbase/adapter-interface -S
使用
import {
AbstractSDKRequest,
IRequestOptions,
IUploadRequestOptions,
StorageInterface,
WebSocketInterface,
WebSocketContructor,
SDKAdapterInterface,
StorageType,
formatUrl
} from '@cloudbase/adapter-interface';
function isMatch(): boolean {
return true;
}
export class Request extends AbstractSDKRequest {
public post(options: IRequestOptions) {
return new Promise(resolve=>{
resolve();
})
}
public upload(options: IUploadRequestOptions) {
return new Promise(resolve=>{
resolve();
})
}
public download(options: IRequestOptions) {
return new Promise(resolve=>{
resolve();
})
}
}
export const Storage: StorageInterface = {
setItem(key: string, value: any) {
},
getItem(key: string): any {
},
removeItem(key: string) {
},
clear() {
}
};
export class WebSocket {
constructor(url: string, options: object = {}) {
const socketTask: WebSocketInterface = {
set onopen(cb) {
},
set onmessage(cb) {
},
set onclose(cb) {
},
set onerror(cb) {
},
send: (data) => {
},
close: (code? : number, reason? : string) => {
},
get readyState() {
return readyState;
},
CONNECTING: 0,
OPEN: 1,
CLOSING: 2,
CLOSED: 3
};
return socketTask;
}
}
function genAdapter() {
const adapter: SDKAdapterInterface = {
root: window,
reqClass: Request,
wsClass: WebSocket as WebSocketContructor,
localStorage: Storage,
primaryStorage: StorageType.local,
sessionStorage: sessionStorage
};
return adapter;
}
const adapter = {
genAdapter,
isMatch,
runtime: 'wx_mp'
};
export default adapter;