v0.3.0 - Profiles (Check the description for changes)

A lot has been renovated and some small things has been added to v0.3.0:
	* Added profiles (`getProfile`, `setProfile`, `setDefaults`)
	* Renamed `Collection` to `MemoryCollection`
	* Made `wumpfetch.userAgent` read-only
	* Renamed `__test__` to `tests`
	* Removed the `dist` folder
	* Changed the tab size from `4` to `2` in the declarations file
	and wumpfetch TS test file
	* Added JSdoc to `WumpRequest` and `WumpResponse`
	* Fixed up some of the weird spacing in `WumpRequest` and
	`WumpResponse`
	* Cleaned up all test files
This commit is contained in:
Wessel T
2019-08-26 16:52:34 +02:00
parent fc74329800
commit dd302b983e
31 changed files with 965 additions and 864 deletions

View File

@@ -1,35 +1,80 @@
const req = require('./model/WumpRequest');
const metaData = require('../package.json');
const Collection = require('./util/Collection');
const profiles = new Collection();
module.exports.default = {};
module.exports.setDefault = (profileData = {}) => this.default = profileData;
module.exports = (url, method = this.default) => {
if (url instanceof Object) url = Object.assign(this.default, url);
return new req(url, method);
};
module.exports.getProfile = (name = 'main') => { return profiles.get(name); };
module.exports.setProfile = (name = 'main', profileData = {}) => {
if (typeof profileData !== 'object') throw new TypeError(`profileData must be of type object. Received type ${typeof profileData}`);
if (typeof name !== 'string') throw new TypeError(`name must be of type string. Recevied type ${typeof name}`);
return profiles.set(name, profileData);
};
for (const entry of [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH' ]) {
module.exports[entry.toLowerCase()] = (url, method = { method: entry }) => {
if (typeof url === 'object') {
url = Object.assign(this.default, url);
url.method = entry;
}
return new req(url, method);
};
}
module.exports.version = metaData.version;
module.exports.userAgent = `${metaData.name}/${metaData.version} (${metaData.repository.url})`;
const req = require('./model/WumpRequest');
const metaData = require('../package.json');
const MemoryCollection = require('./util/MemoryCollection');
const profiles = new MemoryCollection();
profiles.set('__default__', { headers: { 'User-Agent': `${metaData.name}/${metaData.version} (${metaData.repository.url})` } });
/**
* Create a request with a specific method
*
* @param {object | string} url - The URL to send the request to or a `ReqOptions` object
* @param {object | string} [method=profiles.__default__] - The method to use when sending the request or a `ReqOptions` object
* @returns {req} - The request class
*/
module.exports = (url, method = profiles.get('__default__')) => {
if (typeof url === 'object' && typeof method === 'string') url.method = method;
if (typeof url === 'object' && !url.overwriteDefaults) Object.assign(url, profiles.get('__default__'));
if (typeof method === 'object' && !method.overwriteDefaults) Object.assign(method, profiles.get('__default__'));
return new req(url, method);
};
/**
* Set the defaults for every request
*
* @param {object} [profileData={}] - The default request options to use
* @returns {object} - The new defaults
*/
module.exports.setDefaults = (profileData = {}) => profiles.set('__default__', profileData);
/**
* Get a profile that you've previously saved
*
* @param {string} [name=main] - The profile to get
* @returns {object} - The profile if found
*/
module.exports.getProfile = (name = 'main') => { return profiles.get(name); };
/**
* Save a profile globally
*
* @param {string} [name=main] - The profile's name, used to identify later on
* @param {string} [profileData={}] - The profile's request data
* @returns {object} - The newly added profile
*
*/
module.exports.setProfile = (name = 'main', profileData = {}) => {
if (typeof name !== 'string') throw new TypeError(`name must be of type string. Recevied type ${typeof name}`);
if (typeof profileData !== 'object') throw new TypeError(`profileData must be of type object. Received type ${typeof profileData}`);
profiles.set(name, profileData);
};
for (const entry of [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH' ]) {
/**
* Create a request with a specific method
*
* @param {object | string} url - The URL to send the request to or a `ReqOptions` object
* @param {object | string} [method=profiles.__default__] - The method to use when sending the request or a `ReqOptions` object
* @returns {req} - The request class
*/
module.exports[entry.toLowerCase()] = (url, method = profiles.get('__default__')) => {
if (typeof url === 'object' && !url.overwriteDefaults) Object.assign(url, profiles.get('__default__'));
if (typeof method === 'object' && !method.overwriteDefaults) Object.assign(method, profiles.get('__default__'));
if (typeof url === 'object') url.method = entry;
if (typeof method === 'object') method.method = entry;
return new req(url, method);
};
}
/**
* The current running version of wumpfetch
*/
module.exports.version = metaData.version;
/**
* The default user-agent that's used when no user agent is provided
* @readonly
*/
module.exports.userAgent = `${metaData.name}/${metaData.version} (${metaData.repository.url})`;

View File

@@ -1,176 +1,253 @@
const zlib = require('zlib');
const http = require('http');
const https = require('https');
const { URL } = require('url');
const { stringify } = require('querystring');
const { join: pJoin } = require('path');
const w = require('../index');
const WumpRes = require('./WumpResponse');
const metaData = require('../../package.json');
const compressions = [ 'gzip', 'deflate' ];
module.exports = class WumpRequest {
constructor (url = {}, method = {}) {
const o = (typeof url === 'string' ? method : url);
if (typeof url !== 'string' && !o.hasOwnProperty('url')) throw new Error('Missing URL parameter');
this.o = {
'm': typeof o === 'string' ? o : (o.method || 'GET'),
'url': typeof url === 'string' ? new URL(url) : typeof o.url === 'string' ? new URL(o.url) : url,
'SDA': typeof o.sendDataAs === 'string' ? o.sendDataAs : o.data && typeof o.data === 'object' ? 'json' : undefined,
'data': o.body || o.data || o.json || (o.form ? stringify(o.form) : undefined),
'parse': o.parse,
'chain': typeof o.chain === 'boolean' ? o.chain : true,
'follow': !!(o.followRedirects),
'rHeaders': typeof o.headers === 'object' ? o.headers : {},
'streamed': !!(o.streamed),
'compressed': !!(o.compressed),
'timeoutTime': typeof o.timeout === 'number' ? o.timeout : null,
'coreOptions': typeof o.coreOptions === 'object' ? o.coreOptions : {}
};
if (typeof o.core === 'object') for (const key of Object.keys(o.core)) this.option(key, o.core[key]);
if (!this.o.chain) return this.send();
return this;
}
query (name, value) {
if (typeof a === 'object') for (const key of Object.keys(name)) this.o.url.searchParams.append(key, name[key]);
else this.o.url.searchParams.append(name, value);
return this;
}
body (data, sendAs) {
this.o.SDA = typeof data === 'object' && !sendAs && !Buffer.isBuffer(data) ? 'json' : sendAs ? sendAs.toLowerCase() : 'buffer';
this.o.data = this.sendAs === 'form' ? stringify(data) : this.SDA === 'json' ? JSON.stringify(data) : data;
return this;
}
header (name, value) {
if (typeof name === 'object') for (const key of Object.keys(name)) this.o.rHeaders[key.toLowerCase()] = name[key];
else this.o.rHeaders[name.toLowerCase()] = value;
return this;
}
compress () {
this.compressed = true;
if (!this.o.rHeaders['accept-encoding']) this.o.rHeaders['accept-encoding'] = compressions.join(', ');
return this;
}
path (p) {
this.o.url.pathname = pJoin(this.o.url.pathname, p);
return this;
}
stream () {
this.o.streamed = true;
return this;
}
option (name, value) {
this.o.coreOptions[name] = value;
return this;
}
timeout (timeout = 0) {
this.o.timeoutTime = timeout;
return this;
}
send () {
return new Promise((resolve, reject) => {
if (this.o.data) {
if (!this.o.rHeaders.hasOwnProperty('user-agent')) this.o.rHeaders['User-Agent'] = w.userAgent || `${metaData.name}/${metaData.version} (${metaData.repository.url})`;
if (this.o.SDA === 'json' && !this.o.rHeaders.hasOwnProperty('content-type')) this.o.rHeaders['Content-Type'] = 'application/json';
if (this.o.SDA === 'form') {
if (!this.o.rHeaders.hasOwnProperty('content-type')) this.o.rHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
if (!this.o.rHeaders.hasOwnProperty('content-length')) this.o.rHeaders['Content-Length'] = Buffer.byteLength(this.o.data);
}
}
let req;
const options = {
'host': this.o.url.hostname,
'port': this.o.url.port,
'path': `${this.o.url.pathname}${this.o.url.search}`,
'method': this.o.m,
'headers': this.o.rHeaders,
'protocol': this.o.url.protocol,
...this.o.coreOptions
};
const handler = async (res) => {
let stream = res;
if (this.o.compressed) {
switch (res.headers['content-encoding']) {
case 'gzip': stream = res.pipe(zlib.createGunzip()); break;
case 'defalte': stream = res.pipe(zlib.createInflate()); break;
}
}
let wumpRes;
if (this.o.streamed) resolve(stream);
else {
wumpRes = new WumpRes(res);
if (res.headers.hasOwnProperty('location') && this.o.follow) {
this.o.url = (new URL(res.headers['location'], this.o.url)).toString();
return await w(this.o);
}
stream.on('error', (e) => reject(e));
stream.on('data', (c) => wumpRes._addChunk(c));
stream.on('end', () => {
if (this.o.parse) {
switch (this.o.parse) {
case 'json': wumpRes.body = JSON.parse(wumpRes.body); break;
case 'text': wumpRes.body = wumpRes.body.toString(); break;
default: wumpRes.body = wumpRes.body;
}
}
resolve(wumpRes);
});
}
};
switch (this.o.url.protocol) {
case 'http:': req = http.request(options, handler); break;
case 'https:': req = https.request(options, handler); break;
default: throw new Error(`Bad URL protocol: ${this.o.url.protocol}`);
}
if (this.o.timeoutTime) {
req.setTimeout(this.o.timeoutTime, () => {
req.abort();
if (!this.o.streamed) reject(new Error('Timeout reached'));
});
}
req.on('error', (e) => reject(e));
if (this.o.data) {
if (this.o.SDA === 'json') req.write(JSON.stringify(this.o.data));
else if (this.o.data instanceof Object) req.write(JSON.stringify(this.o.data));
else req.write(this.o.data);
}
req.end();
});
}
};
const zlib = require('zlib');
const http = require('http');
const https = require('https');
const { URL } = require('url');
const { stringify } = require('querystring');
const { join: pJoin } = require('path');
const wump = require('../index');
const WumpRes = require('./WumpResponse');
const metaData = require('../../package.json');
const compressions = [ 'gzip', 'deflate' ];
module.exports = class WumpRequest {
/**
* Create the initial request
*
* @param {object | string} [url={}] - The URL to send the request to or a `ReqOptions` object
* @param {object | string} [method={}] - The method to use when sending the request or a `ReqOptions` object
* @throws {Error} - Missing URL parameter
* @returns {WumpRequest} - The request class
*/
constructor (url = {}, method = {}) {
const options = (typeof url === 'string' ? method : url);
if (typeof url !== 'string' && !options.hasOwnProperty('url')) throw new Error('Missing URL parameter');
this.o = {
'm': typeof method === 'string' ? method : (options.method || 'GET'),
'url': typeof url === 'string' ? new URL(url) : typeof options.url === 'string' ? new URL(options.url) : url,
'SDA': typeof options.sendDataAs === 'string' ? options.sendDataAs : options.data && typeof options.data === 'object' ? 'json' : undefined,
'data': options.body || options.data || options.json || (options.form ? stringify(options.form) : undefined),
'parse': options.parse,
'chain': typeof options.chain === 'boolean' ? options.chain : true,
'follow': !!(options.followRedirects),
'rHeaders': typeof options.headers === 'object' ? options.headers : {},
'streamed': !!(options.streamed),
'compressed': !!(options.compressed),
'timeoutTime': typeof options.timeout === 'number' ? options.timeout : null,
'coreOptions': typeof options.coreOptions === 'object' ? options.coreOptions : {}
};
if (typeof options.coreOptions === 'object') {
for (const key of Object.keys(options.coreOptions)) {
this.option(key, options.coreOptions[key]);
}
}
if (!this.o.chain) {
return this.send();
}
return this;
}
/**
* Append the request's body
*
* @param {any} data - The data to append
* @param {string} sendAs - The way to send the data, either `json`, `form` or `buffer`
* @returns {WumpRequest} - The request class
*/
body(data, sendAs) {
this.o.SDA = typeof data === 'object' && !sendAs && !Buffer.isBuffer(data) ? 'json' : sendAs ? sendAs.toLowerCase() : 'buffer';
this.o.data = this.sendAs === 'form' ? stringify(data) : this.SDA === 'json' ? JSON.stringify(data) : data;
return this;
}
/**
* Update the path of the URL (protocol://url.domain/:path)
*
* @param {string} path - The path to append to the url
* @returns {WumpRequest} - The request class
*/
path(path) {
this.o.url.pathname = pJoin(this.o.url.pathname, path);
return this;
}
/**
* Append the request's URL with a query (?name=value)
*
* @param {object | string} name - The name of the query or an object with multiple queries
* @param {string} value - The value of the query if `name` is of type `string`
* @returns {WumpRequest} - The request class
*/
query (name, value) {
if (typeof name === 'object') {
for (const key of Object.keys(name)) {
this.o.url.searchParams.append(key, name[key]);
}
} else this.o.url.searchParams.append(name, value);
return this;
}
/**
*
* @param {string | object} name - The name of the header or an object containing multiple headers
* @param {string} value - The content of the header if `name` is type of `string`
* @returns {WumpRequest} - The request class
*/
header (name, value) {
if (typeof name === 'object') {
for (const key of Object.keys(name)) {
this.o.rHeaders[key.toLowerCase()] = name[key];
}
} else this.o.rHeaders[name.toLowerCase()] = value;
return this;
}
/**
* Change one of NodeJS' `http` library options
*
* @param {string} name - The name of the option
* @param {any} value - The value of the optiob
* @returns {WumpRequest} - The request class
*/
option(name, value) {
this.o.coreOptions[name] = value;
return this;
}
/**
* Add a request timeout
*
* @param {number} timeout - The time before timing out (ms)
* @returns {WumpRequest} - The request class
*/
timeout(timeout = 0) {
this.o.timeoutTime = timeout;
return this;
}
/**
* Keep a connection alive with the server
*
* @returns {WumpRequest} - The request class
*/
stream() {
this.o.streamed = true;
return this;
}
/**
* Accept all encodings from the server
*
* @returns {WumpRequest} - The request class
*/
compress() {
this.compressed = true;
if (!this.o.rHeaders['accept-encoding']) this.o.rHeaders['accept-encoding'] = compressions.join(', ');
return this;
}
/**
* Finish off the request by sending it to the specified URL
*
* @throws {Error} - Bad URL Protocol
* @throws {Error} - Timeout reached
* @returns {Promise} - Teh response data
*/
send () {
return new Promise((resolve, reject) => {
if (this.o.data) {
if (!this.o.rHeaders.hasOwnProperty('user-agent')) this.o.rHeaders['User-Agent'] = `${metaData.name}/${metaData.version} (${metaData.repository.url})`;
if (this.o.SDA === 'json' && !this.o.rHeaders.hasOwnProperty('content-type')) this.o.rHeaders['Content-Type'] = 'application/json';
if (this.o.SDA === 'form') {
if (!this.o.rHeaders.hasOwnProperty('content-type')) this.o.rHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
if (!this.o.rHeaders.hasOwnProperty('content-length')) this.o.rHeaders['Content-Length'] = Buffer.byteLength(this.o.data);
}
}
let req;
const options = {
'host': this.o.url.hostname,
'port': this.o.url.port,
'path': `${this.o.url.pathname}${this.o.url.search}`,
'method': this.o.m,
'headers': this.o.rHeaders,
'protocol': this.o.url.protocol,
...this.o.coreOptions
};
const handler = async (res) => {
let stream = res;
if (this.o.compressed) {
switch (res.headers['content-encoding']) {
case 'gzip': stream = res.pipe(zlib.createGunzip()); break;
case 'defalte': stream = res.pipe(zlib.createInflate()); break;
}
}
let wumpRes;
if (this.o.streamed) {
resolve(stream);
} else {
wumpRes = new WumpRes(res);
if (res.headers.hasOwnProperty('location') && this.o.follow) {
this.o.url = (new URL(res.headers['location'], this.o.url)).toString();
return await wump(this.o);
}
stream.on('error', (e) => reject(e));
stream.on('data', (c) => wumpRes._addChunk(c));
stream.on('end', () => {
if (this.o.parse) {
switch (this.o.parse) {
case 'json': wumpRes.body = JSON.parse(wumpRes.body); break;
case 'text': wumpRes.body = wumpRes.body.toString(); break;
default: wumpRes.body = wumpRes.body;
}
}
resolve(wumpRes);
});
}
};
switch (this.o.url.protocol) {
case 'http:': req = http.request(options, handler); break;
case 'https:': req = https.request(options, handler); break;
default: throw new Error(`Bad URL protocol: ${this.o.url.protocol}`);
}
if (this.o.timeoutTime) {
req.setTimeout(this.o.timeoutTime, () => {
req.abort();
if (!this.o.streamed) reject(new Error('Timeout reached'));
});
}
req.on('error', (e) => reject(e));
if (this.o.data) {
if (this.o.SDA === 'json') req.write(JSON.stringify(this.o.data));
else if (this.o.data instanceof Object) req.write(JSON.stringify(this.o.data));
else req.write(this.o.data);
}
req.end();
});
}
};

View File

@@ -1,21 +1,21 @@
module.exports = class WumpResponse {
constructor (res) {
this.body = Buffer.alloc(0);
this.coreRes = res;
this.headers = res.headers;
this.statusCode = res.statusCode;
}
_addChunk (chunk) {
this.body = Buffer.concat([ this.body, chunk ]);
}
parse () {
if (this.headers['content-type'].includes('application/json')) return JSON.parse(this.body);
else return this.body.toString();
}
text () { return this.body.toString(); }
json () { return JSON.parse(this.body); }
buffer () { return this.body; }
};
module.exports = class WumpResponse {
constructor (res) {
this.body = Buffer.alloc(0);
this.coreRes = res;
this.headers = res.headers;
this.statusCode = res.statusCode;
}
_addChunk (chunk) {
this.body = Buffer.concat([ this.body, chunk ]);
}
parse () {
if (this.headers['content-type'].includes('application/json')) return this.json();
else return this.text();
}
text() { return this.body.toString(); }
json() { return JSON.parse(this.body); }
buffer() { return this.body; }
};

View File

@@ -1,21 +1,23 @@
module.exports = class PikminCollection extends Map {
constructor() {
super();
}
filter(callback) {
let result = [];
for (const entry of Array.from(this.values())) {
if (callback(entry)) result.push(entry);
}
return result;
}
map(callback) {
let result = [];
for (const value of Array.from(this.values())) result.push(callback(value));
return result;
}
};
module.exports = class MemoryCollection extends Map {
constructor() {
super();
}
filter(callback) {
let result = [];
for (const entry of Array.from(this.values())) {
if (callback(entry)) result.push(entry);
}
return result;
}
map(callback) {
let result = [];
for (const value of Array.from(this.values())) {
result.push(callback(value));
}
return result;
}
};