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 * Move slug creation from media service to the api service * Use typing data from #868 * Update src/constants/media.js Co-authored-by: sarayourfriend <24264157+sarayourfriend@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: sarayourfriend <24264157+sarayourfriend@users.noreply.github.com> * edit api-service styles; format * fix decode-media-data type import * Return decoded data from the media services Co-authored-by: sarayourfriend <24264157+sarayourfriend@users.noreply.github.com>
- Loading branch information
1 parent
c1eb220
commit e77826c
Showing
15 changed files
with
190 additions
and
209 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,83 @@ | ||
import ApiService from '~/data/api-service' | ||
|
||
import decodeMediaData from '~/utils/decode-media-data' | ||
|
||
/** | ||
* @template {import('../store/types').FrontendMediaType} [T=any] | ||
*/ | ||
class MediaService { | ||
/** | ||
* @param {T} mediaType | ||
*/ | ||
constructor(mediaType) { | ||
/** @type {T} */ | ||
this.mediaType = 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 {import('axios').AxiosResponse<T>} data | ||
* @returns {import('../store/types').MediaStoreResult<T>} | ||
*/ | ||
transformResults(data) { | ||
return { | ||
...data, | ||
results: data.results.reduce((acc, item) => { | ||
acc[item.id] = decodeMediaData(item, this.mediaType) | ||
return acc | ||
}, /** @type {Record<string, import('../store/types').DetailFromMediaType<T>>} */ ({})), | ||
} | ||
} | ||
|
||
/** | ||
* Search for media items by keyword. | ||
* @param {import('../store/types').ApiQueryParams} params | ||
* @return {Promise<import('axios').AxiosResponse<import('../store/types').MediaResult<T[]>>>} | ||
*/ | ||
async search(params) { | ||
const res = await ApiService.query(this.mediaType, params) | ||
return this.transformResults(res.data) | ||
} | ||
|
||
/** | ||
* Retrieve media details by its id. | ||
* SSR-called | ||
* @param {{ id: string }} params | ||
* @return {Promise<import('axios').AxiosResponse<import('../store/types').MediaResult<T>>>} | ||
*/ | ||
async getMediaDetail(params) { | ||
if (!params.id) { | ||
throw new Error( | ||
`MediaService.getMediaDetail() id parameter required to retrieve ${this.mediaType} details.` | ||
) | ||
} | ||
|
||
const res = await ApiService.get(this.mediaType, params.id) | ||
return decodeMediaData(res.data, this.mediaType) | ||
} | ||
|
||
/** | ||
* Retrieve related media | ||
* @param {{ id: string }} params | ||
* @return {Promise<import('axios').AxiosResponse<import('../store/types').MediaResult<T[]>>>} | ||
*/ | ||
async getRelatedMedia(params) { | ||
if (!params.id) { | ||
throw new Error( | ||
`MediaService.getRelatedMedia() id parameter required to retrieve related media.` | ||
) | ||
} | ||
|
||
const res = await ApiService.get(this.mediaType, `${params.id}/related`) | ||
return { | ||
...res.data, | ||
results: res.data.results.map((item) => | ||
decodeMediaData(item, this.mediaType) | ||
), | ||
} | ||
} | ||
} | ||
|
||
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
Oops, something went wrong.