import {z} from 'zod' export type MtCredentialsGoogle = {apiKey?: string | undefined}; export type MtCredentialsGoogleAutoml = {credentials?: string | undefined}; export type MtCredentialsMicrosoft = {apiKey?: string | undefined, model?: string | undefined}; export type MtCredentialsDeepl = {apiKey?: string | undefined}; export type MtCredentialsAmazon = {accessKey?: string | undefined, secretKey?: string | undefined}; export type MtCredentialsModernmt = {apiKey?: string | undefined}; export type MtCredentialsCustomMt = {url?: string | undefined}; export type EnterpriseMtCreateForm = {name: string, type: 'google' | 'google_automl' | 'microsoft' | 'deepl' | 'amazon' | 'modernmt' | 'custom_mt', credentials: MtCredentialsGoogle | MtCredentialsGoogleAutoml | MtCredentialsMicrosoft | MtCredentialsDeepl | MtCredentialsAmazon | MtCredentialsModernmt | MtCredentialsCustomMt, groupId?: number | undefined, enabledLanguageIds?: Array | undefined, enabledProjectIds?: Array | undefined, isEnabled?: boolean | undefined}; export type CreateApiMtsArgs = {body: EnterpriseMtCreateForm}; export type CreateApiMtsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const enterpriseMt = z.object({id: z.number().int().optional(), groupId: z.number().int().optional(), name: z.string().optional(), type: z.string().optional(), credentials: z.object({crowdin_nmt: z.number().int().optional(), crowdin_nmt_multi_translations: z.number().int().optional()}).optional(), supportedLanguageIds: z.array(z.string()).optional(), supportedLanguagePairs: z.record(z.string(), z.array(z.string())).nullable().optional(), enabledLanguageIds: z.array(z.string()).optional(), enabledProjectIds: z.array(z.number().int()).optional(), projectIds: z.array(z.number().int()).optional(), isEnabled: z.boolean().optional()}); export const enterpriseMtResource = z.object({data: enterpriseMt}); 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 }; /** * Add MT * * POST /mts */ export const createApiMts = async (args: CreateApiMtsArgs, options?: CreateApiMtsOptions) => { 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(/\/+$/, '')}/mts${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(enterpriseMtResource, await res.json()) };