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 ProductPutRequest = {address: Address, description: string, price_currency: string, price_amount: string, 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<'black' | 'grey' | 'white' | 'brown' | 'tan' | 'cream' | 'yellow' | 'red' | 'burgundy' | 'orange' | 'pink' | 'purple' | 'blue' | 'navy' | 'green' | 'khaki' | 'multi' | 'silver' | 'gold'>) | undefined, style?: (Array<'streetwear' | 'sportswear' | 'loungewear' | 'goth' | 'retro' | 'boho' | 'western' | 'indie' | 'skater' | 'rave' | 'costume' | 'cosplay' | 'grunge' | 'emo' | 'minimalist' | 'preppy' | 'avant_garde' | 'punk' | 'glam' | 'regency' | 'casual' | 'techwear' | 'futuristic' | 'cottage' | 'fairy' | 'kidcore' | 'y2_k' | 'biker' | 'gorpcore' | 'twee' | 'coquette' | 'whimsygoth'>) | undefined, age?: (Array<'modern' | 'y2k' | '90s' | '80s' | '70s' | '60s' | '50s' | 'antique'>) | undefined, source?: (Array<'vintage' | 'preloved' | 'reworked' | 'custom' | 'handmade' | 'deadstock' | 'designer' | 'repaired'>) | undefined, brand_name: string, attributes: {[key: string]: Array}, is_boosted?: boolean | undefined}; export type UpdateApiApiV1ProductsBySkuSkuArgs = {sku: string, body: ProductPutRequest}; export type UpdateApiApiV1ProductsBySkuSkuOptions = {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 or update a listing on Depop * * PUT /api/v1/products/by-sku/{sku}/ */ export const updateApiApiV1ProductsBySkuSku = async (args: UpdateApiApiV1ProductsBySkuSkuArgs, options?: UpdateApiApiV1ProductsBySkuSkuOptions) => { 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/by-sku/${encodeURIComponent(String(args.sku))}/${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(productCreateOrUpdateResponse, await res.json()) };