-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
179 lines (153 loc) · 7.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import axios from 'axios';
import { ReturnType } from "../typings/types";
import {
IiTunesSearch,
ISearchAllOptions
} from "./interfaces";
export class iTunesSearch implements IiTunesSearch {
options = {} as ISearchAllOptions;
defaultOptions = {
limit: 1,
country: 'US',
language: 'en',
attribute: null,
entity: null,
timeout: 2000
} as ISearchAllOptions;
searchAlbum = async (term: string, options?: ISearchAllOptions): Promise<ReturnType> =>
await this.performSearch(term, { ...options, ...{ entity: "album" } }, "searchAlbum")
searchSong = async (term: string, options?: ISearchAllOptions): Promise<ReturnType> =>
await this.performSearch(term, { ...options, ...{ media: "music" } }, "searchSong")
searchArtist = async (term: string, options?: ISearchAllOptions): Promise<ReturnType> =>
await this.performSearch(term, { ...options, ...{ entity: "allArtist", attribute: "allArtistTerm" } }, "searchArtist")
searchApp = async (term: string, options?: ISearchAllOptions): Promise<ReturnType> =>
await this.performSearch(term, { ...options, ...{ entity: "software"} }, "searchApp")
searchMovie = async (term: string, options?: ISearchAllOptions): Promise<ReturnType> =>
await this.performSearch(term, { ...options, ...{ entity: "movie"} }, "searchMovie")
searchMusicVideo = async (term: string, options?: ISearchAllOptions): Promise<ReturnType> =>
await this.performSearch(term, { ...options, ...{ media: "musicVideo"} }, "searchMusicVideo")
searchBook = async (term: string, options?: ISearchAllOptions): Promise<ReturnType> =>
await this.performSearch(term, { ...options, ...{ entity: "ebook"} }, "searchBook")
searchAudiobook = async (term: string, options?: ISearchAllOptions): Promise<ReturnType> =>
await this.performSearch(term, { ...options, ...{ entity: "audiobook"} }, "searchAudiobook")
searchPodcast = async (term: string, options?: ISearchAllOptions): Promise<ReturnType> =>
await this.performSearch(term, { ...options, ...{ entity: "podcast"} }, "searchPodcast")
searchAll = async (term: string, options?: ISearchAllOptions): Promise<ReturnType> =>
await this.performSearch(term, { ...options }, "searchAll")
private performSearch = async (term: string, options: ISearchAllOptions, validate:string): Promise<ReturnType | any> => {
const searchObject: any = this.validate(term, options, validate);
// eslint-disable-next-line no-useless-catch
try {
const iTunesFetch = axios.create({
baseURL: 'https://itunes.apple.com',
timeout: searchObject.timeout
});
if (searchObject.timeout) delete searchObject.timeout;
const querystring = new URLSearchParams(searchObject);
const searchQueryStr = `/search?term=${encodeURI(term)}&${querystring.toString()}`;
const { data } = await iTunesFetch.get(searchQueryStr);
return data;
} catch (e: any) {
if (axios.isAxiosError(e)) {
if (e.code) {
throw new Error(`Error: ${e.code}. Reference: https://www.ibm.com/docs/en/zos/2.2.0?topic=codes-sockets-return-errnos`);
} else {
throw new Error('Network Disconnected');
}
}
}
}
private validate(term:string, options:ISearchAllOptions, validate:string): ReturnType {
//cleaning up nulls
const newOptions:any = Object.fromEntries(
Object.entries({ ...this.defaultOptions, ...options })
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.filter(([ _, value ]) => value !== null)
);
// eslint-disable-next-line max-len
const isoCountry = new RegExp(/^(AD|AE|AF|AG|AI|AL|AM|AO|AQ|AR|AS|AT|AU|AW|AX|AZ|BA|BB|BD|BE|BF|BG|BH|BI|BJ|BL|BM|BN|BO|BQ|BR|BS|BT|BV|BW|BY|BZ|CA|CC|CD|CF|CG|CH|CI|CK|CL|CM|CN|CO|CR|CU|CV|CW|CX|CY|CZ|DE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d|sn|si|sk|sl|so|st|es|su|sw|ss|sv|ta|te|tg|th|ti|bo|tk|tl|tn|to|tr|ts|tt|tw|ty|ug|uk|ur|uz|ve|vi|vo|wa|cy|wo|fy|xh|yi|yo|za|zu)$/, "g");
const entityList: any[] = [
"album",
"allArtist",
"allTrack",
"audiobook",
"audiobookAuthor",
"ebook",
"iPadSoftware",
"macSoftware",
"mix",
"movie",
"movieArtist",
"musicArtist",
"musicTrack",
"musicVideo",
"podcast",
"podcastAuthor",
"shortFilm",
"shortFilmArtist",
"software",
"song",
"tvEpisode",
"tvSeason",
];
const attributeList: any[] = [
"actorTerm",
"albumTerm",
"allArtistTerm",
"allTrackTerm",
"artistTerm",
"authorTerm",
"composerTerm",
"descriptionTerm",
"directorTerm",
"featureFilmTerm",
"genreIndex",
"keywordsTerm",
"languageTerm",
"mixTerm",
"movieArtistTerm",
"movieTerm",
"producerTerm",
"ratingIndex",
"ratingTerm",
"releaseYearTerm",
"shortFilmTerm",
"showTerm",
"softwareDeveloper",
"songTerm",
"titleTerm",
"tvEpisodeTerm",
"tvSeasonTerm",
];
if (validate === 'searchAll') {
if (newOptions.entity && !entityList.includes(newOptions.entity))
throw new TypeError(`The entity with value ${JSON.stringify(newOptions.entity)} is not valid.`
+ ` Expected one of the following list ${JSON.stringify(entityList)}`);
if (newOptions.attribute && !attributeList.includes(newOptions.attribute))
throw new TypeError(`The attribute with value ${JSON.stringify(newOptions.attribute)} is not valid.`
+ ` Expected one of the following list ${JSON.stringify(attributeList)}`);
if (!newOptions.entity || !newOptions.attribute)
throw new TypeError('searchAll expects to have both "entity" and "attribute" values in the options object');
}
if (typeof term !== "string" || !term.trim().length)
throw new TypeError('A "term" is a string required on any search. "term" cannot have empty spaces as well.');
if (!Number.isInteger(newOptions.limit))
throw new TypeError(`Optional argument "limit" must be a number`
+`. Passed ${ typeof newOptions.limit }(${ newOptions.limit })`);
if (!isoCountry.test(newOptions.country as string))
throw new TypeError(
`Optional argument "country" must be a string and should be a valid ISO 3166-1 `
+`Alpha 2 country code (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). `
+`Passed ${ typeof newOptions.country }( ${ JSON.stringify(newOptions.country) } )`
);
if (!isoLanguage.test(newOptions.language as string))
throw new TypeError(
`Optional argument "language" must be a string and should be a valid ISO 639-1`
+ ` language code (https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes). `
+`Passed ${ typeof newOptions.language }( ${ JSON.stringify(newOptions.language) } )`
);
return newOptions;
}
}