smartrequest/ts/smartrequest.formdata.ts

100 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-07-28 23:19:50 +00:00
import * as plugins from './smartrequest.plugins.js';
import * as interfaces from './smartrequest.interfaces.js';
import { request } from './smartrequest.request.js';
2018-07-19 14:16:02 +00:00
/**
* the interfae for FormFieldData
*/
export interface IFormField {
name: string;
2018-08-13 23:47:54 +00:00
type: 'string' | 'filePath' | 'Buffer';
2019-10-27 13:32:27 +00:00
payload: string | Buffer;
fileName?: string;
2020-01-12 19:36:58 +00:00
contentType?: string;
2018-07-19 14:16:02 +00:00
}
2018-08-13 23:47:54 +00:00
const appendFormField = async (formDataArg: plugins.formData, formDataField: IFormField) => {
2019-10-27 13:32:27 +00:00
switch (formDataField.type) {
case 'string':
formDataArg.append(formDataField.name, formDataField.payload);
break;
case 'filePath':
if (typeof formDataField.payload !== 'string') {
2019-11-21 17:32:30 +00:00
throw new Error(
`Payload for key ${
formDataField.name
} must be of type string. Got ${typeof formDataField.payload} instead.`
);
2019-10-27 13:32:27 +00:00
}
const fileData = plugins.fs.readFileSync(
plugins.path.join(process.cwd(), formDataField.payload)
);
formDataArg.append('file', fileData, {
filename: formDataField.fileName ? formDataField.fileName : 'upload.pdf',
2020-08-24 12:01:38 +00:00
contentType: 'application/pdf',
2019-10-27 13:32:27 +00:00
});
break;
case 'Buffer':
2020-01-12 19:36:58 +00:00
formDataArg.append(formDataField.name, formDataField.payload, {
2019-10-27 13:32:27 +00:00
filename: formDataField.fileName ? formDataField.fileName : 'upload.pdf',
2020-08-24 12:01:38 +00:00
contentType: formDataField.contentType ? formDataField.contentType : 'application/pdf',
2019-10-27 13:32:27 +00:00
});
break;
2018-07-19 14:16:02 +00:00
}
};
export const postFormData = async (
urlArg: string,
optionsArg: interfaces.ISmartRequestOptions = {},
payloadArg: IFormField[]
) => {
const form = new plugins.formData();
for (const formField of payloadArg) {
await appendFormField(form, formField);
}
2019-09-29 14:42:56 +00:00
const requestOptions = {
...optionsArg,
2018-07-19 23:06:25 +00:00
method: 'POST',
headers: {
2018-08-13 23:47:54 +00:00
...optionsArg.headers,
2020-08-24 12:01:38 +00:00
...form.getHeaders(),
},
2020-08-24 12:01:38 +00:00
requestBody: form,
2019-09-29 14:42:56 +00:00
};
2018-07-19 14:16:02 +00:00
// lets fire the actual request for sending the formdata
2018-07-19 21:32:43 +00:00
const response = await request(urlArg, requestOptions);
return response;
2018-07-19 14:16:02 +00:00
};
2022-02-15 17:53:02 +00:00
export const postFormDataUrlEncoded = async (
urlArg: string,
optionsArg: interfaces.ISmartRequestOptions = {},
2022-02-15 17:57:42 +00:00
payloadArg: { key: string; content: string }[]
2022-02-15 17:53:02 +00:00
) => {
2022-02-15 17:57:42 +00:00
let resultString = '';
2022-02-15 17:53:02 +00:00
for (const keyContentPair of payloadArg) {
if (resultString) {
resultString += '&';
}
2022-02-15 17:57:42 +00:00
resultString += `${encodeURIComponent(keyContentPair.key)}=${encodeURIComponent(
keyContentPair.content
)}`;
2022-02-15 17:53:02 +00:00
}
2022-02-15 22:09:15 +00:00
const requestOptions: interfaces.ISmartRequestOptions = {
...optionsArg,
method: 'POST',
headers: {
...optionsArg.headers,
'content-type': 'application/x-www-form-urlencoded',
},
2022-07-28 23:19:50 +00:00
requestBody: resultString,
2022-02-15 22:09:15 +00:00
};
2022-02-15 17:53:02 +00:00
// lets fire the actual request for sending the formdata
2022-02-15 22:09:15 +00:00
const response = await request(urlArg, requestOptions);
2022-02-15 17:53:02 +00:00
return response;
};