import {z} from 'zod' export type CustomCollectionImageRequest = {alt?: string | undefined, attachment?: string | undefined, created_at?: string | undefined, height?: number | undefined, src?: string | undefined, width?: number | undefined}; export type CustomCollectionMetafield = {key?: string | undefined, namespace?: string | undefined, value?: string | undefined, value_type?: string | undefined}; export type CustomCollectionRequest = {body_html?: string | undefined, fields?: string | undefined, handle?: string | undefined, id?: number | undefined, image?: CustomCollectionImageRequest | undefined, limit?: number | undefined, metafields?: Array | undefined, page?: number | undefined, product_id?: number | undefined, published?: boolean | undefined, published_status?: string | undefined, sort_order?: string | undefined, title?: string | undefined}; export type CollectionUpdateRequestSwagger = {custom_collection?: CustomCollectionRequest | undefined, is_create_redirect?: boolean | undefined}; export type UpdateApiAdminCollectionsCustomCustomCollectionIdDJsonArgs = {custom_collection_id: number, body: CollectionUpdateRequestSwagger}; export type UpdateApiAdminCollectionsCustomCustomCollectionIdDJsonOptions = {baseUrl?: string | undefined, APPACCESSTOKEN?: string | undefined, fetch?: typeof fetch | undefined}; export const customCollectionImage = z.object({alt: z.string().optional(), created_at: z.string().optional(), height: z.number().int().optional(), src: z.string().optional(), width: z.number().int().optional()}); export const customCollectionMetafield = z.object({key: z.string().optional(), namespace: z.string().optional(), value: z.string().optional(), value_type: z.string().optional()}); export const collectionUpdateResponseSwagger = z.object({body_html: z.string().optional(), handle: z.string().optional(), id: z.number().int().optional(), image: customCollectionImage.optional(), metafields: z.array(customCollectionMetafield).optional(), published: z.boolean().optional(), sort_order: z.string().optional(), title: z.string().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 or update a collection * * PUT /admin/collections/custom/{custom_collection_id:\\d+}.json */ export const updateApiAdminCollectionsCustomCustomCollectionIdDJson = async (args: UpdateApiAdminCollectionsCustomCustomCollectionIdDJsonArgs, options?: UpdateApiAdminCollectionsCustomCustomCollectionIdDJsonOptions) => { 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/collections/custom/${encodeURIComponent(String(args['custom_collection_id:\\\\d+']))}.json${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(collectionUpdateResponseSwagger, await res.json()) };