import {z} from 'zod' export type GetApiProjectsProjectIdWorkflowStepsStepIdArgs = {projectId: number, stepId: number}; export type GetApiProjectsProjectIdWorkflowStepsStepIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const translateStepResponse = z.object({data: z.object({id: z.number().int().optional(), title: z.string().optional(), type: z.string().optional(), languages: z.array(z.string()).optional(), config: z.object({assignees: z.object({de: z.array(z.number().int()).optional(), it: z.array(z.number().int()).optional()}).optional()}).optional()}).optional()}); export const proofreadStepResponse = z.object({data: z.object({id: z.number().int().optional(), title: z.string().optional(), type: z.string().optional(), languages: z.array(z.string()).optional(), config: z.object({assignees: z.object({de: z.array(z.number().int()).optional(), it: z.array(z.number().int()).optional()}).optional()}).optional()}).optional()}); export const translateByVendorStepResponse = z.object({data: z.object({id: z.number().int().optional(), title: z.string().optional(), type: z.string().optional(), languages: z.array(z.string()).optional(), config: z.object({vendorId: z.number().int().optional()}).optional()}).optional()}); export const proofreadByVendorStepResponse = z.object({data: z.object({id: z.number().int().optional(), title: z.string().optional(), type: z.string().optional(), languages: z.array(z.string()).optional(), config: z.object({vendorId: z.number().int().optional()}).optional()}).optional()}); export const tmPreTranslateStepResponse = z.object({data: z.object({id: z.number().int().optional(), title: z.string().optional(), type: z.string().optional(), languages: z.array(z.string()).optional(), config: z.object({minRelevant: z.string().optional(), autoSubstitution: z.number().int().optional()}).optional()}).optional()}); export const mtPreTranslateStepResponse = z.object({data: z.object({id: z.number().int().optional(), title: z.string().optional(), type: z.string().optional(), languages: z.array(z.string()).optional(), config: z.object({mtId: z.number().int().optional()}).optional()}).optional()}); export const crowdsourceStepResponse = z.object({data: z.object({id: z.number().int().optional(), title: z.string().optional(), type: z.string().optional(), languages: z.array(z.string()).optional(), config: z.object({}).optional()}).optional()}); export const getApiProjectsProjectIdWorkflowStepsStepIdResponse = z.union([translateStepResponse, proofreadStepResponse, translateByVendorStepResponse, proofreadByVendorStepResponse, tmPreTranslateStepResponse, mtPreTranslateStepResponse, crowdsourceStepResponse]); 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 }; /** * Get Workflow Step * * GET /projects/{projectId}/workflow-steps/{stepId} */ export const getApiProjectsProjectIdWorkflowStepsStepId = async (args: GetApiProjectsProjectIdWorkflowStepsStepIdArgs, options?: GetApiProjectsProjectIdWorkflowStepsStepIdOptions) => { const {baseUrl = "https://api.crowdin.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.projectId))}/workflow-steps/${encodeURIComponent(String(args.stepId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiProjectsProjectIdWorkflowStepsStepIdResponse, await res.json()) };