import {z} from 'zod' export type Phone = {code?: string | undefined, number?: string | undefined}; export type Document = {type?: string | undefined, number?: string | undefined}; export type Address = {line_1?: string | undefined, line_2?: string | undefined, city?: string | undefined, state?: string | undefined, country?: string | undefined, zip_code?: string | undefined}; export type WalletDetails = {payment_processing_key?: string | undefined, payment_processing_certificate?: string | undefined, merchant_identity_key?: string | undefined, merchant_identity_certificate?: string | undefined, merchant_identity_password?: string | undefined}; export type PaymentMethodConfig = {provider_id: string, payment_method_type: string, merchant_id?: string | undefined, wallet_details?: WalletDetails | undefined}; export type SellerCreate = {account_id: string, merchant_seller_id: string, name?: string | undefined, email?: string | undefined, phone?: Phone | undefined, document?: Document | undefined, address?: Address | undefined, country?: string | undefined, website?: string | undefined, industry?: string | undefined, merchant_category_code?: string | undefined, payment_methods?: Array | undefined}; export type CreateApiSellersArgs = {'public-api-key': string, 'private-secret-key': string, body: SellerCreate}; export type CreateApiSellersOptions = {baseUrl?: string | undefined, publicApiKey?: string | undefined, privateSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const phone = z.object({code: z.string().optional(), number: z.string().optional()}); export const document = z.object({type: z.string().optional(), number: z.string().optional()}); export const address = z.object({line_1: z.string().optional(), line_2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), zip_code: z.string().optional()}); export const walletDetails = z.object({payment_processing_key: z.string().optional(), payment_processing_certificate: z.string().optional(), merchant_identity_key: z.string().optional(), merchant_identity_certificate: z.string().optional(), merchant_identity_password: z.string().optional()}); export const paymentMethodConfig = z.object({provider_id: z.string(), payment_method_type: z.string(), merchant_id: z.string().optional(), wallet_details: walletDetails.optional()}); export const seller = z.object({seller_id: z.string().optional(), merchant_seller_id: z.string().optional(), name: z.string().optional(), email: z.string().optional(), phone: phone.optional(), document: document.optional(), address: address.optional(), country: z.string().min(2).max(2).optional(), website: z.string().optional(), industry: z.string().optional(), merchant_category_code: z.string().min(4).max(4).optional(), payment_methods: z.array(paymentMethodConfig).optional(), created_at: z.string().optional(), updated_at: 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 Seller * * POST /sellers */ export const createApiSellers = async (args: CreateApiSellersArgs, options?: CreateApiSellersOptions) => { const {baseUrl = "https://api-sandbox.y.uno/v1", fetch: fetchFn = fetch, publicApiKey, privateSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['public-api-key'] != null) headers.set("public-api-key", String(args['public-api-key'])) if (args['private-secret-key'] != null) headers.set("private-secret-key", String(args['private-secret-key'])) if (publicApiKey !== undefined) headers.set("PUBLIC-API-KEY", publicApiKey) if (privateSecretKey !== undefined) headers.set("PRIVATE-SECRET-KEY", privateSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/sellers${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(seller, await res.json()) };