import {z} from 'zod' export type ExportLeftHashesHelperAPI = {hashlistId?: number | undefined}; export type CreateApiApiV2HelperExportLeftHashesArgs = {body: ExportLeftHashesHelperAPI}; export type CreateApiApiV2HelperExportLeftHashesOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fileSingleResponse = 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 }; /** * Create Helpers * * POST /api/v2/helper/exportLeftHashes */ export const createApiApiV2HelperExportLeftHashes = async (args: CreateApiApiV2HelperExportLeftHashesArgs, options: CreateApiApiV2HelperExportLeftHashesOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.api+json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/helper/exportLeftHashes${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(fileSingleResponse, await res.json()) };