import {z} from 'zod' export type GetApiConsumersConsumerIdSyncsSyncidArgs = {consumer_id: string, syncid: string, preview_executionid?: (string | null) | undefined}; export type GetApiConsumersConsumerIdSyncsSyncidOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const syncConsumerStatus = z.enum(["active", "inactive"]); export const readMappingItem = z.object({source_id: z.string().nullable().optional(), target_id: z.string().nullable().optional()}); export const readSyncMappingItem = z.object({name: z.string(), description: z.string().nullable().optional(), display_order: z.number().int().optional(), challenge_question: z.string().nullable().optional(), conditions: z.record(z.string(), z.record(z.string(), z.unknown())).nullable().optional(), values: z.array(readMappingItem), hidden_source_ids: z.array(z.string()), sub_mapping_name: z.string(), sub_mapping_description: z.string().nullable().optional()}); export const syncSkipReason = z.enum(["presync", "connector_feature", "challenge"]); export const readSyncSkipMappingItem = z.object({name: z.string(), skip_reason: syncSkipReason}); export const datastoreStatus = z.enum(["active", "inactive"]); export const datastoreColumn = z.object({name: z.string(), title: z.string(), type: z.string(), optional: z.boolean().nullable().optional()}); export const datastoreDef = z.object({columns: z.array(datastoreColumn), search_column: z.string().nullable().optional()}); export const flowDataStoreItemOutput = z.object({id: z.string().nullable().optional(), name: z.string(), status: datastoreStatus.nullable().optional(), definition: datastoreDef}); export const flowConfigOutput = z.object({definitionFields: z.array(z.record(z.string(), z.unknown())).nullable().optional(), doorkeyFields: z.array(z.record(z.string(), z.unknown())).nullable().optional(), customFields: z.array(z.record(z.string(), z.unknown())).nullable().optional(), datastores: z.array(flowDataStoreItemOutput).nullable().optional()}); export const triggerType = z.enum(["timer", "event"]); export const readFlowTrigger = z.object({id: z.string(), type: triggerType, cronschedule: z.string().nullable().optional()}); export const readFlowConsumerItem = z.object({name: z.string(), description: z.string().nullable().optional(), id: z.string(), config: flowConfigOutput.nullable().optional(), values: z.record(z.string(), z.unknown()), enabled_on: z.string().nullable().optional(), trigger: readFlowTrigger}); export const syncConsumerItem = z.object({syncid: z.string(), sync_name: z.string(), status: syncConsumerStatus.optional(), status_details: z.string().nullable().optional(), enabled: z.boolean().optional(), link_createdon: z.string(), link_mappings: z.array(readSyncMappingItem).nullable().optional(), skip_mappings: z.array(readSyncSkipMappingItem).nullable().optional(), link_metadata: z.record(z.string(), z.unknown()).nullable().optional(), link_presync: z.record(z.string(), z.unknown()).nullable().optional(), enabled_flows: z.array(readFlowConsumerItem).nullable().optional()}); 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 sync information for one consumer * * GET /consumers/{consumer_id}/syncs/{syncid} */ export const getApiConsumersConsumerIdSyncsSyncid = async (args: GetApiConsumersConsumerIdSyncsSyncidArgs, options?: GetApiConsumersConsumerIdSyncsSyncidOptions) => { 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.preview_executionid != null) query.append("preview_executionid", String(args.preview_executionid)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/consumers/${encodeURIComponent(String(args.consumer_id))}/syncs/${encodeURIComponent(String(args.syncid))}${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(syncConsumerItem, await res.json()) };