UNPKG

@codybrom/denim

Version:

A Deno/TypeScript library for the Threads API

58 lines (51 loc) 1.84 kB
import { REPLY_FIELDS, THREADS_API_BASE_URL } from "../constants.ts"; import type { PaginationOptions, ThreadsListResponse } from "../types.ts"; import { getAPI } from "../utils/getAPI.ts"; const DEFAULT_FIELDS = [ ...REPLY_FIELDS, "alt_text", "link_attachment_url", "poll_attachment", ]; /** * Retrieves a list of posts where the user is mentioned. * * @param userId - The user ID of the Threads account * @param accessToken - The access token for authentication * @param options - Optional pagination parameters * @param fields - Optional array of fields to return * @returns A Promise that resolves to the ThreadsListResponse * @throws Will throw an error if the API request fails */ export async function getMentions( userId: string, accessToken: string, options?: PaginationOptions, fields?: string[], ): Promise<ThreadsListResponse> { const api = getAPI(); if (api) { return api.getMentions(userId, accessToken, options, fields); } const fieldList = (fields ?? DEFAULT_FIELDS).join(","); const url = new URL(`${THREADS_API_BASE_URL}/${userId}/mentions`); url.searchParams.append("fields", fieldList); url.searchParams.append("access_token", accessToken); if (options) { if (options.since) url.searchParams.append("since", String(options.since)); if (options.until) url.searchParams.append("until", String(options.until)); if (options.limit) { url.searchParams.append("limit", options.limit.toString()); } if (options.after) url.searchParams.append("after", options.after); if (options.before) url.searchParams.append("before", options.before); } const response = await fetch(url.toString()); if (!response.ok) { const errorBody = await response.text(); throw new Error( `Failed to get mentions (${response.status}): ${errorBody}`, ); } return await response.json(); }