import {z} from 'zod' export type ProjectFileType = 'auto' | 'android' | 'macosx' | 'resx' | 'properties' | 'gettext' | 'yaml' | 'php' | 'json' | 'xml' | 'ini' | 'rc' | 'resw' | 'resjson' | 'qtts' | 'joomla' | 'chrome' | 'dtd' | 'dklang' | 'flex' | 'nsh' | 'wxl' | 'xliff' | 'xliff_two' | 'html' | 'haml' | 'txt' | 'csv' | 'md' | 'mdx_v1' | 'mdx_v2' | 'flsnp' | 'fm_html' | 'fm_md' | 'mediawiki' | 'docx' | 'vsdx' | 'xlsx' | 'sbv' | 'properties_play' | 'properties_xml' | 'maxthon' | 'go_json' | 'dita' | 'idml' | 'mif' | 'stringsdict' | 'plist' | 'vtt' | 'vdf' | 'srt' | 'stf' | 'toml' | 'contentful_rt' | 'svg' | 'js' | 'coffee' | 'ts' | 'i18next_json' | 'xaml' | 'arb' | 'adoc' | 'fbt' | 'webxml' | 'nestjs_i18n' | 'loc'; export type AiFileTranslationsForm = {storageId: number, sourceLanguageId?: string | undefined, targetLanguageId: string, type?: ProjectFileType | undefined, parserVersion?: number | undefined, tmIds?: Array | undefined, glossaryIds?: Array | undefined, styleGuideIds?: Array | undefined, aiPromptId?: number | undefined, aiProviderId?: number | undefined, aiModelId?: string | undefined, instructions?: Array | undefined, attachmentIds?: Array | undefined}; export type CreateApiUsersUserIdAiFileTranslationsArgs = {userId: number, body: AiFileTranslationsForm}; export type CreateApiUsersUserIdAiFileTranslationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const aiFileTranslationsJobResponseModel = z.object({identifier: z.string().optional(), status: z.string().optional(), progress: z.number().int().optional(), attributes: z.object({stage: z.string().optional(), error: z.object({stage: z.string().optional(), message: z.string().optional()}).nullable().optional(), downloadName: z.string().nullable().optional(), sourceLanguageId: z.string().nullable().optional(), targetLanguageId: z.string().optional(), originalFileName: z.string().optional(), detectedType: z.string().nullable().optional(), parserVersion: z.number().int().nullable().optional()}).optional(), createdAt: z.string().optional(), updatedAt: z.string().nullable().optional(), startedAt: z.string().nullable().optional(), finishedAt: z.string().nullable().optional()}); export const aiFileTranslationsJobResource = z.object({data: aiFileTranslationsJobResponseModel}); 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 }; /** * AI File Translations * * POST /users/{userId}/ai/file-translations */ export const createApiUsersUserIdAiFileTranslations = async (args: CreateApiUsersUserIdAiFileTranslationsArgs, options?: CreateApiUsersUserIdAiFileTranslationsOptions) => { const {baseUrl = "https://api.crowdin.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/users/${encodeURIComponent(String(args.userId))}/ai/file-translations${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(aiFileTranslationsJobResource, await res.json()) };