import {z} from 'zod' export type TranslationProfileType = 'QUOKKA' | 'PLAI'; export type GetApiV1TranslationProfilesArgs = {name?: string | undefined, type?: TranslationProfileType | undefined, sort?: ('name' | 'dateCreated') | undefined, order?: ('asc' | 'desc') | undefined, pageNumber?: number | undefined, pageSize?: number | undefined}; export type GetApiV1TranslationProfilesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const translationEngineV1 = z.object({id: z.string().optional(), uid: z.string().optional(), baseName: z.string().optional(), name: z.string().optional(), type: z.string().optional(), category: z.string().optional(), enabled: z.boolean().optional(), glossarySupported: z.boolean().optional()}); export const glossaryV1 = z.object({uid: z.string().optional(), name: z.string().optional()}); export const translationProfileV1 = z.object({uid: z.string().optional(), name: z.string().optional(), engines: z.array(translationEngineV1).optional(), glossaries: z.array(glossaryV1).optional(), dateCreated: z.string().optional(), shape: z.string().optional()}); export const listTranslationProfilesResponseV1 = z.object({pageNumber: z.number().int().gte(0).optional(), content: z.array(translationProfileV1).optional(), numberOfElements: z.number().int().optional(), totalElements: z.number().int().optional(), pageSize: z.number().int().optional(), totalPages: z.number().int().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 }; /** * List Language AI Profiles * * GET /v1/translationProfiles */ export const getApiV1TranslationProfiles = async (args: GetApiV1TranslationProfilesArgs, options?: GetApiV1TranslationProfilesOptions) => { const {baseUrl = "https://eu.phrase.com/control-hub", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.type != null) query.append("type", String(args.type)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (args.pageNumber != null) query.append("pageNumber", String(args.pageNumber)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/translationProfiles${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(listTranslationProfilesResponseV1, await res.json()) };