add formData capability
This commit is contained in:
62
ts/smartrequest.formdata.ts
Normal file
62
ts/smartrequest.formdata.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import * as plugins from "./smartrequest.plugins";
|
||||
import * as interfaces from "./smartrequest.interfaces";
|
||||
import { request } from "./smartrequest.request";
|
||||
|
||||
/**
|
||||
* the interfae for FormFieldData
|
||||
*/
|
||||
export interface IFormField {
|
||||
name: string;
|
||||
type: "string" | "filePath" | "Buffer";
|
||||
payload: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve the FormData headers in reliable way
|
||||
* @param formDataArg
|
||||
*/
|
||||
const getFormDataHeaders = (formDataArg: plugins.formData) => {
|
||||
const done = plugins.smartpromise.defer();
|
||||
formDataArg.getLength((err, length) => {
|
||||
if (err) {
|
||||
done.reject(err);
|
||||
}
|
||||
const headers = Object.assign(
|
||||
{ "Content-Length": length },
|
||||
formDataArg.getHeaders()
|
||||
);
|
||||
done.resolve(headers);
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
const appendFormField = async (
|
||||
formDataArg: plugins.formData,
|
||||
formDataField: IFormField
|
||||
) => {
|
||||
if (formDataField.type === "filePath") {
|
||||
formDataArg.append("type", "image");
|
||||
let fileData = plugins.fs.createReadStream(
|
||||
plugins.path.join(__dirname, formDataField.payload)
|
||||
);
|
||||
formDataArg.append("media", fileData, "upload.pdf");
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
const pipeLog: any = async (...args) => {
|
||||
console.log(args);
|
||||
};
|
||||
const requestOptions = Object.assign({}, optionsArg, { requestBody: form });
|
||||
|
||||
// lets fire the actual request for sending the formdata
|
||||
request(urlArg, requestOptions);
|
||||
};
|
@ -1,7 +1,10 @@
|
||||
import * as url from 'url';
|
||||
import * as formData from 'form-data';
|
||||
import * as fs from 'fs';
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
import * as path from 'path';
|
||||
import * as url from 'url';
|
||||
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
|
||||
export { url, http, https, smartpromise };
|
||||
export {formData, http, https, fs, path, url, smartpromise };
|
||||
|
@ -86,7 +86,28 @@ export let request = async (
|
||||
|
||||
|
||||
// lets perform the actual request
|
||||
let request = requestModule.request(optionsArg, async response => {
|
||||
let request = requestModule.request(optionsArg);
|
||||
|
||||
// lets write the requestBody
|
||||
if (optionsArg.requestBody) {
|
||||
if (
|
||||
typeof optionsArg.requestBody !== "string"
|
||||
&& !(optionsArg.requestBody instanceof plugins.formData)
|
||||
) {
|
||||
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
|
||||
request.write(optionsArg.requestBody);
|
||||
} else if (optionsArg.requestBody instanceof plugins.formData) {
|
||||
optionsArg.requestBody.pipe(request);
|
||||
}
|
||||
}
|
||||
|
||||
// lets handle an error
|
||||
request.on("error", e => {
|
||||
console.error(e);
|
||||
});
|
||||
|
||||
// lets handle the response
|
||||
request.on("response", async response => {
|
||||
if (streamArg) {
|
||||
done.resolve(response);
|
||||
} else {
|
||||
@ -95,16 +116,6 @@ export let request = async (
|
||||
}
|
||||
});
|
||||
|
||||
// lets write the requestBody
|
||||
if (optionsArg.requestBody) {
|
||||
if (typeof optionsArg.requestBody !== "string") {
|
||||
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
|
||||
}
|
||||
request.write(optionsArg.requestBody);
|
||||
}
|
||||
request.on("error", e => {
|
||||
console.error(e);
|
||||
});
|
||||
request.end();
|
||||
|
||||
const result = await done.promise;
|
||||
|
Reference in New Issue
Block a user