import {z} from 'zod' export type Address = {country_code: string, state?: string | undefined}; export type DepopShippingDetails = {address_id: number, shipping_provider_id: string, parcel_size_id: string, payer: 'buyer' | 'seller'}; export type PublicPicture = {url: string}; export type ProductCreateRequest = {sku?: string | undefined, address: Address, description: string, price_currency: string, price_amount: string, discount_price?: string | undefined, auto_send_offer_price?: string | undefined, auto_negotiate_offer_price?: string | undefined, international_shipping_cost?: string | undefined, national_shipping_cost?: string | undefined, depop_shipping?: DepopShippingDetails | undefined, quantity: number, pictures: Array, department: string, product_type: string, size_set_id?: number | undefined, size_id?: number | undefined, condition: 'brand_new' | 'used_like_new' | 'used_excellent' | 'used_good' | 'used_fair', colour?: Array | undefined, style?: Array | undefined, age?: Array | undefined, source?: Array | undefined, brand_name: string, attributes?: {[key: string]: Array} | undefined, is_boosted?: boolean | undefined}; export type CreateApiApiV1ProductsArgs = {body: ProductCreateRequest}; export type CreateApiApiV1ProductsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const productCreateOrUpdateResponse = z.object({product_id: z.number(), slug: z.string(), sku: z.string().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 a new product * * POST /api/v1/products/ */ export const createApiApiV1Products = async (args: CreateApiApiV1ProductsArgs, options?: CreateApiApiV1ProductsOptions) => { const {baseUrl = "https://partnerapi-staging.depop.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/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(productCreateOrUpdateResponse, await res.json()) };