import {z} from 'zod' export type GetApiStoriesStoryCommentsArgs = {story: number, page?: number | undefined, per_page?: number | undefined}; export type GetApiStoriesStoryCommentsOptions = {baseUrl?: string | undefined, OAuth2Application?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationLink = z.object({label: z.string().optional(), url: z.string().nullable().optional(), active: z.boolean().optional()}); export const getApiStoriesStoryCommentsResponse = z.object({current_page: z.number().int().optional(), data: z.array(z.object({body: z.string().optional(), commentable_id: z.number().int().optional(), commentable_type: z.enum(["feed_item", "update", "story"]).optional(), commenter_name: z.string().optional(), author_display_name: z.string().nullable().optional(), created_at: z.string().optional(), id: z.number().int().optional(), likes_count: z.number().int().optional(), member_id: z.number().int().optional(), updated_at: z.string().optional()})).optional(), first_page_url: z.string().nullable().optional(), from: z.number().int().nullable().optional(), last_page: z.number().int().optional(), last_page_url: z.string().nullable().optional(), next_page_url: z.string().nullable().optional(), path: z.string().nullable().optional(), per_page: z.number().int().optional(), prev_page_url: z.string().nullable().optional(), links: z.array(paginationLink).optional(), to: z.number().int().nullable().optional(), total: z.number().int().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 }; /** * listStoryComments * * GET /stories/{story}/comments * * Also accepts auth: OAuth2Member */ export const getApiStoriesStoryComments = async (args: GetApiStoriesStoryCommentsArgs, options?: GetApiStoriesStoryCommentsOptions) => { const {baseUrl = "https://pro.gofundme.com/api/2.0", fetch: fetchFn = fetch, OAuth2Application} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (OAuth2Application !== undefined) headers.set('authorization', `Bearer ${OAuth2Application}`) const url = `${baseUrl.replace(/\/+$/, '')}/stories/${encodeURIComponent(String(args.story))}/comments${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(getApiStoriesStoryCommentsResponse, await res.json()) };