import {z} from 'zod' export type ProductFileJobTypes = 'CATALOG_CONN_ITEM_VARIANT_IMPORT' | 'CATALOG_CONN_ITEM_VARIANT_EXPORT' | 'CATALOG_CONN_BUNDLE_IMPORT' | 'CATALOG_CONN_BUNDLE_EXPORT'; export type Locale = string; export type FormatTypes = 'csv' | 'jsonl'; export type CreateApiCatalogConnectorFilesArgs = {'x-fabric-tenant-id': string, 'x-fabric-request-id'?: string | undefined, body: {type: ProductFileJobTypes, name: string, locale: Locale, formatType: FormatTypes}}; export type CreateApiCatalogConnectorFilesOptions = {baseUrl?: string | undefined, authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const productFileJobTypes = z.enum(["CATALOG_CONN_ITEM_VARIANT_IMPORT", "CATALOG_CONN_ITEM_VARIANT_EXPORT", "CATALOG_CONN_BUNDLE_IMPORT", "CATALOG_CONN_BUNDLE_EXPORT"]); export const locale = z.string(); export const formatTypes = z.enum(["csv", "jsonl"]); export const file = z.object({id: z.string().optional(), type: productFileJobTypes.optional(), name: z.string().optional(), locale: locale.optional(), formatType: formatTypes.optional(), createdAt: z.string().optional(), updatedBy: z.string().optional(), updatedAt: z.string().optional(), location: z.string().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 }; /** * Create a file object and retrieve the file upload URL * * POST /catalog-connector-files */ export const createApiCatalogConnectorFiles = async (args: CreateApiCatalogConnectorFilesArgs, options?: CreateApiCatalogConnectorFilesOptions) => { 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-files${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(file, await res.json()) };