From 93595a222bccb7c6a7a41a628cdb0bc46ecaba80 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Mon, 18 Jun 2018 07:39:42 +0200 Subject: [PATCH] feat(scope) switch to pushrocks scope --- dist/index.d.ts | 9 --- dist/index.js | 63 -------------------- dist/smartrequest.interfaces.d.ts | 5 -- dist/smartrequest.interfaces.js | 3 - dist/smartrequest.plugins.d.ts | 5 -- dist/smartrequest.plugins.js | 11 ---- dist/smartrequest.request.d.ts | 7 --- dist/smartrequest.request.js | 87 ---------------------------- package-lock.json | 96 ++++++++++++++++++++++++++++++- package.json | 7 ++- ts/index.ts | 65 +-------------------- ts/smartrequest.binary.ts | 24 ++++++++ ts/smartrequest.json.ts | 52 +++++++++++++++++ 13 files changed, 177 insertions(+), 257 deletions(-) delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/smartrequest.interfaces.d.ts delete mode 100644 dist/smartrequest.interfaces.js delete mode 100644 dist/smartrequest.plugins.d.ts delete mode 100644 dist/smartrequest.plugins.js delete mode 100644 dist/smartrequest.request.d.ts delete mode 100644 dist/smartrequest.request.js create mode 100644 ts/smartrequest.binary.ts create mode 100644 ts/smartrequest.json.ts diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 059ef5b..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import * as interfaces from './smartrequest.interfaces'; -import { extendedIncomingMessage } from './smartrequest.request'; -export { request, extendedIncomingMessage } from './smartrequest.request'; -export { ISmartRequestOptions } from './smartrequest.interfaces'; -export declare let get: (domainArg: string, optionsArg?: interfaces.ISmartRequestOptions) => Promise; -export declare let getBinary: (domainArg: string, optionsArg?: interfaces.ISmartRequestOptions) => Promise; -export declare let post: (domainArg: string, optionsArg?: interfaces.ISmartRequestOptions) => Promise; -export declare let put: (domainArg: string, optionsArg?: interfaces.ISmartRequestOptions) => Promise; -export declare let del: (domainArg: string, optionsArg?: interfaces.ISmartRequestOptions) => Promise; diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index de57a57..0000000 --- a/dist/index.js +++ /dev/null @@ -1,63 +0,0 @@ -"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()); - }); -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const plugins = require("./smartrequest.plugins"); -const smartrequest_request_1 = require("./smartrequest.request"); -var smartrequest_request_2 = require("./smartrequest.request"); -exports.request = smartrequest_request_2.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.getBinary = (domainArg, optionsArg = {}) => __awaiter(this, void 0, void 0, function* () { - const done = plugins.smartq.defer(); - const response = yield smartrequest_request_1.request(domainArg, optionsArg, true); - var data = []; - response.on('data', function (chunk) { - data.push(chunk); - }).on('end', function () { - //at this point data is an array of Buffers - //so Buffer.concat() can make us a new Buffer - //of all of them together - const buffer = Buffer.concat(data); - response.body = buffer.toString('binary'); - done.resolve(); - }); - yield done.promise; - return response; -}); -exports.post = (domainArg, optionsArg = {}) => __awaiter(this, void 0, void 0, function* () { - optionsArg.method = 'POST'; - if (typeof optionsArg.requestBody === 'object' && - (!optionsArg.headers || !optionsArg.headers['Content-Type'])) { - // make sure headers exist - if (!optionsArg.headers) { - optionsArg.headers = {}; - } - // assign the right Content-Type, leaving all other headers in place - Object.assign(optionsArg.headers, { - 'Content-Type': 'application/json' - }); - } - 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBRUEsa0RBQWtEO0FBR2xELGlFQUEwRTtBQUUxRSwrREFBMEU7QUFBakUseUNBQUEsT0FBTyxDQUFBO0FBR0wsUUFBQSxHQUFHLEdBQUcsQ0FBTyxTQUFpQixFQUFFLGFBQThDLEVBQUUsRUFBRSxFQUFFO0lBQzdGLFVBQVUsQ0FBQyxNQUFNLEdBQUcsS0FBSyxDQUFDO0lBQzFCLElBQUksUUFBUSxHQUFHLE1BQU0sOEJBQU8sQ0FBQyxTQUFTLEVBQUUsVUFBVSxDQUFDLENBQUM7SUFDcEQsT0FBTyxRQUFRLENBQUM7QUFDbEIsQ0FBQyxDQUFBLENBQUM7QUFFUyxRQUFBLFNBQVMsR0FBRyxDQUFPLFNBQWlCLEVBQUUsYUFBOEMsRUFBRSxFQUFFLEVBQUU7SUFDbkcsTUFBTSxJQUFJLEdBQUcsT0FBTyxDQUFDLE1BQU0sQ0FBQyxLQUFLLEVBQUUsQ0FBQztJQUNwQyxNQUFNLFFBQVEsR0FBRyxNQUFNLDhCQUFPLENBQUMsU0FBUyxFQUFFLFVBQVUsRUFBRSxJQUFJLENBQUMsQ0FBQztJQUM1RCxJQUFJLElBQUksR0FBRyxFQUFFLENBQUM7SUFFZCxRQUFRLENBQUMsRUFBRSxDQUFDLE1BQU0sRUFBRSxVQUFTLEtBQUs7UUFDOUIsSUFBSSxDQUFDLElBQUksQ0FBQyxLQUFLLENB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DQUFDLFNBQVMsRUFBRSxVQUFVLENBQUMsQ0FBQztJQUNwRCxPQUFPLFFBQVEsQ0FBQztBQUNsQixDQUFDLENBQUEsQ0FBQztBQUVTLFFBQUEsR0FBRyxHQUFHLENBQU8sU0FBaUIsRUFBRSxhQUE4QyxFQUFFLEVBQUUsRUFBRTtJQUM3RixVQUFVLENBQUMsTUFBTSxHQUFHLEtBQUssQ0FBQztJQUMxQixJQUFJLFFBQVEsR0FBRyxNQUFNLDhCQUFPLENBQUMsU0FBUyxFQUFFLFVBQVUsQ0FBQyxDQUFDO0lBQ3BELE9BQU8sUUFBUSxDQUFDO0FBQ2xCLENBQUMsQ0FBQSxDQUFDO0FBRVMsUUFBQSxHQUFHLEdBQUcsQ0FBTyxTQUFpQixFQUFFLGFBQThDLEVBQUUsRUFBRSxFQUFFO0lBQzdGLFVBQVUsQ0FBQyxNQUFNLEdBQUcsUUFBUSxDQUFDO0lBQzdCLElBQUksUUFBUSxHQUFHLE1BQU0sOEJBQU8sQ0FBQyxTQUFTLEVBQUUsVUFBVSxDQUFDLENBQUM7SUFDcEQsT0FBTyxRQUFRLENBQUM7QUFDbEIsQ0FBQyxDQUFBLENBQUMifQ== \ No newline at end of file diff --git a/dist/smartrequest.interfaces.d.ts b/dist/smartrequest.interfaces.d.ts deleted file mode 100644 index 287ccf9..0000000 --- a/dist/smartrequest.interfaces.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -/// -import * as https from 'https'; -export interface ISmartRequestOptions extends https.RequestOptions { - requestBody?: any; -} diff --git a/dist/smartrequest.interfaces.js b/dist/smartrequest.interfaces.js deleted file mode 100644 index 6a06697..0000000 --- a/dist/smartrequest.interfaces.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRyZXF1ZXN0LmludGVyZmFjZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHJlcXVlc3QuaW50ZXJmYWNlcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0= \ No newline at end of file diff --git a/dist/smartrequest.plugins.d.ts b/dist/smartrequest.plugins.d.ts deleted file mode 100644 index c5d9ff5..0000000 --- a/dist/smartrequest.plugins.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -import * as url from 'url'; -import * as http from 'http'; -import * as https from 'https'; -import * as smartq from 'smartq'; -export { url, http, https, smartq }; diff --git a/dist/smartrequest.plugins.js b/dist/smartrequest.plugins.js deleted file mode 100644 index e91881c..0000000 --- a/dist/smartrequest.plugins.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const url = require("url"); -exports.url = url; -const http = require("http"); -exports.http = http; -const https = require("https"); -exports.https = https; -const smartq = require("smartq"); -exports.smartq = smartq; -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRyZXF1ZXN0LnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHJlcXVlc3QucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLDJCQUEyQjtBQU1sQixrQkFBRztBQUxaLDZCQUE2QjtBQUtmLG9CQUFJO0FBSmxCLCtCQUErQjtBQUlYLHNCQUFLO0FBRnpCLGlDQUFpQztBQUVOLHdCQUFNIn0= \ No newline at end of file diff --git a/dist/smartrequest.request.d.ts b/dist/smartrequest.request.d.ts deleted file mode 100644 index 757bffe..0000000 --- a/dist/smartrequest.request.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -/// -import * as interfaces from './smartrequest.interfaces'; -import { IncomingMessage } from 'http'; -export interface extendedIncomingMessage extends IncomingMessage { - body: any; -} -export declare let request: (domainArg: string, optionsArg?: interfaces.ISmartRequestOptions, streamArg?: boolean) => Promise; diff --git a/dist/smartrequest.request.js b/dist/smartrequest.request.js deleted file mode 100644 index 0d69362..0000000 --- a/dist/smartrequest.request.js +++ /dev/null @@ -1,87 +0,0 @@ -"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()); - }); -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const plugins = require("./smartrequest.plugins"); -; -let buildResponse = (incomingMessageArg) => { - let done = plugins.smartq.defer(); - // Continuously update stream with data - let body = ''; - incomingMessageArg.on('data', function (chunkArg) { - body += chunkArg; - }); - incomingMessageArg.on('end', function () { - try { - incomingMessageArg.body = JSON.parse(body); - } - catch (err) { - incomingMessageArg.body = body; - } - done.resolve(incomingMessageArg); - }); - return done.promise; -}; -exports.request = (domainArg, optionsArg = {}, streamArg = false) => __awaiter(this, void 0, void 0, function* () { - let done = plugins.smartq.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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRyZXF1ZXN0LnJlcXVlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHJlcXVlc3QucmVxdWVzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQ0Esa0RBQWtEO0FBT2pELENBQUM7QUFFRixJQUFJLGFBQWEsR0FBRyxDQUFDLGtCQUFtQyxFQUFvQyxFQUFFO0lBQzVGLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxNQUFNLENBQUMsS0FBSyxFQUEyQixDQUFDO0lBQzNELHVDQUF1QztJQUN2QyxJQUFJLElBQUksR0FBRyxFQUFFLENBQUM7SUFDZCxrQkFBa0IsQ0FBQyxFQUFFLENBQUMsTUFBTSxFQUFFLFVBQVMsUUFBUTtRQUM3QyxJQUFJLElBQUksUUFBUSxDQUFDO0lBQ25CLENBQUMsQ0FBQyxDQUFDO0lBRUgsa0JBQWtCLENBQUMsRUFBRSxDQUFDLEtBQUssRUFBRTtRQUMzQixJQUFJO1lBQ0Qsa0JBQThDLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQyxLQUFLLENBQUMsSUFBSSxDQUFDLENBQUM7U0FDekU7UUFBQyxPQUFPLEdBQUcsRUFBRTtZQUNYLGtCQUE4QyxDQUFDLElBQUksR0FBRyxJQUFJLENBQUM7U0FDN0Q7UUFDRCxJQUFJLENBQUMsT0FBTyxDQUFDLGtCQUE2QyxDQUFDLENBQUM7SUFDOUQsQ0FBQyxDQUFDLENBQUM7SUFDS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ENBQUMsS0FBSyxDQUFDLFVBQVUsQ0FBQyxXQUFXLENBQUMsQ0FBQztTQUN2QztRQUNELE9BQU8sQ0FBQyxFQUFFLENBQUMsT0FBTyxFQUFFLENBQUMsQ0FBQyxFQUFFO1lBQ3RCLE9BQU8sQ0FBQyxLQUFLLENBQUMsQ0FBQyxDQUFDLENBQUM7UUFDbkIsQ0FBQyxDQUFDLENBQUM7UUFDSCxPQUFPLENBQUMsR0FBRyxFQUFFLENBQUM7S0FDZjtTQUFNO1FBQ0wsTUFBTSxJQUFJLEtBQUssQ0FBQyx5QkFBeUIsU0FBUyxDQUFDLFFBQVEsRUFBRSxDQUFDLENBQUM7S0FDaEU7SUFDRCxPQUFPLElBQUksQ0FBQyxPQUFPLENBQUM7QUFDdEIsQ0FBQyxDQUFBLENBQUMifQ== \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 9d35381..93b2a53 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,9 +1,19 @@ { - "name": "smartrequest", + "name": "@pushrocks/smartrequest", "version": "1.0.13", "lockfileVersion": 1, "requires": true, "dependencies": { + "@gitzone/tsrun": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@gitzone/tsrun/-/tsrun-1.0.5.tgz", + "integrity": "sha512-zSiRnlvNAlBmrqknwH9B0gIRx7IEegPzmN+DZ4yN3f+L62WlVbKyPLeeV09SKE8BFiGo+0VIGiQW3EV/9mXMfw==", + "dev": true, + "requires": { + "ts-node": "^6.1.0", + "typescript": "^2.9.1" + } + }, "@types/chai": { "version": "4.1.4", "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.1.4.tgz", @@ -46,6 +56,12 @@ "integrity": "sha1-kQ3lDvzHwJ49gvL4er1rcAwYgYo=", "dev": true }, + "arrify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/arrify/-/arrify-1.0.1.tgz", + "integrity": "sha1-iYUI2iIm84DfkEcoRWhJwVAaSw0=", + "dev": true + }, "assertion-error": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", @@ -68,6 +84,12 @@ "integrity": "sha512-DpLh5EzMR2kzvX1KIlVC0VkC3iZtHKTgdtZ0a3pglBZdaQFjt5S9g9xd1lE+YvXyfd6mtCeRnrUfOLYiTMlNSw==", "dev": true }, + "buffer-from": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.0.tgz", + "integrity": "sha512-c5mRlguI/Pe2dSZmpER62rSCu0ryKmWddzRYsuXc50U2/g8jMOulc31VZMa4mYx31U5xsmSOpDCgH88Vl9cDGQ==", + "dev": true + }, "chai": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chai/-/chai-4.1.2.tgz", @@ -121,6 +143,12 @@ "object-keys": "^1.0.8" } }, + "diff": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", + "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==", + "dev": true + }, "early": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/early/-/early-2.1.1.tgz", @@ -226,6 +254,12 @@ "integrity": "sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==", "dev": true }, + "make-error": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.4.tgz", + "integrity": "sha512-0Dab5btKVPhibSalc9QGXb559ED7G7iLjFXBaj9Wq8O3vorueR5K5jaE3hkG6ZQINyhA/JgG6Qk4qdFQjsYV6g==", + "dev": true + }, "memwatch-next": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/memwatch-next/-/memwatch-next-0.3.0.tgz", @@ -242,6 +276,23 @@ "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, + "requires": { + "minimist": "0.0.8" + }, + "dependencies": { + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + } + } + }, "nan": { "version": "2.10.0", "resolved": "https://registry.npmjs.org/nan/-/nan-2.10.0.tgz", @@ -306,6 +357,22 @@ "util.promisify": "^1.0.0" } }, + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + }, + "source-map-support": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.6.tgz", + "integrity": "sha512-N4KXEz7jcKqPf2b2vZF11lQIz9W5ZMuUcIOGj243lduidkf2fjkVKJS9vNxVWn3u/uxX38AcE8U9nnH9FPcq+g==", + "dev": true, + "requires": { + "buffer-from": "^1.0.0", + "source-map": "^0.6.0" + } + }, "tapbundle": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/tapbundle/-/tapbundle-2.0.0.tgz", @@ -319,12 +386,33 @@ "smartq": "^1.1.1" } }, + "ts-node": { + "version": "6.1.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-6.1.1.tgz", + "integrity": "sha512-79FnymLGDBd/nXoiak1L6w6fd9Zz9Ge/x8/Aglaeh31KkqRLDzbfT1vBGlO5dqc76WzufTlW4IYl7e01CVUF5A==", + "dev": true, + "requires": { + "arrify": "^1.0.0", + "diff": "^3.1.0", + "make-error": "^1.1.1", + "minimist": "^1.2.0", + "mkdirp": "^0.5.1", + "source-map-support": "^0.5.6", + "yn": "^2.0.0" + } + }, "type-detect": { "version": "4.0.8", "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", "dev": true }, + "typescript": { + "version": "2.9.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.9.2.tgz", + "integrity": "sha512-Gr4p6nFNaoufRIY4NMdpQRNmgxVIGMs4Fcu/ujdYk3nAZqk7supzBE9idmvfZIlH/Cuj//dvi+019qEue9lV0w==", + "dev": true + }, "typings-global": { "version": "1.0.28", "resolved": "https://registry.npmjs.org/typings-global/-/typings-global-1.0.28.tgz", @@ -349,6 +437,12 @@ "define-properties": "^1.1.2", "object.getownpropertydescriptors": "^2.0.3" } + }, + "yn": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/yn/-/yn-2.0.0.tgz", + "integrity": "sha1-5a2ryKz0CPY4X8dklWhMiOavaJo=", + "dev": true } } } diff --git a/package.json b/package.json index 49a6a9c..bda63cc 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { - "name": "smartrequest", + "name": "@pushrocks/smartrequest", "version": "1.0.13", "private": false, "description": "dropin replacement for request", "main": "dist/index.js", "typings": "dist/index.d.ts", "scripts": { - "test": "(npmts)", - "build": "echo \"Not needed for now\"" + "test": "tsrun test/test.ts", + "build": "(npmts)" }, "repository": { "type": "git", @@ -26,6 +26,7 @@ "smartq": "^1.1.1" }, "devDependencies": { + "@gitzone/tsrun": "^1.0.5", "@types/node": "^10.3.3", "tapbundle": "^2.0.0", "typings-test": "^1.0.3" diff --git a/ts/index.ts b/ts/index.ts index 97f07be..7a035e4 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,66 +1,5 @@ -import * as https from 'https'; - -import * as plugins from './smartrequest.plugins'; -import * as interfaces from './smartrequest.interfaces'; - -import { request, extendedIncomingMessage } from './smartrequest.request'; - export { request, extendedIncomingMessage } from './smartrequest.request'; export { ISmartRequestOptions } from './smartrequest.interfaces'; -export let get = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => { - optionsArg.method = 'GET'; - let response = await request(domainArg, optionsArg); - return response; -}; - -export let getBinary = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => { - const done = plugins.smartq.defer(); - const response = await request(domainArg, optionsArg, true); - var data = []; - - response.on('data', function(chunk) { - data.push(chunk); - }).on('end', function() { - //at this point data is an array of Buffers - //so Buffer.concat() can make us a new Buffer - //of all of them together - const buffer = Buffer.concat(data); - response.body = buffer.toString('binary'); - done.resolve(); - }); - await done.promise; - return response; -} - -export let post = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => { - optionsArg.method = 'POST'; - if ( - typeof optionsArg.requestBody === 'object' && - (!optionsArg.headers || !optionsArg.headers['Content-Type']) - ) { - // make sure headers exist - if (!optionsArg.headers) { - optionsArg.headers = {}; - } - - // assign the right Content-Type, leaving all other headers in place - Object.assign(optionsArg.headers, { - 'Content-Type': 'application/json' - }); - } - let response = await request(domainArg, optionsArg); - return response; -}; - -export let put = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => { - optionsArg.method = 'PUT'; - let response = await request(domainArg, optionsArg); - return response; -}; - -export let del = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => { - optionsArg.method = 'DELETE'; - let response = await request(domainArg, optionsArg); - return response; -}; +export * from './smartrequest.json'; +export * from './smartrequest.binary'; diff --git a/ts/smartrequest.binary.ts b/ts/smartrequest.binary.ts new file mode 100644 index 0000000..66dfafd --- /dev/null +++ b/ts/smartrequest.binary.ts @@ -0,0 +1,24 @@ +// this file implements methods to get and post binary data. +import * as interfaces from './smartrequest.interfaces'; +import { request } from './smartrequest.request'; + +import * as plugins from './smartrequest.plugins'; + +export const getBinary = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => { + const done = plugins.smartq.defer(); + const response = await request(domainArg, optionsArg, true); + const data = []; + + response.on('data', function(chunk) { + data.push(chunk); + }).on('end', function() { + //at this point data is an array of Buffers + //so Buffer.concat() can make us a new Buffer + //of all of them together + const buffer = Buffer.concat(data); + response.body = buffer.toString('binary'); + done.resolve(); + }); + await done.promise; + return response; +} \ No newline at end of file diff --git a/ts/smartrequest.json.ts b/ts/smartrequest.json.ts new file mode 100644 index 0000000..b0b8bac --- /dev/null +++ b/ts/smartrequest.json.ts @@ -0,0 +1,52 @@ +// This file implements methods to get and post JSON in a simple manner. + +import * as interfaces from './smartrequest.interfaces'; +import { request } from './smartrequest.request'; + +/** + * gets Json and puts the right headers + handles response aggregation + * @param domainArg + * @param optionsArg + */ +export const getJson = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => { + optionsArg.method = 'GET'; + optionsArg.headers = { + ...optionsArg.headers, + + } + let response = await request(domainArg, optionsArg); + return response; +}; + +export const postJson = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => { + optionsArg.method = 'POST'; + if ( + typeof optionsArg.requestBody === 'object' && + (!optionsArg.headers || !optionsArg.headers['Content-Type']) + ) { + // make sure headers exist + if (!optionsArg.headers) { + optionsArg.headers = {}; + } + + // assign the right Content-Type, leaving all other headers in place + optionsArg.headers = { + ...optionsArg.headers, + 'Content-Type': 'application/json' + }; + } + let response = await request(domainArg, optionsArg); + return response; +}; + +export const putJson = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => { + optionsArg.method = 'PUT'; + let response = await request(domainArg, optionsArg); + return response; +}; + +export const delJson = async (domainArg: string, optionsArg: interfaces.ISmartRequestOptions = {}) => { + optionsArg.method = 'DELETE'; + let response = await request(domainArg, optionsArg); + return response; +}; \ No newline at end of file