import {z} from 'zod' export type CreateApiAccessGrantsUnmanagedGetArgs = {body: {access_grant_id: string}}; export type CreateApiAccessGrantsUnmanagedGetOptions = {baseUrl?: string | undefined, patWithWorkspace?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiAccessGrantsUnmanagedGetResponse = z.object({access_grant: z.object({access_grant_id: z.string(), access_method_ids: z.array(z.string()), created_at: z.string(), display_name: z.string(), ends_at: z.string().nullable(), errors: z.array(z.object({created_at: z.string(), error_code: z.literal("cannot_create_requested_access_methods"), message: z.string(), missing_device_ids: z.array(z.string()).optional()})), location_ids: z.array(z.string()), name: z.string().nullable(), pending_mutations: z.array(z.discriminatedUnion("mutation_code", [z.object({created_at: z.string(), from: z.object({device_ids: z.array(z.string())}), message: z.string(), mutation_code: z.literal("updating_spaces"), to: z.object({common_code_key: z.string().nullable().optional(), device_ids: z.array(z.string())})}), z.object({access_method_ids: z.array(z.string()), 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_access_times"), to: z.object({ends_at: z.string().nullable(), starts_at: z.string().nullable()})})])), requested_access_methods: z.array(z.object({code: z.string().min(4).max(9).optional(), created_access_method_ids: z.array(z.string()), created_at: z.string(), display_name: z.string(), instant_key_max_use_count: z.number().int().gte(1).optional(), mode: z.enum(["code", "card", "mobile_key", "cloud_key"])})), reservation_key: z.string().optional(), space_ids: z.array(z.string()), starts_at: z.string(), user_identity_id: z.string().optional(), warnings: z.array(z.discriminatedUnion("warning_code", [z.object({created_at: z.string(), message: z.string(), warning_code: z.literal("being_deleted")}), z.object({created_at: z.string(), message: z.string(), warning_code: z.literal("underprovisioned_access")}), z.object({created_at: z.string(), failed_devices: z.array(z.object({device_id: z.string(), error_code: z.string(), message: z.string()})).optional(), message: z.string(), warning_code: z.literal("overprovisioned_access")}), z.object({access_method_ids: z.array(z.string()), created_at: z.string(), message: z.string(), warning_code: z.literal("updating_access_times")}), z.object({created_at: z.string(), device_id: z.string(), message: z.string(), new_code: z.string(), original_code: z.string(), warning_code: z.literal("requested_code_unavailable")}), z.object({created_at: z.string(), device_id: z.string(), message: z.string(), warning_code: z.literal("device_does_not_support_access_codes")}), z.object({created_at: z.string(), device_id: z.string(), message: z.string(), reason: z.enum(["duration_exceeds_max", "times_do_not_match_slots", "ongoing_not_supported"]), warning_code: z.literal("device_time_constraints_violated")})])), workspace_id: z.string()})}); 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 }; /** * Get an Unmanaged Access Grant * * POST /access_grants/unmanaged/get * * Also accepts auth: console_session_with_workspace, api_key, client_session_with_customer */ export const createApiAccessGrantsUnmanagedGet = async (args: CreateApiAccessGrantsUnmanagedGetArgs, options?: CreateApiAccessGrantsUnmanagedGetOptions) => { const {baseUrl = "https://connect.getseam.com", fetch: fetchFn = fetch, patWithWorkspace} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (patWithWorkspace !== undefined) headers.set('authorization', `Bearer ${patWithWorkspace}`) const url = `${baseUrl.replace(/\/+$/, '')}/access_grants/unmanaged/get${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(createApiAccessGrantsUnmanagedGetResponse, await res.json()) };