import {z} from 'zod' export type ProjectUid = string; export type FileUri = string; export type Hashcode = string; export type TranslationJobUid = string; export type SearchJobRequest = {fileUris?: Array | undefined, hashcodes?: Array | undefined, translationJobUids?: Array | undefined}; export type CreateApiJobsApiV3ProjectsProjectIdJobsSearchArgs = {projectId: ProjectUid, body: SearchJobRequest}; export type CreateApiJobsApiV3ProjectsProjectIdJobsSearchOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dateTime = z.string(); export const localeId = z.string(); export const translationJobUid = z.string(); export const listJobsResponseItem = z.object({createdDate: dateTime.optional(), description: z.string().optional(), dueDate: dateTime.optional(), jobName: z.string().optional(), jobNumber: z.string().optional(), jobStatus: z.string().optional(), targetLocaleIds: z.array(localeId).optional(), translationJobUid: translationJobUid.optional(), referenceNumber: z.string().optional()}); export const listJobsResponse = z.object({response: z.object({code: z.literal("SUCCESS"), data: z.object({items: z.array(listJobsResponseItem).optional(), totalCount: z.number().optional()})})}); 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 }; /** * Search for job * * POST /jobs-api/v3/projects/{projectId}/jobs/search */ export const createApiJobsApiV3ProjectsProjectIdJobsSearch = async (args: CreateApiJobsApiV3ProjectsProjectIdJobsSearchArgs, options?: CreateApiJobsApiV3ProjectsProjectIdJobsSearchOptions) => { 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(/\/+$/, '')}/jobs-api/v3/projects/${encodeURIComponent(String(args.projectId))}/jobs/search${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(listJobsResponse, await res.json()) };