import {z} from 'zod' export type CreateCollectResponse = {collection_id?: number | undefined, product_id?: number | undefined}; export type CreateCollectRequestSwagger = {collect?: CreateCollectResponse | undefined}; export type CreateApiAdminCollectsJsonArgs = {body: CreateCollectRequestSwagger}; export type CreateApiAdminCollectsJsonOptions = {baseUrl?: string | undefined, APPACCESSTOKEN?: string | undefined, fetch?: typeof fetch | undefined}; export const collect = z.object({collection_id: z.number().int().optional(), featured: z.boolean().optional(), id: z.number().int().optional(), position: z.number().optional(), product_id: z.number().int().optional(), sort_value: z.string().optional()}); export const createCollectResponseSwagger = z.object({collect: collect.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 }; /** * Adds a product to a custom collection. * * POST /admin/collects.json */ export const createApiAdminCollectsJson = async (args: CreateApiAdminCollectsJsonArgs, options?: CreateApiAdminCollectsJsonOptions) => { 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/collects.json${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(createCollectResponseSwagger, await res.json()) };