Files
Automata/packages/nodes-base/nodes/BambooHr/v1/actions/employeeDocument/upload/execute.ts
कारतोफ्फेलस्क्रिप्ट™ 5eb0d52459 refactor: Unify binary-data assertion across all nodes (no-changelog) (#5624)
2023-03-06 17:33:32 +01:00

40 lines
1.3 KiB
TypeScript

import type { IDataObject, IExecuteFunctions } from 'n8n-workflow';
import { apiRequest } from '../../../transport';
export async function upload(this: IExecuteFunctions, index: number) {
let body: IDataObject = {};
const requestMethod = 'POST';
const id: string = this.getNodeParameter('employeeId', index) as string;
const category = this.getNodeParameter('categoryId', index) as string;
const options = this.getNodeParameter('options', index);
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', index);
const { fileName, mimeType } = this.helpers.assertBinaryData(index, binaryPropertyName);
const binaryDataBuffer = await this.helpers.getBinaryDataBuffer(index, binaryPropertyName);
body = {
json: false,
formData: {
file: {
value: binaryDataBuffer,
options: {
filename: fileName,
contentType: mimeType,
},
},
fileName,
category,
},
resolveWithFullResponse: true,
};
if (options.hasOwnProperty('share') && body.formData) {
Object.assign(body.formData, options.share ? { share: 'yes' } : { share: 'no' });
}
//endpoint
const endpoint = `employees/${id}/files`;
const { headers } = await apiRequest.call(this, requestMethod, endpoint, {}, {}, body);
return this.helpers.returnJsonArray({ fileId: headers.location.split('/').pop() });
}