import {z} from 'zod' export type FallbackLanguagesScheme = {languageId?: Array | undefined}; export type PreTranslationForm = {languageIds: Array, branchIds?: Array | undefined, translateUntranslatedOnly?: boolean | undefined, labelIds?: Array | undefined, excludeLabelIds?: Array | undefined, labelMatchRule?: ('all' | 'any') | undefined, excludeLabelMatchRule?: ('all' | 'any') | undefined, method?: ('tm' | 'mt' | 'ai') | undefined, priority?: ('low' | 'normal' | 'high') | undefined, engineId?: number | undefined, aiPromptId?: number | undefined, autoApproveOption?: ('all' | 'exceptAutoSubstituted' | 'perfectMatchApprovedOnly' | 'perfectMatchOnly' | 'none') | undefined, duplicateTranslations?: boolean | undefined, skipApprovedTranslations?: boolean | undefined, scope?: ('untranslated' | 'translated' | 'all') | undefined, translationModifiedBefore?: (string | null) | undefined, translationModifiedAfter?: (string | null) | undefined, replaceTranslationsOption?: ('none' | 'autoTranslated' | 'all') | undefined, resetApprovalStatus?: boolean | undefined, notifyOnCompletion?: boolean | undefined, translateWithPerfectMatchOnly?: boolean | undefined, fallbackLanguages?: FallbackLanguagesScheme | undefined, sourceLanguageId?: string | undefined, customInstruction?: string | undefined} | {taskId: number, method?: ('tm' | 'mt' | 'ai') | undefined, priority?: ('low' | 'normal' | 'high') | undefined, engineId?: number | undefined, aiPromptId?: number | undefined, autoApproveOption?: ('all' | 'exceptAutoSubstituted' | 'perfectMatchApprovedOnly' | 'perfectMatchOnly' | 'none') | undefined, duplicateTranslations?: boolean | undefined, skipApprovedTranslations?: boolean | undefined, scope?: ('untranslated' | 'translated' | 'all') | undefined, translationModifiedBefore?: (string | null) | undefined, translationModifiedAfter?: (string | null) | undefined, replaceTranslationsOption?: ('none' | 'autoTranslated' | 'all') | undefined, resetApprovalStatus?: boolean | undefined, notifyOnCompletion?: boolean | undefined, translateWithPerfectMatchOnly?: boolean | undefined, fallbackLanguages?: FallbackLanguagesScheme | undefined, sourceLanguageId?: string | undefined, customInstruction?: string | undefined}; export type CreateApiProjectsProjectIdPreTranslationsArgs = {projectId: number, body: PreTranslationForm}; export type CreateApiProjectsProjectIdPreTranslationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const crowdinPreTranslateAttributes = z.object({languageIds: z.array(z.string()).optional(), branchIds: z.array(z.number().int()).optional(), taskId: z.number().int().optional(), method: z.string().optional(), autoApproveOption: z.string().optional(), duplicateTranslations: z.boolean().optional(), skipApprovedTranslations: z.boolean().optional(), translateUntranslatedOnly: z.boolean().optional(), scope: z.enum(["untranslated", "translated", "all"]).optional(), translationModifiedBefore: z.string().nullable().optional(), translationModifiedAfter: z.string().nullable().optional(), replaceTranslationsOption: z.enum(["none", "autoTranslated", "all"]).optional(), resetApprovalStatus: z.boolean().optional(), translateWithPerfectMatchOnly: z.boolean().optional(), priority: z.enum(["low", "normal", "high"]).optional(), notifyOnCompletion: z.boolean().optional()}); export const crowdinPreTranslation = z.object({identifier: z.string().optional(), status: z.enum(["created", "in_progress", "canceled", "failed", "finished"]).optional(), progress: z.number().int().optional(), attributes: crowdinPreTranslateAttributes.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), startedAt: z.string().optional(), finishedAt: z.string().optional()}); export const crowdinPreTranslationResource = z.object({data: crowdinPreTranslation}); 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 }; /** * Apply Auto-Translation * * POST /projects/{projectId}/pre-translations */ export const createApiProjectsProjectIdPreTranslations = async (args: CreateApiProjectsProjectIdPreTranslationsArgs, options?: CreateApiProjectsProjectIdPreTranslationsOptions) => { 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(/\/+$/, '')}/projects/${encodeURIComponent(String(args.projectId))}/pre-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(crowdinPreTranslationResource, await res.json()) };