Add profiles

This commit is contained in:
Wessel Tip
2019-07-08 13:05:13 +02:00
parent c91ed60ae4
commit f1ec45d913
2 changed files with 48 additions and 7 deletions

View File

@@ -1,15 +1,35 @@
const req = require('./model/WumpRequest');
const common = [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH' ];
const metaData = require('../package.json');
const Collection = require('./util/Collection');
module.exports = (url, method) => { return new req(url, method); };
const profiles = new Collection();
for (const method of common) {
module.exports[method.toLowerCase()] = (url, method) => {
if (typeof url === 'string') return new req(url, { method: method, ...method });
else return new req({ method: method, ...url }, method);
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})`;
module.exports.userAgent = `${metaData.name}/${metaData.version} (${metaData.repository.url})`;

21
lib/util/Collection.js Normal file
View File

@@ -0,0 +1,21 @@
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;
}
};