import {z} from 'zod' export type AgreementStatus = 0 | 1 | 2 | 3; export type AgreementType = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13; export type GetApiApiV1AgreementsArgs = {OrganizationId?: number | undefined, OrganizationIds?: (Array | null) | undefined, PricelistIds?: (Array | null) | undefined, Status?: AgreementStatus | undefined, AgreementTypes?: (Array | null) | undefined, PublisherIds?: (Array | null) | undefined, ProgramIds?: (Array | null) | undefined, SearchDate?: (string | null) | undefined, AgreementIds?: (Array | null) | undefined, SalesPriceCurrency?: (string | null) | undefined, TermRequired?: boolean | undefined, PublisherId?: number | undefined, EndDateFrom?: (string | null) | undefined, EndDateTo?: (string | null) | undefined, Page?: number | undefined, PageSize?: number | undefined, Search?: (string | null) | undefined}; export type GetApiApiV1AgreementsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const objectReference = z.object({id: z.number().int().optional(), name: z.string().nullable().optional()}); export const agreementType = 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(10), z.literal(11), z.literal(12), z.literal(13)]); export const agreement = z.object({id: z.number().int().optional(), organization: objectReference.optional(), name: z.string().nullable().optional(), salesPriceCurrencyCode: z.string().nullable().optional(), startDate: z.string().optional(), endDate: z.string().optional(), disabled: z.boolean().optional(), publisher: objectReference.optional(), program: objectReference.optional(), number: z.string().nullable().optional(), customerNumber: z.string().nullable().optional(), masterAgreement: z.string().nullable().optional(), hasTerms: z.boolean().optional(), agreementType: agreementType.optional(), parentAgreementId: z.number().int().nullable().optional(), commitmentLevel: z.number().optional(), consumerId: z.number().int().nullable().optional()}); export const getApiApiV1AgreementsResponse = z.array(agreement); 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 /api/v1/Agreements */ export const getApiApiV1Agreements = async (args: GetApiApiV1AgreementsArgs, options: GetApiApiV1AgreementsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.OrganizationId != null) query.append("OrganizationId", String(args.OrganizationId)) for (const item of args.OrganizationIds ?? []) query.append("OrganizationIds", String(item)) for (const item of args.PricelistIds ?? []) query.append("PricelistIds", String(item)) if (args.Status != null) query.append("Status", String(args.Status)) for (const item of args.AgreementTypes ?? []) query.append("AgreementTypes", String(item)) for (const item of args.PublisherIds ?? []) query.append("PublisherIds", String(item)) for (const item of args.ProgramIds ?? []) query.append("ProgramIds", String(item)) if (args.SearchDate != null) query.append("SearchDate", String(args.SearchDate)) for (const item of args.AgreementIds ?? []) query.append("AgreementIds", String(item)) if (args.SalesPriceCurrency != null) query.append("SalesPriceCurrency", String(args.SalesPriceCurrency)) if (args.TermRequired != null) query.append("TermRequired", String(args.TermRequired)) if (args.PublisherId != null) query.append("PublisherId", String(args.PublisherId)) if (args.EndDateFrom != null) query.append("EndDateFrom", String(args.EndDateFrom)) if (args.EndDateTo != null) query.append("EndDateTo", String(args.EndDateTo)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.PageSize != null) query.append("PageSize", String(args.PageSize)) if (args.Search != null) query.append("Search", String(args.Search)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/Agreements${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(getApiApiV1AgreementsResponse, await res.json()) };