import {z} from 'zod' export type GetApiV1LinksArgs = {limit?: number | undefined, after?: string | undefined, before?: string | undefined, state?: ('active' | 'pending' | 'disabled' | 'archived') | undefined, scope?: string | undefined, q?: string | undefined}; export type GetApiV1LinksOptions = {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 link = 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 paginationMeta = z.object({after: z.string().nullable().optional(), before: z.string().nullable().optional(), limit: z.number().int()}); export const linkListResponse = z.object({entries: z.array(link), metadata: paginationMeta}); 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 }; /** * List scheduling links * * GET /v1/links * * Also accepts auth: PersonalAccessToken */ export const getApiV1Links = async (args: GetApiV1LinksArgs, options?: GetApiV1LinksOptions) => { const {baseUrl = "https://api.savvycal.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.state != null) query.append("state", String(args.state)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.q != null) query.append("q", String(args.q)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/links${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(linkListResponse, await res.json()) };