Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / lib / apps / desktopVersions.ts
blob978b91b5b34d7e9bccb9668e7c0ffa1b8ded3958
1 import type { DESKTOP_APP_NAMES, DESKTOP_PLATFORMS, RELEASE_CATEGORIES } from '@proton/shared/lib/constants';
2 import { getDownloadUrl } from '@proton/shared/lib/helpers/url';
4 export interface Props {
5     appName: DESKTOP_APP_NAMES;
6     platform: DESKTOP_PLATFORMS;
7     version: 'latest' | string;
8     category: RELEASE_CATEGORIES;
11 interface RELEASE {
12     CategoryName: RELEASE_CATEGORIES;
13     Version: string;
14     ReleaseDate: string;
15     File: {
16         Url: string;
17         Sha512CheckSum: 'string';
18         Args: string;
19     };
20     ReleaseNotes: {
21         Type: string;
22         Notes: string[];
23     }[];
27  * This helper will help to fetch specific or latest version of a desktop app
28  * It currently only support Drive apps.
29  */
30 export const fetchDesktopVersion = async ({
31     appName,
32     platform,
33     category,
34     version,
35 }: Props): Promise<
36     | {
37           url: string;
38           version: string;
39       }
40     | undefined
41 > => {
42     try {
43         const response = await fetch(getDownloadUrl(`/${appName}/${platform}/version.json`));
44         if (!response.ok) {
45             throw new Error(response.statusText);
46         }
47         const { Releases: releases }: { Releases: RELEASE[] } = await response.json();
49         const filteredReleases = releases.filter((release) => release.CategoryName === category);
50         if (!filteredReleases.length) {
51             return undefined;
52         }
53         if (version === 'latest') {
54             // We assume that latest release is always the first one
55             const latestRelease = filteredReleases[0];
56             return { url: latestRelease.File.Url, version: latestRelease.Version };
57         } else {
58             const release = filteredReleases.find((release) => release.Version === version);
59             if (!release) {
60                 return undefined;
61             }
62             return { url: release.File.Url, version: release.Version };
63         }
64     } catch (e: any) {
65         throw Error(`Failed to fetch ${appName} ${platform}@${version} app's url`, {
66             cause: e,
67         });
68     }