import {z} from 'zod' export type GetApiV3UsersArgs = {'page[number]'?: number | undefined, 'page[size]'?: number | undefined, sort?: string | undefined, 'fields[user]'?: string | undefined, 'filter[id]'?: string | undefined, 'filter[email]'?: string | undefined, 'filter[mobile_phone]'?: string | undefined, 'filter[locale]'?: string | undefined, 'filter[timezone]'?: string | undefined, 'filter[created_at]'?: string | undefined, 'filter[updated_at]'?: string | undefined, 'Accept-Language'?: string | undefined}; export type GetApiV3UsersOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const user = z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({name: z.string().optional(), email: z.string().optional(), mobile_phone: z.string().optional(), locale: z.string().optional(), timezone: z.string().optional(), avatar: z.object({original: z.string().optional(), thumb: z.string().optional(), normal: z.string().optional()}).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional(), meta: z.object({temp_id: z.string().optional()}).optional()}); export const getApiV3UsersResponse = z.object({data: z.array(user).optional(), meta: z.object({pagination: z.object({total_records: z.number().int().optional(), page: z.number().int().optional(), size: z.number().int().optional()}).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 users * * GET /v3/users * * Also accepts auth: OAuth2 */ export const getApiV3Users = async (args: GetApiV3UsersArgs, options?: GetApiV3UsersOptions) => { const {baseUrl = "https://api.bookingexperts.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (args['fields[user]'] != null) query.append("fields[user]", String(args['fields[user]'])) if (args['filter[id]'] != null) query.append("filter[id]", String(args['filter[id]'])) if (args['filter[email]'] != null) query.append("filter[email]", String(args['filter[email]'])) if (args['filter[mobile_phone]'] != null) query.append("filter[mobile_phone]", String(args['filter[mobile_phone]'])) if (args['filter[locale]'] != null) query.append("filter[locale]", String(args['filter[locale]'])) if (args['filter[timezone]'] != null) query.append("filter[timezone]", String(args['filter[timezone]'])) if (args['filter[created_at]'] != null) query.append("filter[created_at]", String(args['filter[created_at]'])) if (args['filter[updated_at]'] != null) query.append("filter[updated_at]", String(args['filter[updated_at]'])) if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v3/users${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(getApiV3UsersResponse, await res.json()) };