import {z} from 'zod' export type ExternalAPIContractsRequestsV1AddCommentRequest = {requestId?: (string | null) | undefined, itemId: string, content: string | null, parentCommentId?: (string | null) | undefined}; export type CreateApiApiV1CommentsArgs = {body: ExternalAPIContractsRequestsV1AddCommentRequest}; export type CreateApiApiV1CommentsOptions = {baseUrl: string, TeamhoodAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const externalAPIContractsResponsesV1EntitiesComment = z.object({id: z.string(), itemId: z.string(), parentCommentId: z.string().nullable().optional(), userId: z.string().nullable().optional(), userName: z.string().nullable(), content: z.string().nullable(), createdOn: z.string(), editedOn: z.string().nullable().optional(), isPinned: z.boolean(), isResolved: z.boolean(), attachmentIds: z.array(z.string()).nullable()}); 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 }; /** * Add Comment to an item * * POST /api/v1/comments */ export const createApiApiV1Comments = async (args: CreateApiApiV1CommentsArgs, options: CreateApiApiV1CommentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, TeamhoodAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TeamhoodAPIKey !== undefined) headers.set("X-ApiKey", TeamhoodAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/comments${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(externalAPIContractsResponsesV1EntitiesComment, await res.json()) };