import {z} from 'zod' export type GetApiV1ClientsClientIdStreamsArgs = {client_id: string, include_inactive?: boolean | undefined}; export type GetApiV1ClientsClientIdStreamsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const scoreChange = z.object({change: z.number(), percentage: z.number().nullable().optional(), previousScore: z.number()}); export const scoreChanges = z.object({"7day": scoreChange.nullable().optional(), "30day": scoreChange.nullable().optional()}); export const streamResponse = z.object({id: z.string(), name: z.string(), customerId: z.string(), parentClientId: z.string().nullable().optional(), parentClientName: z.string().nullable().optional(), score: z.number(), streamState: z.string().nullable().optional(), goals: z.string().nullable().optional(), hasInsufficientData: z.boolean().nullable().optional(), historicalData: z.array(z.number().int()).nullable().optional(), scoreChanges: scoreChanges.nullable().optional()}); export const streamListResponse = z.object({streams: z.array(streamResponse), total: z.number().int(), parentClientId: z.string(), customerId: 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 }; /** * List Client Streams * * GET /v1/clients/{client_id}/streams */ export const getApiV1ClientsClientIdStreams = async (args: GetApiV1ClientsClientIdStreamsArgs, options: GetApiV1ClientsClientIdStreamsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include_inactive != null) query.append("include_inactive", String(args.include_inactive)) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/clients/${encodeURIComponent(String(args.client_id))}/streams${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(streamListResponse, await res.json()) };