import {z} from 'zod' export type PatchApiOrdersOrderIdInternalDocumentsDocumentIdArgs = {orderId: number, documentId: number, body: {title?: (string | null) | undefined, notes?: (string | null) | undefined, file?: string | undefined}}; export type PatchApiOrdersOrderIdInternalDocumentsDocumentIdOptions = {baseUrl?: string | undefined, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const internalDocumentUploaderDto = z.object({id: z.number(), firstName: z.string(), lastName: z.string(), email: z.string()}); export const orderInternalDocumentDto = z.object({id: z.number(), orderId: z.number(), fileName: z.string(), originalFileName: z.string(), filePath: z.string(), mimeType: z.string().optional(), fileSize: z.number().optional(), uploadedBy: z.number(), uploadedByUser: internalDocumentUploaderDto.nullable().optional(), title: z.string().optional(), notes: z.string().optional(), createdAt: z.string(), updatedAt: 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 }; /** * Update internal document metadata and/or replace the file * * PATCH /orders/{orderId}/internal-documents/{documentId} */ export const patchApiOrdersOrderIdInternalDocumentsDocumentId = async (args: PatchApiOrdersOrderIdInternalDocumentsDocumentIdArgs, options?: PatchApiOrdersOrderIdInternalDocumentsDocumentIdOptions) => { const {baseUrl = "https://api.findmydocument.net", fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (accessToken !== undefined) headers.set('authorization', `Bearer ${accessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/${encodeURIComponent(String(args.orderId))}/internal-documents/${encodeURIComponent(String(args.documentId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(orderInternalDocumentDto, await res.json()) };