import {z} from 'zod' export type GetApiBrandIdPromptsArgs = {brand_id: number, limit?: number | undefined, offset?: number | undefined, stage?: (string | null) | undefined, status?: ('active' | 'paused' | 'archived' | 'all') | undefined}; export type GetApiBrandIdPromptsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const promptListing = z.object({id: z.number().int(), text: z.string(), stage: z.string(), persona_id: z.number().int().nullable(), platforms: z.array(z.enum(["chatgpt", "claude", "google_ai_overviews", "perplexity", "meta", "google_ai_mode", "google_gemini", "copilot", "grok"])), tags: z.array(z.string()), topics: z.array(z.string()), status: z.enum(["active", "paused", "archived"]), created_at: z.string()}); export const aggregationGranularity = z.enum(["daily", "weekly", "monthly"]); export const topDomainSummary = z.object({domain: z.string(), domain_owner: z.string(), observation_count: z.number().int()}); export const ownerTimeSeriesPoint = z.object({time_bucket: z.string(), brand: z.number().int(), competitor: z.number().int(), other: z.number().int()}); export const segmentTotal = z.object({name: z.string(), observation_count: z.number().int()}); export const topDomainsMetadata = z.object({domains: z.array(topDomainSummary), grand_total: z.number().int(), owner_totals: z.record(z.string(), z.number().int()), owner_time_series: z.array(ownerTimeSeriesPoint).nullable().optional(), segment_totals: z.array(segmentTotal).optional()}); export const timeSeriesMetadata = z.object({aggregation_granularity: aggregationGranularity, period_count: z.number().int(), start_date: z.string(), end_date: z.string(), top_domains: topDomainsMetadata.nullable().optional()}); export const collectionResponsePromptListing = z.object({total: z.number().int(), offset: z.number().int().optional(), limit: z.number().int().nullable().optional(), items: z.array(promptListing), metadata: timeSeriesMetadata.nullable().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 }; /** * List Prompts * * GET /{brand_id}/prompts */ export const getApiBrandIdPrompts = async (args: GetApiBrandIdPromptsArgs, options?: GetApiBrandIdPromptsOptions) => { const {baseUrl = "https://api.scrunchai.com/v1", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.stage != null) query.append("stage", String(args.stage)) if (args.status != null) query.append("status", String(args.status)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.brand_id))}/prompts${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(collectionResponsePromptListing, await res.json()) };