import {z} from 'zod' export type UpdateSignupFormInput = {name?: string | undefined, segment_id?: (string | null) | undefined, topic_id?: (string | null) | undefined, fields?: (Array<{name: string, type: 'email' | 'text' | 'select', required: boolean, label: string, placeholder?: string | undefined, options?: Array | undefined}>) | undefined, success_message?: string | undefined, redirect_url?: (string | null) | undefined, active?: boolean | undefined, human_check?: ({site_key: string, secret: string} | null) | undefined}; export type PatchApiSignupFormsIdArgs = {id: string, body: UpdateSignupFormInput}; export type PatchApiSignupFormsIdOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const signupForm = z.object({id: z.number().int(), publicId: z.string(), name: z.string(), segmentId: z.number().int().nullable().optional(), topicId: z.number().int().nullable().optional(), fields: z.array(z.object({})), styling: z.object({}).nullable().optional(), successMessage: z.string().nullable().optional(), redirectUrl: z.string().nullable().optional(), humanCheckSiteKey: z.string().nullable().optional(), humanCheckEnabled: z.boolean().optional(), active: z.boolean(), submissionCount: z.number().int().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const patchApiSignupFormsIdResponse = z.object({signupForm: signupForm}); 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 }; /** * Update a form * * PATCH /signup-forms/{id} * * Also accepts auth: bearerOAuth */ export const patchApiSignupFormsId = async (args: PatchApiSignupFormsIdArgs, options?: PatchApiSignupFormsIdOptions) => { const {baseUrl = "https://api.poststack.dev", fetch: fetchFn = fetch, bearerApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/signup-forms/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiSignupFormsIdResponse, await res.json()) };