Skip to content

Commit

Permalink
Feature: Added config param throwEmpty
Browse files Browse the repository at this point in the history
- Default is `true`
- Set to `false` to return an empty array instead of throwing error 'no results'
  • Loading branch information
fvdm committed Dec 16, 2023
1 parent da2031e commit 9cf1090
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ It returns a Promise.
The parameters below can be included along with the
normal API params.

param | type | default | description
:-----------|:-------|:----------------|:-----------
[timeout] | number | 5000 | Wait time out in ms
[userAgent] | string | searchitunes.js | User-Agent header
... | mixed | | API parameters
param | type | default | description
:------------|:-------|:----------------|:-----------
[timeout] | number | 5000 | Wait time out in ms
[userAgent] | string | searchitunes.js | User-Agent header
[throwEmpty] | bool | true | Throw `no results` instead of returning `[]`
... | mixed | | API parameters

```js
itunes( {
Expand Down Expand Up @@ -120,10 +121,21 @@ searchitunes( { id: [ 123, 789 ] } )
## Errors
API errors are handled and thrown, prefixed with `API: `.
API errors are handled and thrown, prefixed with `API: `
When nothing is found it will throw with message `no results`.
When nothing is found it will throw with `no results` message. You can prevent
this behavior by setting `throwEmpty` to `false` to return an empty array.
```js
searchitunes( {
throwEmpty: false,
entity: 'software',
term: 'github',
} )
.then( console.log )
.catch( console.error )
;
```
## Unlicense
Expand Down
15 changes: 11 additions & 4 deletions searchitunes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ License: Unlicense (Public Domain, see UNLICENSE file)
*
* @return {Promise<object|array>}
*
* @param {object} params Parameters to send along
* @param {number} [params.timeout=5000] Wait timeout in ms
* @param {string} [params.userAgent] Custom User-Agent header
* @param {object} params Parameters to send along
* @param {number} [params.timeout=5000] Wait timeout in ms
* @param {string} [params.userAgent] Custom User-Agent header
* @param {bool} [params.throwEmpty=true] Throw 'no results' instead of returning an empty array
*/

module.exports = async function SearchItunes ( {

Check warning on line 21 in searchitunes.js

View workflow job for this annotation

GitHub Actions / Node (21)

Async function 'SearchItunes' has a complexity of 11. Maximum allowed is 8

Check warning on line 21 in searchitunes.js

View workflow job for this annotation

GitHub Actions / Node (20)

Async function 'SearchItunes' has a complexity of 11. Maximum allowed is 8

Check warning on line 21 in searchitunes.js

View workflow job for this annotation

GitHub Actions / Node (18)

Async function 'SearchItunes' has a complexity of 11. Maximum allowed is 8

timeout = 5000,
userAgent = 'searchitunes.js',
throwEmpty = true,
trackId,

} ) {
Expand All @@ -38,6 +40,7 @@ module.exports = async function SearchItunes ( {

delete params.timeout;
delete params.userAgent;
delete params.throwEmpty;

// Convert trackId from a search response
if ( trackId ) {
Expand Down Expand Up @@ -90,7 +93,11 @@ module.exports = async function SearchItunes ( {

// Empty result
if ( ! data.results || ! data.results.length ) {
throw new Error( 'no results' );
if ( throwEmpty ) {
throw new Error( 'no results' );
}

return [];
}

// Lookup response
Expand Down
28 changes: 26 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dotest.add( 'Interface', test => {
} );


dotest.add( 'Error: no results', async test => {
dotest.add( 'Error: no results (throwEmpty = true)', async test => {
let error;
let data;

Expand Down Expand Up @@ -203,7 +203,31 @@ dotest.add( 'Default timeout', async test => {
} );


dotest.add( 'API error', async test => {
dotest.add( 'throwEmpty = false', async test => {
let error;
let data;

try {
data = await app( {
entity: 'software',
term: 'SomethingThatShouldNotExistForAPITestingPuposes',
throwEmpty: false,
} );
}
catch ( err ) {
error = err;
}

test( error )
.isArray( 'fail', 'data', data )
.isEmpty( 'fail', 'data', data )
.isUndefined( 'fail', 'error', error )
.done()
;
} );


dotest.add( 'Error: API error', async test => {
let error;
let data;

Expand Down

0 comments on commit 9cf1090

Please sign in to comment.