import {z} from 'zod' export type OrganizationID = string; export type LogFilter = {field: 'instance' | 'level' | 'process' | 'body', op: 'in' | 'contains' | 'icontains' | 'regex' | 'iregex', values?: Array | undefined, value?: string | undefined}; export type BranchLogsRequest = {start: string, end: string, filters?: Array | undefined, limit?: number | undefined, cursor?: string | undefined}; export type CreateApiOrganizationsOrganizationIDProjectsProjectIDBranchesBranchIDLogsArgs = {organizationID: OrganizationID, projectID: string, branchID: string, body: BranchLogsRequest}; export type CreateApiOrganizationsOrganizationIDProjectsProjectIDBranchesBranchIDLogsOptions = {baseUrl?: string | undefined, xata?: string | undefined, fetch?: typeof fetch | undefined}; export const logLevel = z.enum(["debug", "info", "warning", "error"]); export const logEntry = z.object({timestamp: z.string(), instanceID: z.string(), level: logLevel.optional(), message: z.string(), process: z.string().optional()}); export const branchLogs = z.object({start: z.string(), end: z.string(), logs: z.array(logEntry), nextCursor: z.string().nullable()}); 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 }; /** * Retrieve branch logs * * POST /organizations/{organizationID}/projects/{projectID}/branches/{branchID}/logs */ export const createApiOrganizationsOrganizationIDProjectsProjectIDBranchesBranchIDLogs = async (args: CreateApiOrganizationsOrganizationIDProjectsProjectIDBranchesBranchIDLogsArgs, options?: CreateApiOrganizationsOrganizationIDProjectsProjectIDBranchesBranchIDLogsOptions) => { const {baseUrl = "https://api.xata.tech", fetch: fetchFn = fetch, xata} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xata !== undefined) headers.set('authorization', `Bearer ${xata}`) const url = `${baseUrl.replace(/\/+$/, '')}/organizations/${encodeURIComponent(String(args.organizationID))}/projects/${encodeURIComponent(String(args.projectID))}/branches/${encodeURIComponent(String(args.branchID))}/logs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(branchLogs, await res.json()) };