import {z} from 'zod' export type FilePatch = {data: {type: 'file', attributes: {accessGroupId?: number | undefined, fileType?: (0 | 1 | 2 | 100) | undefined, filename?: string | undefined, isSecret?: boolean | undefined}}}; export type PatchApiApiV2UiFilesIdArgs = {id: number, body: FilePatch}; export type PatchApiApiV2UiFilesIdOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const filePostPatchResponse = z.object({jsonapi: z.object({version: z.string(), ext: z.array(z.string()).optional()}), links: z.object({self: z.string()}), data: z.object({id: z.number().int(), type: z.literal("file"), attributes: z.object({filename: z.string(), size: z.number().int(), isSecret: z.boolean(), fileType: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(100)]), accessGroupId: z.number().int(), lineCount: z.number().int()}), links: z.object({self: z.string()}), relationships: z.object({accessGroup: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.object({type: z.literal("accessGroup"), id: z.number().int()}).nullable().optional()})})}), included: z.array(z.object({id: z.number().int(), type: z.literal("accessGroup"), attributes: z.object({groupName: 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 }; /** * Update Files * * PATCH /api/v2/ui/files/{id} */ export const patchApiApiV2UiFilesId = async (args: PatchApiApiV2UiFilesIdArgs, options: PatchApiApiV2UiFilesIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/ui/files/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(filePostPatchResponse, await res.json()) };