feat: implement getFileById method

This commit is contained in:
StevanFreeborn
2023-02-06 21:28:11 -06:00
parent fac2367052
commit 04e1ebdf8d
8 changed files with 718 additions and 101 deletions
+37
View File
@@ -1,8 +1,10 @@
import { type AxiosResponse } from 'axios';
import { FieldType } from '../enums/FieldType';
import { App } from './App';
import { CollectionResponse } from './CollectionResponse';
import { CreatedWithIdResponse } from './CreatedWithIdResponse';
import { Field } from './Field';
import { File } from './File';
import { FileInfo } from './FileInfo';
import { FormulaField } from './FormulaField';
import { GetPagedAppsResponse } from './GetPagedAppsResponse';
@@ -221,6 +223,41 @@ export class ApiResponse<T> {
);
}
public asFileType(response: AxiosResponse): ApiResponse<File> {
const apiResponse = this as ApiResponse<any>;
let fileName = response.headers['content-disposition']
.split(';')[1]
.split('=')[1];
fileName = fileName.substring(1, fileName.length - 1);
const contentType =
response.headers['content-type'] ??
response.headers['Content-Type'] ??
null;
let contentLength =
response.headers['content-length'] ??
response.headers['Content-Length'] ??
0;
contentLength = parseInt(contentLength);
const file = new File(
apiResponse.data,
fileName,
contentType,
contentLength
);
return new ApiResponse<File>(
apiResponse.statusCode,
apiResponse.message,
file
);
}
/**
* @method asFileCollectionType - Converts the field item to the appropriate field object based upon the field item's type.
* @param fieldItem - The field item to convert.
+46
View File
@@ -0,0 +1,46 @@
import { type Readable } from 'stream';
/**
* @class File - Represents a file retrieved from Onspring.
*/
export class File {
/**
* @property {Readable} stream - The stream of the file.
*/
public stream: Readable;
/**
* @property {string} fileName - The name of the file.
*/
public fileName: string;
/**
* @property {string} contentType - The content type of the file.
*/
public contentType: string;
/**
* @property {number} contentLength - The content length of the file.
*/
public contentLength: number;
/**
* @constructor - Creates a new instance of File.
* @param {Readable} stream - The stream of the file.
* @param {string} fileName - The name of the file.
* @param {string} contentType - The content type of the file.
* @param {number} contentLength - The content length of the file.
* @returns {File} - A new instance of File.
*/
constructor(
stream: Readable,
fileName: string,
contentType: string,
contentLength: number
) {
this.stream = stream;
this.fileName = fileName;
this.contentType = contentType;
this.contentLength = contentLength;
}
}
+34 -2
View File
@@ -1,10 +1,10 @@
import axios from 'axios';
import { type AxiosInstance, type AxiosRequestConfig } from 'axios';
import { PagingRequest } from './PagingRequest';
import { ArgumentValidator } from './ArgumentValidator';
import { EndpointFactory } from './EndpointFactory';
import { ApiResponseFactory } from './ApiResponseFactory';
import { type AxiosInstance, type AxiosRequestConfig } from 'axios';
import { type ApiResponse } from './ApiResponse';
import { PagingRequest } from './PagingRequest';
import { type GetPagedAppsResponse } from './GetPagedAppsResponse';
import { type App } from './App';
import { type CollectionResponse } from './CollectionResponse';
@@ -13,6 +13,7 @@ import { type GetPagedFieldsResponse } from './GetPagedFieldsResponse';
import { type SaveFileRequest } from './SaveFileRequest';
import { type CreatedWithIdResponse } from './CreatedWithIdResponse';
import { type FileInfo } from './FileInfo';
import { type File } from './File';
/**
* @class OnspringClient - A client that can communicate with the Onspring API.
@@ -200,6 +201,37 @@ export class OnspringClient {
return apiResponse.asFileInfoType();
}
/**
* @method getFileById - Gets a file by its id.
* @param {number} recordId - The id of the record that the file is attached to.
* @param {number} fieldId - The id of the field that the file is attached to.
* @param {number} fileId - The id of the file to get.
* @returns {Promise<ApiResponse<File>>} - A promise that resolves to an ApiResponse of type File.
*/
public async getFileById(
recordId: number,
fieldId: number,
fileId: number
): Promise<ApiResponse<File>> {
const endpoint = EndpointFactory.getFileByIdEndpoint(
recordId,
fieldId,
fileId
);
const response = await this._client.get(endpoint, {
responseType: 'stream',
});
const apiResponse = ApiResponseFactory.getApiResponse<any>(response);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asFileType(response);
}
/**
* @method saveFile - Saves a file to a record in Onspring.
* @param {SaveFileRequest} request - The request that will be used to save the file.