import {z} from 'zod' export type GetApiV1BroadcastsCountArgs = {broadcast_id?: Array | undefined, record_status?: (Array<'active' | 'deleted' | 'duplicate'>) | undefined, broadcast_status?: (Array<'cancelled' | 'predicted' | 'scheduled'>) | undefined, 'start.gt'?: string | undefined, 'start.gte'?: string | undefined, 'start.lt'?: string | undefined, 'start.lte'?: string | undefined, 'start.tz'?: string | undefined, 'updated.gt'?: string | undefined, 'updated.gte'?: string | undefined, 'updated.lt'?: string | undefined, 'updated.lte'?: string | undefined, 'updated.tz'?: string | undefined, 'first_seen.gt'?: string | undefined, 'first_seen.gte'?: string | undefined, 'first_seen.lt'?: string | undefined, 'first_seen.lte'?: string | undefined, 'first_seen.tz'?: string | undefined, 'phq_viewership.gt'?: number | undefined, 'phq_viewership.gte'?: number | undefined, 'phq_viewership.lt'?: number | undefined, 'phq_viewership.lte'?: number | undefined, 'location.place_id'?: Array | undefined, 'location.origin'?: string | undefined, 'event.event_id'?: Array | undefined, 'event.category'?: Array | undefined, 'event.label'?: Array | undefined, 'event.entity_id'?: Array | undefined}; export type GetApiV1BroadcastsCountOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const broadcastCountResponse = z.object({count: z.number().int()}); 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 }; /** * Count Broadcasts * * GET /v1/broadcasts/count/ */ export const getApiV1BroadcastsCount = async (args: GetApiV1BroadcastsCountArgs, options?: GetApiV1BroadcastsCountOptions) => { const {baseUrl = "https://api.predicthq.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.broadcast_id ?? []) query.append("broadcast_id", String(item)) for (const item of args.record_status ?? []) query.append("record_status", String(item)) for (const item of args.broadcast_status ?? []) query.append("broadcast_status", String(item)) if (args['start.gt'] != null) query.append("start.gt", String(args['start.gt'])) if (args['start.gte'] != null) query.append("start.gte", String(args['start.gte'])) if (args['start.lt'] != null) query.append("start.lt", String(args['start.lt'])) if (args['start.lte'] != null) query.append("start.lte", String(args['start.lte'])) if (args['start.tz'] != null) query.append("start.tz", String(args['start.tz'])) if (args['updated.gt'] != null) query.append("updated.gt", String(args['updated.gt'])) if (args['updated.gte'] != null) query.append("updated.gte", String(args['updated.gte'])) if (args['updated.lt'] != null) query.append("updated.lt", String(args['updated.lt'])) if (args['updated.lte'] != null) query.append("updated.lte", String(args['updated.lte'])) if (args['updated.tz'] != null) query.append("updated.tz", String(args['updated.tz'])) if (args['first_seen.gt'] != null) query.append("first_seen.gt", String(args['first_seen.gt'])) if (args['first_seen.gte'] != null) query.append("first_seen.gte", String(args['first_seen.gte'])) if (args['first_seen.lt'] != null) query.append("first_seen.lt", String(args['first_seen.lt'])) if (args['first_seen.lte'] != null) query.append("first_seen.lte", String(args['first_seen.lte'])) if (args['first_seen.tz'] != null) query.append("first_seen.tz", String(args['first_seen.tz'])) if (args['phq_viewership.gt'] != null) query.append("phq_viewership.gt", String(args['phq_viewership.gt'])) if (args['phq_viewership.gte'] != null) query.append("phq_viewership.gte", String(args['phq_viewership.gte'])) if (args['phq_viewership.lt'] != null) query.append("phq_viewership.lt", String(args['phq_viewership.lt'])) if (args['phq_viewership.lte'] != null) query.append("phq_viewership.lte", String(args['phq_viewership.lte'])) for (const item of args['location.place_id'] ?? []) query.append("location.place_id", String(item)) if (args['location.origin'] != null) query.append("location.origin", String(args['location.origin'])) for (const item of args['event.event_id'] ?? []) query.append("event.event_id", String(item)) for (const item of args['event.category'] ?? []) query.append("event.category", String(item)) for (const item of args['event.label'] ?? []) query.append("event.label", String(item)) for (const item of args['event.entity_id'] ?? []) query.append("event.entity_id", String(item)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/broadcasts/count/${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(broadcastCountResponse, await res.json()) };