import {z} from 'zod' export type GetApiV1ChangesArgs = {pageSize?: number | undefined, skip?: number | undefined, sort?: string | undefined, applicationId?: number | undefined, entityPath?: string | undefined, userId?: number | undefined, createdBefore?: string | undefined, createdAfter?: string | undefined, withTotalResultSize?: boolean | undefined, managementKeyId?: number | undefined, includeOld?: boolean | undefined}; export type GetApiV1ChangesOptions = {baseUrl?: string | undefined, managerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const change = z.object({id: z.number().int(), created: z.string(), userId: z.number().int(), applicationId: z.number().int().optional(), entity: z.string(), old: z.object({}).optional(), new: z.object({}).optional(), managementKeyId: z.number().int().optional()}); export const getApiV1ChangesResponse = z.object({totalResultSize: z.number().int().optional(), hasMore: z.boolean().optional(), data: z.array(change)}); 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 audit logs for an account * * GET /v1/changes * * Also accepts auth: management_key */ export const getApiV1Changes = async (args: GetApiV1ChangesArgs, options?: GetApiV1ChangesOptions) => { const {baseUrl = "https://yourbaseurl.talon.one", fetch: fetchFn = fetch, managerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.applicationId != null) query.append("applicationId", String(args.applicationId)) if (args.entityPath != null) query.append("entityPath", String(args.entityPath)) if (args.userId != null) query.append("userId", String(args.userId)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (args.withTotalResultSize != null) query.append("withTotalResultSize", String(args.withTotalResultSize)) if (args.managementKeyId != null) query.append("managementKeyId", String(args.managementKeyId)) if (args.includeOld != null) query.append("includeOld", String(args.includeOld)) if (managerAuth !== undefined) headers.set("Authorization", managerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/changes${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(getApiV1ChangesResponse, await res.json()) };