import {z} from 'zod' export type GetApiPropertyTranslationReadPropertyTranslationsArgs = {entityName: string, entityId: string, locale?: string | undefined, propertyNames?: Array | undefined}; export type GetApiPropertyTranslationReadPropertyTranslationsOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyTranslationValue = z.object({locale: z.string().optional(), text: z.string().optional()}); export const propertyTranslation = z.object({propertyName: z.string().optional(), values: z.array(propertyTranslationValue).optional()}); export const getApiPropertyTranslationReadPropertyTranslationsResponse = z.object({result: z.array(propertyTranslation).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 }; /** * GET /propertyTranslation/readPropertyTranslations */ export const getApiPropertyTranslationReadPropertyTranslations = async (args: GetApiPropertyTranslationReadPropertyTranslationsArgs, options: GetApiPropertyTranslationReadPropertyTranslationsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.entityName != null) query.append("entityName", String(args.entityName)) if (args.entityId != null) query.append("entityId", String(args.entityId)) if (args.locale != null) query.append("locale", String(args.locale)) for (const item of args.propertyNames ?? []) query.append("propertyNames", String(item)) if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/propertyTranslation/readPropertyTranslations${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(getApiPropertyTranslationReadPropertyTranslationsResponse, await res.json()) };