import {z} from 'zod' export type UpsertDatasetItemBody = {expectedOutput: unknown, ifRowId?: (string | null) | undefined, input: unknown, logicalId?: (string | null) | undefined, metadata: unknown, restore?: boolean | undefined, tags?: Array | undefined}; export type UpdateApiApiOrgIdDatasetsDatasetIdItemsArgs = {org_id: string, dataset_id: string, body: {idempotencyKey?: (string | null) | undefined, items: Array}}; export type UpdateApiApiOrgIdDatasetsDatasetIdItemsOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const upsertOutcomeBody = z.enum(["created", "updated", "restored", "unchanged"]); export const upsertedDatasetItemBody = z.object({globalVersion: z.number().int(), logicalId: z.string(), outcome: upsertOutcomeBody, rowId: z.string()}); export const updateApiApiOrgIdDatasetsDatasetIdItemsResponse = z.object({datasetVersion: z.number().int(), items: z.array(upsertedDatasetItemBody), replayed: z.boolean()}); 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 or update Dataset Cases under client-supplied identity * * PUT /api/{org_id}/datasets/{dataset_id}/items */ export const updateApiApiOrgIdDatasetsDatasetIdItems = async (args: UpdateApiApiOrgIdDatasetsDatasetIdItemsArgs, options: UpdateApiApiOrgIdDatasetsDatasetIdItemsOptions) => { const {baseUrl, 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 (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/datasets/${encodeURIComponent(String(args.dataset_id))}/items${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiApiOrgIdDatasetsDatasetIdItemsResponse, await res.json()) };