/
St.Anton
/
marvel_starter
Обзор
Документация
Войти
/
St.Anton
/
marvel_starter
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/services/MarvelService.js
100 строк
3 KB
St.Anton
Adds Character Finder and single character information display
11 июл 2022, 18:13
11 июл 2022, 18:13
4071487
Код
Авторство
О чём код?
import { useHttp } from "../hooks/http.hook"; const useMarvelService = () => { const { error, loading, request, clearError } = useHttp(); const _apiBase = "https://gateway.marvel.com:443/v1/public/"; const _apiKey = "apikey=5384c4510ace48c6fd92d05377104684"; const _baseOffset = 210; const _baseLimit = 9; const _apiOffsets = (count) => { return `offset=${count}`; }; const _apiLimit = (limit) => { return `limit=${limit}`; }; // Characters const getAllCharacters = async ( apiLimit = _baseLimit, baseOffset = _baseOffset ) => { const _urlCharacters = `${_apiBase}characters?${_apiLimit( apiLimit )}&${_apiOffsets(baseOffset)}&${_apiKey}`; const res = await request(_urlCharacters); return res.data.results.map(_transformCharacter); }; const getCharacterByName = async (name) => { const res = await request( `${_apiBase}characters?name=${name}&${_apiKey}` ); return res.data.results.map(_transformCharacter); }; const getCharacter = async (id) => { const _urlCharacter = `${_apiBase}characters/${id}?${_apiKey}`; const res = await request(_urlCharacter); return _transformCharacter(res.data.results[0]); }; //Comics const getAllComics = async (apiLimit = 8, baseOffset = 0) => { const _urlComics = `${_apiBase}comics?${_apiLimit( apiLimit )}&${_apiOffsets(baseOffset)}&${_apiKey}`; const res = await request(_urlComics); return res.data.results.map(_transformComics); }; const getComic = async (id) => { const _urlComic = `${_apiBase}comics/${id}?${_apiKey}`; const res = await request(_urlComic); return _transformComics(res.data.results[0]); }; const _transformCharacter = (char) => { return { id: char.id, name: char.name, description: char.description, thumbnail: `${char.thumbnail.path}.${char.thumbnail.extension}`, homepage: char.urls[0].url, wiki: char.urls[1].url, comics: char.comics.items, }; }; const _transformComics = (comic) => { return { id: comic.id, title: comic.title, description: comic.description || "There is no description", pageCount: comic.pageCount ? `${comic.pageCount} p.` : "No information about the number of pages", thumbnail: `${comic.thumbnail.path}.${comic.thumbnail.extension}`, language: comic.textObjects.language || "en-us", price: comic.prices[0].price ? `${comic.prices[0].price}$` : "not available", }; }; return { clearError, error, getAllCharacters, getAllComics, getCharacter, getCharacterByName, getComic, loading, }; }; export default useMarvelService;