@cloudbase/adapter-interface
Version:
cloudbase javascript sdk adapter interface
115 lines (114 loc) • 3.56 kB
TypeScript
export declare enum StorageType {
local = "local",
none = "none",
session = "session"
}
export interface ResponseObject<T = any> {
data?: T;
statusCode?: number;
[key: string]: any;
}
export declare type IRequestMethod = 'get' | 'post' | 'upload' | 'download' | 'put';
export interface IRequestConfig {
timeout?: number;
timeoutMsg?: string;
restrictedMethods?: Array<IRequestMethod>;
}
export interface IRequestOptions {
url?: string;
data?: object;
headers?: object;
method?: string;
responseType?: XMLHttpRequestResponseType;
[key: string]: any;
}
export interface IUploadRequestOptions extends IRequestOptions {
file: string;
name: string;
data: {
success_action_status?: string;
[key: string]: any;
};
onUploadProgress?: (...args: any[]) => void;
}
export interface SDKRequestInterface {
get?: (options: IRequestOptions) => any;
post: (options: IRequestOptions) => any;
upload: (options: IRequestOptions) => any;
download: (options: IRequestOptions) => any;
fetch?: (options: IFetchOptions) => any;
}
interface AbortSignal {
aborted: boolean;
addEventListener: (event: string, listener: CallableFunction) => unknown;
}
export declare type IFetchOptions = Omit<RequestInit, "signal"> & {
url: string;
enableAbort?: boolean;
stream?: boolean;
signal?: AbortSignal;
timeout?: number;
};
export declare abstract class AbstractSDKRequest implements SDKRequestInterface {
abstract post(options: IRequestOptions): any;
abstract upload(options: IRequestOptions): any;
abstract download(options: IRequestOptions): any;
}
export interface NodeRequestInterface {
send: (action: string, data?: any, ...args: any[]) => Promise<any>;
}
export declare type SDKRequestConstructor = new (options?: any) => SDKRequestInterface;
export declare type NodeRequestConstructor = new (options: any) => NodeRequestInterface;
export interface WebSocketInterface {
send: (data?: string | ArrayBuffer) => void;
close: (code?: number, reason?: string) => void;
onopen: any;
onclose: any;
onerror: any;
onmessage: any;
readyState: number;
CONNECTING: number;
OPEN: number;
CLOSING: number;
CLOSED: number;
}
export declare type WebSocketContructor = new (url: string, ...args: any[]) => WebSocketInterface;
export interface StorageInterface {
mode?: 'async' | 'sync';
setItem: (key: string, value: any) => void;
getItem: (key: string) => any;
removeItem?: (key: string) => void;
clear?: () => void;
[key: string]: any;
}
export declare abstract class AbstractStorage implements StorageInterface {
abstract setItem(key: string, value: any): void;
abstract getItem(key: string): any;
}
export interface CaptchaToken {
captcha_token: string;
expires_in: number;
expires_at?: Date | null;
}
export interface SDKAdapterInterface {
root: any;
wsClass: WebSocketContructor;
reqClass: SDKRequestConstructor;
localStorage?: StorageInterface;
sessionStorage?: StorageInterface;
primaryStorage?: StorageType;
captchaOptions?: {
openURIWithCallback?: (url: string) => Promise<CaptchaToken>;
};
getAppSign?(): string;
}
export interface NodeAdapterInterface {
wsClass: WebSocketContructor;
reqClass: NodeRequestConstructor;
}
export interface CloudbaseAdapter {
runtime: string;
isMatch: () => boolean;
genAdapter: (options?: any) => SDKAdapterInterface | NodeAdapterInterface;
}
export {};