import {z} from 'zod' export type TableUrn = string; export type RecordUrn = string; export type RecordValue = {name: string, value: unknown, recordUrn?: RecordUrn | undefined}; export type RecordBatchUpsertItem = {id: string, urn?: RecordUrn | undefined, values: Array}; export type CreateApiReferenceDataV1TablesTableUrnDraftBatchUpsertArgs = {tableUrn: TableUrn, body: {data: Array}}; export type CreateApiReferenceDataV1TablesTableUrnDraftBatchUpsertOptions = {baseUrl?: string | undefined, JWT?: string | undefined, fetch?: typeof fetch | undefined}; export const batchOperationMeta = z.object({total: z.number().int(), success: z.number().int(), fail: z.number().int()}); export const recordUrn = z.string(); export const recordValue = z.object({name: z.string(), value: z.unknown(), recordUrn: recordUrn.optional()}); export const userUrn = z.string(); export const recordState = z.string(); export const recordWithState = z.object({urn: recordUrn, values: z.array(recordValue), lastChangedAt: z.string().optional(), lastChangedByUrn: userUrn.optional(), state: recordState}); export const validationErrorDetail = z.object({detail: z.string(), pointer: z.string()}); export const recordBatchResultItem = z.union([z.object({id: z.string(), status: z.string(), data: recordWithState, error: z.object({type: z.string(), title: z.string(), detail: z.string(), errors: z.array(validationErrorDetail).optional()}).optional()}), z.object({id: z.string(), status: z.string(), data: recordWithState.optional(), error: z.object({type: z.string(), title: z.string(), detail: z.string(), errors: z.array(validationErrorDetail).optional()})})]); export const createApiReferenceDataV1TablesTableUrnDraftBatchUpsertResponse = z.object({meta: batchOperationMeta, results: z.array(recordBatchResultItem)}); 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 }; /** * Batch upsert table draft records * * POST /reference-data/v1/tables/{tableUrn}/draft/batch/upsert */ export const createApiReferenceDataV1TablesTableUrnDraftBatchUpsert = async (args: CreateApiReferenceDataV1TablesTableUrnDraftBatchUpsertArgs, options?: CreateApiReferenceDataV1TablesTableUrnDraftBatchUpsertOptions) => { const {baseUrl = "https://your-environment.ataccama.one/api", fetch: fetchFn = fetch, JWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (JWT !== undefined) headers.set('authorization', `Bearer ${JWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/reference-data/v1/tables/${encodeURIComponent(String(args.tableUrn))}/draft/batch/upsert${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(createApiReferenceDataV1TablesTableUrnDraftBatchUpsertResponse, await res.json()) };