import {z} from 'zod' export type CreateApiAccessGrantsRequestAccessMethodsArgs = {body: {access_grant_id: string, requested_access_methods: Array<{code?: string | undefined, instant_key_max_use_count?: number | undefined, mode: 'code' | 'card' | 'mobile_key' | 'cloud_key'}>}}; export type CreateApiAccessGrantsRequestAccessMethodsOptions = {baseUrl?: string | undefined, patWithWorkspace?: string | undefined, fetch?: typeof fetch | undefined}; export const accessGrant = z.object({access_grant_id: z.string(), access_grant_key: z.string().optional(), access_method_ids: z.array(z.string()), client_session_token: z.string().optional(), created_at: z.string(), customization_profile_id: z.string().optional(), 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()})), instant_key_url: 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(), 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 createApiAccessGrantsRequestAccessMethodsResponse = z.object({access_grant: accessGrant}); 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 }; /** * Add Requested Access Methods to Access Grant * * POST /access_grants/request_access_methods * * Also accepts auth: console_session_with_workspace, api_key, client_session_with_customer */ export const createApiAccessGrantsRequestAccessMethods = async (args: CreateApiAccessGrantsRequestAccessMethodsArgs, options?: CreateApiAccessGrantsRequestAccessMethodsOptions) => { 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/request_access_methods${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(createApiAccessGrantsRequestAccessMethodsResponse, await res.json()) };