import {z} from 'zod' export type GetApiUsersIdRepostsArgs = {id: string, offset?: number | undefined, limit?: number | undefined, user_id?: string | undefined}; export type GetApiUsersIdRepostsOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const versionMetadata = z.object({service: z.string(), version: z.string()}); export const activity = z.object({timestamp: z.string(), item_type: z.enum(["track", "playlist"]), item: z.object({}), class: z.string()}); export const reposts = z.object({latest_chain_block: z.number().int(), latest_indexed_block: z.number().int(), latest_chain_slot_plays: z.number().int(), latest_indexed_slot_plays: z.number().int(), signature: z.string(), timestamp: z.string(), version: versionMetadata, data: z.array(activity).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 }; /** * Gets the given user's reposts * * GET /users/{id}/reposts */ export const getApiUsersIdReposts = async (args: GetApiUsersIdRepostsArgs, options: GetApiUsersIdRepostsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.user_id != null) query.append("user_id", String(args.user_id)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/users/${encodeURIComponent(String(args.id))}/reposts${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(reposts, await res.json()) };