import {z} from 'zod' export type Client = {name: string, vatCode?: string | undefined, isTaxPayer?: boolean | undefined, regCom?: string | undefined, address?: string | undefined, city?: string | undefined, county?: string | undefined, country: string, email?: string | undefined, phone?: string | undefined, contact?: string | undefined, iban?: string | undefined, bank?: string | undefined, code?: string | undefined, saveToDb?: boolean | undefined}; export type Product = {name: string, code?: string | undefined, productDescription?: string | undefined, isService?: boolean | undefined, quantity: number, measuringUnitName: string, price: number, currency?: ('RON' | 'EUR' | 'USD' | 'GBP' | 'CAD' | 'AUD' | 'CHF' | 'TRY' | 'CZK' | 'DKK' | 'HUF' | 'MDL' | 'SEK' | 'NOK' | 'JPY' | 'EGP' | 'PLN' | 'RUB' | 'AED' | 'BRL' | 'CNY' | 'HRK' | 'INR' | 'KRW' | 'MXN' | 'NZD' | 'RSD' | 'THB' | 'UAH' | 'XDR' | 'ZAR') | undefined, exchangeRate?: number | undefined, isTaxIncluded?: boolean | undefined, taxName?: string | undefined, taxPercentage: number, warehouseName?: string | undefined, isDiscount?: boolean | undefined, numberOfItems?: number | undefined, discountType?: (1 | 2) | undefined, discountValue?: number | undefined, discountPercentage?: number | undefined, translatedName?: string | undefined, translatedMeasuringUnit?: string | undefined, saveToDb?: boolean | undefined, useSBProductName?: boolean | undefined}; export type EstimateRequest = {companyVatCode: string, seriesName: string, issueDate?: string | undefined, dueDate?: string | undefined, isDraft?: boolean | undefined, client: Client, products: Array, currency?: ('RON' | 'EUR' | 'USD' | 'GBP' | 'CAD' | 'AUD' | 'CHF' | 'TRY' | 'CZK' | 'DKK' | 'HUF' | 'MDL' | 'SEK' | 'NOK' | 'JPY' | 'EGP' | 'PLN' | 'RUB' | 'AED' | 'BRL' | 'CNY' | 'HRK' | 'INR' | 'KRW' | 'MXN' | 'NZD' | 'RSD' | 'THB' | 'UAH' | 'XDR' | 'ZAR') | undefined, exchangeRate?: number | undefined, paymentUrl?: string | undefined, sendEmail?: boolean | undefined, email?: ({to?: string | undefined, cc?: string | undefined, bcc?: string | undefined}) | undefined, language?: string | undefined, observations?: string | undefined, mentions?: string | undefined, precision?: number | undefined, issuerName?: string | undefined, issuerCnp?: string | undefined, delegateName?: string | undefined, delegateIdentityCard?: string | undefined, delegateAuto?: string | undefined, useIntraCif?: boolean | undefined, aviz?: string | undefined}; export type CreateApiEstimateV2Args = {body: EstimateRequest}; export type CreateApiEstimateV2Options = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const estimateResponseV2 = z.object({errorText: z.string().optional(), message: z.string().optional(), number: z.string().optional(), series: z.string().optional(), url: z.string().optional(), documentUrl: z.string().optional(), documentId: z.number().int().optional(), documentViewUrl: 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 }; /** * Emitere proforma * * POST /estimate/v2 */ export const createApiEstimateV2 = async (args: CreateApiEstimateV2Args, options?: CreateApiEstimateV2Options) => { const {baseUrl = "https://ws.smartbill.ro/SBORO/api", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/estimate/v2${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(estimateResponseV2, await res.json()) };