import {z} from 'zod' export type GetApiV1ExtendedPropertiesAffinityArgs = {affinity: string, Filter?: string | undefined, Page?: number | undefined, PageSize?: number | undefined, SortColumn?: string | undefined, SortOrder?: string | undefined, Account: unknown}; export type GetApiV1ExtendedPropertiesAffinityOptions = {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 extendedProperty = z.object({affinity: extendedPropertyAffinity.optional(), ownerId: z.number().int().optional(), key: z.string().nullable().optional(), value: z.string().nullable().optional()}); export const extendedPropertyPagedResult = z.object({pageSize: z.number().int().optional(), page: z.number().int().optional(), totalCount: z.number().int().optional(), filteredCount: z.number().int().optional(), data: z.array(extendedProperty).nullable().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 global extended properties that are not attached to any object * * GET /v1/ExtendedProperties/{affinity} */ export const getApiV1ExtendedPropertiesAffinity = async (args: GetApiV1ExtendedPropertiesAffinityArgs, options: GetApiV1ExtendedPropertiesAffinityOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Filter != null) query.append("Filter", String(args.Filter)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.PageSize != null) query.append("PageSize", String(args.PageSize)) if (args.SortColumn != null) query.append("SortColumn", String(args.SortColumn)) if (args.SortOrder != null) query.append("SortOrder", String(args.SortOrder)) if (args.Account != null) query.append("Account", String(args.Account)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ExtendedProperties/${encodeURIComponent(String(args.affinity))}${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(extendedPropertyPagedResult, await res.json()) };