import {z} from 'zod' export type GetApiV1PriceagreementIdPeriodArgs = {id: string, auditUser: string, page?: number | undefined, size?: number | undefined}; export type GetApiV1PriceagreementIdPeriodOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const priceAgreementPeriod = z.object({lowest: z.boolean().optional(), priceListId: z.string().max(64).optional(), code: z.string().max(64).optional(), type: z.enum(["PER_EACH", "ABSOLUTE", "PERCENT"]).optional(), validFrom: z.string().optional(), validTo: z.string().optional(), value: z.number().optional(), id: z.string().max(64).optional(), priceAgreementId: z.string().max(64).optional()}); export const getApiV1PriceagreementIdPeriodResponse = z.array(priceAgreementPeriod); 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 }; /** * Retrieves all price agreements periods * * GET /v1/priceagreement/{id}/period */ export const getApiV1PriceagreementIdPeriod = async (args: GetApiV1PriceagreementIdPeriodArgs, options: GetApiV1PriceagreementIdPeriodOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.auditUser != null) query.append("auditUser", String(args.auditUser)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/priceagreement/${encodeURIComponent(String(args.id))}/period${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(getApiV1PriceagreementIdPeriodResponse, await res.json()) };