import {z} from 'zod' export type CreateApiV2ModuleTableIdRowArgs = {id: string, authorization: string, body: Record}; export type CreateApiV2ModuleTableIdRowOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const servicesTableSchemaCollaboratorsUserResponse = z.object({id: z.string(), name: z.string(), email: z.string(), permission: z.string().optional()}); export const servicesTableSchemaCollaboratorsTeamResponse = z.object({id: z.string(), name: z.string(), permission: z.string().optional(), user_ids: z.array(z.string()).optional()}); export const entityCollaborationResponse = z.object({allow_collaboration: z.boolean(), allow_edit: z.boolean(), entity_column: z.string(), permission: z.string().optional()}); export const collaboratorsResponse = z.object({users: z.array(servicesTableSchemaCollaboratorsUserResponse), teams: z.array(servicesTableSchemaCollaboratorsTeamResponse), entity: entityCollaborationResponse.optional()}); export const sourceType = z.enum(["TEMPLATE", "RISK_ASSESSMENT", "ENTITY", "PROJECT_COMPONENT", "OTHER", "TABLE"]); export const source = z.object({type: sourceType, value: z.string()}); export const recordSource = z.object({type: z.string(), value: z.string(), args: z.object({})}); export const rowResponse = z.object({id: z.string(), collaborators: collaboratorsResponse, source: source, created_at: z.string(), updated_at: z.string(), values: z.object({}), permitted: z.boolean().optional(), record_source: recordSource.optional(), overrided_values: z.object({}).optional(), archived_at: 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 }; /** * Ex Create Row * * POST /v2/module/table/{id}/row */ export const createApiV2ModuleTableIdRow = async (args: CreateApiV2ModuleTableIdRowArgs, options: CreateApiV2ModuleTableIdRowOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/module/table/${encodeURIComponent(String(args.id))}/row${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(rowResponse, await res.json()) };