Compare commits

...

17 Commits

Author SHA1 Message Date
acebe6a381 1.1.42 2019-10-28 16:18:15 +01:00
7031504852 1.1.41 2019-10-27 14:41:49 +01:00
3010a1da9a fix(core): update 2019-10-27 14:41:48 +01:00
cdead33be4 1.1.40 2019-10-27 14:39:07 +01:00
5e23649702 fix(core): update 2019-10-27 14:39:06 +01:00
cc6bd5726a 1.1.39 2019-10-27 14:38:13 +01:00
f487584e80 fix(core): update 2019-10-27 14:38:12 +01:00
443bccd4c9 1.1.38 2019-10-27 14:36:18 +01:00
f359856b1d fix(core): update 2019-10-27 14:36:17 +01:00
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
7 changed files with 64 additions and 28 deletions

View File

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

View File

@ -20,7 +20,22 @@ dropin replacement for request
Use TypeScript for best in class instellisense. Use TypeScript for best in class instellisense.
> note: smartrequest uses the **native** node request module under the hood (not the one from npm) ### Features
* supports http
* supports https
* supports unix socks
* supports formData
* supports file uploads
* supports best practice keepAlive
* dedicated functions for working with JSON request/response cycles
* written in TypeScript
* continuously updated
* uses node native http and https modules
* used in modules like @pushrocks/smartproxy and @apiglobal/typedrequest
* commercial support available at [https://lossless.support](https://lossless.support)
> note: smartrequest uses the **native** node http/https modules under the hood (not the bloated one from npm)
```javascript ```javascript
import * as smartrequest from 'smartrequest' import * as smartrequest from 'smartrequest'
@ -31,23 +46,24 @@ let options: smartrequest.ISmartRequestOptions = { // typed options
"Content-Type": "application/json" "Content-Type": "application/json"
"Authorization": "Bearer token" "Authorization": "Bearer token"
}, },
requestBody: { requestBody: JSON.stringify({
key1: 'value1', key1: 'value1',
key2: 3 key2: 3
} })
} }
smartrequest.post('https://example.com', options).then(res => { smartrequest.request('https://example.com', options).then(res => {
console.log(res.status) console.log(res.status)
console.log(res.body) // if json, body will be parsed automatically console.log(res.body) // if json, body will be parsed automatically
}).catch(err => { }).catch(err => {
console.log(err) console.log(err)
}) })
// also available // dedicated JSON metods are available:
smartrequest.get(...) smartrequest.getJson(...)
smartrequest.put(...) smartrequest.postJson(...)
smartrequest.del(...) smartrequest.putJson(...)
smartrequest.delJson(...)
// streaming // streaming
smartrequest.get('https://example.com/bigfile.mp4', optionsArg, true).then(res => { // third arg = true signals streaming smartrequest.get('https://example.com/bigfile.mp4', optionsArg, true).then(res => { // third arg = true signals streaming

2
package-lock.json generated
View File

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

View File

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

View File

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

View File

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

View File

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