import {z} from 'zod' export type CommonEnumPricingSchemeInputEnum = 'price' | 'margin'; export type CommonPartLinePriceBreakInputDto = {quantity?: number | undefined, price?: (number | null) | undefined, margin?: (number | null) | undefined, showOnPdf?: boolean | undefined}; export type CommonExternalReferenceDto = {type?: (string | null) | undefined, externalId: string, displayId?: (string | null) | undefined, status?: (string | null) | undefined, url?: (string | null) | undefined, modifiedUtc?: (string | null) | undefined, lastSyncedUtc?: (string | null) | undefined}; export type QuotePartLineItemCreateDto = {itemId: string, quantity?: number | undefined, price?: (number | null) | undefined, pricingScheme?: CommonEnumPricingSchemeInputEnum | undefined, priceBreaks?: (Array | null) | undefined, description?: (string | null) | undefined, internalNotes?: (string | null) | undefined, itemNotes?: (string | null) | undefined, isTaxable?: (boolean | null) | undefined, taxCodeId?: (string | null) | undefined, taxRate?: (number | null) | undefined, overrideTaxRate?: (boolean | null) | undefined, externalReferences?: ({[key: string]: CommonExternalReferenceDto} | null) | undefined, isPercentageDiscount?: (boolean | null) | undefined, discountAmount?: (number | null) | undefined, accountingCodeId?: (string | null) | undefined, leadTimeDays?: (number | null) | undefined}; export type QuotePartLineItemCreateBatchDto = {lineItems: Array}; export type CreateApiApiQuotesQuoteIdPartLineItemsBatchArgs = {quoteId: string, body: QuotePartLineItemCreateBatchDto}; export type CreateApiApiQuotesQuoteIdPartLineItemsBatchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const batchCreatedResponseDto = z.object({ids: z.array(z.string())}); 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 one or more part line items on a quote * * POST /api/quotes/{quoteId}/part-line-items/batch */ export const createApiApiQuotesQuoteIdPartLineItemsBatch = async (args: CreateApiApiQuotesQuoteIdPartLineItemsBatchArgs, options?: CreateApiApiQuotesQuoteIdPartLineItemsBatchOptions) => { const {baseUrl = "https://api.fulcrumpro.com", 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(/\/+$/, '')}/api/quotes/${encodeURIComponent(String(args.quoteId))}/part-line-items/batch${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(batchCreatedResponseDto, await res.json()) };