import {z} from 'zod' export type ProjectUid = string; export type TranslationJobUid = string; export type WorkflowUid = string; export type LocaleId = string; export type LocaleWorkflowPairDTO = {workflowUid?: WorkflowUid | undefined, targetLocaleId?: LocaleId | undefined}; export type WorkflowStepUid = string; export type LocaleWorkflowStepPairDTO = {workflowStepUid?: WorkflowStepUid | undefined, targetLocaleId?: LocaleId | undefined}; export type CreateJobCostReportRequest = {contentType?: ('TRANSLATION' | 'ALL_CONTENT' | 'CLAIMING' | 'UNAUTHORIZED') | undefined, tags?: Array | undefined, localeWorkflows?: Array | undefined, fuzzyProfileUid?: string | undefined, localeWorkflowStepsFilter?: Array | undefined}; export type CreateApiEstimatesApiV2ProjectsProjectIdJobsTranslationJobUidReportsCostArgs = {projectId: ProjectUid, translationJobUid: TranslationJobUid, body: CreateJobCostReportRequest}; export type CreateApiEstimatesApiV2ProjectsProjectIdJobsTranslationJobUidReportsCostOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const estimateJobGenericReport = z.object({reportUid: z.string().optional(), reportType: z.string().optional(), reportStatus: z.string().optional()}); export const estimateJobGenericReportResponse = z.object({response: z.object({code: z.enum(["SUCCESS", "GENERAL_ERROR", "MAINTENANCE_MODE_ERROR"]), data: z.array(estimateJobGenericReport)})}); 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 }; /** * Generate Cost Estimate Report for the Job * * POST /estimates-api/v2/projects/{projectId}/jobs/{translationJobUid}/reports/cost */ export const createApiEstimatesApiV2ProjectsProjectIdJobsTranslationJobUidReportsCost = async (args: CreateApiEstimatesApiV2ProjectsProjectIdJobsTranslationJobUidReportsCostArgs, options?: CreateApiEstimatesApiV2ProjectsProjectIdJobsTranslationJobUidReportsCostOptions) => { const {baseUrl = "https://api.smartling.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/estimates-api/v2/projects/${encodeURIComponent(String(args.projectId))}/jobs/${encodeURIComponent(String(args.translationJobUid))}/reports/cost${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(estimateJobGenericReportResponse, await res.json()) };