import {z} from 'zod' export type GetApiUserAddressesArgs = {filter?: unknown | undefined, restaurant_id?: unknown | undefined, user_id?: unknown | undefined, q?: unknown | undefined}; export type GetApiUserAddressesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const userAddress = z.object({id: z.number().int().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), user_id: z.number().int().optional(), title: z.string().optional(), building_address: z.string().optional(), address2: z.string().optional(), landmark: z.string().optional(), city_id: z.number().int().optional(), state_id: z.number().int().optional(), country_id: z.number().int().optional(), zip_code: z.string().optional(), latitude: z.number().optional(), longitude: z.number().optional(), is_active: z.number().int().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 }; /** * Fetch all user addresses * * GET /user_addresses */ export const getApiUserAddresses = async (args: GetApiUserAddressesArgs, options: GetApiUserAddressesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", String(args.filter)) if (args.restaurant_id != null) query.append("restaurant_id", String(args.restaurant_id)) if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.q != null) query.append("q", String(args.q)) const url = `${baseUrl.replace(/\/+$/, '')}/user_addresses${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(userAddress, await res.json()) };