Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
acebe6a381 | |||
7031504852 | |||
3010a1da9a | |||
cdead33be4 | |||
5e23649702 | |||
cc6bd5726a | |||
f487584e80 | |||
443bccd4c9 | |||
f359856b1d | |||
bda04f124b | |||
466187fd52 | |||
d22504317e | |||
6e31d84798 | |||
36472b7306 | |||
e86f14b8d8 | |||
2b9e7f6dd2 | |||
5e4afccf9c |
@ -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:
|
||||||
|
32
README.md
32
README.md
@ -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
2
package-lock.json
generated
@ -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": {
|
||||||
|
@ -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",
|
||||||
|
@ -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);
|
||||||
|
@ -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
|
|
||||||
};
|
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user