import { FileApi, Configuration, type UploadedFileDto, type AssociateFilesWithApplicationFormRequest } from '~~/.api-client' import { cleanDoubleSlashes, withoutTrailingSlash } from 'ufo' import { wrappedFetchWrap } from '~/utils/wrappedFetch' export function useFileApi() { const appBaseUrl = useRuntimeConfig().app.baseURL const { serverApiBasePath, clientProxyBasePath } = useRuntimeConfig().public const basePath = withoutTrailingSlash( cleanDoubleSlashes(import.meta.client ? appBaseUrl + clientProxyBasePath : clientProxyBasePath + serverApiBasePath) ) const fileApiClient = new FileApi(new Configuration({ basePath, fetchApi: wrappedFetchWrap(useRequestFetch()) })) async function uploadFile(params: { file: File applicationFormId?: string formElementReference: string organizationId?: string }): Promise { return fileApiClient.uploadFile(params) } async function downloadFileContent(id: string, inline = false): Promise { return fileApiClient.downloadFileContent({ id, inline }) } function getFileViewUrl(id: string): string { return `${basePath}/files/${id}/content?inline=true` } async function deleteFile(id: string): Promise { return fileApiClient.deleteFile({ id }) } async function getFilesByApplicationForm( applicationFormId: string, formElementReference?: string ): Promise { return fileApiClient.getFilesByApplicationForm({ applicationFormId, formElementReference }) } async function associateFilesWithApplicationForm(applicationFormId: string, fileIds: string[]): Promise { const associateFilesWithApplicationFormRequest: AssociateFilesWithApplicationFormRequest = { fileIds } return fileApiClient.associateFilesWithApplicationForm({ applicationFormId, associateFilesWithApplicationFormRequest }) } return { uploadFile, downloadFileContent, getFileViewUrl, deleteFile, getFilesByApplicationForm, associateFilesWithApplicationForm } }