feat: implement saveFile method

This commit is contained in:
StevanFreeborn
2023-02-03 16:01:20 -06:00
parent 1b046b4cfc
commit 3dee5b690c
7 changed files with 147 additions and 2 deletions
+4
View File
@@ -1,6 +1,10 @@
{ {
"extends": "@istanbuljs/nyc-config-typescript", "extends": "@istanbuljs/nyc-config-typescript",
"check-coverage": true, "check-coverage": true,
"branches": 80,
"lines": 80,
"functions": 80,
"statements": 80,
"all": true, "all": true,
"include": ["src/**/*.ts"], "include": ["src/**/*.ts"],
"exclude": ["src/index.ts", "**/*.spec.ts"], "exclude": ["src/index.ts", "**/*.spec.ts"],
+2 -1
View File
@@ -9,7 +9,8 @@
"version": "1.0.0", "version": "1.0.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"axios": "^1.2.4" "axios": "^1.2.4",
"form-data": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2", "@istanbuljs/nyc-config-typescript": "^1.0.2",
+2 -1
View File
@@ -42,6 +42,7 @@
"typescript": "^4.9.5" "typescript": "^4.9.5"
}, },
"dependencies": { "dependencies": {
"axios": "^1.2.4" "axios": "^1.2.4",
"form-data": "^4.0.0"
} }
} }
+15
View File
@@ -1,5 +1,6 @@
import { App } from './App'; import { App } from './App';
import { CollectionResponse } from './CollectionResponse'; import { CollectionResponse } from './CollectionResponse';
import { CreatedWithIdResponse } from './CreatedWithIdResponse';
import { Field } from './Field'; import { Field } from './Field';
import { GetPagedAppsResponse } from './GetPagedAppsResponse'; import { GetPagedAppsResponse } from './GetPagedAppsResponse';
import { GetPagedFieldsResponse } from './GetPagedFieldsResponse'; import { GetPagedFieldsResponse } from './GetPagedFieldsResponse';
@@ -199,4 +200,18 @@ export class ApiResponse<T> {
getPagedFieldsResponse getPagedFieldsResponse
); );
} }
public AsCreatedWithIdResponseType(): ApiResponse<CreatedWithIdResponse> {
const apiResponse = this as ApiResponse<any>;
const createdWithIdResponse = new CreatedWithIdResponse(
apiResponse.data.id
);
return new ApiResponse<CreatedWithIdResponse>(
apiResponse.statusCode,
apiResponse.message,
createdWithIdResponse
);
}
} }
+15
View File
@@ -0,0 +1,15 @@
export class CreatedWithIdResponse {
/**
* @property {number} id - The id of the created object.
*/
public id: number;
/**
* @constructor - Creates a new instance of the CreatedWithIdResponse class.
* @param id - The id of the created object.
* @returns {CreatedWithIdResponse} - A new instance of the CreatedWithIdResponse class.
*/
constructor(id: number) {
this.id = id;
}
}
+23
View File
@@ -10,6 +10,8 @@ import { type App } from './App';
import { type CollectionResponse } from './CollectionResponse'; import { type CollectionResponse } from './CollectionResponse';
import { type Field } from './Field'; import { type Field } from './Field';
import { type GetPagedFieldsResponse } from './GetPagedFieldsResponse'; import { type GetPagedFieldsResponse } from './GetPagedFieldsResponse';
import { type SaveFileRequest } from './SaveFileRequest';
import { type CreatedWithIdResponse } from './CreatedWithIdResponse';
/** /**
* @class OnspringClient - A client that can communicate with the Onspring API. * @class OnspringClient - A client that can communicate with the Onspring API.
@@ -169,6 +171,27 @@ export class OnspringClient {
return apiResponse.AsGetPagedFieldsResponseType(); return apiResponse.AsGetPagedFieldsResponseType();
} }
/**
* @method saveFile - Saves a file to a record in Onspring.
* @param {SaveFileRequest} request - The request that will be used to save the file.
* @returns {Promise<ApiResponse<CreatedWithIdResponse>>} - A promise that resolves to an ApiResponse of type CreatedWithIdResponse.
*/
public async saveFile(
request: SaveFileRequest
): Promise<ApiResponse<CreatedWithIdResponse>> {
const endpoint = EndpointFactory.getSaveFileEndpoint();
const formData = request.AsFormData();
const apiResponse = await this.post<any>(endpoint, formData, {
headers: formData.getHeaders(),
});
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.AsCreatedWithIdResponseType();
}
/** /**
* @method get - Makes a GET request to the specified endpoint. * @method get - Makes a GET request to the specified endpoint.
* @param {string} endpoint - The endpoint that will be used to make the request. * @param {string} endpoint - The endpoint that will be used to make the request.
+86
View File
@@ -0,0 +1,86 @@
import FormData = require('form-data');
/**
* @class SaveFileRequest - Represents a request to save a file.
*/
export class SaveFileRequest {
/**
* @property {number} recordId - The id of the record that the file will be saved to.
*/
public recordId: number;
/**
* @property {number} fieldId - The id of the field that the file will be saved to.
*/
public fieldId: number;
/**
* @property {string} notes - The notes for the file.
*/
public notes: string;
/**
* @property {Date} modifiedDate - The modified date for the file.
*/
public modifiedDate: Date;
/**
* @property {string} fileName - The name of the file.
*/
public fileName: string;
/**
* @property {string} contentType - The content type of the file.
*/
public contentType: string;
/**
* @property {ReadableStream} fileStream - The file stream.
*/
public fileStream: ReadableStream;
/**
* @constructor - Creates a new SaveFileRequest.
* @param {number} recordId - The id of the record that the file will be saved to.
* @param {number} fieldId - The id of the field that the file will be saved to.
* @param {string} notes - The notes for the file.
* @param {Date} modifiedDate - The modified date for the file.
* @param {string} fileName - The name of the file.
* @param {string} contentType - The content type of the file.
* @param {ReadableStream} fileStream - The file stream.
* @returns {SaveFileRequest} - A new SaveFileRequest.
*/
constructor(
recordId: number,
fieldId: number,
notes: string,
modifiedDate: Date,
fileName: string,
contentType: string,
fileStream: ReadableStream
) {
this.recordId = recordId;
this.fieldId = fieldId;
this.notes = notes;
this.modifiedDate = modifiedDate;
this.fileName = fileName;
this.contentType = contentType;
this.fileStream = fileStream;
}
/**
* @method AsFormData - Converts the SaveFileRequest to a FormData object.
* @returns {FormData} - The SaveFileRequest as a FormData object.
*/
public AsFormData(): FormData {
const formData = new FormData();
formData.append('recordId', this.recordId.toString());
formData.append('fieldId', this.fieldId.toString());
formData.append('notes', this.notes);
formData.append('modifiedDate', this.modifiedDate.toUTCString());
formData.append('fileName', this.fileName);
formData.append('contentType', this.contentType);
formData.append('file', this.fileStream, this.fileName);
return formData;
}
}