import {z} from 'zod' export type Variant = Record; export type HubDbTableRowV3Request = {childTableId: number, displayIndex: number, name?: string | undefined, path?: string | undefined, values: {[key: string]: Variant}}; export type BatchInputHubDbTableRowV3Request = {inputs: Array}; export type CreateApiCmsHubdb202603TablesTableIdOrNameRowsDraftBatchCreateArgs = {tableIdOrName: string, body: BatchInputHubDbTableRowV3Request}; export type CreateApiCmsHubdb202603TablesTableIdOrNameRowsDraftBatchCreateOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const hubDbTableRowV3 = z.object({childTableId: z.string(), createdAt: z.string(), id: z.string(), name: z.string(), path: z.string(), publishedAt: z.string(), updatedAt: z.string(), values: z.record(z.string(), z.object({}))}); export const batchResponseHubDbTableRowV3 = z.object({completedAt: z.string(), links: z.record(z.string(), z.string()).optional(), requestedAt: z.string().optional(), results: z.array(hubDbTableRowV3), startedAt: z.string(), status: z.enum(["CANCELED", "COMPLETE", "PENDING", "PROCESSING"])}); 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 rows in batch * * POST /cms/hubdb/2026-03/tables/{tableIdOrName}/rows/draft/batch/create * * Also accepts auth: oauth2 */ export const createApiCmsHubdb202603TablesTableIdOrNameRowsDraftBatchCreate = async (args: CreateApiCmsHubdb202603TablesTableIdOrNameRowsDraftBatchCreateArgs, options?: CreateApiCmsHubdb202603TablesTableIdOrNameRowsDraftBatchCreateOptions) => { const {baseUrl = "https://api.hubapi.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/cms/hubdb/2026-03/tables/${encodeURIComponent(String(args.tableIdOrName))}/rows/draft/batch/create${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(batchResponseHubDbTableRowV3, await res.json()) };