import {z} from 'zod' export type GetApiPublicRandomusersUserRandomOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPublicRandomusersUserRandomResponse = z.object({data: z.object({cell: z.string().optional(), dob: z.object({age: z.number().optional(), date: z.string().optional()}).optional(), email: z.string().optional(), gender: z.string().optional(), id: z.number().optional(), location: z.object({city: z.string().optional(), coordinates: z.object({latitude: z.string().optional(), longitude: z.string().optional()}).optional(), country: z.string().optional(), postcode: z.number().optional(), state: z.string().optional(), street: z.object({name: z.string().optional(), number: z.number().optional()}).optional(), timezone: z.object({description: z.string().optional(), offset: z.string().optional()}).optional()}).optional(), login: z.object({md5: z.string().optional(), password: z.string().optional(), salt: z.string().optional(), sha1: z.string().optional(), sha256: z.string().optional(), username: z.string().optional(), uuid: z.string().optional()}).optional(), name: z.object({first: z.string().optional(), last: z.string().optional(), title: z.string().optional()}).optional(), nat: z.string().optional(), phone: z.string().optional(), picture: z.object({large: z.string().optional(), medium: z.string().optional(), thumbnail: z.string().optional()}).optional(), registered: z.object({age: z.number().optional(), date: z.string().optional()}).optional()}).optional(), message: z.string().optional(), statusCode: z.number().optional(), success: z.boolean().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 a random user * * GET /public/randomusers/user/random */ export const getApiPublicRandomusersUserRandom = async (options?: GetApiPublicRandomusersUserRandomOptions) => { const {baseUrl = "https://api.freeapi.app/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/public/randomusers/user/random${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(getApiPublicRandomusersUserRandomResponse, await res.json()) };