import {z} from 'zod' export type NotificationTypeDetail = {type?: string | undefined, category?: string | undefined}; export type ContactPost = {email?: string | undefined, subscriptionType?: ('ALL' | 'CUSTOM' | 'NOTHING') | undefined, notificationSubscriptions?: Array | undefined, notificationCategories?: (Array<'ORDERS' | 'RETURN_SHIPMENTS' | 'ITEMS' | 'INBOUNDS' | 'CUSTOMER_INQUIRY' | 'SHOPS' | 'RETURN_LINK_LABEL' | 'SHIPMENT_LABEL' | 'INVENTORY' | 'API_KEY_REQUESTS' | 'IMPORTANT'>) | undefined}; export type CreateApiContactsSellerIdArgs = {sellerId: number, body: ContactPost}; export type CreateApiContactsSellerIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationTypeDetail = z.object({type: z.string().optional(), category: z.string().optional()}); export const contactGet = z.object({email: z.string().optional(), subscriptionType: z.enum(["ALL", "CUSTOM", "NOTHING"]).optional(), notificationSubscriptions: z.array(notificationTypeDetail).optional(), id: z.number().int().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 contact for the given seller * * POST /contacts/{sellerId} */ export const createApiContactsSellerId = async (args: CreateApiContactsSellerIdArgs, options?: CreateApiContactsSellerIdOptions) => { const {baseUrl = "https://api.quivo.co", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/contacts/${encodeURIComponent(String(args.sellerId))}${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(contactGet, await res.json()) };