import {z} from 'zod' export type CreateApiApiServicesArgs = {body: {data?: (Array<{attributes?: ({customs_cost?: number | undefined, ean_barcode?: string | undefined, estimated_total_cost?: number | undefined, financial_cost?: number | undefined, item_code?: string | undefined, item_description?: string | undefined, other_cost?: number | undefined, purchase_price?: number | undefined, sales_price?: number | undefined, sales_price_2?: number | undefined, sales_price_3?: number | undefined, transport_cost?: number | undefined, type?: string | undefined}) | undefined, type?: string | undefined}>) | undefined}}; export type CreateApiApiServicesOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiServicesResponse = z.object({data: z.array(z.object({attributes: z.object({accounting_number: z.unknown(), applied_tax_code: z.unknown(), applied_tax_exemption_reason_id: z.unknown(), customs_cost: z.number().optional(), ean_barcode: z.string().optional(), estimated_total_cost: z.number().optional(), financial_cost: z.number().optional(), is_active: z.boolean().optional(), is_merchandise: z.unknown(), item_code: z.string().optional(), item_description: z.string().optional(), location_in_warehouse: z.unknown(), notes: z.unknown(), other_cost: z.number().optional(), product_inventory_type: z.unknown(), purchase_price: z.number().optional(), sales_price: z.number().optional(), sales_price_2: z.number().optional(), sales_price_2_vat_display: z.unknown(), sales_price_3: z.number().optional(), sales_price_3_vat_display: z.unknown(), sales_price_includes_vat: z.unknown(), sales_price_vat_display: z.unknown(), service_group: z.unknown(), tax_code: z.unknown(), transport_cost: z.number().optional()}).optional(), id: z.string().optional(), relationships: z.object({applied_tax_exemption_reason: z.object({data: z.unknown()}).optional(), company: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional(), item_families: z.object({data: z.unknown()}).optional(), tax_exemption_reasons: z.object({data: z.unknown()}).optional(), unit_of_measure: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional()}).optional(), type: z.string().optional()})).optional(), meta: z.object({observed: z.object({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 Serviço * * POST /api/services */ export const createApiApiServices = async (args: CreateApiApiServicesArgs, options: CreateApiApiServicesOptions) => { 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/services${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(createApiApiServicesResponse, await res.json()) };