import {z} from 'zod' export type GetApiV2ModuleTableIdRowRowIdArgs = {id: string, row_id: string, authorization: string}; export type GetApiV2ModuleTableIdRowRowIdOptions = {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 Get Row By Id * * GET /v2/module/table/{id}/row/{row_id} */ export const getApiV2ModuleTableIdRowRowId = async (args: GetApiV2ModuleTableIdRowRowIdArgs, options: GetApiV2ModuleTableIdRowRowIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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/${encodeURIComponent(String(args.row_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) 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()) };