import {z} from 'zod' export type GetApiV1ItemsDefinedPropertiesArgs = {Account: unknown}; export type GetApiV1ItemsDefinedPropertiesOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const extendedPropertyAffinity = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9), z.literal(14), z.literal(15), z.literal(20), z.literal(21), z.literal(22), z.literal(23), z.literal(24), z.literal(25), z.literal(26), z.literal(27), z.literal(31), z.literal(33), z.literal(34), z.literal(35), z.literal(81), z.literal(100), z.literal(101), z.literal(102), z.literal(103), z.literal(104), z.literal(105), z.literal(200), z.literal(201), z.literal(210), z.literal(211), z.literal(220), z.literal(230), z.literal(240), z.literal(241), z.literal(300), z.literal(310), z.literal(311), z.literal(400), z.literal(401)]); export const getDefinedPropertyParametersResponse = z.object({id: z.number().int().optional(), affinity: extendedPropertyAffinity.optional(), key: z.string().nullable().optional(), showOnReports: z.boolean().optional(), useAsFilter: z.boolean().optional(), isRequired: z.boolean().optional(), accountId: z.number().int().nullable().optional()}); export const getApiV1ItemsDefinedPropertiesResponse = z.array(getDefinedPropertyParametersResponse); 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 the defined properties for items * * GET /v1/Items/definedProperties */ export const getApiV1ItemsDefinedProperties = async (args: GetApiV1ItemsDefinedPropertiesArgs, options: GetApiV1ItemsDefinedPropertiesOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Account != null) query.append("Account", String(args.Account)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Items/definedProperties${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(getApiV1ItemsDefinedPropertiesResponse, await res.json()) };