import {z} from 'zod' export type PublicTierNCollectionSettingsUpdateRequest = {maxTier?: (number | null) | undefined, scopePrivate?: (boolean | null) | undefined, scopeShared?: (boolean | null) | undefined, scopePublic?: (boolean | null) | undefined, sourceCustomer?: (boolean | null) | undefined, sourceCustoms?: (boolean | null) | undefined, sourceMedia?: (boolean | null) | undefined, sourcePrewavePrediction?: (boolean | null) | undefined, minShipments?: (number | null) | undefined, probability?: (number | null) | undefined, shipmentsPeriodFrom?: (string | null) | undefined, shipmentsPeriodTo?: (string | null) | undefined, suspectedSuppliers?: (boolean | null) | undefined, logisticsProvider?: (boolean | null) | undefined}; export type UpdateApiPublicV1CollectionsCollectionIdTierNSettingsArgs = {collectionId: number, body: PublicTierNCollectionSettingsUpdateRequest}; export type UpdateApiPublicV1CollectionsCollectionIdTierNSettingsOptions = {baseUrl?: string | undefined, TokenAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const publicTierNCollectionSettings = z.object({collectionId: z.number().int(), collectionName: z.string(), maxTier: z.number().int(), targetIds: z.array(z.number().int()), scopePrivate: z.boolean(), scopeShared: z.boolean(), scopePublic: z.boolean(), sourceCustomer: z.boolean(), sourceCustoms: z.boolean(), sourceMedia: z.boolean(), sourcePrewavePrediction: z.boolean(), minShipments: z.number().int().nullable().optional(), probability: z.number().int().nullable().optional(), shipmentsPeriodFrom: z.string().nullable().optional(), shipmentsPeriodTo: z.string().nullable().optional(), suspectedSuppliers: z.boolean(), logisticsProvider: z.boolean(), commodityFilterTargetIds: z.array(z.number().int())}); 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 }; /** * Update tier-n collection settings * * PUT /public/v1/collections/{collectionId}/tier-n/settings */ export const updateApiPublicV1CollectionsCollectionIdTierNSettings = async (args: UpdateApiPublicV1CollectionsCollectionIdTierNSettingsArgs, options?: UpdateApiPublicV1CollectionsCollectionIdTierNSettingsOptions) => { const {baseUrl = "https://api.prewave.com", fetch: fetchFn = fetch, TokenAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TokenAuthentication !== undefined) headers.set("X-Auth-Token", TokenAuthentication) const url = `${baseUrl.replace(/\/+$/, '')}/public/v1/collections/${encodeURIComponent(String(args.collectionId))}/tier-n/settings${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(publicTierNCollectionSettings, await res.json()) };