fix(core): update

This commit is contained in:
Philipp Kunz 2019-10-27 14:32:27 +01:00
parent d22504317e
commit 466187fd52

View File

@ -8,18 +8,33 @@ import { request } from './smartrequest.request';
export interface IFormField { export interface IFormField {
name: string; name: string;
type: 'string' | 'filePath' | 'Buffer'; type: 'string' | 'filePath' | 'Buffer';
payload: string; payload: string | Buffer;
fileName?: string;
} }
const appendFormField = async (formDataArg: plugins.formData, formDataField: IFormField) => { const appendFormField = async (formDataArg: plugins.formData, formDataField: IFormField) => {
if (formDataField.type === 'filePath') { switch (formDataField.type) {
const fileData = plugins.fs.readFileSync( case 'string':
plugins.path.join(process.cwd(), formDataField.payload) formDataArg.append(formDataField.name, formDataField.payload);
); break;
formDataArg.append('file', fileData, { case 'filePath':
filename: 'upload.pdf', if (typeof formDataField.payload !== 'string') {
contentType: 'application/pdf' throw new Error(`Payload for key ${formDataField.name} must be of type string. Got ${typeof formDataField.payload} instead.`);
}); }
const fileData = plugins.fs.readFileSync(
plugins.path.join(process.cwd(), formDataField.payload)
);
formDataArg.append('file', fileData, {
filename: formDataField.fileName ? formDataField.fileName : 'upload.pdf',
contentType: 'application/pdf'
});
break;
case 'Buffer':
formDataArg.append('file', formDataField.payload, {
filename: formDataField.fileName ? formDataField.fileName : 'upload.pdf',
contentType: 'application/pdf'
});
break;
} }
}; };