Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: asyncstorage, request module for bytedance #66

Merged
merged 16 commits into from
Apr 16, 2020
8 changes: 6 additions & 2 deletions packages/asyncstorage/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { isWeb, isWeex, isMiniApp, isWeChatMiniprogram } from 'universal-env';
import { isWeb, isWeex, isMiniApp, isWeChatMiniProgram, isByteDanceMicroApp } from 'universal-env';
import webModule from './web/index';
import weexModule from './weex/index';
import miniAppModule from './miniapp/ali/index';
import weChatModule from './miniapp/wechat/index';
import bytedanceModule from './miniapp/bytedance/index';
import { AsyncStorage } from './types';

let AsyncStorage: AsyncStorage;
Expand All @@ -19,8 +20,11 @@ if (isMiniApp) {
AsyncStorage = miniAppModule;
}

if (isWeChatMiniprogram) {
if (isWeChatMiniProgram) {
AsyncStorage = weChatModule;
}
if (isByteDanceMicroApp) {
AsyncStorage = bytedanceModule;
}

export default AsyncStorage;
84 changes: 84 additions & 0 deletions packages/asyncstorage/src/miniapp/bytedance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { AsyncStorage } from '../../types';

declare const tt: any;

const AsyncStorage: AsyncStorage = {
getItem: (key: string): Promise<string | null> => {
return new Promise((resolve, reject): void => {
tt.getStorage({
key,
success: (res): void => {
resolve(res.data);
},
fail: (e): void => {
reject(e);
}
});
});
},
setItem: (key: string, value: object | string): Promise<null> => {
return new Promise((resolve, reject): void => {
tt.setStorage({
key,
data: value,
success: (): void => {
resolve(null);
},
fail: (e): void => {
reject(e);
}
});
});
},
removeItem: (key: string): Promise<null> => {
return new Promise((resolve, reject): void => {
tt.removeStorage({
key,
success: (): void => {
resolve(null);
},
fail: (e): void => {
reject(e);
}
});
});
},
getAllKeys: (): Promise<string[]> => {
return new Promise((resolve, reject): void => {
tt.getStorageInfo({
success: (res): void => {
resolve(res.keys);
},
fail: (e): void => {
reject(e);
}
});
});
},
clear: (): Promise<null> => {
return new Promise((resolve, reject): void => {
tt.clearStorage({
success: (): void => {
resolve(null);
},
fail: (e): void => {
reject(e);
}
});
});
},
length: (): Promise<number> => {
return new Promise((resolve, reject): void => {
tt.getStorageInfo({
success: (res): void => {
resolve(res.keys.length);
},
fail: (e): void => {
reject(e);
}
});
});
}
};

export default AsyncStorage;
11 changes: 9 additions & 2 deletions packages/request/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isWeex, isMiniApp, isWeChatMiniProgram, isWeb } from 'universal-env';
import { isWeex, isMiniApp, isWeChatMiniProgram, isWeb, isByteDanceMicroApp } from 'universal-env';
import {
RequestOptions,
DEFAULT_REQUEST_OPTIONS
Expand All @@ -8,6 +8,7 @@ import webModule from './web/index';
import weexModule from './weex/index';
import miniAppModule from './miniapp/index';
import weChatModule from './wechat-miniprogram/index';
import bytedanceModule from './miniapp-bytedance/index';

function dutyChain(...fns) {
for (let i = 0; i < fns.length; i++) {
Expand Down Expand Up @@ -49,7 +50,12 @@ function handleWeChatMiniprogram(afterOptions) {
}
return null;
}

function handleBytedanceMiniprogram(afterOptions) {
if (isByteDanceMicroApp) {
const request = bytedanceModule;
return request(afterOptions);
}
}
export default function(options: RequestOptions) {
let afterOptions: RequestOptions = Object.assign({},
DEFAULT_REQUEST_OPTIONS,
Expand All @@ -63,5 +69,6 @@ export default function(options: RequestOptions) {
handleWeex.bind(null, afterOptions),
handleWeb.bind(null, afterOptions),
handleMiniApp.bind(null, afterOptions),
handleBytedanceMiniprogram.bind(null, afterOptions),
handleWeChatMiniprogram.bind(null, afterOptions));
}
45 changes: 45 additions & 0 deletions packages/request/src/miniapp-bytedance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
RequestOptions,
ResponseData,
ERROR_REQUEST_TIMEOUT,
ERROR_REQUEST_ABORT
} from '../types';
import {
object2json
} from '../utils';
declare const tt: any;

export default function(options: RequestOptions): Promise<ResponseData> {
return new Promise((resolve, reject) => {
let { url, method, data, dataType, headers, timeout } = options;
let timeoutTimer;
let requestTask = tt.request({
url,
headers,
method,
data,
timeout,
dataType,
success: function(res: ResponseData) {
resolve(res);
},
fail: function(res) {
reject({
code: ERROR_REQUEST_ABORT.code,
message: object2json(res)
});
},
complete() {
if (timeoutTimer) {
clearTimeout(timeoutTimer);
}
}
});
if (timeout) {
timeoutTimer = setTimeout(() => {
requestTask.abort();
reject(ERROR_REQUEST_TIMEOUT);
}, timeout);
}
});
}