import {z} from 'zod' export type PlaceCreate = {name?: (string | null) | undefined, external_id?: (string | null) | undefined, latitude: number, longitude: number, radius_meters: number}; export type PlaceListUpdate = {version?: number | undefined, name?: string | undefined, description?: (string | null) | undefined, sensitive_location_attestation?: boolean | undefined, places?: Array | undefined}; export type CreateApiPlaceListsPlaceListIdArgs = {place_list_id: number, body: PlaceListUpdate}; export type CreateApiPlaceListsPlaceListIdOptions = {baseUrl?: string | undefined, Auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const place = z.object({id: z.number().int().optional(), name: z.string().max(255).nullable().optional(), external_id: z.string().max(255).nullable().optional(), geometry_type: z.literal("circle").optional(), latitude: z.number().gte(-90).lte(90).nullable().optional(), longitude: z.number().gte(-180).lte(180).nullable().optional(), radius_meters: z.number().int().gte(1000).lte(30000).nullable().optional()}); export const placeListFull = z.object({id: z.number().int().optional(), entity_type: z.string().optional(), version: z.number().int().optional(), organization_id: z.number().int().optional(), name: z.string().max(255).optional(), description: z.string().nullable().optional(), filename: z.string().max(255).nullable().optional(), content_updated_on: z.string().nullable().optional(), places_count: z.number().int().optional(), strategy_count: z.number().int().optional(), created_on: z.string().optional(), updated_on: z.string().optional(), places: z.array(place).optional()}); export const singleMetadata = z.object({status: z.string()}); export const createApiPlaceListsPlaceListIdResponse = z.object({data: placeListFull.optional(), meta: singleMetadata.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 }; /** * Update Place List * * POST /place_lists/{place_list_id} */ export const createApiPlaceListsPlaceListId = async (args: CreateApiPlaceListsPlaceListIdArgs, options?: CreateApiPlaceListsPlaceListIdOptions) => { const {baseUrl = "https://api.mediamath.com/api/v3.0", fetch: fetchFn = fetch, Auth0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Auth0 !== undefined) headers.set('authorization', `Bearer ${Auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/place_lists/${encodeURIComponent(String(args.place_list_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiPlaceListsPlaceListIdResponse, await res.json()) };