import {z} from 'zod' export type GetApiCheckShadowbanArgs = {username: string}; export type GetApiCheckShadowbanOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const verificationShadowbanAccountState = z.object({possibly_sensitive: z.boolean().optional(), protected: z.boolean().optional(), statuses_count: z.number().int().optional()}); export const verificationShadowbanCheckResult = z.object({reason: z.string().optional(), status: z.string().optional(), tweets_found: z.number().int().optional()}); export const verificationShadowbanChecks = z.object({search_ban: verificationShadowbanCheckResult.optional(), search_suggestion_ban: verificationShadowbanCheckResult.optional()}); export const verificationCheckShadowbanResp = z.object({account: verificationShadowbanAccountState.optional(), checked_at: z.string().optional(), checks: verificationShadowbanChecks.optional(), is_shadowbanned: z.boolean().optional(), user_id: z.string().optional(), username: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Check Shadowban * * GET /check-shadowban */ export const getApiCheckShadowban = async (args: GetApiCheckShadowbanArgs, options?: GetApiCheckShadowbanOptions) => { const {baseUrl = "https://api.sorsa.io/v3", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.username != null) query.append("username", String(args.username)) if (ApiKeyAuth !== undefined) headers.set("ApiKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/check-shadowban${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(verificationCheckShadowbanResp, await res.json()) };