import type { TiktokVideoOptions } from "./options"; import type { TiktokVideoListSchema } from "./schema"; import { UnauthorizedError } from "~/shared/datasources/errors"; import type { TiktokFullOAuthConfig } from "../oauth/config"; import type { DatasourceFetcher } from "~/shared/datasources/fetcher"; const fetchTiktokVideoDatasource: DatasourceFetcher< TiktokVideoListSchema, TiktokFullOAuthConfig, TiktokVideoOptions > = async ({ options, oauth }) => { const params = new URLSearchParams({ access_token: oauth.config.accessToken, fields: ["id", "title", "video_description", "duration", "cover_image_url", "embed_link"].join(","), }); const url = `https://open.tiktokapis.com/v2/video/list/?${params.toString()}`; const body = options; const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${oauth.config.accessToken}`, }, body: JSON.stringify(body), }); if (!response.ok) { if (response.status === 401) { throw new UnauthorizedError(`fetchTiktokVideoDatasource Error: Unauthorized.`); } throw new Error(`Response status: ${response.status}`); } const data = (await response.json()) as TiktokVideoListSchema; return data; }; export default fetchTiktokVideoDatasource;