import {z} from 'zod' export type GetApiLocationsAddOnCountArgs = {addOnId: string, filterInput?: string | undefined, selectionType?: ('ALL_ITEMS' | 'SELECTED_ITEMS') | undefined, locationIds?: Array | undefined, 'Synup-Version': string, Authorization: string}; export type GetApiLocationsAddOnCountOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const addOnCountV2 = z.object({id: z.string().optional(), name: z.string().optional(), count: z.number().int().optional()}); export const locationAddOnCount = z.object({enabled: z.number().int().optional(), disabled: z.number().int().optional(), othersEnabled: z.array(addOnCountV2).optional()}); export const getApiLocationsAddOnCountResponse = z.object({data: z.object({locationAddOnCount: locationAddOnCount.optional()}).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 }; /** * Location add-on count * * GET /locations/add-on-count */ export const getApiLocationsAddOnCount = async (args: GetApiLocationsAddOnCountArgs, options?: GetApiLocationsAddOnCountOptions) => { const {baseUrl = "https://api.synup.com/api/v4", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.addOnId != null) query.append("addOnId", String(args.addOnId)) if (args.filterInput != null) query.append("filterInput", String(args.filterInput)) if (args.selectionType != null) query.append("selectionType", String(args.selectionType)) for (const item of args.locationIds ?? []) query.append("locationIds", String(item)) if (args['Synup-Version'] != null) headers.set("Synup-Version", String(args['Synup-Version'])) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/locations/add-on-count${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(getApiLocationsAddOnCountResponse, await res.json()) };