import {z} from 'zod' export type FormsFieldParameters = {label: string, field_type?: (string | null) | undefined, allow_multiple?: (boolean | null) | undefined, key: string, hidden?: (boolean | null) | undefined}; export type CreateApiFormsFieldSetsFieldSetIdFieldsArgs = {field_set_id: string, body: {forms_field?: FormsFieldParameters | undefined}}; export type CreateApiFormsFieldSetsFieldSetIdFieldsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const formsFieldAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), field_set_id: z.number().int().optional(), label: z.string().optional(), field_type: z.string().nullable().optional(), allow_multiple: z.boolean().nullable().optional(), key: z.string().optional(), hidden: z.boolean().nullable().optional(), sort_order: z.number().int().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().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 }; /** * Create Field * * POST /forms/field_sets/{field_set_id}/fields * * Also accepts auth: OAuth2 */ export const createApiFormsFieldSetsFieldSetIdFields = async (args: CreateApiFormsFieldSetsFieldSetIdFieldsArgs, options?: CreateApiFormsFieldSetsFieldSetIdFieldsOptions) => { const {baseUrl = "https://myworkspace.myclickfunnels.com/api/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/forms/field_sets/${encodeURIComponent(String(args.field_set_id))}/fields${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(formsFieldAttributes, await res.json()) };