import {z} from 'zod' export type GetApiV1PrivacyConfigOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const customDenyPattern = z.object({name: z.string().min(1).max(64), regex: z.string().min(1).max(1024), replacement_label: z.string().min(1).max(64), description: z.string().max(256).nullable().optional(), score: z.number().gte(0).lte(1).optional()}); export const customAllowPattern = z.object({name: z.string().min(1).max(64), regex: z.string().min(1).max(1024), description: z.string().max(256).nullable().optional()}); export const privacyConfigResponse = z.object({id: z.string(), organization_id: z.string(), pii_scrubbing_enabled: z.boolean(), scrub_types: z.array(z.string()), custom_deny_patterns: z.array(customDenyPattern), custom_allow_patterns: z.array(customAllowPattern), score_threshold: z.number(), audio_retention_policy: z.enum(["no_audio", "raw", "redacted"]), audio_redaction_fill: z.enum(["silence", "tone_1khz"]), supported_languages: z.array(z.string()), default_language: z.string(), language_detection_enabled: z.boolean(), 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 }; /** * Get privacy config for organization (creates defaults if none) * * GET /v1/privacy/config */ export const getApiV1PrivacyConfig = async (options: GetApiV1PrivacyConfigOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/privacy/config${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(privacyConfigResponse, await res.json()) };