import {z} from 'zod' export type APISignalReactionUpsert = {reaction: 'useful' | 'not_useful' | 'dismissed' | 'actioned', reason?: (string | null) | undefined}; export type UpdateApiBrandIdSignalsSignalIdReactionArgs = {brand_id: number, signal_id: number, body: APISignalReactionUpsert}; export type UpdateApiBrandIdSignalsSignalIdReactionOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const aPISignalReaction = z.object({user_id: z.number().int(), insight_id: z.number().int(), fingerprint: z.string(), reaction: z.enum(["useful", "not_useful", "dismissed", "actioned"]), reason: z.string().nullable().optional(), surface: z.enum(["in_app", "email", "slack", "api"]), created_at: z.string(), updated_at: z.string()}); 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 }; /** * Set Signal Reaction * * PUT /{brand_id}/signals/{signal_id}/reaction */ export const updateApiBrandIdSignalsSignalIdReaction = async (args: UpdateApiBrandIdSignalsSignalIdReactionArgs, options?: UpdateApiBrandIdSignalsSignalIdReactionOptions) => { const {baseUrl = "https://api.scrunchai.com/v1", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.brand_id))}/signals/${encodeURIComponent(String(args.signal_id))}/reaction${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(aPISignalReaction, await res.json()) };