diff --git a/src/components/ActionBar/ActionBar.tsx b/src/components/ActionBar/ActionBar.tsx index f034a17..200acaa 100644 --- a/src/components/ActionBar/ActionBar.tsx +++ b/src/components/ActionBar/ActionBar.tsx @@ -9,6 +9,22 @@ import Tooltip from '@material-ui/core/Tooltip'; import { DetailContextConsumer, VersionPageConsumerProps } from '../../pages/version/Version'; import { Fab, ActionListItem } from './styles'; import { isURL } from '../../utils/url'; +import api from '../../utils/api'; + +export interface Action { + icon: string; + title: string; + handler?: Function; +} + +async function downloadHandler(link: string): Promise { + await api.request(link, 'GET', { + headers: { + ['accept']: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', + }, + credentials: 'include', + }); +} const ACTIONS = { homepage: { @@ -22,6 +38,7 @@ const ACTIONS = { tarball: { icon: , title: 'Download tarball', + handler: downloadHandler, }, }; @@ -57,13 +74,31 @@ class ActionBar extends Component { const renderList = Object.keys(actionsMap).reduce((component, value, key) => { const link = actionsMap[value]; if (link && isURL(link)) { - const fab = {ACTIONS[value]['icon']}; - component.push( - // @ts-ignore - - <>{this.renderIconsWithLink(link, fab)} - - ); + const actionItem: Action = ACTIONS[value]; + if (actionItem.handler) { + const fab = ( + + { + /* eslint-disable @typescript-eslint/no-non-null-assertion */ + actionItem.handler!(link); + }} + size={'small'}> + {actionItem['icon']} + + + ); + component.push(fab); + } else { + const fab = {actionItem['icon']}; + component.push( + // @ts-ignore + + <>{this.renderIconsWithLink(link, fab)} + + ); + } } return component; }, []); diff --git a/src/utils/api.ts b/src/utils/api.ts index eec6396..1ee08b6 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -25,7 +25,7 @@ function handleResponseType(response: Response): Promise<[boolean, Blob | string } class API { - public request(url: string, method = 'GET', options: RequestInit = { headers: {} }): Promise { + public request(url: string, method = 'GET', options: RequestInit = { headers: {} }, isFile: boolean = false): Promise { if (!window.VERDACCIO_API_URL) { throw new Error('VERDACCIO_API_URL is not defined!'); } @@ -50,11 +50,11 @@ class API { }) // @ts-ignore .then(handleResponseType) - .then(([responseOk, body]) => { - if (responseOk) { - resolve(body); + .then(response => { + if (response[0]) { + resolve(response[1]); } else { - reject(body); + reject(new Error('something went wrong')); } }) .catch(error => {