160 lines
7.4 KiB
TypeScript
160 lines
7.4 KiB
TypeScript
/**
|
|
* Front-end API wrapper for this module.
|
|
* Call guide: import the named API object, then call the matching method from a Vue page or composable.
|
|
*/
|
|
import type { AxiosProgressEvent } from 'axios'
|
|
import api from './index'
|
|
import type { IdType } from '@/utils/id'
|
|
|
|
/**
|
|
* Purpose: ChunkUploadInitRequest supports file.ts by holding or executing the chunk upload init request page or API role.
|
|
* Usage: import, call, read, or update this interface from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
export interface ChunkUploadInitRequest {
|
|
fileName: string
|
|
fileSize: number
|
|
contentType: string
|
|
fileFingerprint: string
|
|
scene: string
|
|
}
|
|
|
|
/**
|
|
* Purpose: ChunkUploadSessionVO supports file.ts by holding or executing the chunk upload session vo page or API role.
|
|
* Usage: import, call, read, or update this interface from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
export interface ChunkUploadSessionVO {
|
|
uploadId: string
|
|
chunkSize: number
|
|
totalChunks: number
|
|
uploadedChunks: number[]
|
|
}
|
|
|
|
/**
|
|
* fileApi groups all API calls for this page domain.
|
|
* Call guide: pass the typed payload or identifier into the method below, and use the returned Promise in the page script.
|
|
*/
|
|
export const fileApi = {
|
|
/**
|
|
* `uploadAvatar` API call. Use this method from the page script to invoke the backend endpoint tied to this feature.
|
|
* Call guide: supply the parameters below, await the returned Promise, and handle loading / error UI in the caller.
|
|
* @param file: File caller-supplied value for `file: File`
|
|
*/
|
|
uploadAvatar: (file: File) => {
|
|
const formData = new FormData()
|
|
formData.append('file', file)
|
|
return api.post<string>('/user/upload-avatar', formData, {
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data'
|
|
}
|
|
})
|
|
},
|
|
uploadProfileImage: (file: File) => {
|
|
const formData = new FormData()
|
|
formData.append('file', file)
|
|
return api.post<string>('/user/upload-profile-image', formData, {
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data'
|
|
}
|
|
}) as unknown as Promise<string>
|
|
},
|
|
/**
|
|
* `initAvatarChunkUpload` API call. Use this method from the page script to invoke the backend endpoint tied to this feature.
|
|
* Call guide: supply the parameters below, await the returned Promise, and handle loading / error UI in the caller.
|
|
* @param data: ChunkUploadInitRequest caller-supplied value for `data: ChunkUploadInitRequest`
|
|
*/
|
|
initAvatarChunkUpload: (data: ChunkUploadInitRequest) => {
|
|
return api.post<ChunkUploadSessionVO>('/user/upload-avatar/init', data) as unknown as Promise<ChunkUploadSessionVO>
|
|
},
|
|
uploadAvatarChunk: (
|
|
/**
|
|
* Purpose: data supports file.ts by holding or executing the data page or API role.
|
|
* Usage: import, call, read, or update this page-data from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
data: { uploadId: string; chunkIndex: number; totalChunks: number; chunk: Blob },
|
|
config?: { signal?: AbortSignal; onUploadProgress?: (event: AxiosProgressEvent) => void }
|
|
) => {
|
|
const formData = new FormData()
|
|
formData.append('uploadId', data.uploadId)
|
|
formData.append('chunkIndex', String(data.chunkIndex))
|
|
formData.append('totalChunks', String(data.totalChunks))
|
|
formData.append('chunk', data.chunk)
|
|
return api.post<boolean>('/user/upload-avatar/chunk', formData, {
|
|
/**
|
|
* Purpose: headers supports file.ts by holding or executing the headers page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data'
|
|
},
|
|
/**
|
|
* Purpose: signal supports file.ts by holding or executing the signal page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
signal: config?.signal,
|
|
/**
|
|
* Purpose: onUploadProgress supports file.ts by holding or executing the on upload progress page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
onUploadProgress: config?.onUploadProgress
|
|
})
|
|
},
|
|
/**
|
|
* `completeAvatarChunkUpload` API call. Use this method from the page script to invoke the backend endpoint tied to this feature.
|
|
* Call guide: supply the parameters below, await the returned Promise, and handle loading / error UI in the caller.
|
|
* @param uploadId: string caller-supplied value for `uploadId: string`
|
|
*/
|
|
completeAvatarChunkUpload: (uploadId: string) => {
|
|
return api.post<string>('/user/upload-avatar/complete', { uploadId }) as unknown as Promise<string>
|
|
},
|
|
/**
|
|
* `cancelAvatarChunkUpload` API call. Use this method from the page script to invoke the backend endpoint tied to this feature.
|
|
* Call guide: supply the parameters below, await the returned Promise, and handle loading / error UI in the caller.
|
|
* @param uploadId: string caller-supplied value for `uploadId: string`
|
|
*/
|
|
cancelAvatarChunkUpload: (uploadId: string) => {
|
|
return api.delete<boolean>(`/user/upload-avatar/${uploadId}`)
|
|
},
|
|
initProfileImageChunkUpload: (data: ChunkUploadInitRequest) => {
|
|
return api.post<ChunkUploadSessionVO>('/user/upload-profile-image/init', data) as unknown as Promise<ChunkUploadSessionVO>
|
|
},
|
|
uploadProfileImageChunk: (
|
|
data: { uploadId: string; chunkIndex: number; totalChunks: number; chunk: Blob },
|
|
config?: { signal?: AbortSignal; onUploadProgress?: (event: AxiosProgressEvent) => void }
|
|
) => {
|
|
const formData = new FormData()
|
|
formData.append('uploadId', data.uploadId)
|
|
formData.append('chunkIndex', String(data.chunkIndex))
|
|
formData.append('totalChunks', String(data.totalChunks))
|
|
formData.append('chunk', data.chunk)
|
|
return api.post<boolean>('/user/upload-profile-image/chunk', formData, {
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data'
|
|
},
|
|
signal: config?.signal,
|
|
onUploadProgress: config?.onUploadProgress
|
|
})
|
|
},
|
|
completeProfileImageChunkUpload: (uploadId: string) => {
|
|
return api.post<string>('/user/upload-profile-image/complete', { uploadId }) as unknown as Promise<string>
|
|
},
|
|
cancelProfileImageChunkUpload: (uploadId: string) => {
|
|
return api.delete<boolean>(`/user/upload-profile-image/${uploadId}`)
|
|
},
|
|
|
|
/**
|
|
* `getSignedUrl` API call. Use this method from the page script to invoke the backend endpoint tied to this feature.
|
|
* Call guide: supply the parameters below, await the returned Promise, and handle loading / error UI in the caller.
|
|
* @param fileId: IdType caller-supplied value for `fileId: IdType`
|
|
* @param expiryTimeInMinutes?: number caller-supplied value for `expiryTimeInMinutes?: number`
|
|
*/
|
|
getSignedUrl: (fileId: IdType, expiryTimeInMinutes?: number) => {
|
|
return api.get<string>(`/file/get-signed-url/${fileId}`, {
|
|
/**
|
|
* Purpose: params supports file.ts by holding or executing the params page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
params: { expiryTimeInMinutes }
|
|
})
|
|
}
|
|
}
|