import {z} from 'zod' export type GetApiApiBusinessBusinessIdBranchBranchIdProductArgs = {businessId: string, branchId: string, productType?: string | undefined, searchQuery?: string | undefined, archivedOnly?: boolean | undefined, outOfStock?: boolean | undefined, lowStock?: boolean | undefined, includeArchived?: boolean | undefined, updated_from?: string | undefined, updated_to?: string | undefined, size?: number | undefined, page?: number | undefined}; export type GetApiApiBusinessBusinessIdBranchBranchIdProductOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const product = z.object({productId: z.string().optional(), parentProductId: z.string().optional(), name: z.string().optional(), brandId: z.string().optional(), brandName: z.string().optional(), categoryId: z.string().optional(), categoryName: z.string().optional(), archived: z.boolean().optional(), price: z.number().optional(), minQuantity: z.number().optional(), maxQuantity: z.number().optional(), type: z.string().optional(), barcode: z.string().optional(), measurementQuantity: z.number().optional(), measurementUnit: z.string().optional(), reorderCount: z.number().int().optional(), reorderCost: z.number().optional(), quantityInStock: z.number().optional(), code: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const pageMetadata = z.object({size: z.number().int().optional(), totalElements: z.number().int().optional(), totalPages: z.number().int().optional(), number: z.number().int().optional()}); export const dataPagedModelProduct = z.object({_embedded: z.object({products: z.array(product).optional()}).optional(), page: pageMetadata.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 }; /** * List products * * GET /api/business/{businessId}/branch/{branchId}/product */ export const getApiApiBusinessBusinessIdBranchBranchIdProduct = async (args: GetApiApiBusinessBusinessIdBranchBranchIdProductArgs, options?: GetApiApiBusinessBusinessIdBranchBranchIdProductOptions) => { const {baseUrl = "http://api-gateway-eu.phorest.com/third-party-api-server", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.productType != null) query.append("productType", String(args.productType)) if (args.searchQuery != null) query.append("searchQuery", String(args.searchQuery)) if (args.archivedOnly != null) query.append("archivedOnly", String(args.archivedOnly)) if (args.outOfStock != null) query.append("outOfStock", String(args.outOfStock)) if (args.lowStock != null) query.append("lowStock", String(args.lowStock)) if (args.includeArchived != null) query.append("includeArchived", String(args.includeArchived)) if (args.updated_from != null) query.append("updated_from", String(args.updated_from)) if (args.updated_to != null) query.append("updated_to", String(args.updated_to)) if (args.size != null) query.append("size", String(args.size)) if (args.page != null) query.append("page", String(args.page)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/business/${encodeURIComponent(String(args.businessId))}/branch/${encodeURIComponent(String(args.branchId))}/product${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(dataPagedModelProduct, await res.json()) };