import {z} from 'zod' export type CreateApiCardsArgs = {body: {card: {token: string, type: 'mifare_desfire'}} | {card: {uid: string, type: 'third_party_hf'}} | {card: {type: 'third_party_hf_mifare_classic', data_block_value: string}} | {card: {type: 'third_party_lf_hid_corporate1000_35', facility_code: number, card_number: number}} | {card: {type: 'third_party_lf_hid_h10301', facility_code: number, card_number: number}} | {card: {type: 'third_party_lf_hid_h10302', card_number: number}} | {card: {type: 'third_party_lf_hid_h10304', facility_code: number, card_number: number}} | {card: {type: 'third_party_lf_hid_h2004064', facility_code: number, card_number: number}} | {card: {type: 'third_party_lf_em_410x_26', facility_code: number, card_number: number}} | {card: {type: 'third_party_lf_em_410x_32_34', card_id: number}} | {card: {type: 'third_party_lf_keyscan_c15001', facility_code: number, card_number: number, oem: number}} | {card: {type: 'third_party_lf_hid_d10202', facility_code: number, card_number: number}} | {card: {uid: string, type: 'third_party_lf', length: number}} | {card: {uid: string, type: 'third_party_qr_code'}} | {card: {uid: string, type: 'third_party_barcode'}} | {card: {uid: string, type: 'third_party_qr_code_rotating_timestamp', validity_window?: ('PT5S' | 'PT10S' | 'PT30S' | 'PT1M' | 'PT5M' | 'PT1H' | 'P1D') | undefined}} | {card: {uid: string, type: 'third_party_barcode_rotating_timestamp', validity_window?: ('PT5S' | 'PT10S' | 'PT30S' | 'PT1M' | 'PT5M' | 'PT1H' | 'P1D') | undefined}} | {card: {uid: string, type: 'third_party_qr_code_club_os', validity_window?: ('PT5S' | 'PT10S' | 'PT30S' | 'PT1M' | 'PT5M' | 'PT1H' | 'P1D') | undefined}}}; export type CreateApiCardsOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCardsResponse = z.union([z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), token: z.string(), uid: z.string(), type: z.literal("mifare_desfire"), model: z.enum(["card-black-1.0", "card-black-1.1", "card-black-2.0", "card-white-1.0", "card-white-1.1", "card-white-2.0", "card-custom-1.1", "fob-1.0", "fob-1.1", "fob-2.0", "tag-1.0", "tag-1.1", "tag-2.0"]), frequency: z.literal("HF"), color: z.enum(["white", "black", "custom", "unknown"]), form_factor: z.enum(["card", "fob", "tag", "unknown"]), third_party: z.literal(false)}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string(), type: z.literal("third_party_hf"), model: z.literal("credential-third-party"), frequency: z.literal("HF"), third_party: z.literal(true)}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), type: z.literal("third_party_hf_mifare_classic"), model: z.literal("credential-third-party"), frequency: z.literal("HF"), third_party: z.literal(true), data_block_value: z.string()}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string(), type: z.literal("third_party_lf_hid_corporate1000_35"), model: z.literal("credential-third-party-lf"), frequency: z.literal("LF"), third_party: z.literal(true), format_name: z.literal("hid_corporate1000_35"), facility_code: z.number().int(), card_number: z.number().int()}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string(), type: z.literal("third_party_lf_hid_h10301"), model: z.literal("credential-third-party-lf"), frequency: z.literal("LF"), third_party: z.literal(true), format_name: z.literal("hid_h10301"), facility_code: z.number().int(), card_number: z.number().int(), two_factor_pin: z.string().nullable(), send_two_factor_pin_notification: z.boolean()}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string(), type: z.literal("third_party_lf_hid_h10302"), model: z.literal("credential-third-party-lf"), frequency: z.literal("LF"), third_party: z.literal(true), format_name: z.literal("hid_h10302"), card_number: z.number().int(), two_factor_pin: z.string().nullable(), send_two_factor_pin_notification: z.boolean()}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string(), type: z.literal("third_party_lf_hid_h10304"), model: z.literal("credential-third-party-lf"), frequency: z.literal("LF"), third_party: z.literal(true), format_name: z.literal("hid_h10304"), facility_code: z.number().int(), card_number: z.number().int(), two_factor_pin: z.string().nullable(), send_two_factor_pin_notification: z.boolean()}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string(), type: z.literal("third_party_lf_hid_h2004064"), model: z.literal("credential-third-party-lf"), frequency: z.literal("LF"), third_party: z.literal(true), format_name: z.literal("hid_h2004064"), facility_code: z.number().int(), card_number: z.number().int()}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string(), type: z.literal("third_party_lf_em_410x_26"), model: z.literal("credential-third-party-lf"), frequency: z.literal("LF"), third_party: z.literal(true), format_name: z.literal("em_410x_26"), facility_code: z.number().int(), card_number: z.number().int(), two_factor_pin: z.string().nullable(), send_two_factor_pin_notification: z.boolean()}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string(), type: z.literal("third_party_lf_em_410x_32_34"), model: z.literal("credential-third-party-lf"), frequency: z.literal("LF"), third_party: z.literal(true), format_name: z.literal("em_410x_32_34"), card_id: z.number().int(), two_factor_pin: z.string().nullable(), send_two_factor_pin_notification: z.boolean()}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string(), type: z.literal("third_party_lf_keyscan_c15001"), model: z.literal("credential-third-party-lf"), frequency: z.literal("LF"), third_party: z.literal(true), format_name: z.literal("keyscan_c15001"), facility_code: z.number().int(), card_number: z.number().int(), oem: z.number().int(), two_factor_pin: z.string().nullable(), send_two_factor_pin_notification: z.boolean()}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string(), type: z.literal("third_party_lf_hid_d10202"), model: z.literal("credential-third-party-lf"), frequency: z.literal("LF"), third_party: z.literal(true), format_name: z.literal("hid_d10202"), facility_code: z.number().int(), card_number: z.number().int(), two_factor_pin: z.string().nullable(), send_two_factor_pin_notification: z.boolean()}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string(), type: z.literal("third_party_lf"), model: z.literal("credential-third-party-lf"), frequency: z.literal("LF"), third_party: z.literal(true), two_factor_pin: z.string().nullable(), send_two_factor_pin_notification: z.boolean()}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string().max(2000), type: z.literal("third_party_qr_code"), model: z.literal("credential-third-party-optical"), frequency: z.literal("optical"), third_party: z.literal(true)}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string().max(2000), type: z.literal("third_party_barcode"), model: z.literal("credential-third-party-optical"), frequency: z.literal("optical"), third_party: z.literal(true)}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string().max(2000), type: z.literal("third_party_qr_code_rotating_timestamp"), model: z.literal("credential-third-party-optical"), frequency: z.literal("optical"), third_party: z.literal(true), validity_window: z.enum(["PT5S", "PT10S", "PT30S", "PT1M", "PT5M", "PT1H", "P1D"])}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string().max(2000), type: z.literal("third_party_barcode_rotating_timestamp"), model: z.literal("credential-third-party-optical"), frequency: z.literal("optical"), third_party: z.literal(true), validity_window: z.enum(["PT5S", "PT10S", "PT30S", "PT1M", "PT5M", "PT1H", "P1D"])}), z.object({id: z.number().int(), last_used_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), assigned: z.boolean(), assigned_at: z.string().nullable(), activated_at: z.string().nullable(), deactivated_at: z.string().nullable(), activated: z.boolean(), assigned_by_id: z.number().int().nullable(), assigned_by: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), uid: z.string().max(2000), type: z.literal("third_party_qr_code_club_os"), model: z.literal("credential-third-party-optical"), frequency: z.literal("optical"), third_party: z.literal(true), validity_window: z.enum(["PT5S", "PT10S", "PT30S", "PT1M", "PT5M", "PT1H", "P1D"])})]); 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 card * * POST /cards * * Also accepts auth: OAuth2 */ export const createApiCards = async (args: CreateApiCardsArgs, options?: CreateApiCardsOptions) => { const {baseUrl = "https://api.kisi.io", fetch: fetchFn = fetch, KisiLogin} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (KisiLogin !== undefined) headers.set("Authorization", KisiLogin) const url = `${baseUrl.replace(/\/+$/, '')}/cards${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(createApiCardsResponse, await res.json()) };