import {z} from 'zod' export type GetApiApiV1ProductsArgs = {limit?: number | undefined, cursor?: string | undefined, sort_by?: ('created_date_desc' | 'created_date_asc' | 'id_desc' | 'id_asc') | undefined, state?: ('selling' | 'sold' | 'all') | undefined, from_created_date?: string | undefined, to_created_date?: string | undefined}; export type GetApiApiV1ProductsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({country_code: z.string(), state: z.string().optional()}); export const depopShippingDetails = z.object({address_id: z.number().int(), shipping_provider_id: z.string(), parcel_size_id: z.string(), payer: z.enum(["buyer", "seller"])}); export const picture = z.object({url: z.string(), height: z.number(), width: z.number()}); export const product = z.object({sku: z.string().nullable().optional(), product_id: z.number(), slug: z.string(), status: z.enum(["STATUS_ONSALE", "STATUS_PURCHASED", "STATUS_MARKED_AS_SOLD", "STATUS_DELETED", "STATUS_DELETED_ONSALE", "STATUS_DELETED_PURCHASED", "STATUS_DELETED_MARKED_AS_SOLD", "STATUS_PRODUCT_BANNED_ONSALE", "STATUS_USER_BANNED_ONSALE", "STATUS_PRODUCT_BANNED_PURCHASED", "STATUS_USER_BANNED_PURCHASED"]), address: address, description: z.string().max(1000), price_currency: z.string(), price_amount: z.string(), current_price: z.string().optional(), discount_price: z.string().optional(), auto_send_offer_price: z.string().optional(), auto_negotiate_offer_price: z.string().optional(), international_shipping_cost: z.string().optional(), national_shipping_cost: z.string().optional(), depop_shipping: depopShippingDetails.optional(), quantity: z.number(), pictures: z.array(picture), department: z.string(), product_type: z.string(), size_set_id: z.number().optional(), size_id: z.number().optional(), condition: z.enum(["brand_new", "used_like_new", "used_excellent", "used_good", "used_fair"]).optional(), colour: z.array(z.enum(["black", "grey", "white", "brown", "tan", "cream", "yellow", "red", "burgundy", "orange", "pink", "purple", "blue", "navy", "green", "khaki", "multi", "silver", "gold"])), style: z.array(z.enum(["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"])), age: z.array(z.enum(["modern", "y2k", "90s", "80s", "70s", "60s", "50s", "antique"])), source: z.array(z.enum(["vintage", "preloved", "reworked", "custom", "handmade", "deadstock", "designer", "repaired"])), brand: z.string(), attributes: z.record(z.string(), z.array(z.string())), is_boosted: z.boolean(), created_at: z.string(), updated_at: z.string()}); export const getApiApiV1ProductsResponse = z.object({meta: z.object({cursor: z.string().optional(), has_more: z.boolean().optional()}).optional(), data: z.array(product).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 }; /** * Get all products * * GET /api/v1/products/ */ export const getApiApiV1Products = async (args: GetApiApiV1ProductsArgs, options?: GetApiApiV1ProductsOptions) => { const {baseUrl = "https://partnerapi-staging.depop.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.state != null) query.append("state", String(args.state)) if (args.from_created_date != null) query.append("from_created_date", String(args.from_created_date)) if (args.to_created_date != null) query.append("to_created_date", String(args.to_created_date)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV1ProductsResponse, await res.json()) };