import {z} from 'zod' export type CreateApiAccessMethodsGetArgs = {body: {access_method_id: string}}; export type CreateApiAccessMethodsGetOptions = {baseUrl?: string | undefined, patWithWorkspace?: string | undefined, fetch?: typeof fetch | undefined}; export const accessMethod = z.object({access_method_id: z.string(), client_session_token: z.string().optional(), code: z.string().nullable().optional(), created_at: z.string(), customization_profile_id: z.string().optional(), display_name: z.string(), errors: z.array(z.object({created_at: z.string(), error_code: z.literal("failed_to_issue"), message: z.string()})), instant_key_url: z.string().optional(), is_assignment_required: z.boolean().optional(), is_encoding_required: z.boolean().optional(), is_issued: z.boolean(), is_ready_for_assignment: z.boolean().optional(), is_ready_for_encoding: z.boolean().optional(), issued_at: z.string().nullable(), mode: z.enum(["code", "card", "mobile_key", "cloud_key"]), 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("provisioning_access"), to: z.object({device_ids: z.array(z.string())})}), z.object({created_at: z.string(), from: z.object({device_ids: z.array(z.string())}), message: z.string(), mutation_code: z.literal("revoking_access"), to: z.object({device_ids: z.array(z.string())})}), 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_access_times"), to: z.object({ends_at: z.string().nullable(), starts_at: z.string().nullable()})})])), 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("updating_access_times")}), z.object({created_at: z.string(), message: z.string(), original_access_method_id: z.string().optional(), warning_code: z.literal("pulled_backup_access_code")}), z.object({created_at: z.string(), message: z.string(), warning_code: z.literal("delay_in_issuing")})])), workspace_id: z.string()}); export const createApiAccessMethodsGetResponse = z.object({access_method: accessMethod}); 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 Access Method * * POST /access_methods/get * * Also accepts auth: console_session_with_workspace, api_key */ export const createApiAccessMethodsGet = async (args: CreateApiAccessMethodsGetArgs, options?: CreateApiAccessMethodsGetOptions) => { 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_methods/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(createApiAccessMethodsGetResponse, await res.json()) };