import {z} from 'zod' export type GetApiV5PersonsPersonIdLocationsArgs = {person_id: string, countries?: Array | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiV5PersonsPersonIdLocationsOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const city = z.object({name: z.string().optional()}); export const country = z.object({isoCode: z.string().optional(), name: z.string().optional()}); export const zonedDateTime = z.string(); export const state = z.object({isoCode: z.string().optional(), name: z.string().optional()}); export const geoIp = z.object({accuracyRadiusKm: z.number().int().nullable().optional(), city: city.optional(), country: country.optional(), ipAddress: z.string().optional(), location: z.lazy(() => location.optional()), state: state.optional(), zipCode: z.string().nullable().optional()}); export const location: z.ZodType = z.object({createdDate: z.string().optional(), deviceId: z.string().optional(), geoIp: geoIp.optional()}); export const personLocationResponse = z.object({accuracy_radius_km: z.number().int(), city: city, country: country, created_date: zonedDateTime, device_id: z.string(), ip_address: z.string(), location: location, state: state, zip_code: z.string()}); export const getApiV5PersonsPersonIdLocationsResponse = z.array(personLocationResponse); 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 }; /** * List person locations * * GET /v5/persons/{person_id}/locations * * Also accepts auth: QUERY, COOKIE */ export const getApiV5PersonsPersonIdLocations = async (args: GetApiV5PersonsPersonIdLocationsArgs, options?: GetApiV5PersonsPersonIdLocationsOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.countries ?? []) query.append("countries", String(item)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v5/persons/${encodeURIComponent(String(args.person_id))}/locations${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(getApiV5PersonsPersonIdLocationsResponse, await res.json()) };