import {z} from 'zod' export type GetApiApiOrgIdScorersArgs = {org_id: string, scorerType?: string | undefined}; export type GetApiApiOrgIdScorersOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const scorerResponseBody = z.object({createdAt: z.number().int(), description: z.string().nullable().optional(), entityId: z.string(), id: z.string(), isActive: z.boolean(), name: z.string(), orgId: z.string(), outputSchema: z.unknown(), params: z.unknown(), producesScoreConfigId: z.string().nullable().optional(), producesScoreConfigVersion: z.number().int().nullable().optional(), referenceBased: z.boolean(), scorerType: z.string(), template: z.string(), updatedAt: z.number().int(), variables: z.array(z.string()), version: z.number().int()}); export const getApiApiOrgIdScorersResponse = z.object({list: z.array(scorerResponseBody)}); 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 scorers * * GET /api/{org_id}/scorers */ export const getApiApiOrgIdScorers = async (args: GetApiApiOrgIdScorersArgs, options: GetApiApiOrgIdScorersOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.scorerType != null) query.append("scorerType", String(args.scorerType)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/scorers${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(getApiApiOrgIdScorersResponse, await res.json()) };