import {z} from 'zod' export type ProductPatchFields = {add_collections?: Array | undefined, add_tags?: Array | undefined, import_progress?: Array | undefined, published?: boolean | undefined, remove_collections?: Array | undefined, remove_tags?: Array | undefined}; export type ProductPatchRequest = {count_retry?: number | undefined, data?: ProductPatchFields | undefined, dest_shop_id?: number | undefined, exclude_ids?: Array | undefined, has_custom_option?: string | undefined, has_image?: string | undefined, ids?: Array | undefined, keep_current_product_id?: boolean | undefined, page?: number | undefined, published_status?: string | undefined, shop_id?: number | undefined, title?: string | undefined, title_mode?: string | undefined, type?: string | undefined}; export type PatchApiAdminProductsJsonArgs = {body: ProductPatchRequest}; export type PatchApiAdminProductsJsonOptions = {baseUrl?: string | undefined, APPACCESSTOKEN?: string | undefined, fetch?: typeof fetch | undefined}; export const patchProductResponseSwagger = z.object({is_process_background: z.boolean().optional(), status: z.boolean().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 }; /** * Patch multiple product. * * PATCH /admin/products.json */ export const patchApiAdminProductsJson = async (args: PatchApiAdminProductsJsonArgs, options?: PatchApiAdminProductsJsonOptions) => { const {baseUrl = "https://shop-name.onshopbase.com", fetch: fetchFn = fetch, APPACCESSTOKEN} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APPACCESSTOKEN !== undefined) headers.set("APP_ACCESS_TOKEN", APPACCESSTOKEN) const url = `${baseUrl.replace(/\/+$/, '')}/admin/products.json${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchProductResponseSwagger, await res.json()) };