import {z} from 'zod' export type GetApiAssetsAssetIdCommentsArgs = {asset_id: string, 'page[number]'?: number | undefined, 'page[size]'?: number | undefined, 'sort[]'?: (Array<'created_at' | 'updated_at' | '-created_at' | '-updated_at'>) | undefined, 'filter[resolved]'?: ('true' | 'false') | undefined, 'filter[created_at][operator]'?: 'gt' | undefined, 'filter[created_at][value]'?: string | undefined}; export type GetApiAssetsAssetIdCommentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const comment: z.ZodType = z.object({id: z.string(), asset_id: z.string(), parent_id: z.string().nullable(), resolved: z.boolean(), text: z.string(), mentions: z.array(z.object({user_id: z.string().optional(), name: z.string().optional()})), author: z.object({id: z.string().optional(), name: z.string().optional()}).nullable(), is_external: z.boolean(), created_at: z.string(), updated_at: z.string(), replies: z.array(z.lazy(() => comment))}); export const commentList = z.object({meta: z.object({total: z.number().int().optional(), current_page: z.number().int().optional(), last_page: z.number().int().optional(), per_page: z.number().int().optional(), from: z.number().int().nullable().optional(), to: z.number().int().nullable().optional()}).optional(), data: z.array(comment).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 }; /** * List comments on an asset * * GET /assets/{asset_id}/comments */ export const getApiAssetsAssetIdComments = async (args: GetApiAssetsAssetIdCommentsArgs, options?: GetApiAssetsAssetIdCommentsOptions) => { const {baseUrl = "https://enterprise.knak.io/api/published/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) for (const item of args['sort[]'] ?? []) query.append("sort[]", String(item)) if (args['filter[resolved]'] != null) query.append("filter[resolved]", String(args['filter[resolved]'])) if (args['filter[created_at][operator]'] != null) query.append("filter[created_at][operator]", String(args['filter[created_at][operator]'])) if (args['filter[created_at][value]'] != null) query.append("filter[created_at][value]", String(args['filter[created_at][value]'])) const url = `${baseUrl.replace(/\/+$/, '')}/assets/${encodeURIComponent(String(args.asset_id))}/comments${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(commentList, await res.json()) };