import {z} from 'zod' export type GetApiPublicRandomusersArgs = {page?: string | undefined, limit?: string | undefined}; export type GetApiPublicRandomusersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPublicRandomusersResponse = z.object({data: z.object({currentPageItems: z.number().optional(), data: z.array(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.union([z.number(), z.string(), z.number(), z.number(), z.number(), z.number(), z.number(), 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(), limit: z.number().optional(), nextPage: z.boolean().optional(), page: z.number().optional(), previousPage: z.boolean().optional(), totalItems: z.number().optional(), totalPages: z.number().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 users * * GET /public/randomusers */ export const getApiPublicRandomusers = async (args: GetApiPublicRandomusersArgs, options?: GetApiPublicRandomusersOptions) => { 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") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/public/randomusers${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(getApiPublicRandomusersResponse, await res.json()) };