import {z} from 'zod' export type CampaignChannel = {campaign_id?: number | undefined, channel_name: 'DoubletheDonation' | 'Facebook' | 'Instagram' | 'MetaCapi' | 'MetaFrapi' | 'Zapier', created_at?: string | undefined, id?: number | undefined, status: boolean, metadata?: ({employer_match_on_donation_page?: boolean | undefined, employer_match_on_thankyou_page?: boolean | undefined, employer_match_preselect?: boolean | undefined} | {progress_metrics?: boolean | undefined, external_fundraiser_id?: string | undefined} | null) | undefined, processing?: boolean | undefined, updated_at?: string | undefined}; export type CreateApiCampaignsCampaignChannelsArgs = {campaign: number, body: CampaignChannel}; export type CreateApiCampaignsCampaignChannelsOptions = {baseUrl?: string | undefined, OAuth2Member?: string | undefined, fetch?: typeof fetch | undefined}; export const campaignChannel = z.object({campaign_id: z.number().int().optional(), channel_name: z.enum(["DoubletheDonation", "Facebook", "Instagram", "MetaCapi", "MetaFrapi", "Zapier"]), created_at: z.string().optional(), id: z.number().int().optional(), status: z.boolean(), metadata: z.union([z.object({employer_match_on_donation_page: z.boolean().optional(), employer_match_on_thankyou_page: z.boolean().optional(), employer_match_preselect: z.boolean().optional()}), z.object({progress_metrics: z.boolean().optional(), external_fundraiser_id: z.string().optional()})]).nullable().optional(), processing: z.boolean().optional(), updated_at: 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 }; /** * createCampaignChannel * * POST /campaigns/{campaign}/channels */ export const createApiCampaignsCampaignChannels = async (args: CreateApiCampaignsCampaignChannelsArgs, options?: CreateApiCampaignsCampaignChannelsOptions) => { 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))}/channels${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(campaignChannel, await res.json()) };