import { getRedditApi as getRedditContractApi, RedditApiError, } from "@automate.ax/integration-contracts/reddit" import { parseRetryAfter, retryableActionError, terminalActionError, } from "../../automation/actions" export const REDDIT_ACCOUNT = "reddit" /** Builds the OAuth account requirement for one Reddit scope. */ /** @param requiredScope - Least-privilege Reddit OAuth scope. */ export function redditAccountRequirement(requiredScope: string) { return { connections: [{ connectionMethodId: "oauth", requiredScope }], } as const } /** Creates a Reddit client whose failures carry action retry disposition. */ /** @param secret - Stored Reddit OAuth secret. */ export function getRedditApi(secret: unknown) { const api = getRedditContractApi(secret) const request = api.request.bind(api) api.request = async (path, options) => { try { return await request(path, options) } catch (error) { if (error instanceof RedditApiError) { if (error.status === 429) { throw retryableActionError(error, { retryAt: parseRetryAfter(error.retryAfter), }) } if (error.status < 500) throw terminalActionError(error) } throw error } } return api } /** Encodes one Reddit path segment without changing the path shape. */ /** @param value - Untrusted path-segment value. */ export function redditPathSegment(value: string) { return encodeURIComponent(value) }