initial
This commit is contained in:
parent
ea3539b39e
commit
f50403467f
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
node_modules/
|
||||
coverage/
|
||||
public/
|
||||
pages/
|
7
dist/index.d.ts
vendored
Normal file
7
dist/index.d.ts
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
import * as interfaces from './smartrequest.interfaces';
|
||||
import { request } from './smartrequest.request';
|
||||
export { request };
|
||||
export declare let get: (domainArg: string, optionsArg?: interfaces.SmartRequestOptions) => Promise<{}>;
|
||||
export declare let post: (domainArg: string, optionsArg?: interfaces.SmartRequestOptions) => Promise<{}>;
|
||||
export declare let put: (domainArg: string, optionsArg?: interfaces.SmartRequestOptions) => Promise<{}>;
|
||||
export declare let del: (domainArg: string, optionsArg?: interfaces.SmartRequestOptions) => Promise<{}>;
|
32
dist/index.js
vendored
Normal file
32
dist/index.js
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
const smartrequest_request_1 = require("./smartrequest.request");
|
||||
exports.request = smartrequest_request_1.request;
|
||||
exports.get = (domainArg, optionsArg = {}) => __awaiter(this, void 0, void 0, function* () {
|
||||
optionsArg.method = 'GET';
|
||||
let response = yield smartrequest_request_1.request(domainArg, optionsArg);
|
||||
return response;
|
||||
});
|
||||
exports.post = (domainArg, optionsArg = {}) => __awaiter(this, void 0, void 0, function* () {
|
||||
optionsArg.method = 'POST';
|
||||
let response = yield smartrequest_request_1.request(domainArg, optionsArg);
|
||||
return response;
|
||||
});
|
||||
exports.put = (domainArg, optionsArg = {}) => __awaiter(this, void 0, void 0, function* () {
|
||||
optionsArg.method = 'PUT';
|
||||
let response = yield smartrequest_request_1.request(domainArg, optionsArg);
|
||||
return response;
|
||||
});
|
||||
exports.del = (domainArg, optionsArg = {}) => __awaiter(this, void 0, void 0, function* () {
|
||||
optionsArg.method = 'DELETE';
|
||||
let response = yield smartrequest_request_1.request(domainArg, optionsArg);
|
||||
return response;
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7QUFLQSxpRUFBZ0Q7QUFJNUMsaURBQU87QUFHQSxRQUFBLEdBQUcsR0FBRyxDQUFPLFNBQWlCLEVBQUUsYUFBNkMsRUFBRTtJQUN0RixVQUFVLENBQUMsTUFBTSxHQUFHLEtBQUssQ0FBQTtJQUN6QixJQUFJLFFBQVEsR0FBRyxNQUFNLDhCQUFPLENBQUMsU0FBUyxFQUFFLFVBQVUsQ0FBQyxDQUFBO0lBQ25ELE1BQU0sQ0FBQyxRQUFRLENBQUE7QUFDbkIsQ0FBQyxDQUFBLENBQUE7QUFFVSxRQUFBLElBQUksR0FBRyxDQUFPLFNBQWlCLEVBQUUsYUFBNkMsRUFBRTtJQUN2RixVQUFVLENBQUMsTUFBTSxHQUFHLE1BQU0sQ0FBQTtJQUMxQixJQUFJLFFBQVEsR0FBRyxNQUFNLDhCQUFPLENBQUMsU0FBUyxFQUFFLFVBQVUsQ0FBQyxDQUFBO0lBQ25ELE1BQU0sQ0FBQyxRQUFRLENBQUE7QUFDbkIsQ0FBQyxDQUFBLENBQUE7QUFFVSxRQUFBLEdBQUcsR0FBRyxDQUFPLFNBQWlCLEVBQUUsYUFBNkMsRUFBRTtJQUN0RixVQUFVLENBQUMsTUFBTSxHQUFHLEtBQUssQ0FBQTtJQUN6QixJQUFJLFFBQVEsR0FBRyxNQUFNLDhCQUFPLENBQUMsU0FBUyxFQUFFLFVBQVUsQ0FBQyxDQUFBO0lBQ25ELE1BQU0sQ0FBQyxRQUFRLENBQUE7QUFDbkIsQ0FBQyxDQUFBLENBQUE7QUFFVSxRQUFBLEdBQUcsR0FBRyxDQUFPLFNBQWlCLEVBQUUsYUFBNkMsRUFBRTtJQUN0RixVQUFVLENBQUMsTUFBTSxHQUFHLFFBQVEsQ0FBQTtJQUM1QixJQUFJLFFBQVEsR0FBRyxNQUFNLDhCQUFPLENBQUMsU0FBUyxFQUFFLFVBQVUsQ0FBQyxDQUFBO0lBQ25ELE1BQU0sQ0FBQyxRQUFRLENBQUE7QUFDbkIsQ0FBQyxDQUFBLENBQUEifQ==
|
4
dist/smartrequest.interfaces.d.ts
vendored
Normal file
4
dist/smartrequest.interfaces.d.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
import * as https from 'https';
|
||||
export interface SmartRequestOptions extends https.RequestOptions {
|
||||
requestBody?: any;
|
||||
}
|
2
dist/smartrequest.interfaces.js
vendored
Normal file
2
dist/smartrequest.interfaces.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
"use strict";
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRyZXF1ZXN0LmludGVyZmFjZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHJlcXVlc3QuaW50ZXJmYWNlcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0=
|
6
dist/smartrequest.plugins.d.ts
vendored
Normal file
6
dist/smartrequest.plugins.d.ts
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
import 'typings-global';
|
||||
import * as url from 'url';
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
import * as q from 'smartq';
|
||||
export { url, http, https, q };
|
11
dist/smartrequest.plugins.js
vendored
Normal file
11
dist/smartrequest.plugins.js
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
require("typings-global");
|
||||
const url = require("url");
|
||||
exports.url = url;
|
||||
const http = require("http");
|
||||
exports.http = http;
|
||||
const https = require("https");
|
||||
exports.https = https;
|
||||
const q = require("smartq");
|
||||
exports.q = q;
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRyZXF1ZXN0LnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHJlcXVlc3QucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBQ3ZCLDJCQUEwQjtBQU90QixrQkFBRztBQU5QLDZCQUE0QjtBQU94QixvQkFBSTtBQU5SLCtCQUE4QjtBQU8xQixzQkFBSztBQUxULDRCQUEyQjtBQU12QixjQUFDIn0=
|
2
dist/smartrequest.request.d.ts
vendored
Normal file
2
dist/smartrequest.request.d.ts
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import * as interfaces from './smartrequest.interfaces';
|
||||
export declare let request: (domainArg: string, optionsArg?: interfaces.SmartRequestOptions, streamArg?: boolean) => Promise<{}>;
|
85
dist/smartrequest.request.js
vendored
Normal file
85
dist/smartrequest.request.js
vendored
Normal file
@ -0,0 +1,85 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
const plugins = require("./smartrequest.plugins");
|
||||
let buildResponse = (responseArg) => {
|
||||
let done = plugins.q.defer();
|
||||
// Continuously update stream with data
|
||||
let body = '';
|
||||
responseArg.on('data', function (chunkArg) {
|
||||
body += chunkArg;
|
||||
});
|
||||
responseArg.on('end', function () {
|
||||
try {
|
||||
responseArg.body = JSON.parse(body);
|
||||
}
|
||||
catch (err) {
|
||||
responseArg.body = body;
|
||||
}
|
||||
done.resolve(responseArg);
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
exports.request = (domainArg, optionsArg = {}, streamArg = false) => __awaiter(this, void 0, void 0, function* () {
|
||||
let done = plugins.q.defer();
|
||||
let parsedUrl;
|
||||
if (domainArg) {
|
||||
parsedUrl = plugins.url.parse(domainArg);
|
||||
optionsArg.hostname = parsedUrl.hostname;
|
||||
if (parsedUrl.port) {
|
||||
optionsArg.port = parseInt(parsedUrl.port);
|
||||
}
|
||||
optionsArg.path = parsedUrl.path;
|
||||
}
|
||||
if (!parsedUrl || parsedUrl.protocol === 'https:') {
|
||||
let request = plugins.https.request(optionsArg, response => {
|
||||
if (streamArg) {
|
||||
done.resolve(response);
|
||||
}
|
||||
else {
|
||||
buildResponse(response).then(done.resolve);
|
||||
}
|
||||
});
|
||||
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();
|
||||
}
|
||||
else if (parsedUrl.protocol === 'http:') {
|
||||
let request = plugins.http.request(optionsArg, response => {
|
||||
if (streamArg) {
|
||||
done.resolve(response);
|
||||
}
|
||||
else {
|
||||
buildResponse(response).then(done.resolve);
|
||||
}
|
||||
});
|
||||
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();
|
||||
}
|
||||
else {
|
||||
throw new Error(`unsupported protocol: ${parsedUrl.protocol}`);
|
||||
}
|
||||
return done.promise;
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRyZXF1ZXN0LnJlcXVlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHJlcXVlc3QucmVxdWVzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7QUFDQSxrREFBaUQ7QUFHakQsSUFBSSxhQUFhLEdBQUcsQ0FBQyxXQUFXO0lBQzVCLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUE7SUFDNUIsdUNBQXVDO0lBQ3ZDLElBQUksSUFBSSxHQUFHLEVBQUUsQ0FBQztJQUNkLFdBQVcsQ0FBQyxFQUFFLENBQUMsTUFBTSxFQUFFLFVBQVUsUUFBUTtRQUNyQyxJQUFJLElBQUksUUFBUSxDQUFDO0lBQ3JCLENBQUMsQ0FBQyxDQUFDO0lBQ0gsV0FBVyxDQUFDLEVBQUUsQ0FBQyxLQUFLLEVBQUU7UUFDbEIsSUFBSSxDQUFDO1lBQ0QsV0FBVyxDQUFDLElBQUksR0FBRyxJQUFJLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxDQUFDO1FBQ3hDLENBQUM7UUFBQyxLQUFLLENBQUEsQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDO1lBQ1YsV0FBVyxDQUFDLElBQUksR0FBRyxJQUFJLENBQUE7UUFDM0IsQ0FBQztRQUNELElBQUksQ0FBQyxPQUFPLENBQUMsV0FBVyxDQUFDLENBQUE7SUFDN0IsQ0FBQyxDQUFDLEN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MsQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxDQUFBO1lBQzlDLENBQUM7UUFDTCxDQUFDLENBQUMsQ0FBQTtRQUNGLEVBQUUsQ0FBQyxDQUFDLFVBQVUsQ0FBQyxXQUFXLENBQUMsQ0FBQyxDQUFDO1lBQ3pCLEVBQUUsQ0FBQSxDQUFDLE9BQU8sVUFBVSxDQUFDLFdBQVcsS0FBSyxRQUFRLENBQUMsQ0FBQyxDQUFDO2dCQUM1QyxVQUFVLENBQUMsV0FBVyxHQUFHLElBQUksQ0FBQyxTQUFTLENBQUMsVUFBVSxDQUFDLFdBQVcsQ0FBQyxDQUFBO1lBQ25FLENBQUM7WUFDRCxPQUFPLENBQUMsS0FBSyxDQUFDLFVBQVUsQ0FBQyxXQUFXLENBQUMsQ0FBQTtRQUN6QyxDQUFDO1FBQ0QsT0FBTyxDQUFDLEVBQUUsQ0FBQyxPQUFPLEVBQUUsQ0FBQyxDQUFDO1lBQ2xCLE9BQU8sQ0FBQyxLQUFLLENBQUMsQ0FBQyxDQUFDLENBQUM7UUFDckIsQ0FBQyxDQUFDLENBQUE7UUFDRixPQUFPLENBQUMsR0FBRyxFQUFFLENBQUE7SUFDakIsQ0FBQztJQUFDLElBQUksQ0FBQyxDQUFDO1FBQ0osTUFBTSxJQUFJLEtBQUssQ0FBQyx5QkFBeUIsU0FBUyxDQUFDLFFBQVEsRUFBRSxDQUFDLENBQUE7SUFDbEUsQ0FBQztJQUNELE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFBO0FBQ3ZCLENBQUMsQ0FBQSxDQUFBIn0=
|
11
package.json
11
package.json
@ -3,6 +3,7 @@
|
||||
"version": "1.0.0",
|
||||
"description": "dropin replacement for request",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "(npmts)"
|
||||
},
|
||||
@ -18,5 +19,13 @@
|
||||
"bugs": {
|
||||
"url": "https://gitlab.com/pushrocks/smartrequest/issues"
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartrequest#README"
|
||||
"homepage": "https://gitlab.com/pushrocks/smartrequest#README",
|
||||
"dependencies": {
|
||||
"smartq": "^1.1.0",
|
||||
"typings-global": "^1.0.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"smartchai": "^1.0.1",
|
||||
"typings-test": "^1.0.3"
|
||||
}
|
||||
}
|
||||
|
1
test/test.d.ts
vendored
Normal file
1
test/test.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
import 'typings-test';
|
18
test/test.js
Normal file
18
test/test.js
Normal file
@ -0,0 +1,18 @@
|
||||
"use strict";
|
||||
require("typings-test");
|
||||
const smartchai_1 = require("smartchai");
|
||||
const smartrequest = require("../dist/index");
|
||||
describe('smartrequest', function () {
|
||||
it('should request a html document over https', function () {
|
||||
this.timeout(10000);
|
||||
return smartchai_1.expect(smartrequest.get('https://encrypted.google.com/')).to.eventually.property('body').be.a('string');
|
||||
});
|
||||
it('should request a JSON document over https', function () {
|
||||
return smartchai_1.expect(smartrequest.get('https://jsonplaceholder.typicode.com/posts/1')).to.eventually.property('body').property('id').equal(1);
|
||||
});
|
||||
it('should post a JSON document over http', function () {
|
||||
this.timeout(5000);
|
||||
return smartchai_1.expect(smartrequest.post('http://md5.jsontest.com/?text=example_text')).to.eventually.property('body').property('md5').equal('fa4c6baa0812e5b5c80ed8885e55a8a6');
|
||||
});
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUVyQix5Q0FBa0M7QUFFbEMsOENBQTZDO0FBRTdDLFFBQVEsQ0FBQyxjQUFjLEVBQUU7SUFDckIsRUFBRSxDQUFDLDJDQUEyQyxFQUFFO1FBQzVDLElBQUksQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUE7UUFDbkIsTUFBTSxDQUFDLGtCQUFNLENBQUMsWUFBWSxDQUFDLEdBQUcsQ0FBQywrQkFBK0IsQ0FBQyxDQUFDLENBQUMsRUFBRSxDQUFDLFVBQVUsQ0FBQyxRQUFRLENBQUMsTUFBTSxDQUFDLENBQUMsRUFBRSxDQUFDLENBQUMsQ0FBQyxRQUFRLENBQUMsQ0FBQTtJQUNsSCxDQUFDLENBQUMsQ0FBQTtJQUVGLEVBQUUsQ0FBQywyQ0FBMkMsRUFBRTtRQUM1QyxNQUFNLENBQUMsa0JBQU0sQ0FBQyxZQUFZLENBQUMsR0FBRyxDQUFDLDhDQUE4QyxDQUFDLENBQUMsQ0FBQyxFQUFFLENBQUMsVUFBVSxDQUFDLFFBQVEsQ0FBQyxNQUFNLENBQUMsQ0FBQyxRQUFRLENBQUMsSUFBSSxDQUFDLENBQUMsS0FBSyxDQUFDLENBQUMsQ0FBQyxDQUFBO0lBQzFJLENBQUMsQ0FBQyxDQUFBO0lBRUYsRUFBRSxDQUFDLHVDQUF1QyxFQUFFO1FBQ3hDLElBQUksQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUE7UUFDbEIsTUFBTSxDQUFDLGtCQUFNLENBQUMsWUFBWSxDQUFDLElBQUksQ0FBQyw0Q0FBNEMsQ0FBQyxDQUFDLENBQUMsRUFBRSxDQUFDLFVBQVUsQ0FBQyxRQUFRLENBQUMsTUFBTSxDQUFDLENBQUMsUUFBUSxDQUFDLEtBQUssQ0FBQyxDQUFDLEtBQUssQ0FBQyxrQ0FBa0MsQ0FBQyxDQUFBO0lBQzNLLENBQUMsQ0FBQyxDQUFBO0FBQ04sQ0FBQyxDQUFDLENBQUEifQ==
|
21
test/test.ts
Normal file
21
test/test.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import 'typings-test'
|
||||
|
||||
import { expect } from 'smartchai'
|
||||
|
||||
import * as smartrequest from '../dist/index'
|
||||
|
||||
describe('smartrequest', function() {
|
||||
it('should request a html document over https', function() {
|
||||
this.timeout(10000)
|
||||
return expect(smartrequest.get('https://encrypted.google.com/')).to.eventually.property('body').be.a('string')
|
||||
})
|
||||
|
||||
it('should request a JSON document over https', function(){
|
||||
return expect(smartrequest.get('https://jsonplaceholder.typicode.com/posts/1')).to.eventually.property('body').property('id').equal(1)
|
||||
})
|
||||
|
||||
it('should post a JSON document over http', function() {
|
||||
this.timeout(5000)
|
||||
return expect(smartrequest.post('http://md5.jsontest.com/?text=example_text')).to.eventually.property('body').property('md5').equal('fa4c6baa0812e5b5c80ed8885e55a8a6')
|
||||
})
|
||||
})
|
35
ts/index.ts
Normal file
35
ts/index.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import * as https from 'https'
|
||||
|
||||
import * as plugins from './smartrequest.plugins'
|
||||
import * as interfaces from './smartrequest.interfaces'
|
||||
|
||||
import { request } from './smartrequest.request'
|
||||
|
||||
|
||||
export {
|
||||
request
|
||||
}
|
||||
|
||||
export let get = async (domainArg: string, optionsArg: interfaces.SmartRequestOptions = {}) => {
|
||||
optionsArg.method = 'GET'
|
||||
let response = await request(domainArg, optionsArg)
|
||||
return response
|
||||
}
|
||||
|
||||
export let post = async (domainArg: string, optionsArg: interfaces.SmartRequestOptions = {}) => {
|
||||
optionsArg.method = 'POST'
|
||||
let response = await request(domainArg, optionsArg)
|
||||
return response
|
||||
}
|
||||
|
||||
export let put = async (domainArg: string, optionsArg: interfaces.SmartRequestOptions = {}) => {
|
||||
optionsArg.method = 'PUT'
|
||||
let response = await request(domainArg, optionsArg)
|
||||
return response
|
||||
}
|
||||
|
||||
export let del = async (domainArg: string, optionsArg: interfaces.SmartRequestOptions = {}) => {
|
||||
optionsArg.method = 'DELETE'
|
||||
let response = await request(domainArg, optionsArg)
|
||||
return response
|
||||
}
|
6
ts/smartrequest.interfaces.ts
Normal file
6
ts/smartrequest.interfaces.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import * as plugins from './smartrequest.plugins'
|
||||
import * as https from 'https'
|
||||
|
||||
export interface SmartRequestOptions extends https.RequestOptions {
|
||||
requestBody?: any
|
||||
}
|
13
ts/smartrequest.plugins.ts
Normal file
13
ts/smartrequest.plugins.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import 'typings-global'
|
||||
import * as url from 'url'
|
||||
import * as http from 'http'
|
||||
import * as https from 'https'
|
||||
|
||||
import * as q from 'smartq'
|
||||
|
||||
export {
|
||||
url,
|
||||
http,
|
||||
https,
|
||||
q
|
||||
}
|
72
ts/smartrequest.request.ts
Normal file
72
ts/smartrequest.request.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import * as https from 'https'
|
||||
import * as plugins from './smartrequest.plugins'
|
||||
import * as interfaces from './smartrequest.interfaces'
|
||||
|
||||
let buildResponse = (responseArg): Promise<any> => {
|
||||
let done = plugins.q.defer()
|
||||
// Continuously update stream with data
|
||||
let body = '';
|
||||
responseArg.on('data', function (chunkArg) {
|
||||
body += chunkArg;
|
||||
});
|
||||
responseArg.on('end', function () {
|
||||
try {
|
||||
responseArg.body = JSON.parse(body);
|
||||
} catch(err) {
|
||||
responseArg.body = body
|
||||
}
|
||||
done.resolve(responseArg)
|
||||
});
|
||||
return done.promise
|
||||
}
|
||||
|
||||
export let request = async (domainArg: string, optionsArg: interfaces.SmartRequestOptions = {}, streamArg: boolean = false) => {
|
||||
let done = plugins.q.defer()
|
||||
let parsedUrl: plugins.url.Url
|
||||
if (domainArg) {
|
||||
parsedUrl = plugins.url.parse(domainArg)
|
||||
optionsArg.hostname = parsedUrl.hostname
|
||||
if(parsedUrl.port) { optionsArg.port = parseInt(parsedUrl.port) }
|
||||
optionsArg.path = parsedUrl.path
|
||||
}
|
||||
if (!parsedUrl || parsedUrl.protocol === 'https:') {
|
||||
let request = plugins.https.request(optionsArg, response => {
|
||||
if (streamArg) {
|
||||
done.resolve(response)
|
||||
} else {
|
||||
buildResponse(response).then(done.resolve)
|
||||
}
|
||||
})
|
||||
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()
|
||||
} else if (parsedUrl.protocol === 'http:') {
|
||||
let request = plugins.http.request(optionsArg, response => {
|
||||
if (streamArg) {
|
||||
done.resolve(response)
|
||||
} else {
|
||||
buildResponse(response).then(done.resolve)
|
||||
}
|
||||
})
|
||||
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()
|
||||
} else {
|
||||
throw new Error(`unsupported protocol: ${parsedUrl.protocol}`)
|
||||
}
|
||||
return done.promise
|
||||
}
|
Loading…
Reference in New Issue
Block a user