forked from tudinhacoustic/tiktok-shop
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
89 lines (81 loc) · 2.33 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import axios from 'axios';
import Common from './common/common';
import {Authorized} from './services/authorized';
import TikTok from './api/api';
interface Config {
appKey: string;
appSecret: string;
accessToken: string;
shopCipher: string;
shopId: string;
}
function TikTokClient({appKey, appSecret, accessToken, shopCipher, shopId}: Config){
if (!appKey) {
throw new Error('appKey is required');
}
if (!accessToken) {
throw new Error('accessToken is required');
}
if (!appSecret) {
throw new Error('appSecret is required');
}
return new TikTok({appKey, accessToken, shopCipher, shopId, appSecret});
}
function signature(config: Config, path: string) {
const error = Common.checkConfig(config);
if (error) {
throw new Error(error);
}
if (!path) {
throw new Error('path is required');
}
return Common.signature(config, path);
}
async function authCodeToken(config: Config, authCode: string) {
const error = Common.checkConfig(config);
if (error) {
throw new Error(error);
}
if (!authCode) {
throw new Error('authCode is required');
}
const authConfig = {app_key: config.appKey, app_secret:config.appSecret}
const url = Authorized.generateTokenByAuthCodeUrl(authConfig, authCode);
try {
const response = await axios.get(url);
return response.data
} catch (error) {
throw error;
}
}
async function generateToken(config: Config, refreshToken: string) {
const error = Common.checkConfig(config);
if (error) {
throw new Error(error);
}
if (!refreshToken) {
throw new Error('refreshToken is required');
}
const authConfig = {app_key: config.appKey, app_secret:config.appSecret}
const url = Authorized.generateTokenByRefreshToken(authConfig, refreshToken);
try {
const response = await axios.get(url);
return response.data.data ? response.data.data : response.data;
} catch (error) {
throw error;
}
}
function signByUrl(url: string = '', appSecret: string = '') {
const error = Common.checkUrl(url, appSecret);
if (error) {
throw new Error(error);
}
return Common.signByUrl(url, appSecret);
}
export {
signature,
authCodeToken,
generateToken,
signByUrl,
TikTokClient
}