Skip to content

Commit

Permalink
Merge branch 'main' of github.com-tbogard:tbogard/itunes-search
Browse files Browse the repository at this point in the history
  • Loading branch information
Erick Rodriguez committed Jan 17, 2022
2 parents e5d7504 + 9c3dc41 commit 82fb867
Show file tree
Hide file tree
Showing 21 changed files with 53 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
@@ -1 +1 @@
14.17.1
14.18.1
1 change: 0 additions & 1 deletion dist/iTunesSearch/index.d.ts
Expand Up @@ -3,7 +3,6 @@ import { IiTunesSearch, ISearchAllOptions } from "./interfaces";
export declare class iTunesSearch implements IiTunesSearch {
options: ISearchAllOptions;
defaultOptions: ISearchAllOptions;
private iTunesFetch;
searchAlbum: (term: string, options?: ISearchAllOptions | undefined) => Promise<ReturnType>;
searchSong: (term: string, options?: ISearchAllOptions | undefined) => Promise<ReturnType>;
searchArtist: (term: string, options?: ISearchAllOptions | undefined) => Promise<ReturnType>;
Expand Down
2 changes: 1 addition & 1 deletion dist/iTunesSearch/index.d.ts.map

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

21 changes: 12 additions & 9 deletions dist/iTunesSearch/index.js
Expand Up @@ -10,15 +10,12 @@ class iTunesSearch {
this.options = {};
this.defaultOptions = {
limit: 1,
country: "US",
language: "en",
country: null,
language: null,
attribute: null,
entity: null
};
this.iTunesFetch = axios_1.default.create({
baseURL: 'https://itunes.apple.com',
entity: null,
timeout: 2000
});
};
this.searchAlbum = async (term, options) => await this.performSearch(term, { ...options, ...{ entity: "album" } }, "searchAlbum");
this.searchSong = async (term, options) => await this.performSearch(term, { ...options, ...{ media: "music" } }, "searchSong");
this.searchArtist = async (term, options) => await this.performSearch(term, { ...options, ...{ entity: "allArtist", attribute: "allArtistTerm" } }, "searchArtist");
Expand All @@ -30,11 +27,17 @@ class iTunesSearch {
this.searchPodcast = async (term, options) => await this.performSearch(term, { ...options, ...{ entity: "podcast" } }, "searchPodcast");
this.searchAll = async (term, options) => await this.performSearch(term, { ...options }, "searchAll");
this.performSearch = async (term, options, validate) => {
const searchObject = this.validate(term, options, validate);
try {
const searchObject = this.validate(term, options, validate);
const iTunesFetch = axios_1.default.create({
baseURL: 'https://itunes.apple.com',
timeout: options?.timeout || 2000
});
if (options.timeout)
delete options.timeout;
const querystring = new URLSearchParams(searchObject);
const searchQueryStr = `/search?term=${encodeURI(term)}&${querystring.toString()}`;
const { data } = await this.iTunesFetch.get(searchQueryStr);
const { data } = await iTunesFetch.get(searchQueryStr);
return data;
}
catch (e) {
Expand Down
2 changes: 2 additions & 0 deletions dist/iTunesSearch/interfaces.d.ts
Expand Up @@ -6,13 +6,15 @@ export interface ISearchAllOptions {
attribute?: AttributeType | string | null | undefined;
language?: string | null | undefined;
country?: string | null | undefined;
timeout?: number | null | undefined;
}
export interface ISearchMandatoryAllOptions {
limit?: number;
entity: EntityType;
attribute: AttributeType;
language?: string;
country?: string;
timeout?: number;
}
export interface IiTunesSearch {
defaultOptions: ISearchAllOptions;
Expand Down
2 changes: 1 addition & 1 deletion dist/iTunesSearch/interfaces.d.ts.map

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

2 changes: 1 addition & 1 deletion dist/web.minified/itunesSearch.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/searchAlbum.md
Expand Up @@ -19,6 +19,7 @@ Please Check [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) For La
| limit | number || Required if options is defined. A Numeric value that sets the limit of results by the search term | 1 |
| language | string | 🔴 | a language code based on [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) | "en" |
| country | string | 🔴 | a country code based on [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) | "US" |
| timeout | number | 🔴 | The amout of milliseconds that will be allowed the search to take before timeout. | 2000 |

## Example of use:

Expand Down
1 change: 1 addition & 0 deletions docs/searchAll.md
Expand Up @@ -21,6 +21,7 @@ Please Check [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) For La
| attribute | string | 🟡 if you need more precision | The attribute you want to search for in the stores, relative to the specified media type. For example, if you want to search for an artist by name specify `entity=allArtist&attribute=allArtistTerm.` Check the list of Attributes. | |
| language | string | 🔴 | a language code based on [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) | "en" |
| country | string | 🔴 | a country code based on [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) | "US" |
| timeout | number | 🔴 | The amout of milliseconds that will be allowed the search to take before timeout. | 2000 |

##### Entities for each media type

Expand Down
1 change: 1 addition & 0 deletions docs/searchApp.md
Expand Up @@ -19,6 +19,7 @@ Please Check [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) For La
| limit | number || Required if options is defined. A Numeric value that sets the limit of results by the search term | 1 |
| language | string | 🔴 | a language code based on [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) | "en" |
| country | string | 🔴 | a country code based on [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) | "US" |
| timeout | number | 🔴 | The amout of milliseconds that will be allowed the search to take before timeout. | 2000 |

## Example of use:

Expand Down
1 change: 1 addition & 0 deletions docs/searchArtist.md
Expand Up @@ -19,6 +19,7 @@ Please Check [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) For La
| limit | number || Required if options is defined. A Numeric value that sets the limit of results by the search term | 1 |
| language | string | 🔴 | a language code based on [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) | "en" |
| country | string | 🔴 | a country code based on [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) | "US" |
| timeout | number | 🔴 | The amout of milliseconds that will be allowed the search to take before timeout. | 2000 |

## Example of use:

Expand Down
1 change: 1 addition & 0 deletions docs/searchAudiobook.md
Expand Up @@ -19,6 +19,7 @@ Please Check [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) For La
| limit | number || Required if options is defined. A Numeric value that sets the limit of results by the search term | 1 |
| language | string | 🔴 | a language code based on [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) | "en" |
| country | string | 🔴 | a country code based on [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) | "US" |
| timeout | number | 🔴 | The amout of milliseconds that will be allowed the search to take before timeout. | 2000 |

## Example of use:

Expand Down
1 change: 1 addition & 0 deletions docs/searchBook.md
Expand Up @@ -19,6 +19,7 @@ Please Check [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) For La
| limit | number || Required if options is defined. A Numeric value that sets the limit of results by the search term | 1 |
| language | string | 🔴 | a language code based on [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) | "en" |
| country | string | 🔴 | a country code based on [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) | "US" |
| timeout | number | 🔴 | The amout of milliseconds that will be allowed the search to take before timeout. | 2000 |

## Example of use:

Expand Down
1 change: 1 addition & 0 deletions docs/searchMovie.md
Expand Up @@ -19,6 +19,7 @@ Please Check [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) For La
| limit | number || Required if options is defined. A Numeric value that sets the limit of results by the search term | 1 |
| language | string | 🔴 | a language code based on [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) | "en" |
| country | string | 🔴 | a country code based on [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) | "US" |
| timeout | number | 🔴 | The amout of milliseconds that will be allowed the search to take before timeout. | 2000 |

## Example of use:

Expand Down
1 change: 1 addition & 0 deletions docs/searchMusicVideo.md
Expand Up @@ -19,6 +19,7 @@ Please Check [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) For La
| limit | number || Required if options is defined. A Numeric value that sets the limit of results by the search term | 1 |
| language | string | 🔴 | a language code based on [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) | "en" |
| country | string | 🔴 | a country code based on [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) | "US" |
| timeout | number | 🔴 | The amout of milliseconds that will be allowed the search to take before timeout. | 2000 |

## Example of use:

Expand Down
1 change: 1 addition & 0 deletions docs/searchPodcast.md
Expand Up @@ -19,6 +19,7 @@ Please Check [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) For La
| limit | number || Required if options is defined. A Numeric value that sets the limit of results by the search term | 1 |
| language | string | 🔴 | a language code based on [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) | "en" |
| country | string | 🔴 | a country code based on [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) | "US" |
| timeout | number | 🔴 | The amout of milliseconds that will be allowed the search to take before timeout. | 2000 |

## Example of use:

Expand Down
1 change: 1 addition & 0 deletions docs/searchSong.md
Expand Up @@ -19,6 +19,7 @@ Please Check [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) For La
| limit | number || Required if options is defined. A Numeric value that sets the limit of results by the search term | 1 |
| language | string | 🔴 | a language code based on [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) | "en" |
| country | string | 🔴 | a country code based on [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) | "US" |
| timeout | number | 🔴 | The amout of milliseconds that will be allowed the search to take before timeout. | 2000 |

## Example of use:

Expand Down
17 changes: 10 additions & 7 deletions examples/html/index.html
@@ -1,24 +1,27 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@tbogard/itunes-search example</title>
</head>

<body>
<pre>

</pre>
<script src="../../build/dist/itunesSearch.min.js"></script>
<script type="text/javascript">
const {searchSong} = iTunesSearch;
const search = async() => {
const result = await searchSong('La Camisa Negra - Juanes');
const formatted = JSON.stringify(result,null,2);
document.querySelector('pre').innerText = formatted
}
search();
const { searchSong } = iTunesSearch;
const search = async () => {
const result = await searchSong('La Camisa Negra - Juanes', { limit: 1, timeout: 5000 });
const formatted = JSON.stringify(result, null, 2);
document.querySelector('pre').innerText = formatted
}
search();
</script>
</body>

</html>
2 changes: 1 addition & 1 deletion examples/node/index.js
@@ -1,5 +1,5 @@
const { searchApp, searchAudiobook, searchBook, searchArtist } = require('../../dist/index');
searchArtist('Hikaru Utada').then(data => {
searchArtist('Hikaru Utada',{limit: 1,timeout:5000}).then(data => {
console.log(JSON.stringify(data,null,2))
})

22 changes: 12 additions & 10 deletions src/iTunesSearch/index.ts
Expand Up @@ -10,16 +10,12 @@ export class iTunesSearch implements IiTunesSearch {
options = {} as ISearchAllOptions;
defaultOptions = {
limit: 1,
country: "US",
language: "en",
country: 'US',
language: 'en',
attribute: null,
entity: null
} as ISearchAllOptions;

private iTunesFetch = axios.create({
baseURL: 'https://itunes.apple.com',
entity: null,
timeout: 2000
})
} as ISearchAllOptions;

searchAlbum = async (term: string, options?: ISearchAllOptions): Promise<ReturnType> =>
await this.performSearch(term, { ...options, ...{ entity: "album" } }, "searchAlbum")
Expand Down Expand Up @@ -52,12 +48,18 @@ export class iTunesSearch implements IiTunesSearch {
await this.performSearch(term, { ...options }, "searchAll")

private performSearch = async (term: string, options: ISearchAllOptions, validate:string): Promise<ReturnType> => {
const searchObject: any = this.validate(term, options, validate);
// eslint-disable-next-line no-useless-catch
try {
const searchObject: any = this.validate(term, options, validate);
const iTunesFetch = axios.create({
baseURL: 'https://itunes.apple.com',
timeout: searchObject?.timeout || 2000
});
if (searchObject.timeout) delete searchObject.timeout;

const querystring = new URLSearchParams(searchObject);
const searchQueryStr = `/search?term=${encodeURI(term)}&${querystring.toString()}`;
const { data } = await this.iTunesFetch.get(searchQueryStr);
const { data } = await iTunesFetch.get(searchQueryStr);
return data;
} catch (e) {
throw e;
Expand Down
2 changes: 2 additions & 0 deletions src/iTunesSearch/interfaces.ts
Expand Up @@ -11,6 +11,7 @@ export interface ISearchAllOptions {
attribute?: AttributeType | string | null | undefined;
language?: string | null | undefined;
country?: string | null | undefined;
timeout?: number | null | undefined;
}

export interface ISearchMandatoryAllOptions {
Expand All @@ -19,6 +20,7 @@ export interface ISearchMandatoryAllOptions {
attribute: AttributeType;
language?: string;
country?: string;
timeout?: number;
}

export interface IiTunesSearch {
Expand Down

0 comments on commit 82fb867

Please sign in to comment.