import {z} from 'zod' export type GetApiSurveyWorkflowsIdArgs = {id: string}; export type GetApiSurveyWorkflowsIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const surveyChoiceAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), label: z.string().nullable().optional(), image: z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), url: z.string().optional()}).nullable().optional()}); export const surveyFieldAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), key: z.string().nullable().optional(), label: z.string().nullable().optional(), field_type: z.string().optional(), allow_multiple: z.boolean().optional(), captured_contact_attribute: z.string().nullable().optional(), options: z.array(surveyChoiceAttributes).optional()}); export const surveyQuestionAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), type: z.enum(["text", "email", "phone_number", "choice", "image_choice", "contact_info", "single"]).optional(), title: z.string().nullable().optional(), field_set_id: z.number().int().nullable().optional(), fields: z.array(surveyFieldAttributes).optional()}); export const surveyWorkflowAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), name: z.string().nullable().optional(), site_id: z.number().int().optional(), form_id: z.number().int().nullable().optional(), enabled: z.boolean().optional(), archived: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), questions: z.array(surveyQuestionAttributes).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 }; /** * Fetch Survey * * GET /survey_workflows/{id} */ export const getApiSurveyWorkflowsId = async (args: GetApiSurveyWorkflowsIdArgs, options?: GetApiSurveyWorkflowsIdOptions) => { const {baseUrl = "https://myworkspace.myclickfunnels.com/api/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/survey_workflows/${encodeURIComponent(String(args.id))}${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(surveyWorkflowAttributes, await res.json()) };