Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for v2.4.0 #1

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/community/dependencies.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Dependencies
description: 'Dependencies we rely on'
position: 14
position: 16
category: 'Community'
---

Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/community/example.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Example Projects
description: 'Example projects using the wrapper'
position: 13
position: 15
category: 'Community'
---

Expand Down
14 changes: 14 additions & 0 deletions docs/content/en/methods/clearData.md
Original file line number Diff line number Diff line change
@@ -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();
```
15 changes: 15 additions & 0 deletions docs/content/en/methods/percentEncoder.md
Original file line number Diff line number Diff line change
@@ -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');
```
4 changes: 2 additions & 2 deletions docs/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
],
},
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions src/helpers/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
50 changes: 42 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import Util from './helpers/util.js';
* @class JokeAPI
* @author Demian <[email protected]>
* @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 {
/**
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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);
Expand All @@ -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 };
}
}

Expand Down
47 changes: 44 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
});