import {z} from 'zod' export type GetApiV2AuditLogsEventsArgs = {workspaceId?: string | undefined, organizationId?: string | undefined, actions?: string | undefined, actorIds?: string | undefined, targetId?: string | undefined, startDate?: number | undefined, endDate?: number | undefined, cursor?: string | undefined, limit?: number | undefined}; export type GetApiV2AuditLogsEventsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const actorType = z.enum(["user", "system", "service"]); export const actor = z.object({type: actorType, id: z.string(), name: z.string().nullable().optional(), email: z.string().nullable().optional()}); export const targetType = z.enum(["workspace", "video", "user", "organization", "asset"]); export const target = z.object({type: targetType, id: z.string(), name: z.string().nullable().optional()}); export const context = z.object({workspaceId: z.string().nullable().optional(), organizationId: z.string().nullable().optional(), ipAddress: z.string().nullable().optional()}); export const metadata = z.object({createdAt: z.string(), processedAt: z.string().nullable().optional()}); export const auditLogEvent = z.object({id: z.string(), action: z.string(), status: z.string(), actor: actor, target: target, context: context, details: z.object({}), metadata: metadata}); export const auditLogsPageResponse = z.object({events: z.array(auditLogEvent), nextCursor: z.string().nullable().optional(), hasMore: z.boolean()}); 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 }; /** * Query audit log events * * GET /v2/auditLogs/events */ export const getApiV2AuditLogsEvents = async (args: GetApiV2AuditLogsEventsArgs, options?: GetApiV2AuditLogsEventsOptions) => { const {baseUrl = "https://api.synthesia.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.workspaceId != null) query.append("workspaceId", String(args.workspaceId)) if (args.organizationId != null) query.append("organizationId", String(args.organizationId)) if (args.actions != null) query.append("actions", String(args.actions)) if (args.actorIds != null) query.append("actorIds", String(args.actorIds)) if (args.targetId != null) query.append("targetId", String(args.targetId)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/auditLogs/events${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(auditLogsPageResponse, await res.json()) };