65 lines
2.0 KiB
TypeScript
65 lines
2.0 KiB
TypeScript
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<UploadedFileDto> {
|
|
return fileApiClient.uploadFile(params)
|
|
}
|
|
|
|
async function downloadFileContent(id: string, inline = false): Promise<Blob> {
|
|
return fileApiClient.downloadFileContent({ id, inline })
|
|
}
|
|
|
|
function getFileViewUrl(id: string): string {
|
|
return `${basePath}/files/${id}/content?inline=true`
|
|
}
|
|
|
|
async function deleteFile(id: string): Promise<void> {
|
|
return fileApiClient.deleteFile({ id })
|
|
}
|
|
|
|
async function getFilesByApplicationForm(
|
|
applicationFormId: string,
|
|
formElementReference?: string
|
|
): Promise<UploadedFileDto[]> {
|
|
return fileApiClient.getFilesByApplicationForm({ applicationFormId, formElementReference })
|
|
}
|
|
|
|
async function associateFilesWithApplicationForm(applicationFormId: string, fileIds: string[]): Promise<void> {
|
|
const associateFilesWithApplicationFormRequest: AssociateFilesWithApplicationFormRequest = { fileIds }
|
|
return fileApiClient.associateFilesWithApplicationForm({
|
|
applicationFormId,
|
|
associateFilesWithApplicationFormRequest
|
|
})
|
|
}
|
|
|
|
return {
|
|
uploadFile,
|
|
downloadFileContent,
|
|
getFileViewUrl,
|
|
deleteFile,
|
|
getFilesByApplicationForm,
|
|
associateFilesWithApplicationForm
|
|
}
|
|
}
|