import {z} from 'zod' export type GetApiEntrancesArgs = {'Accept-Language'?: string | undefined, count?: number | undefined, offset?: number | undefined, sort_field?: string | undefined, sort_direction?: ('ASC' | 'DESC') | undefined, pos_id?: (3 | 6 | 9 | 11) | undefined, external_order_id?: string | undefined, name?: string | undefined, resort_name?: string | undefined, customer_category_name?: string | undefined, card_id?: string | undefined, customer_id?: string | undefined, entrance_type_id?: (0 | 1 | 2) | undefined, state?: (0 | 1) | undefined, external_entrance_id?: string | undefined, is_valid?: boolean | undefined, is_cancelled?: boolean | undefined, valid_from?: string | undefined, valid_to?: string | undefined, product_id?: string | undefined, external_turnstile_permission_id?: string | undefined, order_item_id?: string | undefined}; export type GetApiEntrancesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const entrance = z.object({entrance_id: z.string(), pos_id: z.union([z.literal(3), z.literal(6), z.literal(9), z.literal(11)]), name: z.string().optional(), customer_id: z.string().optional(), resort_name: z.string().optional(), customer_category_name: z.string().optional(), external_order_id: z.string().optional(), external_entrance_id: z.string().optional(), external_turnstile_permission_id: z.string().optional(), entrance_type_id: z.union([z.literal(0), z.literal(1), z.literal(2)]).optional(), order_item_id: z.string().optional(), card_id: z.string(), created_at: z.string().optional(), cancelled_at: z.string().optional(), charged_price: z.number().optional(), product_ids: z.string().optional(), charge_date: z.string().optional(), valid_from: z.string().optional(), valid_to: z.string().optional(), last_change: z.string().optional(), state: z.union([z.literal(0), z.literal(1)])}); export const getApiEntrancesResponse = z.object({data: z.object({entrances: z.array(entrance).optional(), total_items: z.number().int().optional()}).optional()}); 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 all entrances * * GET /entrances * * Also accepts auth: basicAuth */ export const getApiEntrances = async (args: GetApiEntrancesArgs, options?: GetApiEntrancesOptions) => { const {baseUrl = "https://sandbox.crmcarecloud.com/webservice/rest-api/enterprise-interface/v1.0", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (args.count != null) query.append("count", String(args.count)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sort_field != null) query.append("sort_field", String(args.sort_field)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.pos_id != null) query.append("pos_id", String(args.pos_id)) if (args.external_order_id != null) query.append("external_order_id", String(args.external_order_id)) if (args.name != null) query.append("name", String(args.name)) if (args.resort_name != null) query.append("resort_name", String(args.resort_name)) if (args.customer_category_name != null) query.append("customer_category_name", String(args.customer_category_name)) if (args.card_id != null) query.append("card_id", String(args.card_id)) if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.entrance_type_id != null) query.append("entrance_type_id", String(args.entrance_type_id)) if (args.state != null) query.append("state", String(args.state)) if (args.external_entrance_id != null) query.append("external_entrance_id", String(args.external_entrance_id)) if (args.is_valid != null) query.append("is_valid", String(args.is_valid)) if (args.is_cancelled != null) query.append("is_cancelled", String(args.is_cancelled)) if (args.valid_from != null) query.append("valid_from", String(args.valid_from)) if (args.valid_to != null) query.append("valid_to", String(args.valid_to)) if (args.product_id != null) query.append("product_id", String(args.product_id)) if (args.external_turnstile_permission_id != null) query.append("external_turnstile_permission_id", String(args.external_turnstile_permission_id)) if (args.order_item_id != null) query.append("order_item_id", String(args.order_item_id)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/entrances${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiEntrancesResponse, await res.json()) };