import {z} from 'zod' export type Id = string; export type SimpleMultiIdSearchOperation = {op: 'IN', value: Array}; export type StringSchema = string | null; export type StringSearchWithNullableOperation = {op: 'IN', value: Array} | {op: 'CONTAINS', value: StringSchema}; export type ContainsStringSearchOperation = {op: 'CONTAINS', value: StringSchema}; export type SimpleMultiStringSearchOperation = {op: 'IN', value: Array}; export type ProductFileJobTypes = 'CATALOG_CONN_ITEM_VARIANT_IMPORT' | 'CATALOG_CONN_ITEM_VARIANT_EXPORT' | 'CATALOG_CONN_BUNDLE_IMPORT' | 'CATALOG_CONN_BUNDLE_EXPORT'; export type MultiJobType = {op: 'IN', value: Array}; export type JobStatuses = 'SCHEDULED' | 'COMPLETED_PARTIALLY' | 'IN_PROGRESS' | 'COMPLETED' | 'FAILED' | 'UPDATING_SEARCH_INDEX' | 'CANCELLATION_IN_PROGRESS' | 'CANCELLED'; export type MultiJobStatuses = {op: 'IN', value: Array}; export type DatetimeSearchOperation = {op?: 'RANGE' | undefined, fromValue?: string | undefined, toValue?: string | undefined} | {op?: 'IN' | undefined, value?: Array | undefined} | {op?: ('EQUALS' | 'LTE' | 'GTE' | 'LT' | 'GT') | undefined, value?: string | undefined}; export type JobSearchRequestMatch = {id?: SimpleMultiIdSearchOperation | undefined, inputFileId?: SimpleMultiIdSearchOperation | undefined, name?: StringSearchWithNullableOperation | undefined, keyword?: (ContainsStringSearchOperation | SimpleMultiStringSearchOperation) | undefined, type?: MultiJobType | undefined, status?: MultiJobStatuses | undefined, createdAt?: DatetimeSearchOperation | undefined, completedAt?: DatetimeSearchOperation | undefined}; export type CreateApiCatalogConnectorJobsSearchArgs = {'x-fabric-tenant-id': string, 'x-fabric-request-id'?: string | undefined, body: {sort?: string | undefined, match: JobSearchRequestMatch, offset?: number | undefined, limit?: number | undefined}}; export type CreateApiCatalogConnectorJobsSearchOptions = {baseUrl?: string | undefined, authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const job = z.object({id: z.string().optional(), inputFileId: z.string().optional(), outputFiles: z.array(z.string()).optional(), errorFiles: z.array(z.string()).optional(), progressTracker: z.object({updatedAt: z.string().optional(), totalCount: z.number().int().optional(), processedCount: z.number().int().optional(), errorCount: z.number().int().optional()}).optional(), status: z.enum(["IN_PROGRESS", "COMPLETED", "FAILED"]).optional(), statusMessage: z.string().optional(), additionalDetails: z.object({itemsCreated: z.number().int().optional(), itemsUpdated: z.number().int().optional()}).optional(), createdAt: z.string().optional(), completedAt: z.string().optional(), updatedBy: z.string().optional(), updatedAt: z.string().optional()}); export const productJob = z.object({data: z.array(job).optional(), offset: z.number().int().optional(), limit: z.number().int().optional(), count: z.number().int().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 }; /** * Search jobs * * POST /catalog-connector-jobs/search */ export const createApiCatalogConnectorJobsSearch = async (args: CreateApiCatalogConnectorJobsSearchArgs, options?: CreateApiCatalogConnectorJobsSearchOptions) => { const {baseUrl = "https://api.fabric.inc/v3", fetch: fetchFn = fetch, authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-fabric-tenant-id'] != null) headers.set("x-fabric-tenant-id", String(args['x-fabric-tenant-id'])) if (args['x-fabric-request-id'] != null) headers.set("x-fabric-request-id", String(args['x-fabric-request-id'])) if (authorization !== undefined) headers.set('authorization', `Bearer ${authorization}`) const url = `${baseUrl.replace(/\/+$/, '')}/catalog-connector-jobs/search${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(productJob, await res.json()) };