import {z} from 'zod' export type GetApiV3UsersIdArgs = {id: string, 'fields[user]'?: string | undefined, 'Accept-Language'?: string | undefined}; export type GetApiV3UsersIdOptions = {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 getApiV3UsersIdResponse = z.object({data: user.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 user * * GET /v3/users/{id} * * Also accepts auth: OAuth2 */ export const getApiV3UsersId = async (args: GetApiV3UsersIdArgs, options?: GetApiV3UsersIdOptions) => { 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['fields[user]'] != null) query.append("fields[user]", String(args['fields[user]'])) 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/${encodeURIComponent(String(args.id))}${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(getApiV3UsersIdResponse, await res.json()) };