import {z} from 'zod' export type GetApiReportsArchivesArgs = {scopeType?: ('organization' | 'group' | 'project') | undefined, scopeId?: number | undefined, userId?: number | undefined, taskId?: number | undefined, name?: ('costs-estimation-pe' | 'translation-costs-pe' | 'group-translation-costs-pe' | 'pre-translate-accuracy' | 'translator-accuracy') | undefined, dateFrom?: string | undefined, dateTo?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiReportsArchivesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const reportArchive = z.object({id: z.number().int().optional(), scopeType: z.string().optional(), scopeId: z.number().int().optional(), userId: z.number().int().optional(), name: z.string().optional(), webUrl: z.string().optional(), scheme: z.object({}).optional(), status: z.string().optional(), progress: z.number().int().optional(), createdAt: z.string().optional()}); export const reportArchiveResource = z.object({data: reportArchive}); export const pagination = z.object({offset: z.number().int().optional(), limit: z.number().int().optional()}); export const reportArchiveCollectionResource = z.object({data: z.array(reportArchiveResource), pagination: pagination.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 Report Archives * * GET /reports/archives */ export const getApiReportsArchives = async (args: GetApiReportsArchivesArgs, options?: GetApiReportsArchivesOptions) => { const {baseUrl = "https://api.crowdin.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.scopeType != null) query.append("scopeType", String(args.scopeType)) if (args.scopeId != null) query.append("scopeId", String(args.scopeId)) if (args.userId != null) query.append("userId", String(args.userId)) if (args.taskId != null) query.append("taskId", String(args.taskId)) if (args.name != null) query.append("name", String(args.name)) if (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) const url = `${baseUrl.replace(/\/+$/, '')}/reports/archives${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(reportArchiveCollectionResource, await res.json()) };