import {z} from 'zod' export type GetApiWishlistsArgs = {examples_only?: boolean | undefined, exclude_examples?: boolean | undefined, rental_category?: string | undefined}; export type GetApiWishlistsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dBIntSlice = z.array(z.number().int()); export const wishlistUserDetail = z.object({avatar_url: z.string().optional(), first_name: z.string().optional(), id: z.number().int().optional(), last_name: z.string().optional(), user_role: z.string().optional()}); export const wishlistUserDetails = z.array(wishlistUserDetail); export const readManyWishlist = z.object({campground_count: z.number().int().optional(), campgrounds: dBIntSlice.optional(), cover_image_url: z.string().optional(), id: z.number().int().optional(), is_example: z.boolean().optional(), is_public: z.boolean().optional(), rental_count: z.number().int().optional(), rentals: dBIntSlice.optional(), title: z.string().optional(), user_details: wishlistUserDetails.optional(), user_role: z.string().optional()}); export const getApiWishlistsResponse = z.array(readManyWishlist); 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 }; /** * Gets wishlists for current user * * GET /wishlists * * Also accepts auth: API-Key */ export const getApiWishlists = async (args: GetApiWishlistsArgs, options: GetApiWishlistsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.examples_only != null) query.append("examples_only", String(args.examples_only)) if (args.exclude_examples != null) query.append("exclude_examples", String(args.exclude_examples)) if (args.rental_category != null) query.append("rental_category", String(args.rental_category)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/wishlists${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(getApiWishlistsResponse, await res.json()) };