import {z} from 'zod' export type EnterpriseTaskAssignee = {id: number, wordsCount?: number | undefined}; export type EnterpriseTaskAssignedTeams = {id: number, wordsCount?: number | undefined}; export type EnterpriseAdditionalFields = {[key: string]: string | number | boolean | Record}; export type EnterpriseTaskCreateForm = {fileIds?: Array | undefined, directoryIds?: Array | undefined, branchIds?: Array | undefined, type?: (0 | 1) | undefined, workflowStepId?: number | undefined, title: string, languageId: string, labelIds?: Array | undefined, labelMatchRule?: ('all' | 'any' | null) | undefined, excludeLabelIds?: Array | undefined, excludeLabelMatchRule?: ('all' | 'any' | null) | undefined, status?: ('todo' | 'in_progress') | undefined, description?: string | undefined, splitFiles?: boolean | undefined, splitContent?: boolean | undefined, skipAssignedStrings?: boolean | undefined, skipAssignedStringsScope?: ('all' | 'sameWorkflowStep' | null) | undefined, assignees?: Array | undefined, assignedTeams?: Array | undefined, includePreTranslatedStringsOnly?: boolean | undefined, deadline?: string | undefined, startedAt?: string | undefined, dateFrom?: string | undefined, dateTo?: string | undefined, translationsUpdatedDateFrom?: string | undefined, translationsUpdatedDateTo?: string | undefined, generateCostEstimate?: boolean | undefined, generateTranslationCost?: boolean | undefined, reportSettingsTemplateId?: (number | null) | undefined, fields?: (EnterpriseAdditionalFields | null) | undefined, batchId?: (number | null) | undefined} | {stringIds: Array, type?: (0 | 1) | undefined, workflowStepId?: number | undefined, title: string, languageId: string, status?: ('todo' | 'in_progress') | undefined, description?: string | undefined, splitFiles?: boolean | undefined, splitContent?: boolean | undefined, skipAssignedStrings?: boolean | undefined, skipAssignedStringsScope?: ('all' | 'sameWorkflowStep' | null) | undefined, assignees?: Array | undefined, assignedTeams?: Array | undefined, includePreTranslatedStringsOnly?: boolean | undefined, deadline?: string | undefined, startedAt?: string | undefined, dateFrom?: string | undefined, dateTo?: string | undefined, translationsUpdatedDateFrom?: string | undefined, translationsUpdatedDateTo?: string | undefined, generateCostEstimate?: boolean | undefined, generateTranslationCost?: boolean | undefined, reportSettingsTemplateId?: (number | null) | undefined, fields?: (EnterpriseAdditionalFields | null) | undefined, batchId?: (number | null) | undefined}; export type EnterpriseVendorTaskCreateForm = {fileIds?: Array | undefined, directoryIds?: Array | undefined, branchIds?: Array | undefined, workflowStepId: number, title: string, languageId: string, labelIds?: Array | undefined, labelMatchRule?: ('all' | 'any' | null) | undefined, excludeLabelIds?: Array | undefined, excludeLabelMatchRule?: ('all' | 'any' | null) | undefined, description?: string | undefined, skipAssignedStrings?: boolean | undefined, skipAssignedStringsScope?: ('all' | 'sameWorkflowStep' | null) | undefined, includePreTranslatedStringsOnly?: boolean | undefined, deadline?: string | undefined, startedAt?: string | undefined, dateTo?: string | undefined, translationsUpdatedDateFrom?: string | undefined, translationsUpdatedDateTo?: string | undefined, generateCostEstimate?: boolean | undefined, generateTranslationCost?: boolean | undefined, reportSettingsTemplateId?: (number | null) | undefined, fields?: (EnterpriseAdditionalFields | null) | undefined} | {stringIds: Array, workflowStepId: number, title: string, languageId: string, labelIds?: Array | undefined, labelMatchRule?: ('all' | 'any' | null) | undefined, description?: string | undefined, skipAssignedStrings?: boolean | undefined, skipAssignedStringsScope?: ('all' | 'sameWorkflowStep' | null) | undefined, includePreTranslatedStringsOnly?: boolean | undefined, deadline?: string | undefined, startedAt?: string | undefined, dateTo?: string | undefined, translationsUpdatedDateFrom?: string | undefined, translationsUpdatedDateTo?: string | undefined, generateCostEstimate?: boolean | undefined, generateTranslationCost?: boolean | undefined, reportSettingsTemplateId?: (number | null) | undefined, fields?: (EnterpriseAdditionalFields | null) | undefined}; export type EnterprisePendingTaskCreateForm = {precedingTaskId: number, type?: (1 | 3) | undefined, workflowStepId?: number | undefined, title: string, description?: string | undefined, vendor?: (string | null) | undefined, assignees?: Array | undefined, assignedTeams?: Array | undefined, deadline?: string | undefined}; export type CreateApiProjectsProjectIdTasksArgs = {projectId: number, body: EnterpriseTaskCreateForm | EnterpriseVendorTaskCreateForm | EnterprisePendingTaskCreateForm}; export type CreateApiProjectsProjectIdTasksOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const taskAssignee = z.object({id: z.number().int().optional(), username: z.string().optional(), fullName: z.string().optional(), avatarUrl: z.string().optional(), wordsCount: z.number().int().optional(), wordsLeft: z.number().int().optional(), timeSpent: z.number().int().optional()}); export const taskAssignedTeam = z.object({id: z.number().int().optional(), wordsCount: z.number().int().optional(), timeSpent: z.number().int().optional()}); export const taskProgress = z.object({total: z.number().int().optional(), done: z.number().int().optional(), percent: z.number().int().optional()}); export const taskTranslateProgress = z.object({total: z.number().int().optional(), done: z.number().int().optional(), percent: z.number().int().optional()}); export const language = z.object({id: z.string().optional(), name: z.string().optional(), editorCode: z.string().optional(), twoLettersCode: z.string().optional(), threeLettersCode: z.string().optional(), locale: z.string().optional(), androidCode: z.string().optional(), bcp47Code: z.string().optional(), osxCode: z.string().optional(), osxLocale: z.string().optional(), pluralCategoryNames: z.array(z.string()).optional(), pluralRules: z.string().optional(), pluralExamples: z.array(z.string()).optional(), textDirection: z.enum(["ltr", "rtl"]).optional(), dialectOf: z.string().nullable().optional()}); export const estimatedTaskCost = z.object({cost: z.number().optional(), date: z.string().optional(), currency: z.enum(["USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "CNY", "SEK", "NZD", "MXN", "SGD", "HKD", "NOK", "KRW", "TRY", "RUB", "INR", "BRL", "ZAR", "GEL", "UAH", "DDK", "PLN"]).optional()}); export const actualTaskCost = z.object({cost: z.number().optional(), date: z.string().optional(), currency: z.enum(["USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "CNY", "SEK", "NZD", "MXN", "SGD", "HKD", "NOK", "KRW", "TRY", "RUB", "INR", "BRL", "ZAR", "GEL", "UAH", "DDK", "PLN"]).optional()}); export const enterpriseAdditionalFields = z.record(z.string(), z.union([z.string(), z.number().int(), z.boolean(), z.object({})])); export const enterpriseTask = z.object({id: z.number().int().optional(), projectId: z.number().int().optional(), creatorId: z.number().int().optional(), type: z.number().int().optional(), status: z.enum(["todo", "in_progress", "done", "closed"]).optional(), title: z.string().optional(), batchId: z.number().int().optional(), assignees: z.array(taskAssignee).optional(), assignedTeams: z.array(taskAssignedTeam).optional(), progress: taskProgress.optional(), translateProgress: taskTranslateProgress.optional(), sourceLanguageId: z.string().optional(), targetLanguageId: z.string().optional(), description: z.string().optional(), translationUrl: z.string().optional(), webUrl: z.string().optional(), wordsCount: z.number().int().optional(), originalWordsCount: z.number().int().optional(), commentsCount: z.number().int().optional(), deadline: z.string().optional(), startedAt: z.string().optional(), resolvedAt: z.string().optional(), timeRange: z.string().optional(), translationsUpdatedTimeRange: z.string().optional(), workflowStepId: z.number().int().optional(), buyUrl: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), sourceLanguage: language.optional(), targetLanguages: z.array(language).optional(), labelIds: z.array(z.number().int()).optional(), labelMatchRule: z.enum(["all", "any"]).nullable().optional(), excludeLabelIds: z.array(z.number().int()).optional(), excludeLabelMatchRule: z.enum(["all", "any"]).nullable().optional(), precedingTaskId: z.number().int().optional(), estimatedCost: estimatedTaskCost.optional(), actualCost: actualTaskCost.optional(), generateCostEstimate: z.boolean().nullable().optional(), generateTranslationCost: z.boolean().nullable().optional(), reportSettingsTemplateId: z.number().int().nullable().optional(), filesCount: z.number().int().optional(), fileIds: z.array(z.number().int()).optional(), fields: enterpriseAdditionalFields.nullable().optional(), syncScope: z.object({syncedWords: z.number().int().optional(), pendingWords: z.number().int().optional(), skippedWords: z.number().int().optional()}).nullable().optional()}); export const enterpriseFileBasedTaskResource = z.object({data: enterpriseTask}); 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 Project Task * * POST /projects/{projectId}/tasks */ export const createApiProjectsProjectIdTasks = async (args: CreateApiProjectsProjectIdTasksArgs, options?: CreateApiProjectsProjectIdTasksOptions) => { 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))}/tasks${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(enterpriseFileBasedTaskResource, await res.json()) };