podcast-api 1.1.1 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,18 +1,14 @@
1
1
  {
2
2
  "name": "podcast-api",
3
- "version": "1.1.1",
3
+ "version": "2.0.1",
4
4
  "description": "JavaScript bindings for the Listen Notes Podcast API",
5
5
  "main": "src/PodcastApiClient.js",
6
6
  "scripts": {
7
7
  "clean": "rm -rf ./.nyc_output ./node_modules/.cache",
8
8
  "lint": "eslint --ext .js,.jsx .",
9
- "test": "jest"
10
- },
11
- "jest": {
12
- "testMatch": [
13
- "**/tests/*Test.js"
14
- ],
15
- "testEnvironment": "node"
9
+ "test": "jest --config jest.config.node.json && jest --config jest.config.minifare.json",
10
+ "test-node": "jest --config jest.config.node.json",
11
+ "test-workers": "jest --config jest.config.minifare.json"
16
12
  },
17
13
  "repository": {
18
14
  "type": "git",
@@ -23,7 +19,9 @@
23
19
  "podcast",
24
20
  "api",
25
21
  "podcast search",
26
- "search engine"
22
+ "search engine",
23
+ "Cloudflare Workers",
24
+ "Node.js"
27
25
  ],
28
26
  "engines": {
29
27
  "node": ">=10.*"
@@ -33,16 +31,16 @@
33
31
  "bugs": {
34
32
  "url": "https://github.com/ListenNotes/podcast-api-js/issues"
35
33
  },
36
- "homepage": "https://github.com/ListenNotes/podcast-api-js",
34
+ "homepage": "https://www.listennotes.com/api/",
37
35
  "dependencies": {
38
- "axios": "^0.21.1",
39
- "query-string": "^7.0.1"
36
+ "axios": "^0.21.1"
40
37
  },
41
38
  "devDependencies": {
42
39
  "eslint": "^7.25.0",
43
40
  "eslint-plugin-import": "^2.22.1",
44
41
  "eslint-plugin-jsx-a11y": "^6.4.1",
45
42
  "jest": "^26.6.3",
43
+ "jest-environment-miniflare": "^2.10.0",
46
44
  "mocha": "^8.3.2",
47
45
  "prettier": "^2.2.1"
48
46
  }
@@ -0,0 +1,11 @@
1
+ const API_BASE_PROD = 'https://listen-api.listennotes.com/api/v2';
2
+
3
+ const API_BASE_TEST = 'https://listen-api-test.listennotes.com/api/v2';
4
+
5
+ const defaultUserAgent = 'podcasts-api-js';
6
+
7
+ module.exports = {
8
+ API_BASE_PROD,
9
+ API_BASE_TEST,
10
+ defaultUserAgent,
11
+ };
@@ -1,107 +1,8 @@
1
- const axios = require('axios');
2
- const queryString = require('query-string');
3
-
4
- const API_BASE_PROD = 'https://listen-api.listennotes.com/api/v2';
5
-
6
- const API_BASE_TEST = 'https://listen-api-test.listennotes.com/api/v2';
7
-
8
- const defaultUserAgent = 'podcasts-api-js';
9
-
10
- const Client = (config = {}) => {
11
- this.config = config;
12
-
13
- axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
14
- this.httpClient = axios.create({
15
- baseURL: config.apiKey ? API_BASE_PROD : API_BASE_TEST,
16
- timeout: 30000,
17
- headers: {
18
- 'X-ListenAPI-Key': config.apiKey || '',
19
- 'User-Agent': config.userAgent || defaultUserAgent,
20
- },
21
- });
22
-
23
- this._post = (path, params) => this.httpClient.post(path, queryString.stringify(params));
24
-
25
- this.search = (params) => this.httpClient.get('/search', { params });
26
-
27
- this.typeahead = (params) => this.httpClient.get('/typeahead', { params });
28
-
29
- this.spellcheck = (params) => this.httpClient.get('/spellcheck', { params });
30
-
31
- this.fetchRelatedSearches = (params) => this.httpClient.get('/related_searches', { params });
32
-
33
- this.fetchTrendingSearches = (params) => this.httpClient.get('/trending_searches', { params });
34
-
35
- this.fetchBestPodcasts = (params) => this.httpClient.get('/best_podcasts', { params });
36
-
37
- this.fetchPodcastById = (params) => {
38
- const { id, ...otherParams } = params;
39
- return this.httpClient.get(`/podcasts/${id}`, {
40
- params: otherParams,
41
- });
42
- };
43
-
44
- this.fetchEpisodeById = (params) => {
45
- const { id, ...otherParams } = params;
46
- return this.httpClient.get(`/episodes/${id}`, {
47
- params: otherParams,
48
- });
49
- };
50
-
51
- this.batchFetchPodcasts = (params) => this._post('/podcasts', params);
52
-
53
- this.batchFetchEpisodes = (params) => this._post('/episodes', params);
54
-
55
- this.fetchCuratedPodcastsListById = (params) => {
56
- const { id, ...otherParams } = params;
57
- return this.httpClient.get(`/curated_podcasts/${id}`, {
58
- params: otherParams,
59
- });
60
- };
61
-
62
- this.fetchCuratedPodcastsLists = (params) => this.httpClient.get('/curated_podcasts', { params });
63
-
64
- this.fetchPodcastGenres = (params) => this.httpClient.get('/genres', { params });
65
-
66
- this.fetchPodcastRegions = (params) => this.httpClient.get('/regions', { params });
67
-
68
- this.fetchPodcastLanguages = (params) => this.httpClient.get('/languages', { params });
69
-
70
- this.justListen = (params) => this.httpClient.get('/just_listen', { params });
71
-
72
- this.fetchRecommendationsForPodcast = (params) => {
73
- const { id, ...otherParams } = params;
74
- return this.httpClient.get(`/podcasts/${id}/recommendations`, {
75
- params: otherParams,
76
- });
77
- };
78
-
79
- this.fetchRecommendationsForEpisode = (params) => {
80
- const { id, ...otherParams } = params;
81
- return this.httpClient.get(`/episodes/${id}/recommendations`, {
82
- params: otherParams,
83
- });
84
- };
85
-
86
- this.fetchPlaylistById = (params) => {
87
- const { id, ...otherParams } = params;
88
- return this.httpClient.get(`/playlists/${id}`, {
89
- params: otherParams,
90
- });
91
- };
92
-
93
- this.fetchMyPlaylists = (params) => this.httpClient.get('/playlists', { params });
94
-
95
- this.submitPodcast = (params) => this._post('/podcasts/submit', params);
96
-
97
- this.deletePodcast = (params) => {
98
- const { id, reason } = params;
99
- return this.httpClient.delete(`/podcasts/${id}?reason=${reason || ''}`);
100
- };
101
-
102
- return this;
103
- };
1
+ const { ClientForNode } = require('./PodcastApiClientForNode');
2
+ const { ClientForWorkers } = require('./PodcastApiClientForWorkers');
104
3
 
105
4
  module.exports = {
106
- Client,
5
+ Client: ClientForNode,
6
+ ClientForNode,
7
+ ClientForWorkers,
107
8
  };
@@ -0,0 +1,26 @@
1
+ const { API_BASE_PROD, API_BASE_TEST, defaultUserAgent } = require('./Constants');
2
+ const { addApiMethodsToClient } = require('./PodcastApiMethods');
3
+
4
+ const ClientForNode = (config = {}) => {
5
+ const axios = require('axios');
6
+
7
+ axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
8
+ this.httpClient = axios.create({
9
+ baseURL: config.apiKey ? API_BASE_PROD : API_BASE_TEST,
10
+ timeout: 30000,
11
+ headers: {
12
+ 'X-ListenAPI-Key': config.apiKey || '',
13
+ 'User-Agent': config.userAgent || defaultUserAgent,
14
+ },
15
+ });
16
+
17
+ this.httpClient._get = (path, params) => this.httpClient.get(path, { params });
18
+ this.httpClient._post = (path, params) => this.httpClient.post(path, new URLSearchParams(params).toString());
19
+ this.httpClient._delete = (path) => this.httpClient.delete(path);
20
+
21
+ return addApiMethodsToClient(this);
22
+ };
23
+
24
+ module.exports = {
25
+ ClientForNode,
26
+ };
@@ -0,0 +1,83 @@
1
+ const {API_BASE_PROD, API_BASE_TEST, defaultUserAgent} = require('./Constants');
2
+ const {addApiMethodsToClient} = require('./PodcastApiMethods');
3
+
4
+ const _fetch = (path, config, method = 'GET', queryParams = {}, formParams = null) => {
5
+ let url = `${config.apiKey ? API_BASE_PROD : API_BASE_TEST}${path}`;
6
+ const headers = {
7
+ 'X-ListenAPI-Key': config.apiKey || '',
8
+ 'User-Agent': config.userAgent || defaultUserAgent,
9
+ };
10
+ const fetchConfig = {
11
+ method,
12
+ headers,
13
+ };
14
+ let formParamsString = null;
15
+ if (formParams) {
16
+ fetchConfig.body = formParams;
17
+ formParamsString = [...formParams.entries()].map(
18
+ x => `${encodeURIComponent(x[0])}=${encodeURIComponent(x[1])}`).join('&');
19
+ }
20
+ if (queryParams) {
21
+ url = `${url}?${new URLSearchParams(queryParams).toString()}`
22
+ }
23
+ const responseConfig = {
24
+ params: queryParams,
25
+ data: formParamsString,
26
+ url: path,
27
+ method: method.toLowerCase(),
28
+ };
29
+ const responseHeaders = {};
30
+ return fetch(url, fetchConfig).then((response) => {
31
+ if (response.headers) {
32
+ for (const pair of response.headers.entries()) {
33
+ responseHeaders[pair[0]] = pair[1];
34
+ }
35
+ }
36
+
37
+ if (response.ok) {
38
+ return response;
39
+ } else {
40
+ const err = new Error(`HTTP ${response.status}`);
41
+ err.response = response;
42
+ err.response.config = responseConfig;
43
+ err.response.headers = responseHeaders;
44
+ throw err;
45
+ }
46
+ }).then((response) => {
47
+ return response.json();
48
+ }).then((data) => {
49
+ return {
50
+ config: responseConfig,
51
+ headers: responseHeaders,
52
+ data,
53
+ }
54
+ });
55
+ };
56
+
57
+ const ClientForWorkers = (config = {}) => {
58
+ this.httpClient = {
59
+ _get: (path, params) => {
60
+ return _fetch(path, config, 'GET', params);
61
+ },
62
+
63
+ _post: (path, params) => {
64
+ let formData = null;
65
+ if (params && Object.keys(params).length > 0) {
66
+ formData = new FormData();
67
+ Object.keys(params).forEach(function (key) {
68
+ formData.append(key, params[key]);
69
+ });
70
+ }
71
+ return _fetch(path, config, 'POST', {}, formData)
72
+ },
73
+
74
+ _delete: (path) => {
75
+ return _fetch(path, config, 'DELETE')
76
+ },
77
+ };
78
+ return addApiMethodsToClient(this);
79
+ };
80
+
81
+ module.exports = {
82
+ ClientForWorkers,
83
+ };
@@ -0,0 +1,77 @@
1
+ const addApiMethodsToClient = (client) => {
2
+ client.search = (params) => client.httpClient._get('/search', params);
3
+
4
+ client.typeahead = (params) => client.httpClient._get('/typeahead', params);
5
+
6
+ client.spellcheck = (params) => client.httpClient._get('/spellcheck', params);
7
+
8
+ client.fetchRelatedSearches = (params) => client.httpClient._get('/related_searches', params);
9
+
10
+ client.fetchTrendingSearches = (params) => client.httpClient._get('/trending_searches', params);
11
+
12
+ client.fetchBestPodcasts = (params) => client.httpClient._get('/best_podcasts', params);
13
+
14
+ client.fetchPodcastById = (params) => {
15
+ const { id, ...otherParams } = params;
16
+ return client.httpClient._get(`/podcasts/${id}`, otherParams);
17
+ };
18
+
19
+ client.fetchEpisodeById = (params) => {
20
+ const { id, ...otherParams } = params;
21
+ return client.httpClient._get(`/episodes/${id}`, otherParams);
22
+ };
23
+
24
+ client.batchFetchPodcasts = (params) => client.httpClient._post('/podcasts', params);
25
+
26
+ client.batchFetchEpisodes = (params) => client.httpClient._post('/episodes', params);
27
+
28
+ client.fetchCuratedPodcastsListById = (params) => {
29
+ const { id, ...otherParams } = params;
30
+ return client.httpClient._get(`/curated_podcasts/${id}`, otherParams);
31
+ };
32
+
33
+ client.fetchCuratedPodcastsLists = (params) => client.httpClient._get('/curated_podcasts', params);
34
+
35
+ client.fetchPodcastGenres = (params) => client.httpClient._get('/genres', params);
36
+
37
+ client.fetchPodcastRegions = (params) => client.httpClient._get('/regions', params);
38
+
39
+ client.fetchPodcastLanguages = (params) => client.httpClient._get('/languages', params);
40
+
41
+ client.justListen = (params) => client.httpClient._get('/just_listen', params);
42
+
43
+ client.fetchRecommendationsForPodcast = (params) => {
44
+ const { id, ...otherParams } = params;
45
+ return client.httpClient._get(`/podcasts/${id}/recommendations`, otherParams);
46
+ };
47
+
48
+ client.fetchRecommendationsForEpisode = (params) => {
49
+ const { id, ...otherParams } = params;
50
+ return client.httpClient._get(`/episodes/${id}/recommendations`, otherParams);
51
+ };
52
+
53
+ client.fetchPlaylistById = (params) => {
54
+ const { id, ...otherParams } = params;
55
+ return client.httpClient._get(`/playlists/${id}`, otherParams);
56
+ };
57
+
58
+ client.fetchMyPlaylists = (params) => client.httpClient._get('/playlists', params);
59
+
60
+ client.submitPodcast = (params) => client.httpClient._post('/podcasts/submit', params);
61
+
62
+ client.deletePodcast = (params) => {
63
+ const { id, reason } = params;
64
+ return client.httpClient._delete(`/podcasts/${id}?reason=${reason || ''}`);
65
+ };
66
+
67
+ client.fetchAudienceForPodcast = (params) => {
68
+ const { id, ...otherParams } = params;
69
+ return client.httpClient._get(`/podcasts/${id}/audience`, otherParams);
70
+ };
71
+
72
+ return client;
73
+ };
74
+
75
+ module.exports = {
76
+ addApiMethodsToClient,
77
+ };