import {z} from 'zod' export type ProductCreate = {name: string, sku?: (string | null) | undefined, category_id?: (number | null) | undefined, category?: (string | null) | undefined, subcategory?: (string | null) | undefined, brand_id?: (number | null) | undefined, price: number, cost?: (number | null) | undefined, discount?: number | undefined, price_with_discount?: (number | null) | undefined, unit?: string | undefined, weight?: (string | null) | undefined, tags?: (string | null) | undefined, description?: (string | null) | undefined, visible?: boolean | undefined, is_new?: boolean | undefined, recommended?: boolean | undefined, limited?: boolean | undefined, badge?: (string | null) | undefined, image?: (string | null) | undefined}; export type CreateApiApiV1AdminProductsArgs = {body: ProductCreate}; export type CreateApiApiV1AdminProductsOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const productRead = z.object({id: z.number().int(), name: z.string(), sku: z.string().nullable().optional(), category_id: z.number().int().nullable().optional(), category_name: z.string().nullable().optional(), category: z.string().nullable().optional(), subcategory: z.string().nullable().optional(), brand_id: z.number().int().nullable().optional(), brand_name: z.string().nullable().optional(), price: z.number(), cost: z.number().nullable().optional(), discount: z.number().optional(), price_with_discount: z.number().nullable().optional(), unit: z.string().optional(), weight: z.string().nullable().optional(), tags: z.string().nullable().optional(), description: z.string().nullable().optional(), visible: z.boolean(), is_new: z.boolean(), recommended: z.boolean(), limited: z.boolean(), badge: z.string().nullable().optional(), image: z.string().nullable().optional(), sort_order: z.number().int().nullable().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 }; /** * Create Product * * POST /api/v1/admin/products/ */ export const createApiApiV1AdminProducts = async (args: CreateApiApiV1AdminProductsArgs, options: CreateApiApiV1AdminProductsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/admin/products/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(productRead, await res.json()) };