diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dd2977..85386b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,30 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [2.1.0](https://github.com/QGIsK/jokeapi-wrapper/compare/v1.0.8...v2.1.0) (2021-12-10) + + +### Features + +* written documentation ([69ad08d](https://github.com/QGIsK/jokeapi-wrapper/commits/69ad08d55f5f6840f60a11a322daae91a3c428d2)) + + +### Bug Fixes + +* properly return blacklistflags ([6d55189](https://github.com/QGIsK/jokeapi-wrapper/commits/6d551898662e8996bfe78523e858f3a2a8d349e3)) + + +### Other + +* add plausible ([38543a8](https://github.com/QGIsK/jokeapi-wrapper/commits/38543a84bdae412ac922ab8b6d8c0cbab2c065be)) +* change sub domain ([68e567b](https://github.com/QGIsK/jokeapi-wrapper/commits/68e567b55cab765e2bd166b6f09615b82e33cb2a)) +* cleanup readme ([86b0886](https://github.com/QGIsK/jokeapi-wrapper/commits/86b0886e9f25c56e86dfd923b6ab4d201eb1112e)) +* **release:** 2.0.0 ([c3198e0](https://github.com/QGIsK/jokeapi-wrapper/commits/c3198e0a74e03a5e8afab6df660d101dc4fa03a9)) +* setup commit lint, husky, standard-version ([b995167](https://github.com/QGIsK/jokeapi-wrapper/commits/b99516782f172e0687269aa6c152dc61a4e18bef)) +* update packages ([b0cdfcf](https://github.com/QGIsK/jokeapi-wrapper/commits/b0cdfcf9d94d6bbba5b968efa4e6ace25058dccd)) +* update plausible ([c6fbbdf](https://github.com/QGIsK/jokeapi-wrapper/commits/c6fbbdf491b3a7b76e6e7afbebb140000f437fe8)) +* update readme.md ([181a448](https://github.com/QGIsK/jokeapi-wrapper/commits/181a448ab9bf54aa6bfae0e71acc30aad4906b32)) + ## [2.0.0](https://github.com/QGIsK/jokeapi-wrapper/compare/v1.0.8...v2.0.0) (2021-12-10) diff --git a/docs/content/en/community/dependencies.md b/docs/content/en/community/dependencies.md index 96d964f..72d6db8 100644 --- a/docs/content/en/community/dependencies.md +++ b/docs/content/en/community/dependencies.md @@ -1,7 +1,7 @@ --- title: Dependencies description: 'Dependencies we rely on' -position: 14 +position: 16 category: 'Community' --- diff --git a/docs/content/en/community/example.md b/docs/content/en/community/example.md index e208ed6..8b73403 100644 --- a/docs/content/en/community/example.md +++ b/docs/content/en/community/example.md @@ -1,7 +1,7 @@ --- title: Example Projects description: 'Example projects using the wrapper' -position: 13 +position: 15 category: 'Community' --- diff --git a/docs/content/en/methods/clearData.md b/docs/content/en/methods/clearData.md new file mode 100644 index 0000000..0558093 --- /dev/null +++ b/docs/content/en/methods/clearData.md @@ -0,0 +1,14 @@ +--- +title: Clear Data +description: 'This clears ur joke cache ( Jokes are cached to avoid duplicates )' +position: 13 +category: 'Methods' +--- + +## Simple Usage + +This clears ur joke cache ( Jokes are cached to avoid duplicates ) + +```javascript[index.js] +await JokeClient.clearData(); +``` diff --git a/docs/content/en/methods/percentEncoder.md b/docs/content/en/methods/percentEncoder.md new file mode 100644 index 0000000..bcb2c6f --- /dev/null +++ b/docs/content/en/methods/percentEncoder.md @@ -0,0 +1,15 @@ +--- +title: Percent Encoder +description: 'Helper function to encode special characters' +position: 14 +category: 'Methods' +--- + +## Simple Usage + +This is a helper function to encode special characters +Encodes | into %7C + +```javascript[index.js] +await JokeClient._percentEncoder('da|dada'); +``` diff --git a/docs/nuxt.config.js b/docs/nuxt.config.js index 756760e..5fd55f5 100644 --- a/docs/nuxt.config.js +++ b/docs/nuxt.config.js @@ -6,8 +6,8 @@ export default theme({ { async: true, defer: true, - 'data-domain': 'jokewrapper.docs.demiann.dev', - src: 'https://uwu.demiann.dev/js/script.js', + 'data-website-id': '57842630-bdb1-4942-ba30-8515b7864afe', + src: 'https://media.demiann.dev/umami.js', }, ], }, diff --git a/package-lock.json b/package-lock.json index 99336ad..4b515c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@qgisk/jokeapi-wrapper", - "version": "2.0.0", + "version": "2.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@qgisk/jokeapi-wrapper", - "version": "2.0.0", + "version": "2.1.0", "license": "MIT", "dependencies": { "isomorphic-unfetch": "^3.1.0" diff --git a/package.json b/package.json index 7ec6234..c4e00a5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "@qgisk/jokeapi-wrapper", - "version": "2.0.0", + "version": "2.1.0", "description": "Wrapper for Joke API", "exports": "./index.js", "files": [ diff --git a/src/helpers/constants.js b/src/helpers/constants.js index c9719cf..d52b5be 100644 --- a/src/helpers/constants.js +++ b/src/helpers/constants.js @@ -2,7 +2,7 @@ * @kind constant * @description The base url for the Joke API */ -const BASE = 'https://v2.jokeapi.dev'; +const BASE = 'https://stage.jokeapi.dev'; /** * @kind constant diff --git a/src/helpers/util.js b/src/helpers/util.js index 641229a..8fb5c32 100644 --- a/src/helpers/util.js +++ b/src/helpers/util.js @@ -42,23 +42,28 @@ const parseParams = (params, options) => { else parsedParams.format = params.format; } - if ('blacklistFlags' in params || 'blacklistFlags' in options) - parsedParams.blacklistFlags = params.blacklistFlags - ? parseArray(params.blacklistFlags) - : parseArray(options.blacklistFlags); + if ('blacklistFlags' in params || 'blacklistFlags' in options) { + // @ts-ignore TODO :: Fix this TS Error + if (params.blacklistFlags && options.blacklistFlags) parsedParams.blacklistFlags = parseArray(params.blacklistFlags); + // @ts-ignore TODO :: Fix this TS Error + else parsedParams.blacklistFlags = parseArray(params.blacklistFlags || options.blacklistFlags); + } if ('lang' in params || 'lang' in options) parsedParams.lang = params.lang ? params.lang : options.lang; // Just in params + // @ts-ignore TODO :: Fix this TS Error if ('type' in params) parsedParams.type = parseArray(params.type); // set wildcard if ('categories' in params) { wildcard = `/${parseArray(params.categories)}`; + // @ts-ignore TODO :: Fix this TS Error delete parsedParams.categories; } if ('language' in params) { wildcard = `/${params.language}`; + // @ts-ignore TODO :: Fix this TS Error delete parsedParams.language; } diff --git a/src/index.js b/src/index.js index 0c21a1a..1792a62 100644 --- a/src/index.js +++ b/src/index.js @@ -17,12 +17,11 @@ import Util from './helpers/util.js'; * @class JokeAPI * @author Demian * @description A Node.js wrapper for the Joke API with only one dependency. For more information, visit: https://jokeapi.dev/ + * @public + * @license MIT * @example * const JokeAPI = require('jokeapi-wrapper'); * const JokeAPIClient = new JokeAPi(); - * @public - * @version 1.0.8 - * @license MIT */ class JokeAPI { /** @@ -174,12 +173,36 @@ class JokeAPI { return this._request(url, { body: JSON.stringify(params), method: 'POST' }); } + /** + * @function clearData + * @description This clears ur joke cache ( Jokes are cached to avoid duplicates ) + * + * @return {Object} + */ + clearData() { + const url = this._buildUrl('cleardata'); + return this._request(url, { method: 'POST' }); + } + + /** + * @function _percentEncoder + * @description This is a helper function to encode special characters + * + * @param {string} string + * + * @return {string | {error: boolean, message: string}} + */ + _percentEncoder(string) { + if (!string) return { error: true, message: 'You need to supply a string to encode' }; + return string.replace('|', '%7C'); + } + /** * @function _buildUrl * @description Builds the url. * * @param {string} endpoint - * @param {ExtendedParams} params + * @param {ExtendedParams} [params] * @param {Boolean } [testRun] * * @return {string} @@ -228,11 +251,11 @@ class JokeAPI { */ async _request(url, options) { const headers = { - Authorization: this._options.apiKey, + Authorization: `Bearer ${this._options.apiKey}`, 'Content-Type': 'application/json', }; - const res = await fetch(url, { ...options, headers }); + const response = await fetch(url, { ...options, headers }); // @ts-ignore TODO :: Fix this const formattedUrl = new URL(url); @@ -241,9 +264,20 @@ class JokeAPI { const urlSearchParams = new URLSearchParams(formattedUrl.search); const format = urlSearchParams.get('format'); - if (format) return res.text(); + if (format) return response.text(); + + const json = await response.json(); + + /** @type {string[]} */ + const requestHeaders = ['date', 'retry-after', 'ratelimit-limit', 'ratelimit-remaining', 'ratelimit-reset']; + const formattedHeaders = {}; + + requestHeaders.forEach((header) => { + // @ts-ignore TODO :: Fix this TS Error + formattedHeaders[header] = response.headers.get(header); + }); - return res.json(); + return { ...json, headers: formattedHeaders }; } } diff --git a/test.js b/test.js index 0178938..71f0d5a 100644 --- a/test.js +++ b/test.js @@ -120,15 +120,13 @@ test('Gets ping', async (t) => { const ping = await JokeClient.ping(); t.true(!ping.error); - t.is(ping.ping, 'Pong!'); }); test('Gets endpoints', async (t) => { const JokeClient = new JokeAPI(); const endpoints = await JokeClient.endpoints(); - t.true(!endpoints.error); - t.assert(endpoints.length > 0); + t.assert(Object.keys(endpoints).length > 0); }); test('Submits single joke as dry run', async (t) => { @@ -333,3 +331,46 @@ test('Wildcard returns correct language', async (t) => { t.assert(wildcard); t.is(wildcard, `/${params.language}`); }); + +test('Encode correctly', async (t) => { + const JokeClient = new JokeAPI(); + + const string = `dark|programming*dev*`; + const shouldReturn = `dark%7Cprogramming*dev*`; + + const output = await JokeClient._percentEncoder(string); + + t.is(output, shouldReturn); +}); + +test('Clear joke function', async (t) => { + const JokeClient = new JokeAPI(); + + const output = await JokeClient.clearData(); + + t.true(!output.error); + t.true(output.jokeCache.message.includes('Successfully cleared')); +}); + +test('Headers are supplied in JSON Format', async (t) => { + const JokeClient = new JokeAPI(); + + const res = await JokeClient.getJoke(); + + t.true(!res.error); + t.assert(res.headers); + t.assert(res.headers.date); + t.assert(res.headers['retry-after']); + t.assert(res.headers['ratelimit-limit']); + t.assert(res.headers['ratelimit-remaining']); + t.assert(res.headers['ratelimit-reset']); +}); + +test('Headers are not supplied in XML Format', async (t) => { + const JokeClient = new JokeAPI(); + + const res = await JokeClient.getJoke({ format: 'xml' }); + + t.true(!res.error); + t.assert(!res.headers); +});