ministry-platform-provider
Version:
TypeScript client library for Ministry Platform API integration
169 lines (168 loc) • 6.61 kB
JavaScript
export class FileService {
client;
constructor(client) {
this.client = client;
}
/**
* Returns the metadata (descriptions) of the files attached to the specified record.
*/
async getFilesByRecord(table, recordId, defaultOnly) {
try {
await this.client.ensureValidToken();
const queryParams = {};
if (defaultOnly !== undefined) {
queryParams['$default'] = defaultOnly.toString();
}
return await this.client.getHttpClient().get(`/files/${table}/${recordId}`, queryParams);
}
catch (error) {
console.error('Error getting files by record:', error);
throw error;
}
}
/**
* Uploads and attaches multiple files to the specified record.
*/
async uploadFiles(table, recordId, files, params) {
try {
await this.client.ensureValidToken();
const formData = new FormData();
files.forEach((file, index) => {
formData.append(`file-${index}`, file, file.name);
});
// Add optional parameters to form data if provided
if (params?.description) {
formData.append('description', params.description);
}
if (params?.isDefaultImage !== undefined) {
formData.append('isDefaultImage', params.isDefaultImage.toString());
}
if (params?.longestDimension) {
formData.append('longestDimension', params.longestDimension.toString());
}
const queryParams = {};
if (params?.description)
queryParams['$description'] = params.description;
if (params?.isDefaultImage !== undefined)
queryParams['$default'] = params.isDefaultImage.toString();
if (params?.longestDimension)
queryParams['$longestDimension'] = params.longestDimension.toString();
if (params?.userId)
queryParams['$userId'] = params.userId.toString();
return await this.client.getHttpClient().postFormData(`/files/${table}/${recordId}`, formData, queryParams);
}
catch (error) {
console.error('Error uploading files:', error);
throw error;
}
}
/**
* Updates the content and/or metadata of the file corresponding to provided identifier.
*/
async updateFile(fileId, file, params) {
try {
await this.client.ensureValidToken();
const formData = new FormData();
if (file) {
formData.append('file', file, file.name);
}
// Add optional parameters to form data if provided
if (params?.fileName) {
formData.append('fileName', params.fileName);
}
if (params?.description) {
formData.append('description', params.description);
}
if (params?.isDefaultImage !== undefined) {
formData.append('isDefaultImage', params.isDefaultImage.toString());
}
if (params?.longestDimension) {
formData.append('longestDimension', params.longestDimension.toString());
}
const queryParams = {};
if (params?.fileName)
queryParams['$fileName'] = params.fileName;
if (params?.description)
queryParams['$description'] = params.description;
if (params?.isDefaultImage !== undefined)
queryParams['$default'] = params.isDefaultImage.toString();
if (params?.longestDimension)
queryParams['$longestDimension'] = params.longestDimension.toString();
if (params?.userId)
queryParams['$userId'] = params.userId.toString();
return await this.client.getHttpClient().putFormData(`/files/${fileId}`, formData, queryParams);
}
catch (error) {
console.error('Error updating file:', error);
throw error;
}
}
/**
* Deletes the file corresponding to provided identifier.
*/
async deleteFile(fileId, userId) {
try {
await this.client.ensureValidToken();
const queryParams = {};
if (userId) {
queryParams['$userId'] = userId.toString();
}
await this.client.getHttpClient().delete(`/files/${fileId}`, queryParams);
}
catch (error) {
console.error('Error deleting file:', error);
throw error;
}
}
/**
* Returns the content of the file corresponding to provided globally unique identifier.
* This method does NOT require authentication.
*/
async getFileContentByUniqueId(uniqueFileId, thumbnail) {
try {
const queryParams = {};
if (thumbnail !== undefined) {
queryParams['$thumbnail'] = thumbnail.toString();
}
const url = this.client.getHttpClient().buildUrl(`/files/${uniqueFileId}`, queryParams);
const response = await fetch(url, {
method: 'GET'
// No authorization header needed for this endpoint
});
if (!response.ok) {
throw new Error(`GET /files/${uniqueFileId} failed: ${response.status} ${response.statusText}`);
}
return await response.blob();
}
catch (error) {
console.error('Error getting file content by unique ID:', error);
throw error;
}
}
/**
* Returns the file metadata (description) corresponding to provided database identifier.
*/
async getFileMetadata(fileId) {
try {
await this.client.ensureValidToken();
return await this.client.getHttpClient().get(`/files/${fileId}/metadata`);
}
catch (error) {
console.error('Error getting file metadata:', error);
throw error;
}
}
/**
* Returns the file metadata (description) corresponding to provided globally unique identifier.
*/
async getFileMetadataByUniqueId(uniqueFileId) {
try {
await this.client.ensureValidToken();
return await this.client.getHttpClient().get(`/files/${uniqueFileId}/metadata`);
}
catch (error) {
console.error('Error getting file metadata by unique ID:', error);
throw error;
}
}
}