feat: update types

This commit is contained in:
TobyG74 2023-11-06 00:44:02 +07:00
parent 5a921624e2
commit 4484986fae
10 changed files with 74 additions and 15 deletions

55
src/client/cookie.ts Normal file
View File

@ -0,0 +1,55 @@
import { TiktokAPI, SSSTik, MusicalDown, TiktokStalk } from "../index"
export class Client {
private cookie: string[]
constructor() {
this.cookie = []
}
public getCookie(): string {
if (this.cookie.length === 0) throw new Error("Please login your account first!")
return this.cookie[0]
}
public getAllCookie(): string[] {
if (this.cookie.length === 0) throw new Error("Please login your account first!")
return this.cookie
}
public setCookie(cookie: string) {
this.cookie.push(cookie)
}
public deleteCookie(index: number) {
this.cookie.splice(index - 1, 1)
}
public clearCookie() {
this.cookie = []
}
public TiktokDL = (url: string, options: { version: "v1" | "v2" | "v3" }) =>
new Promise(async (resolve, reject) => {
switch (options.version) {
case "v1": {
await TiktokAPI(url).then(resolve).catch(reject)
}
case "v2": {
await SSSTik(url).then(resolve).catch(reject)
}
case "v3": {
await MusicalDown(url).then(resolve).catch(reject)
}
default: {
await TiktokAPI(url).then(resolve).catch(reject)
}
}
})
public TiktokStalk = (username: string) =>
new Promise(async (resolve, reject) => {
const cookie = this.getCookie()
await TiktokStalk(username, { cookie: cookie }).then(resolve).catch(reject)
})
}

View File

@ -1 +1,4 @@
export * from "./utils"
export * from "./utils/dl_tiktokapi"
export * from "./utils/dl_ssstik"
export * from "./utils/dl_musicaldown"
export * from "./utils/tiktok_stalker"

View File

@ -4,7 +4,7 @@ export interface SSSTikFetchTT {
result?: string
}
export interface SSSTikResult {
export interface SSSTikResponse {
status: "success" | "error"
message?: string
result?: {

View File

@ -1,4 +1,4 @@
export interface DLResult {
export interface TiktokAPIResponse {
status: "success" | "error"
message?: string
result?: {

View File

@ -1,6 +1,6 @@
import Axios from "axios"
import { load } from "cheerio"
import { Author, Statistics, SSSTikFetchTT, SSSTikResult } from "../types/ssstik"
import { Author, Statistics, SSSTikFetchTT, SSSTikResponse } from "../types/ssstik"
import { _ssstikapi, _ssstikurl } from "../api"
/**
@ -29,7 +29,7 @@ const fetchTT = () =>
})
export const SSSTik = (url: string) =>
new Promise<SSSTikResult>(async (resolve, reject) => {
new Promise<SSSTikResponse>(async (resolve, reject) => {
const tt: SSSTikFetchTT = await fetchTT()
if (tt.status !== "success") return resolve({ status: "error", message: tt.message })
Axios(_ssstikapi, {

View File

@ -1,9 +1,9 @@
import axios from "axios"
import { _tiktokapi, _tiktokurl } from "../api"
import { Author, DLResult, Statistics, Music } from "../types/tiktokapi"
import { Author, TiktokAPIResponse, Statistics, Music } from "../types/tiktokApi"
export const TiktokAPI = (url: string): Promise<DLResult> =>
new Promise((resolve, reject) => {
export const TiktokAPI = (url: string) =>
new Promise<TiktokAPIResponse>((resolve, reject) => {
url = url.replace("https://vm", "https://vt")
axios
.head(url)

View File

@ -1,2 +0,0 @@
export * from "./switch"
export * from "./stalker"

View File

@ -1,9 +1,12 @@
import { MusicalDown } from "./musicaldown"
import { SSSTik } from "./ssstik"
import { TiktokAPI } from "./tiktokapi"
import { MusicalDownResponse } from "../types/musicaldown"
import { SSSTikResponse } from "../types/ssstik"
import { TiktokAPIResponse } from "../types/tiktokApi"
import { MusicalDown } from "./downloader_musicaldown"
import { SSSTik } from "./downloader_ssstik"
import { TiktokAPI } from "./downloader_tiktokApi"
export const TiktokDL = (url: string, options: { version: "v1" | "v2" | "v3" }) =>
new Promise(async (resolve, reject) => {
new Promise<TiktokAPIResponse | SSSTikResponse | MusicalDownResponse>(async (resolve, reject) => {
switch (options.version) {
case "v1": {
await TiktokAPI(url).then(resolve).catch(reject)

View File

@ -19,7 +19,7 @@ export const TiktokStalk = (username: string, options: { cookie: string }): Prom
axios
.get(`${_tiktokurl}/@${username}`, {
headers: {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36",
cookie: (options?.cookie ? options.cookie : await getCookie()) as string
}
})