import {z} from 'zod' export type Question = {campaign_id?: number | undefined, created_at?: string | undefined, deleted_at?: (string | null) | undefined, id?: number | undefined, is_deleted?: boolean | undefined, is_required?: boolean | undefined, is_pre_conversion?: boolean | undefined, label: string, location: 'donation_page' | 'team_creation' | 'page_creation' | 'campaign_registration' | 'hidden', product_id?: (number | null) | undefined, tag?: ('address' | 'address2' | 'address_type' | 'birthdate' | 'blog' | 'cellphone' | 'city' | 'company_name' | 'country' | 'email_address' | 'email_type' | 'emergency_first_name' | 'emergency_last_name' | 'emergency_phone' | 'event_waiver' | 'first_name' | 'gender' | 'pronoun' | 'homephone' | 'last_name' | 'middle_name' | 'postal_code' | 'prefix' | 'run_walk' | 'shirt_size' | 'state' | 'suffix' | 'text_opt_in' | 'website' | null) | undefined, type: 'boolean' | 'integer' | 'currency' | 'enum' | 'text' | 'string' | 'date' | 'yes_no', updated_at?: (string | null) | undefined, weight?: (number | null) | undefined}; export type CreateApiCampaignsCampaignQuestionsArgs = {campaign: number, body: Question}; export type CreateApiCampaignsCampaignQuestionsOptions = {baseUrl?: string | undefined, OAuth2Member?: string | undefined, fetch?: typeof fetch | undefined}; export const questionEnumOption = z.object({label: z.string().optional(), question_id: z.number().int().optional()}); export const questionWithEnumOptions = z.object({campaign_id: z.number().int().optional(), created_at: z.string().optional(), deleted_at: z.string().nullable().optional(), id: z.number().int().optional(), is_deleted: z.boolean().optional(), is_required: z.boolean().optional(), is_pre_conversion: z.boolean().optional(), label: z.string(), location: z.enum(["donation_page", "team_creation", "page_creation", "campaign_registration", "hidden"]), product_id: z.number().int().nullable().optional(), tag: z.enum(["address", "address2", "address_type", "birthdate", "blog", "cellphone", "city", "company_name", "country", "email_address", "email_type", "emergency_first_name", "emergency_last_name", "emergency_phone", "event_waiver", "first_name", "gender", "pronoun", "homephone", "last_name", "middle_name", "postal_code", "prefix", "run_walk", "shirt_size", "state", "suffix", "text_opt_in", "website"]).nullable().optional(), type: z.enum(["boolean", "integer", "currency", "enum", "text", "string", "date", "yes_no"]), updated_at: z.string().nullable().optional(), weight: z.number().int().nullable().optional(), enum_options: z.array(questionEnumOption).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 }; /** * createCampaignQuestion * * POST /campaigns/{campaign}/questions */ export const createApiCampaignsCampaignQuestions = async (args: CreateApiCampaignsCampaignQuestionsArgs, options?: CreateApiCampaignsCampaignQuestionsOptions) => { const {baseUrl = "https://pro.gofundme.com/api/2.0", fetch: fetchFn = fetch, OAuth2Member} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2Member !== undefined) headers.set('authorization', `Bearer ${OAuth2Member}`) const url = `${baseUrl.replace(/\/+$/, '')}/campaigns/${encodeURIComponent(String(args.campaign))}/questions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(questionWithEnumOptions, await res.json()) };