import {z} from 'zod' export type GetApiProjectsProjectIdPreTranslationsJobIdentifierReportArgs = {projectId: number, jobIdentifier: string}; export type GetApiProjectsProjectIdPreTranslationsJobIdentifierReportOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const skippedInfo = z.object({translation_eq_source: z.number().int().optional(), qa_check: z.number().int().optional(), hidden_strings: z.number().int().optional(), ai_error: z.number().int().optional()}); export const skippedQaCheckCategories = z.object({}); export const targetLanguage = z.object({id: z.string().optional(), files: z.array(z.object({id: z.string().optional(), statistics: z.object({phrases: z.number().int().optional(), words: z.number().int().optional()}).optional()})).optional(), skipped: skippedInfo.optional(), skippedQaCheckCategories: skippedQaCheckCategories.optional()}); export const preTranslationReport = z.object({languages: z.array(targetLanguage).optional(), preTranslateType: z.string().optional()}); export const preTranslationReportResource = z.object({data: preTranslationReport}); 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 }; /** * Auto-Translation Report * * GET /projects/{projectId}/pre-translations/{jobIdentifier}/report */ export const getApiProjectsProjectIdPreTranslationsJobIdentifierReport = async (args: GetApiProjectsProjectIdPreTranslationsJobIdentifierReportArgs, options?: GetApiProjectsProjectIdPreTranslationsJobIdentifierReportOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.projectId))}/pre-translations/${encodeURIComponent(String(args.jobIdentifier))}/report${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(preTranslationReportResource, await res.json()) };