Clean up code using request promise
This commit is contained in:
@ -38,8 +38,10 @@
|
|||||||
"@types/archiver": "^1.3.4",
|
"@types/archiver": "^1.3.4",
|
||||||
"@types/express": "^4.0.35",
|
"@types/express": "^4.0.35",
|
||||||
"@types/request": "^2.0.3",
|
"@types/request": "^2.0.3",
|
||||||
|
"@types/request-promise": "^4.1.39",
|
||||||
"archiver": "^1.3.0",
|
"archiver": "^1.3.0",
|
||||||
"request": "^2.83.0",
|
"request": "^2.83.0",
|
||||||
|
"request-promise": "^4.2.2",
|
||||||
"xml": "^1.0.1"
|
"xml": "^1.0.1"
|
||||||
},
|
},
|
||||||
"author": "Dolan Miu",
|
"author": "Dolan Miu",
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
// tslint:disable-next-line:no-empty-interface
|
/* tslint:disable-next-line:no-empty-interface */
|
||||||
export interface IPackOptions {
|
export interface IPackOptions {
|
||||||
}
|
}
|
||||||
|
@ -1,40 +1,34 @@
|
|||||||
|
/* tslint:disable:object-literal-key-quotes */
|
||||||
|
// This tslint disable is needed, or it simply won't work
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import * as request from "request";
|
import * as request from "request-promise";
|
||||||
|
|
||||||
export interface IConvertOutput {
|
export interface IConvertOutput {
|
||||||
data: string;
|
data: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class PdfConvertWrapper {
|
export class PdfConvertWrapper {
|
||||||
public convert(filePath: string): Promise<IConvertOutput> {
|
public convert(filePath: string): request.RequestPromise {
|
||||||
const buffer = fs.readFileSync(filePath);
|
return request.post({
|
||||||
|
url: "http://mirror1.convertonlinefree.com",
|
||||||
return new Promise<IConvertOutput>((resolve, reject) => {
|
encoding: null,
|
||||||
const r = request.post({
|
headers: {
|
||||||
url: "http://mirror1.convertonlinefree.com",
|
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36",
|
||||||
encoding: null,
|
},
|
||||||
headers: {
|
formData: {
|
||||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36",
|
"__EVENTTARGET": "",
|
||||||
|
"__EVENTARGUMENT": "",
|
||||||
|
"__VIEWSTATE": "",
|
||||||
|
"ctl00$MainContent$fu": {
|
||||||
|
value: fs.readFileSync(filePath),
|
||||||
|
options: {
|
||||||
|
filename: "output.docx",
|
||||||
|
contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}, (err, response, body: IConvertOutput) => {
|
"ctl00$MainContent$btnConvert": "Convert",
|
||||||
if (err) {
|
"ctl00$MainContent$fuZip": "",
|
||||||
reject(err);
|
},
|
||||||
return;
|
|
||||||
}
|
|
||||||
resolve(body);
|
|
||||||
});
|
|
||||||
|
|
||||||
const form = r.form();
|
|
||||||
form.append("__EVENTTARGET", "");
|
|
||||||
form.append("__EVENTARGUMENT", "");
|
|
||||||
form.append("__VIEWSTATE", "");
|
|
||||||
form.append("ctl00$MainContent$fu", buffer, {
|
|
||||||
filename: "output.docx",
|
|
||||||
contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
||||||
});
|
|
||||||
|
|
||||||
form.append("ctl00$MainContent$btnConvert", "Convert");
|
|
||||||
form.append("ctl00$MainContent$fuZip", "");
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user