Files
flipper/src/utils/publicVersionChecker.tsx
Pascal Hartig 263b47f82f Add x-platform update indicator
Summary: Use the new multi-platform update endpoint to indicate when new updates are available for Linux or Windows.

Reviewed By: danielbuechele

Differential Revision: D16939899

fbshipit-source-id: 11c1dc0d4fd19362a1163c613a7b7116c5edf996
2019-08-22 02:32:56 -07:00

81 lines
1.8 KiB
TypeScript

/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
import os from 'os';
import config from '../fb-stubs/config';
const getPlatformSpecifier = (): string => {
switch (os.platform()) {
case 'win32':
return 'windows';
case 'linux':
return 'linux';
case 'darwin':
return 'mac';
default:
throw new Error('Unsupported platform.');
}
};
/**
* @param resp A parsed JSON object retrieved from the update server.
*/
const parseResponse = (resp: any): VersionCheckResult => {
const version = resp.version;
const platforms = resp.platforms;
if (!version || !platforms) {
return {kind: 'error', msg: 'Incomplete response.'};
}
const platformSpecifier = getPlatformSpecifier();
const platform = platforms[platformSpecifier];
if (!platform) {
return {kind: 'error', msg: `Unsupported platform: ${platformSpecifier}.`};
}
return {
kind: 'update-available',
url: platform,
version,
};
};
export type VersionCheckResult =
| {
kind: 'update-available';
url: string;
version: string;
}
| {
kind: 'up-to-date';
}
| {
kind: 'error';
msg: string;
};
export async function checkForUpdate(
currentVersion: string,
): Promise<VersionCheckResult> {
return fetch(`${config.updateServer}?version=${currentVersion}`).then(
(res: Response) => {
switch (res.status) {
case 204:
return {kind: 'up-to-date'};
case 200:
return res.json().then(parseResponse);
default:
return {
kind: 'error',
msg: `Server responded with ${res.statusText}.`,
};
}
},
);
}