import {z} from 'zod' export type GetApiConsumersConsumerIdSyncsSyncidObjectsKindObjectIdArgs = {consumer_id: string, syncid: string, kind: string, object_id: string, language?: (string | null) | undefined}; export type GetApiConsumersConsumerIdSyncsSyncidObjectsKindObjectIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const objectEventItem = z.object({action: z.string(), details: z.record(z.string(), z.unknown()), chainexecution_id: z.string().nullable(), created_at: z.string(), last_at: z.string(), occurrences: z.number().int()}); export const objectLink = z.object({relation: z.enum(["needs", "needed_by"]), kind: z.string(), id: z.string(), source_id: z.string(), scope: z.record(z.string(), z.string()), status: z.string()}); export const syncIssue = z.object({id: z.string(), status: z.enum(["open", "resolved", "ignored"]), kind: z.string(), owner: z.enum(["consumer", "self_healing", "engineering"]), code: z.string(), title: z.string(), description: z.string().nullable(), message: z.string().nullable(), cause: z.record(z.string(), z.unknown()), scope: z.record(z.string(), z.unknown()), occurrences: z.number().int(), first_seen_at: z.string(), last_seen_at: z.string(), resolved_at: z.string().nullable(), objects_count: z.number().int(), actions: z.array(z.string())}); export const objectDetail = z.object({id: z.string(), kind: z.string(), capability: z.string(), source_id: z.string(), scope: z.record(z.string(), z.string()), status: z.string(), target_id: z.string().nullable(), booked_as: z.string().nullable(), attempts: z.number().int(), last_error_code: z.string().nullable(), created_at: z.string(), updated_at: z.string(), succeeded_at: z.string().nullable(), target: z.record(z.string(), z.unknown()).nullable(), history: z.array(objectEventItem), links: z.array(objectLink), issues: z.array(syncIssue)}); 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 }; /** * What a consumer's sync did to one object * * GET /consumers/{consumer_id}/syncs/{syncid}/objects/{kind}/{object_id} */ export const getApiConsumersConsumerIdSyncsSyncidObjectsKindObjectId = async (args: GetApiConsumersConsumerIdSyncsSyncidObjectsKindObjectIdArgs, options?: GetApiConsumersConsumerIdSyncsSyncidObjectsKindObjectIdOptions) => { const {baseUrl = "https://api.chift.eu", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.language != null) query.append("language", String(args.language)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/consumers/${encodeURIComponent(String(args.consumer_id))}/syncs/${encodeURIComponent(String(args.syncid))}/objects/${encodeURIComponent(String(args.kind))}/${encodeURIComponent(String(args.object_id))}${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(objectDetail, await res.json()) };