import {z} from 'zod' export type CreateApiApiCommercialSalesDocumentLinesArgs = {body: {data?: ({attributes?: ({description?: string | undefined, document_id?: number | undefined}) | undefined, type?: string | undefined}) | undefined}}; export type CreateApiApiCommercialSalesDocumentLinesOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiCommercialSalesDocumentLinesResponse = z.object({data: z.object({attributes: z.object({amount: z.number().nullable().optional(), amount_with_header_settlement: z.number().nullable().optional(), base_currency_amount: z.unknown(), base_currency_net_amount: z.unknown(), base_currency_net_unit_price: z.unknown(), base_currency_settlement_with_tax: z.unknown(), base_currency_settlement_without_tax: z.unknown(), base_currency_tax_amount: z.unknown(), base_currency_unit_price: z.unknown(), created_at: z.string().optional(), date: z.unknown(), description: z.string().optional(), exemption_reason: z.unknown(), imported_quantity: z.unknown(), item_code: z.string().optional(), item_id: z.number().nullable().optional(), item_type: z.string().optional(), item_unit_price_includes_vat: z.boolean().optional(), locked: z.unknown(), net_amount: z.number().nullable().optional(), net_amount_with_header_settlement: z.number().nullable().optional(), net_settlement_amount: z.unknown(), net_tax_amount: z.unknown(), net_unit_price: z.number().nullable().optional(), original_document_no: z.unknown(), pending_quantity: z.number().nullable().optional(), pending_retention_value: z.unknown(), pending_total: z.unknown(), quantity: z.number().nullable().optional(), retention_value: z.number().nullable().optional(), settlement_amount: z.number().nullable().optional(), settlement_expression: z.string().nullable().optional(), settlement_percentage: z.number().optional(), settlement_with_tax: z.number().nullable().optional(), settlement_without_tax: z.number().nullable().optional(), tax_amount: z.number().nullable().optional(), tax_code: z.string().nullable().optional(), tax_country_region: z.string().optional(), tax_descriptor_id: z.number().nullable().optional(), tax_percentage: z.number().nullable().optional(), unit_price: z.number().nullable().optional(), updated_at: z.string().optional()}).optional(), id: z.string().optional(), relationships: z.object({document: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional(), product: z.object({data: z.unknown()}).optional(), service: z.object({data: z.unknown()}).optional(), tax: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).nullable().optional()}).optional(), tax_descriptor: z.object({data: z.unknown()}).optional(), unit_of_measure: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).nullable().optional()}).optional()}).optional(), type: z.string().optional()}).optional(), meta: z.object({observed: z.object({lines: z.number().optional(), scalar: z.number().optional()}).optional()}).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 }; /** * Criar Linha de Documentos de Venda para um Produto * * POST /api/commercial_sales_document_lines */ export const createApiApiCommercialSalesDocumentLines = async (args: CreateApiApiCommercialSalesDocumentLinesArgs, options: CreateApiApiCommercialSalesDocumentLinesOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/commercial_sales_document_lines${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(createApiApiCommercialSalesDocumentLinesResponse, await res.json()) };