@qgisk/jokeapi-wrapper
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -1,1 +0,1 @@ | ||
module.exports = exports = require('./src/index'); | ||
module.exports = require('./src/index'); |
{ | ||
"name": "@qgisk/jokeapi-wrapper", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Wrapper for Joke API", | ||
@@ -44,2 +44,3 @@ "main": "index.js", | ||
"eslint-config-prettier": "^8.1.0", | ||
"eslint-plugin-prettier": "^3.1.1", | ||
"eslint-plugin-security": "^1.4.0", | ||
@@ -46,0 +47,0 @@ "prettier": "^2.0.5" |
@@ -10,3 +10,5 @@ # Joke API Client for NodeJS | ||
`npm i @qgisk/jokeapi-wrapper` | ||
```bash | ||
npm i @qgisk/jokeapi-wrapper | ||
``` | ||
@@ -51,7 +53,27 @@ ## • JokeAPI Documentation | ||
## • With Api Key | ||
## • With Options | ||
| Key | Type | | ||
| ---------------- | -------------- | | ||
| `apiKey` | `string` | | ||
| `safemode` | `boolean` | | ||
| `format` | `string` | | ||
| `blacklistFlags` | `string/array` | | ||
| `lang` | `string` | | ||
### • Example | ||
```javascript | ||
const JokeClient = new JokeAPI(<apikey>); | ||
const JokeClient = new JokeAPI({ apiKey: 'exampleapikey', safemode: true, format: 'xml' blacklistflags: ['nsfw'], lang: 'de'}); | ||
``` | ||
## • Categories & BlacklistFlags | ||
can be given in an array or a string seperated by , | ||
### • Example | ||
```javascript | ||
const JokeClient = new JokeAPI({ blacklistflags: ['nsfw', 'explicit'] }); | ||
const joke = await JokeClient.getJoke({ categories: 'coding,dark' }); | ||
``` | ||
@@ -75,3 +97,3 @@ | ||
```javascript | ||
const joke = await JokeClient.getJoke({ categories: ['Coding', 'dark] }); | ||
const joke = await JokeClient.getJoke({ categories: ['Coding', 'dark'] }); | ||
``` | ||
@@ -82,3 +104,3 @@ | ||
```javascript | ||
const joke = await JokeClient.getJoke({ categories: 'Coding, dark' }); | ||
const joke = await JokeClient.getJoke({ categories: 'Coding,dark' }); | ||
``` | ||
@@ -226,2 +248,30 @@ | ||
## • Submitting | ||
For testing define dry-run as anything | ||
### • Example | ||
```javascript | ||
const submitSingle = await JokeClient.submit({ | ||
'dry-run': true, | ||
formatVersion: 3, | ||
category: 'Misc', | ||
type: 'single', | ||
joke: 'testing', | ||
flags: { nsfw: true, religious: false, political: false, racist: false, sexist: false, explicit: true }, | ||
lang: 'en', | ||
}); | ||
const submitDouble = await JokeClient.submit({ | ||
'dry-run': true, | ||
formatVersion: 3, | ||
category: 'misc', | ||
type: 'twopart', | ||
setup: 'Setup', | ||
delivery: 'Delivering', | ||
flags: { nsfw: true, religious: false, political: false, racist: false, sexist: false, explicit: true }, | ||
lang: 'en', | ||
}); | ||
``` | ||
## • Inspiration | ||
@@ -228,0 +278,0 @@ |
@@ -7,3 +7,3 @@ /** | ||
const parseArray = (data) => { | ||
return typeof data == 'array' ? data.split(',') : data; | ||
return typeof data === Array ? data.split(',') : data; | ||
}; | ||
@@ -13,23 +13,40 @@ | ||
* @param {object} params | ||
* @param {object} options | ||
* @returns {object} | ||
*/ | ||
const parseParams = (params) => { | ||
if (!params) return; | ||
const parseParams = (params, options) => { | ||
const parsedParams = params; | ||
let wildcard; | ||
const obj = {}; | ||
// Either in params or options | ||
if ('safemode' in params) obj.safemode = 'safemode'; | ||
if ('format' in params) obj.format = params.format; | ||
if ('blacklistFlags' in params) obj.blacklistFlags = parseArray(params.blacklistFlags); | ||
if ('lang' in params) obj.lang = params.lang; | ||
if ('idRange' in params) obj.idRange = params.idRange; | ||
if ('contains' in params) obj.contains = params.contains; | ||
if ('type' in params) obj.type = parseArray(params.type); | ||
if ('amount' in params) obj.amount = params.amount; | ||
if ('lang' in params) obj.lang = params.lang; | ||
if ('language' in params) obj.language = params.language; | ||
if ('safemode' in options) parsedParams.safemode = 'safemode'; | ||
if ('safemode' in params) parsedParams.safemode ? delete parsedParams.safemode : (parsedParams.safemode = params.safemode); | ||
return obj; | ||
if ('format' in options) parsedParams.format = options.format; | ||
if ('format' in params) params.format === 'json' ? delete parsedParams.format : (parsedParams.format = params.format); | ||
if ('blacklistFlags' in params || 'blacklistFlags' in options) | ||
parsedParams.blacklistFlags = params.blacklistFlags | ||
? parseArray(params.blacklistFlags) | ||
: parseArray(options.blacklistFlags); | ||
if ('lang' in params || 'lang' in options) parsedParams.lang = params.lang ? params.lang : options.lang; | ||
// Just in params | ||
if ('type' in params) parsedParams.type = parseArray(params.type); | ||
// set wildcard | ||
if ('categories' in params) { | ||
wildcard = `/${parseArray(params.categories)}`; | ||
delete parsedParams.categories; | ||
} | ||
if ('language' in params) { | ||
wildcard = `/${params.language}`; | ||
delete parsedParams.language; | ||
} | ||
return { parsedParams, wildcard }; | ||
}; | ||
module.exports = { parseArray, parseParams }; |
@@ -14,3 +14,3 @@ const fetch = require('node-fetch'); | ||
* @public | ||
* @version 1.0.0 | ||
* @version 1.0.4 | ||
* @license MIT | ||
@@ -20,6 +20,11 @@ */ | ||
/** | ||
* @param {string} apiKey OPTIONAL:: Authorization key | ||
* @param {object} options | ||
* @param {string} options.apiKey OPTIONAL:: Authorization key | ||
* @param {boolean} options.safemode Turn on safemode DEFAULT:: off | ||
* @param {string} options.format Change global format DEFAULT:: JSON | ||
* @param {string|array} options.blacklistFlags Globally blacklist certain flags, Check JokeClient.BLACKLIST_FLAGS or https://jokeapi.dev/ for flag names | ||
* @param {string} options.lang Globally change language DEFAULT:: en | ||
*/ | ||
constructor(apiKey) { | ||
this.apiKey = apiKey; | ||
constructor(options = {}) { | ||
this._options = options; | ||
} | ||
@@ -34,9 +39,12 @@ | ||
* @param {string} params.lang | ||
* @param {number} params.idRange | ||
* @param {string} params.idRange i.e. 10-25 | ||
* @param {string} params.contains | ||
* @param {string} params.type | ||
* @param {number} params.amount | ||
* @param {number} params.amount i.e. 5 MAX:: 10 | ||
* @returns {ReturnObject} | ||
*/ | ||
getJoke(params = { categories: 'any' }) { | ||
getJoke(params = {}) { | ||
// eslint-disable-next-line no-param-reassign | ||
if (!params.categories) params.categories = 'any'; | ||
const url = this._buildUrl('joke', params); | ||
@@ -147,2 +155,26 @@ return this._request(url); | ||
/** | ||
* @description This endpoint returns a list / an array of all available endpoints, their usage (method, url and supported parameters) and a short description each. | ||
* @function endpoints() | ||
* @param {number} params.formatVersion | ||
* @param {string} params.category | ||
* @param {string} params.type | ||
* @param {string} params.joke | ||
* @param {object} params.flags | ||
* @param {boolean} params.flags.nsfw | ||
* @param {boolean} params.flags.religious | ||
* @param {boolean} params.flags.political | ||
* @param {boolean} params.flags.racist | ||
* @param {boolean} params.flags.sexist | ||
* @param {boolean} params.flags.explicit | ||
* @param {lang} params.lang | ||
* @returns {ReturnObject} | ||
*/ | ||
submit(params = {}) { | ||
const url = this._buildUrl('submit', undefined, params['dry-run']); | ||
// Remove dry run from body | ||
delete params['dry-run']; | ||
return this._request(url, { body: JSON.stringify(params), method: 'POST' }); | ||
} | ||
/** | ||
* @description Builds the url. | ||
@@ -160,13 +192,16 @@ * @function _buildUrl() | ||
* @param {string} params.lang | ||
* @param {string} method | ||
* @returns {string} | ||
*/ | ||
_buildUrl(endpoint, params) { | ||
const language = params.language; | ||
const categories = Util.parseArray(params.categories); | ||
_buildUrl(endpoint, params, testRun) { | ||
if (testRun) return `${Constants.BASE}/${endpoint}?dry-run`; | ||
const obj = Util.parseParams(params); | ||
const wildcard = language ? `/${language}` : categories ? `/${categories}` : ''; | ||
const url = `${Constants.BASE}/${endpoint}${wildcard}`; | ||
if (params) { | ||
const { parsedParams, wildcard } = Util.parseParams(params, this._options); | ||
const url = wildcard ? `${Constants.BASE}/${endpoint}${wildcard}` : `${Constants.BASE}/${endpoint}`; | ||
return obj ? this._buildQuery(url, obj) : url; | ||
return this._buildQuery(url, parsedParams); | ||
} | ||
return `${Constants.BASE}/${endpoint}`; | ||
} | ||
@@ -192,2 +227,3 @@ | ||
.map((pair) => { | ||
// On some queries there is no value i.e. safemode; so we jsut use the key name | ||
return pair[0] === pair[1] ? pair[0] : pair.map(encodeURIComponent).join('='); | ||
@@ -204,2 +240,3 @@ }) | ||
* @param {string} url | ||
* @param {object} options | ||
* @param {string} options.method | ||
@@ -209,3 +246,7 @@ * @returns {object} | ||
async _request(url, options) { | ||
const headers = this.apiKey ? { Authorization: this.apiKey } : {}; | ||
const headers = { | ||
Authorization: this._options.apiKey, | ||
'Content-Type': 'application/json', | ||
}; | ||
const res = await fetch(url, { ...options, headers }); | ||
@@ -232,2 +273,2 @@ | ||
module.exports = exports = JokeAPI; | ||
module.exports = JokeAPI; |
19928
325
279
6