import {z} from 'zod' export type UpdateLinkRequest = {description?: (string | null) | undefined, name?: string | undefined, private_name?: (string | null) | undefined, slug?: string | undefined, type?: ('recurring' | 'single') | undefined}; export type PatchApiV1LinksLinkIdArgs = {link_id: string, body: UpdateLinkRequest}; export type PatchApiV1LinksLinkIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const fieldOption = z.object({id: z.string(), value: z.string()}); export const linkField = z.object({id: z.string(), is_required: z.boolean(), label: z.string(), options: z.array(fieldOption), type: z.enum(["short_text", "long_text", "checkboxes", "radios", "select", "url", "hidden"])}); export const scopePreview = z.object({id: z.string(), name: z.string(), slug: z.string()}); export const singleLinkResponse = z.object({default_duration: z.number().int(), description: z.string().optional(), durations: z.array(z.number().int()), fields: z.array(linkField), id: z.string(), increment: z.number().int(), name: z.string(), private_name: z.string().nullable().optional(), scope: scopePreview.optional(), slug: z.string(), state: z.enum(["active", "pending", "disabled", "archived", "deleted"])}); 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 scheduling link * * PATCH /v1/links/{link_id} * * Also accepts auth: PersonalAccessToken */ export const patchApiV1LinksLinkId = async (args: PatchApiV1LinksLinkIdArgs, options?: PatchApiV1LinksLinkIdOptions) => { const {baseUrl = "https://api.savvycal.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/links/${encodeURIComponent(String(args.link_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(singleLinkResponse, await res.json()) };