Home Reference Source

src/api/Api.js

  1. import HttpClient from './HttpClient';
  2. import SearchFetcher from './SearchFetcher';
  3. import TrackFetcher from './TrackFetcher';
  4. import AlbumFetcher from './AlbumFetcher';
  5. import ArtistFetcher from './ArtistFetcher';
  6. import FeaturedPlaylistFetcher from './FeaturedPlaylistFetcher';
  7. import FeaturedPlaylistCategoryFetcher from './FeaturedPlaylistCategoryFetcher';
  8. import NewReleaseCategoryFetcher from './NewReleaseCategoryFetcher';
  9. import NewHitsPlaylistFetcher from './NewHitsPlaylistFetcher';
  10. import GenreStationFetcher from './GenreStationFetcher';
  11. import MoodStationFetcher from './MoodStationFetcher';
  12. import ChartFetcher from './ChartFetcher';
  13. import SharedPlaylistFetcher from './SharedPlaylistFetcher';
  14.  
  15. /**
  16. * Fetch KKBOX resources.
  17. */
  18. export default class Api {
  19. /**
  20. * Need access token to initialize.
  21. *
  22. * @param {string} token - Get via Auth.
  23. * @param {string} [territory = 'TW'] - ['TW', 'HK', 'SG', 'MY', 'JP'] The territory for the fetcher.
  24. * @example new Api(token);
  25. * @example new Api(token, 'TW');
  26. */
  27. constructor(token, territory = 'TW') {
  28. this.territory = territory;
  29. this.httpClient = undefined;
  30. this.setToken(token);
  31. }
  32.  
  33. /**
  34. * Set new token and create fetchers with the new token.
  35. *
  36. * @param {string} token - Get via Auth.
  37. * @example api.setToken(token);
  38. */
  39. setToken(token) {
  40. this.httpClient = new HttpClient(token);
  41.  
  42. /**
  43. * @type {SearchFetcher}
  44. */
  45. this.searchFetcher = new SearchFetcher(this.httpClient, this.territory);
  46.  
  47. /**
  48. * @type {TrackFetcher}
  49. */
  50. this.trackFetcher = new TrackFetcher(this.httpClient, this.territory);
  51.  
  52. /**
  53. * @type {AlbumFetcher}
  54. */
  55. this.albumFetcher = new AlbumFetcher(this.httpClient, this.territory);
  56.  
  57. /**
  58. * @type {ArtistFetcher}
  59. */
  60. this.artistFetcher = new ArtistFetcher(this.httpClient, this.territory);
  61.  
  62. /**
  63. * @type {FeaturedPlaylistFetcher}
  64. */
  65. this.featuredPlaylistFetcher = new FeaturedPlaylistFetcher(
  66. this.httpClient,
  67. this.territory
  68. );
  69.  
  70. /**
  71. * @type {FeaturedPlaylistCategoryFetcher}
  72. */
  73. this.featuredPlaylistCategoryFetcher = new FeaturedPlaylistCategoryFetcher(
  74. this.httpClient,
  75. this.territory
  76. );
  77.  
  78. /**
  79. * @type {NewReleaseCategoryFetcher}
  80. */
  81. this.newReleaseCategoryFetcher = new NewReleaseCategoryFetcher(
  82. this.httpClient,
  83. this.territory
  84. );
  85.  
  86. /**
  87. * @type {NewHitsPlaylistFetcher}
  88. */
  89. this.newHitsPlaylistFetcher = new NewHitsPlaylistFetcher(
  90. this.httpClient,
  91. this.territory
  92. );
  93.  
  94. /**
  95. * @type {GenreStationFetcher}
  96. */
  97. this.genreStationFetcher = new GenreStationFetcher(
  98. this.httpClient,
  99. this.territory
  100. );
  101.  
  102. /**
  103. * @type {MoodStationFetcher}
  104. */
  105. this.moodStationFetcher = new MoodStationFetcher(
  106. this.httpClient,
  107. this.territory
  108. );
  109.  
  110. /**
  111. * @type {ChartFetcher}
  112. */
  113. this.chartFetcher = new ChartFetcher(this.httpClient, this.territory);
  114.  
  115. /**
  116. * @type {SharedPlaylistFetcher}
  117. */
  118. this.sharedPlaylistFetcher = new SharedPlaylistFetcher(
  119. this.httpClient,
  120. this.territory
  121. );
  122. }
  123. }