import {z} from 'zod' export type GetApiAttachmentsArgs = {page?: number | undefined, pageSize?: number | undefined, candidateId?: string | undefined, companyId?: string | undefined, vacancyId?: string | undefined, type?: ('UNASSIGNED' | 'CV') | undefined}; export type GetApiAttachmentsOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationDto = z.object({totalItems: z.number(), page: z.number(), hasNextPage: z.boolean()}); export const attachmentCustomTypeDto = z.object({id: z.string(), name: z.string()}); export const dateISO = z.string(); export const attachmentDto = z.object({attachmentId: z.string(), attachmentName: z.string().nullable(), type: z.enum(["UNASSIGNED", "CV"]).nullable(), customType: attachmentCustomTypeDto.nullable(), createdAt: dateISO, extension: z.string().nullable(), size: z.number().nullable()}); export const paginatedAttachmentsDto = z.object({pagination: paginationDto, items: z.array(attachmentDto)}); 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 attachments * * GET /attachments */ export const getApiAttachments = async (args: GetApiAttachmentsArgs, options: GetApiAttachmentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.candidateId != null) query.append("candidateId", String(args.candidateId)) if (args.companyId != null) query.append("companyId", String(args.companyId)) if (args.vacancyId != null) query.append("vacancyId", String(args.vacancyId)) if (args.type != null) query.append("type", String(args.type)) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/attachments${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(paginatedAttachmentsDto, await res.json()) };