import {z} from 'zod' export type GetApiApiValuesListsArgs = {code?: string | undefined}; export type GetApiApiValuesListsOptions = {baseUrl?: string | undefined, FrontBearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const vL11Response200ValuesListsLabelTranslations = z.object({locale: z.string().optional(), value: z.string().optional()}); export const vL11Response200ValuesListsValuesLabelTranslations = z.object({locale: z.string().optional(), value: z.string().optional()}); export const vL11Response200ValuesListsValues = z.object({code: z.string(), label: z.string().optional(), label_translations: z.array(vL11Response200ValuesListsValuesLabelTranslations)}); export const vL11Response200ValuesLists = z.object({code: z.string(), label: z.string().optional(), label_translations: z.array(vL11Response200ValuesListsLabelTranslations), values: z.array(vL11Response200ValuesListsValues).optional()}); export const vL11Response200 = z.object({values_lists: z.array(vL11Response200ValuesLists).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 }; /** * VL11 - Get information about operator's value lists * * GET /api/values_lists * * Also accepts auth: OAuth-2 */ export const getApiApiValuesLists = async (args: GetApiApiValuesListsArgs, options?: GetApiApiValuesListsOptions) => { const {baseUrl = "https://your-instance.mirakl.net", fetch: fetchFn = fetch, FrontBearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.code != null) query.append("code", String(args.code)) if (FrontBearerToken !== undefined) headers.set('authorization', `Bearer ${FrontBearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/values_lists${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(vL11Response200, await res.json()) };