import {z} from 'zod' export type CreateApiAccessCodesCreateMultipleArgs = {body: {allow_external_modification?: boolean | undefined, attempt_for_offline_device?: boolean | undefined, behavior_when_code_cannot_be_shared?: ('throw' | 'create_random_code') | undefined, code?: string | undefined, device_ids: Array, ends_at?: string | undefined, is_external_modification_allowed?: boolean | undefined, name?: string | undefined, prefer_native_scheduling?: boolean | undefined, preferred_code_length?: number | undefined, starts_at?: string | undefined, use_backup_access_code_pool?: boolean | undefined}}; export type CreateApiAccessCodesCreateMultipleOptions = {baseUrl?: string | undefined, clientSession?: string | undefined, fetch?: typeof fetch | undefined}; export const accessCode = z.object({access_code_id: z.string(), code: z.string().nullable(), common_code_key: z.string().nullable(), created_at: z.string(), device_id: z.string(), dormakaba_oracode_metadata: z.object({is_cancellable: z.boolean().optional(), is_early_checkin_able: z.boolean().optional(), is_extendable: z.boolean().optional(), is_overridable: z.boolean().optional(), site_name: z.string().optional(), stay_id: z.number().optional(), user_level_id: z.string().optional(), user_level_name: z.string().nullable().optional()}).nullable().optional(), ends_at: z.string().nullable().optional(), errors: z.array(z.object({error_code: z.string().optional(), message: z.string().optional(), created_at: z.string().optional()})), is_backup: z.boolean().optional(), is_backup_access_code_available: z.boolean(), is_external_modification_allowed: z.boolean(), is_managed: z.literal(true), is_offline_access_code: z.boolean(), is_one_time_use: z.boolean(), is_scheduled_on_device: z.boolean().optional(), is_waiting_for_code_assignment: z.boolean().optional(), name: z.string().nullable(), pending_mutations: z.array(z.discriminatedUnion("mutation_code", [z.object({created_at: z.string(), message: z.string(), mutation_code: z.literal("creating")}), z.object({created_at: z.string(), message: z.string(), mutation_code: z.literal("deferring_creation"), scheduled_at: z.string()}), z.object({created_at: z.string(), message: z.string(), mutation_code: z.literal("deleting")}), z.object({created_at: z.string(), from: z.object({code: z.string().nullable()}), message: z.string(), mutation_code: z.literal("updating_code"), to: z.object({code: z.string().nullable()})}), z.object({created_at: z.string(), from: z.object({name: z.string().nullable()}), message: z.string(), mutation_code: z.literal("updating_name"), to: z.object({name: z.string().nullable()})}), z.object({created_at: z.string(), from: z.object({ends_at: z.string().nullable(), starts_at: z.string().nullable()}), message: z.string(), mutation_code: z.literal("updating_time_frame"), to: z.object({ends_at: z.string().nullable(), starts_at: z.string().nullable()})})])), pulled_backup_access_code_id: z.string().nullable().optional(), starts_at: z.string().nullable().optional(), status: z.enum(["setting", "set", "unset", "removing", "unknown"]), type: z.enum(["time_bound", "ongoing"]), warnings: z.array(z.object({error_code: z.string().optional(), message: z.string().optional(), created_at: z.string().optional()})), workspace_id: z.string()}); export const createApiAccessCodesCreateMultipleResponse = z.object({access_codes: z.array(accessCode)}); 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 Multiple Linked Access Codes * * POST /access_codes/create_multiple * * Also accepts auth: client_session_with_customer, pat_with_workspace, console_session_with_workspace, api_key */ export const createApiAccessCodesCreateMultiple = async (args: CreateApiAccessCodesCreateMultipleArgs, options?: CreateApiAccessCodesCreateMultipleOptions) => { const {baseUrl = "https://connect.getseam.com", fetch: fetchFn = fetch, clientSession} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (clientSession !== undefined) headers.set('authorization', `Bearer ${clientSession}`) const url = `${baseUrl.replace(/\/+$/, '')}/access_codes/create_multiple${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(createApiAccessCodesCreateMultipleResponse, await res.json()) };