This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a single media-service data fetching object
- Loading branch information
Showing
7 changed files
with
82 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import ApiService from '~/data/api-service' | ||
|
||
import decodeMediaData from '~/utils/decode-media-data' | ||
import { AUDIO, IMAGE } from '~/constants/media' | ||
|
||
const slugs = { | ||
[AUDIO]: 'audio', | ||
[IMAGE]: 'images', | ||
} | ||
|
||
const MediaService = (mediaType) => ({ | ||
slug: slugs[mediaType], | ||
/** | ||
* Decodes the text data to avoid encoding problems. | ||
* Also, converts the results from an array of media objects into an object with | ||
* media id as keys. | ||
* @param data | ||
* @returns {*} | ||
*/ | ||
transformResults(data) { | ||
data.results = data.results.reduce((acc, item) => { | ||
acc[item.id] = decodeMediaData(item, mediaType) | ||
return acc | ||
}, {}) | ||
return data | ||
}, | ||
|
||
/** | ||
* Search for media items by keyword. | ||
* @param {Object} params | ||
* @return {Promise<{data: any}>} | ||
*/ | ||
search(params) { | ||
return ApiService.query(this.slug, params) | ||
}, | ||
|
||
/** | ||
* Retrieve media details by its id. | ||
* SSR-called | ||
* @param {object} params | ||
* @param {string} params.id | ||
* @return {Promise<{data: any}>} | ||
*/ | ||
getMediaDetail(params) { | ||
if (!params.id) { | ||
throw new Error( | ||
`MediaService.getMediaDetail() id parameter required to retrieve ${mediaType} details.` | ||
) | ||
} | ||
|
||
return ApiService.get(this.slug, params.id) | ||
}, | ||
|
||
/** | ||
* Retrieve related media | ||
* @param {object} params | ||
* @param {string} params.id | ||
* @return {Promise<{data: any}>} | ||
*/ | ||
getRelatedMedia(params) { | ||
if (!params.id) { | ||
throw new Error( | ||
`MediaService.getRelatedMedia() id parameter required to retrieve related media.` | ||
) | ||
} | ||
|
||
return ApiService.get(this.slug, `${params.id}/related`) | ||
}, | ||
}) | ||
|
||
export default MediaService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters