Compare commits

...

10 Commits

Author SHA1 Message Date
bda04f124b 1.1.37 2019-10-27 14:32:27 +01:00
466187fd52 fix(core): update 2019-10-27 14:32:27 +01:00
d22504317e 1.1.36 2019-09-29 16:42:56 +02:00
6e31d84798 fix(core): update 2019-09-29 16:42:56 +02:00
36472b7306 1.1.35 2019-09-29 00:56:56 +02:00
e86f14b8d8 fix(core): update 2019-09-29 00:56:56 +02:00
2b9e7f6dd2 1.1.34 2019-09-29 00:43:37 +02:00
5e4afccf9c fix(core): update 2019-09-29 00:43:37 +02:00
3de7a1a210 1.1.33 2019-09-29 00:42:51 +02:00
bd2a5eedff fix(core): update 2019-09-29 00:42:51 +02:00
7 changed files with 41 additions and 21 deletions

View File

@ -54,7 +54,7 @@ testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install lts
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
@ -65,7 +65,7 @@ testBuild:
release:
stage: release
script:
- npmci node install lts
- npmci node install stable
- npmci npm publish
only:
- tags
@ -81,6 +81,7 @@ codequality:
allow_failure: true
script:
- npmci command npm install -g tslint typescript
- npmci npm prepare
- npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags:

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartrequest",
"version": "1.1.32",
"version": "1.1.37",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartrequest",
"version": "1.1.32",
"version": "1.1.37",
"private": false,
"description": "dropin replacement for request",
"main": "dist/index.js",

View File

@ -21,7 +21,7 @@ tap.test('should request a JSON document over https', async () => {
.equal(1);
});
tap.test('should post a JSON document over http', async () => {
tap.skip.test('should post a JSON document over http', async () => {
await expect(smartrequest.postJson('http://md5.jsontest.com/?text=example_text'))
.to.eventually.property('body')
.property('md5')

View File

@ -8,16 +8,33 @@ import { request } from './smartrequest.request';
export interface IFormField {
name: string;
type: 'string' | 'filePath' | 'Buffer';
payload: string;
payload: string | Buffer;
fileName?: string;
}
const appendFormField = async (formDataArg: plugins.formData, formDataField: IFormField) => {
if (formDataField.type === 'filePath') {
let fileData = plugins.fs.readFileSync(plugins.path.join(process.cwd(), formDataField.payload));
formDataArg.append('file', fileData, {
filename: 'upload.pdf',
contentType: 'application/pdf'
});
switch (formDataField.type) {
case 'string':
formDataArg.append(formDataField.name, formDataField.payload);
break;
case 'filePath':
if (typeof formDataField.payload !== 'string') {
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;
}
};
@ -30,14 +47,15 @@ export const postFormData = async (
for (const formField of payloadArg) {
await appendFormField(form, formField);
}
const requestOptions = Object.assign({}, optionsArg, {
const requestOptions = {
...optionsArg,
method: 'POST',
headers: {
...optionsArg.headers,
...form.getHeaders()
},
requestBody: form
});
};
// lets fire the actual request for sending the formdata
const response = await request(urlArg, requestOptions);

View File

@ -12,6 +12,4 @@ export { formData, http, https, fs, path, url, smartpromise };
// third party scope
import * as agentkeepalive from 'agentkeepalive';
export {
agentkeepalive
};
export { agentkeepalive };

View File

@ -85,7 +85,8 @@ const httpsAgentKeepAliveFalse = new plugins.https.Agent({
export let request = async (
domainArg: string,
optionsArg: interfaces.ISmartRequestOptions = {},
streamArg: boolean = false
responseStreamArg: boolean = false,
requestDataFunc: (req: plugins.http.ClientRequest) => void = null
): Promise<IExtendedIncomingMessage> => {
const done = plugins.smartpromise.defer<any>();
@ -124,13 +125,13 @@ export let request = async (
case parsedUrl.protocol === 'https:' && optionsArg.keepAlive:
optionsArg.agent = httpsAgent;
return plugins.https;
case parsedUrl.protocol === 'https:' && (!optionsArg.keepAlive):
case parsedUrl.protocol === 'https:' && !optionsArg.keepAlive:
optionsArg.agent = httpsAgentKeepAliveFalse;
return plugins.https;
case parsedUrl.protocol === 'http:' && optionsArg.keepAlive:
optionsArg.agent = httpAgent;
return plugins.http;
case parsedUrl.protocol === 'http:' && (!optionsArg.keepAlive):
case parsedUrl.protocol === 'http:' && !optionsArg.keepAlive:
optionsArg.agent = httpAgentKeepAliveFalse;
return plugins.http;
}
@ -138,7 +139,7 @@ export let request = async (
// lets perform the actual request
const requestToFire = requestModule.request(optionsArg, async response => {
if (streamArg) {
if (responseStreamArg) {
done.resolve(response);
} else {
const builtResponse = await buildUtf8Response(response, optionsArg.autoJsonParse);
@ -159,6 +160,8 @@ export let request = async (
requestToFire.write(optionsArg.requestBody);
requestToFire.end();
}
} else if (requestDataFunc) {
requestDataFunc(requestToFire);
} else {
requestToFire.end();
}