import {z} from 'zod' export type QPilotCoreModelsAvailability = 'Undefined' | 'InStock' | 'OutOfStock' | 'Preorder'; export type GetApiSitesSiteIdProductsArgs = {siteId: number, availability?: QPilotCoreModelsAvailability | undefined, minPrice?: number | undefined, maxPrice?: number | undefined, addToScheduledOrder?: boolean | undefined, processScheduledOrder?: boolean | undefined, search?: string | undefined, groupIds?: Array | undefined, productIds?: Array | undefined, metadataKey?: Array | undefined, metadataValue?: Array | undefined, active?: boolean | undefined, valid?: boolean | undefined, page?: number | undefined, pageSize?: number | undefined, orderBy?: string | undefined, order?: string | undefined}; export type GetApiSitesSiteIdProductsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const qPilotCoreModelsProductValidationErrorCode = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const qPilotCoreModelsWeightUnitType = z.enum(["Pound", "Ounce", "Kilogram", "Gram"]); export const qPilotCoreModelsLengthUnitType = z.enum(["Inch", "Foot", "Yard", "Milimeter", "Centimeter", "Meter"]); export const qPilotCoreModelsAvailability = z.enum(["Undefined", "InStock", "OutOfStock", "Preorder"]); export const qPilotCoreModelsFrequencyType = z.enum(["Days", "Weeks", "Months", "DayOfTheWeek", "DayOfTheMonth"]); export const qPilotCoreModelsFrequencyTypeValues = z.object({frequencyType: qPilotCoreModelsFrequencyType.optional(), displayName: z.string().nullable().optional(), value: z.number().int().optional()}); export const qPilotCoreModelsDtoEntitiesProductDto: z.ZodType = z.object({productId: z.number().int().optional(), siteId: z.number().int().optional(), parentProduct: z.lazy(() => qPilotCoreModelsDtoEntitiesProductDto.optional()), createdUtc: z.string().optional(), updatedUtc: z.string().optional(), lifetimeValue: z.number().nullable().optional(), validationErrorCode: qPilotCoreModelsProductValidationErrorCode.optional(), syncOnUpdate: z.boolean().nullable().optional(), imageUrl: z.string().nullable().optional(), id: z.string().max(50), title: z.string().max(200), price: z.number(), parentProductId: z.string().nullable().optional(), sku: z.string().max(50).nullable().optional(), gtin: z.string().max(50).nullable().optional(), mpn: z.string().max(70).nullable().optional(), description: z.string().nullable().optional(), salePrice: z.number().nullable().optional(), length: z.string().max(10).nullable().optional(), width: z.string().max(10).nullable().optional(), height: z.string().max(10).nullable().optional(), weight: z.string().max(10).nullable().optional(), weightUnitType: qPilotCoreModelsWeightUnitType.optional(), lengthUnitType: qPilotCoreModelsLengthUnitType.optional(), shippingClass: z.string().max(100).nullable().optional(), taxClass: z.string().max(100).nullable().optional(), addToScheduledOrder: z.boolean().optional(), processScheduledOrder: z.boolean().optional(), availability: qPilotCoreModelsAvailability.optional(), stock: z.number().int().nullable().optional(), productGroupIds: z.array(z.number().int()).nullable().optional(), availableFrequencies: z.array(qPilotCoreModelsFrequencyTypeValues).nullable().optional(), backordersAllowed: z.boolean().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), active: z.boolean().nullable().optional(), valid: z.boolean().nullable().optional()}); export const qPilotCoreModelsPagedResultProductDto = z.object({items: z.array(qPilotCoreModelsDtoEntitiesProductDto).nullable().optional(), totalCount: z.number().int().optional(), totalPages: z.number().int().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 }; /** * Get Products * * GET /Sites/{siteId}/Products */ export const getApiSitesSiteIdProducts = async (args: GetApiSitesSiteIdProductsArgs, options?: GetApiSitesSiteIdProductsOptions) => { const {baseUrl = "https://api.qpilot.cloud", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.availability != null) query.append("availability", String(args.availability)) if (args.minPrice != null) query.append("minPrice", String(args.minPrice)) if (args.maxPrice != null) query.append("maxPrice", String(args.maxPrice)) if (args.addToScheduledOrder != null) query.append("addToScheduledOrder", String(args.addToScheduledOrder)) if (args.processScheduledOrder != null) query.append("processScheduledOrder", String(args.processScheduledOrder)) if (args.search != null) query.append("search", String(args.search)) for (const item of args.groupIds ?? []) query.append("groupIds", String(item)) for (const item of args.productIds ?? []) query.append("productIds", String(item)) for (const item of args.metadataKey ?? []) query.append("metadataKey", String(item)) for (const item of args.metadataValue ?? []) query.append("metadataValue", String(item)) if (args.active != null) query.append("active", String(args.active)) if (args.valid != null) query.append("valid", String(args.valid)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.order != null) query.append("order", String(args.order)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/Sites/${encodeURIComponent(String(args.siteId))}/Products${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(qPilotCoreModelsPagedResultProductDto, await res.json()) };