import {z} from 'zod' export type CreateApiSchedulesArgs = {body: {schedule: {name?: (string | null) | undefined, enabled: boolean, override?: boolean | undefined, consequence: 'allow' | 'first_to_arrive' | 'permit' | 'unlock', scheduleable_type: 'Group' | 'Lock' | 'ElevatorStop', scheduleable_id: number, type?: 'holiday_calendar' | undefined, region: 'ar' | 'at' | 'au' | 'au_act' | 'au_nsw' | 'au_nt' | 'au_qld' | 'au_sa' | 'au_tas' | 'au_vic' | 'au_wa' | 'bg' | 'br' | 'by' | 'ca' | 'ca_ab' | 'ca_bc' | 'ca_mb' | 'ca_nb' | 'ca_nl' | 'ca_ns' | 'ca_nt' | 'ca_nu' | 'ca_on' | 'ca_pe' | 'ca_qc' | 'ca_sk' | 'ca_yt' | 'ch' | 'ch_ag' | 'ch_ai' | 'ch_ar' | 'ch_be' | 'ch_bl' | 'ch_bs' | 'ch_fr' | 'ch_ge' | 'ch_gl' | 'ch_gr' | 'ch_ju' | 'ch_lu' | 'ch_ne' | 'ch_nw' | 'ch_ow' | 'ch_sg' | 'ch_sh' | 'ch_so' | 'ch_sz' | 'ch_tg' | 'ch_ti' | 'ch_ur' | 'ch_vd' | 'ch_vs' | 'ch_zg' | 'ch_zh' | 'cl' | 'cn' | 'co' | 'cr' | 'cy' | 'cz' | 'de' | 'de_bb' | 'de_be' | 'de_bw' | 'de_by' | 'de_hb' | 'de_he' | 'de_hh' | 'de_mv' | 'de_ni' | 'de_nw' | 'de_rp' | 'de_sh' | 'de_sl' | 'de_sn' | 'de_st' | 'de_th' | 'dk' | 'ee' | 'es' | 'es_an' | 'es_ar' | 'es_ce' | 'es_cl' | 'es_cm' | 'es_cn' | 'es_ct' | 'es_ex' | 'es_ga' | 'es_ib' | 'es_lo' | 'es_m' | 'es_mu' | 'es_na' | 'es_o' | 'es_pv' | 'es_v' | 'es_vc' | 'fi' | 'fr' | 'gb' | 'gb_con' | 'gb_eng' | 'gb_nir' | 'gb_sct' | 'gb_wls' | 'ge' | 'gr' | 'hk' | 'hr' | 'hu' | 'ie' | 'il' | 'in' | 'in_ap' | 'in_ar' | 'in_as' | 'in_br' | 'in_cg' | 'in_ga' | 'in_gj' | 'in_hp' | 'in_hr' | 'in_jh' | 'in_jk' | 'in_ka' | 'in_kl' | 'in_mh' | 'in_mp' | 'in_mz' | 'in_nl' | 'in_od' | 'in_pb' | 'in_py' | 'in_sk' | 'in_tr' | 'in_ts' | 'in_wb' | 'is' | 'it' | 'it_bl' | 'it_fi' | 'it_ge' | 'it_pd' | 'it_rm' | 'it_ro' | 'it_to' | 'it_tv' | 'it_ve' | 'it_vi' | 'it_vr' | 'jp' | 'ke' | 'kr' | 'kz' | 'li' | 'lt' | 'lu' | 'lv' | 'ma' | 'mc' | 'mx' | 'mx_pue' | 'my' | 'ng' | 'nl' | 'no' | 'nz' | 'pe' | 'ph' | 'pl' | 'pt' | 'ro' | 'ru' | 'se' | 'sg' | 'th' | 'tn' | 'tr' | 'ua' | 'us' | 'us_ak' | 'us_al' | 'us_ar' | 'us_az' | 'us_ca' | 'us_co' | 'us_ct' | 'us_dc' | 'us_de' | 'us_fl' | 'us_ga' | 'us_gu' | 'us_hi' | 'us_ia' | 'us_id' | 'us_il' | 'us_in' | 'us_ks' | 'us_ky' | 'us_la' | 'us_ma' | 'us_md' | 'us_me' | 'us_mi' | 'us_mn' | 'us_mo' | 'us_ms' | 'us_mt' | 'us_nc' | 'us_nd' | 'us_ne' | 'us_nh' | 'us_nj' | 'us_nm' | 'us_nv' | 'us_ny' | 'us_oh' | 'us_ok' | 'us_or' | 'us_pa' | 'us_pr' | 'us_ri' | 'us_sc' | 'us_sd' | 'us_tn' | 'us_tx' | 'us_ut' | 'us_va' | 'us_vi' | 'us_vt' | 'us_wa' | 'us_wi' | 'us_wv' | 'us_wy' | 'vn' | 'za', observed: boolean, ignored_holidays?: Array | undefined}} | {schedule: {name?: (string | null) | undefined, enabled: boolean, override?: boolean | undefined, consequence: 'allow' | 'first_to_arrive' | 'permit' | 'unlock', scheduleable_type: 'Group' | 'Lock' | 'ElevatorStop', scheduleable_id: number, type: 'recurring_event', monday: boolean, tuesday: boolean, wednesday: boolean, thursday: boolean, friday: boolean, saturday: boolean, sunday: boolean}} | {schedule: {name?: (string | null) | undefined, enabled: boolean, override?: boolean | undefined, consequence: 'allow' | 'first_to_arrive' | 'permit' | 'unlock', scheduleable_type: 'Group' | 'Lock' | 'ElevatorStop', scheduleable_id: number, type: 'single_all_day_event', starts_at_date: string, ends_at_date: string}} | {schedule: {name?: (string | null) | undefined, enabled: boolean, override?: boolean | undefined, consequence: 'allow' | 'first_to_arrive' | 'permit' | 'unlock', scheduleable_type: 'Group' | 'Lock' | 'ElevatorStop', scheduleable_id: number, type: 'single_event', starts_at: string, ends_at: string}}}; export type CreateApiSchedulesOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiSchedulesResponse = z.union([z.object({id: z.number().int(), resource_type: z.literal("Schedule"), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), enabled: z.boolean(), override: z.boolean(), consequence: z.enum(["allow", "first_to_arrive", "permit", "unlock"]), scheduleable_type: z.enum(["Group", "Lock", "ElevatorStop"]), scheduleable_id: z.number().int(), scheduleable: z.object({id: z.number().int(), type: z.enum(["Group", "Lock", "ElevatorStop"]), name: z.string()}), place_id: z.number().int().nullable(), place: z.object({id: z.number().int(), resource_type: z.literal("Place"), name: z.string()}).optional(), integration_id: z.number().int().nullable(), type: z.literal("holiday_calendar"), region: z.enum(["ar", "at", "au", "au_act", "au_nsw", "au_nt", "au_qld", "au_sa", "au_tas", "au_vic", "au_wa", "bg", "br", "by", "ca", "ca_ab", "ca_bc", "ca_mb", "ca_nb", "ca_nl", "ca_ns", "ca_nt", "ca_nu", "ca_on", "ca_pe", "ca_qc", "ca_sk", "ca_yt", "ch", "ch_ag", "ch_ai", "ch_ar", "ch_be", "ch_bl", "ch_bs", "ch_fr", "ch_ge", "ch_gl", "ch_gr", "ch_ju", "ch_lu", "ch_ne", "ch_nw", "ch_ow", "ch_sg", "ch_sh", "ch_so", "ch_sz", "ch_tg", "ch_ti", "ch_ur", "ch_vd", "ch_vs", "ch_zg", "ch_zh", "cl", "cn", "co", "cr", "cy", "cz", "de", "de_bb", "de_be", "de_bw", "de_by", "de_hb", "de_he", "de_hh", "de_mv", "de_ni", "de_nw", "de_rp", "de_sh", "de_sl", "de_sn", "de_st", "de_th", "dk", "ee", "es", "es_an", "es_ar", "es_ce", "es_cl", "es_cm", "es_cn", "es_ct", "es_ex", "es_ga", "es_ib", "es_lo", "es_m", "es_mu", "es_na", "es_o", "es_pv", "es_v", "es_vc", "fi", "fr", "gb", "gb_con", "gb_eng", "gb_nir", "gb_sct", "gb_wls", "ge", "gr", "hk", "hr", "hu", "ie", "il", "in", "in_ap", "in_ar", "in_as", "in_br", "in_cg", "in_ga", "in_gj", "in_hp", "in_hr", "in_jh", "in_jk", "in_ka", "in_kl", "in_mh", "in_mp", "in_mz", "in_nl", "in_od", "in_pb", "in_py", "in_sk", "in_tr", "in_ts", "in_wb", "is", "it", "it_bl", "it_fi", "it_ge", "it_pd", "it_rm", "it_ro", "it_to", "it_tv", "it_ve", "it_vi", "it_vr", "jp", "ke", "kr", "kz", "li", "lt", "lu", "lv", "ma", "mc", "mx", "mx_pue", "my", "ng", "nl", "no", "nz", "pe", "ph", "pl", "pt", "ro", "ru", "se", "sg", "th", "tn", "tr", "ua", "us", "us_ak", "us_al", "us_ar", "us_az", "us_ca", "us_co", "us_ct", "us_dc", "us_de", "us_fl", "us_ga", "us_gu", "us_hi", "us_ia", "us_id", "us_il", "us_in", "us_ks", "us_ky", "us_la", "us_ma", "us_md", "us_me", "us_mi", "us_mn", "us_mo", "us_ms", "us_mt", "us_nc", "us_nd", "us_ne", "us_nh", "us_nj", "us_nm", "us_nv", "us_ny", "us_oh", "us_ok", "us_or", "us_pa", "us_pr", "us_ri", "us_sc", "us_sd", "us_tn", "us_tx", "us_ut", "us_va", "us_vi", "us_vt", "us_wa", "us_wi", "us_wv", "us_wy", "vn", "za"]), observed: z.boolean(), ignored_holidays: z.array(z.string()), holidays: z.array(z.object({date: z.string(), name: z.string()}))}), z.object({id: z.number().int(), resource_type: z.literal("Schedule"), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), enabled: z.boolean(), override: z.boolean(), consequence: z.enum(["allow", "first_to_arrive", "permit", "unlock"]), scheduleable_type: z.enum(["Group", "Lock", "ElevatorStop"]), scheduleable_id: z.number().int(), scheduleable: z.object({id: z.number().int(), type: z.enum(["Group", "Lock", "ElevatorStop"]), name: z.string()}), place_id: z.number().int().nullable(), place: z.object({id: z.number().int(), resource_type: z.literal("Place"), name: z.string()}).optional(), integration_id: z.number().int().nullable(), type: z.literal("recurring_event"), monday: z.boolean(), tuesday: z.boolean(), wednesday: z.boolean(), thursday: z.boolean(), friday: z.boolean(), saturday: z.boolean(), sunday: z.boolean(), starts_at_time: z.string(), ends_at_time: z.string(), next_occurence: z.object({starts_at: z.string(), ends_at: z.string()})}), z.object({id: z.number().int(), resource_type: z.literal("Schedule"), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), enabled: z.boolean(), override: z.boolean(), consequence: z.enum(["allow", "first_to_arrive", "permit", "unlock"]), scheduleable_type: z.enum(["Group", "Lock", "ElevatorStop"]), scheduleable_id: z.number().int(), scheduleable: z.object({id: z.number().int(), type: z.enum(["Group", "Lock", "ElevatorStop"]), name: z.string()}), place_id: z.number().int().nullable(), place: z.object({id: z.number().int(), resource_type: z.literal("Place"), name: z.string()}).optional(), integration_id: z.number().int().nullable(), type: z.literal("single_all_day_event"), starts_at_date: z.string(), ends_at_date: z.string()}), z.object({id: z.number().int(), resource_type: z.literal("Schedule"), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), enabled: z.boolean(), override: z.boolean(), consequence: z.enum(["allow", "first_to_arrive", "permit", "unlock"]), scheduleable_type: z.enum(["Group", "Lock", "ElevatorStop"]), scheduleable_id: z.number().int(), scheduleable: z.object({id: z.number().int(), type: z.enum(["Group", "Lock", "ElevatorStop"]), name: z.string()}), place_id: z.number().int().nullable(), place: z.object({id: z.number().int(), resource_type: z.literal("Place"), name: z.string()}).optional(), integration_id: z.number().int().nullable(), type: z.literal("single_event"), starts_at: z.string(), ends_at: 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 }; /** * Create schedule * * POST /schedules * * Also accepts auth: OAuth2 */ export const createApiSchedules = async (args: CreateApiSchedulesArgs, options?: CreateApiSchedulesOptions) => { 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(/\/+$/, '')}/schedules${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(createApiSchedulesResponse, await res.json()) };