import {z} from 'zod' export type GetApiDamElementOptionsElementOptionIdTranslationsTranslationIdArgs = {element_option_id: string, translation_id: string}; export type GetApiDamElementOptionsElementOptionIdTranslationsTranslationIdOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const globalVariableValueIdentifierObject = z.object({data: z.object({type: z.literal("global_variable_value"), id: z.string()}).nullable().optional()}); export const damElementOptionIdentifierObject = z.object({id: z.string(), type: z.literal("dam_element_option")}); export const damElementOptionTranslationData = z.object({id: z.string(), type: z.literal("dam_element_option_translation"), attributes: z.object({translation_value: z.string().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}), meta: z.object({global_variable_value_id: z.string().optional()}), links: z.object({self: z.string()}), relationships: z.object({global_variable_value: globalVariableValueIdentifierObject.optional(), dam_element_option: z.object({data: damElementOptionIdentifierObject}).optional()}).optional()}); export const damElementOption = z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({}).optional()}); export const globalVariableValue = z.object({type: z.literal("global_variable_value"), id: z.string(), attributes: z.object({value: z.string(), import_value: z.string(), property_safe_value: z.string(), global_variable_name: z.string()})}); export const damElementOptionTranslation = z.object({data: damElementOptionTranslationData, included: z.array(z.union([damElementOption, globalVariableValue])).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 }; /** * Returns a single element option translation * * GET /dam/element-options/{element_option_id}/translations/{translation_id} */ export const getApiDamElementOptionsElementOptionIdTranslationsTranslationId = async (args: GetApiDamElementOptionsElementOptionIdTranslationsTranslationIdArgs, options: GetApiDamElementOptionsElementOptionIdTranslationsTranslationIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/dam/element-options/${encodeURIComponent(String(args.element_option_id))}/translations/${encodeURIComponent(String(args.translation_id))}${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(damElementOptionTranslation, await res.json()) };