import {z} from 'zod' export type StructsColumnVO = {aggFunc?: string | undefined, displayName?: string | undefined, field?: string | undefined, groupFunc?: string | undefined, id?: string | undefined}; export type StructsSortEntry = {colId?: string | undefined, pivotFieldGroupId?: string | undefined, sort?: string | undefined}; export type StructsRequestAgGrid = {AllowUnstableOrder?: boolean | undefined, FieldTypes?: Array | undefined, endRow?: number | undefined, filterModel?: {[key: string]: unknown} | undefined, groupKeys?: Array | undefined, pivotCols?: Array | undefined, pivotMode?: boolean | undefined, rowGroupCols?: Array | undefined, sortModel?: Array | undefined, startRow?: number | undefined, ungroupedAggregations?: Array | undefined, valueCols?: Array | undefined}; export type UpsertrowsRequest = {cells?: Array> | undefined, requestAgGrid?: StructsRequestAgGrid | undefined}; export type UpdateApiDatasetHandleColRowUpsertRowsArgs = {handle: string, col: string, row: string, body: UpsertrowsRequest}; export type UpdateApiDatasetHandleColRowUpsertRowsOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const structsResponse = z.object({Message: z.string().optional(), Success: z.boolean().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 }; /** * Upsert rows in a sheet * * PUT /dataset/{handle}/{col}/{row}/upsert-rows */ export const updateApiDatasetHandleColRowUpsertRows = async (args: UpdateApiDatasetHandleColRowUpsertRowsArgs, options: UpdateApiDatasetHandleColRowUpsertRowsOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-GIGASHEET-TOKEN", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/dataset/${encodeURIComponent(String(args.handle))}/${encodeURIComponent(String(args.col))}/${encodeURIComponent(String(args.row))}/upsert-rows${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(structsResponse, await res.json()) };