import {z} from 'zod' export type CreateApiDocumentIdIdCopyArgs = {id: string, body: {sourceDocumentId: string, comment?: string | undefined}}; export type CreateApiDocumentIdIdCopyOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const documentVersion = z.object({id: z.string().optional(), version: z.string().optional(), comment: z.string().optional(), createdDate: z.number().int().optional(), documentSize: z.number().int().optional(), documentVersion: z.string().optional(), lastModifiedDate: z.number().int().optional(), userId: z.string().optional()}); export const document = z.object({id: z.string().optional(), version: z.string().optional(), createdDate: z.number().int().optional(), description: z.string().optional(), documentSize: z.number().int().optional(), documentType: z.enum(["ARTICLE_DATASHEET", "ARTICLE_LABEL", "BLANKET_PURCHASE_ORDER", "BLANKET_SALES_ORDER", "CANCELLATION_UBL", "CANCELLATION_XR", "CONTRACT", "CREDIT_ADVICE", "CREDIT_ADVICE_CANCELLATION", "CREDIT_ADVICE_PRELIMINARY_INVOICE", "CREDIT_ADVICE_UBL", "CREDIT_ADVICE_XR", "CRM_EVENT_LETTER", "CUSTOMER_ARTICLE_PRICE_LIST", "DUNNING", "INCOMING_GOODS", "INCOMING_GOODS_FROM_RETURN", "INCOMING_GOODS_RETURNS_PICKUP_NOTE", "INVENTORY_TAKING", "PERFORMANCE_RECORD", "PRODUCTION_ORDER", "PURCHASE_INVOICE", "PURCHASE_INVOICE_FATTURAPA", "PURCHASE_INVOICE_ZUGFERD", "PURCHASE_ORDER", "PURCHASE_ORDER_CANCELLATION", "PURCHASE_ORDER_DEFAULT", "PURCHASE_ORDER_REQUEST", "PURCHASE_ORDER_REQUEST_OFFER_ITEM_CSV", "PURCHASE_ORDER_REQUEST_SUPPLIER_DOCUMENT", "QUOTATION", "QUOTATION_DEFAULT", "SALES_INVOICE", "SALES_INVOICE_CANCELLATION", "SALES_INVOICE_CANCELLATION_DEFAULT", "SALES_INVOICE_DEFAULT", "SALES_INVOICE_FATTURAPA", "SALES_INVOICE_PRELIMINARY", "SALES_INVOICE_QR", "SALES_INVOICE_UBL", "SALES_INVOICE_XR", "SALES_ORDER", "SALES_ORDER_DEFAULT", "SHIPMENT_CUSTOMS_DECLARATION", "SHIPMENT_DELIVERY_LABEL", "SHIPMENT_DELIVERY_NOTE", "SHIPMENT_DELIVERY_NOTE_DEFAULT", "SHIPMENT_PICKING_LIST", "SHIPMENT_PROFORMA_INVOICE", "SHIPMENT_RETURN_DELIVERY_NOTE", "SHIPMENT_RETURN_LABEL", "SHIPMENT_SERIAL_NUMBERS_CSV", "TICKET", "ZUGFERD_VALIDATION"]).optional(), lastModifiedDate: z.number().int().optional(), mediaType: z.string(), name: z.string(), userId: z.string().optional(), versions: z.array(documentVersion).optional()}); export const createApiDocumentIdIdCopyResponse = z.object({result: document.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 }; /** * POST /document/id/{id}/copy */ export const createApiDocumentIdIdCopy = async (args: CreateApiDocumentIdIdCopyArgs, options: CreateApiDocumentIdIdCopyOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/document/id/${encodeURIComponent(String(args.id))}/copy${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(createApiDocumentIdIdCopyResponse, await res.json()) };