import {z} from 'zod' export type CoursesSectionParametersUpdate = {title?: string | undefined, description?: (string | null) | undefined, days_till_drip_access?: (number | null) | undefined, blocker_section_id?: (number | null) | undefined, blocker_lesson_id?: (number | null) | undefined, is_hidden_from_non_members?: (boolean | null) | undefined}; export type CreateApiCoursesCourseIdSectionsArgs = {course_id: string, body: {courses_section?: CoursesSectionParametersUpdate | undefined}}; export type CreateApiCoursesCourseIdSectionsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coursesSectionAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), course_id: z.number().int().optional(), title: z.string().optional(), published_at: z.string().nullable().optional(), description: z.string().nullable().optional(), days_till_drip_access: z.number().int().nullable().optional(), blocker_section_id: z.number().int().nullable().optional(), blocker_lesson_id: z.number().int().nullable().optional(), current_path: z.string().nullable().optional(), is_hidden_from_non_members: z.boolean().nullable().optional(), upgrade_url: z.string().nullable().optional(), parent_id: z.string().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 Section * * POST /courses/{course_id}/sections * * Also accepts auth: OAuth2 */ export const createApiCoursesCourseIdSections = async (args: CreateApiCoursesCourseIdSectionsArgs, options?: CreateApiCoursesCourseIdSectionsOptions) => { 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(/\/+$/, '')}/courses/${encodeURIComponent(String(args.course_id))}/sections${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(coursesSectionAttributes, await res.json()) };