github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/util/cockroachlabsAPI.ts (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 /** 12 * This module contains all the REST endpoints for communicating with the admin UI. 13 */ 14 15 import moment from "moment"; 16 17 import { 18 VersionList, VersionCheckRequest, 19 } from "src/interfaces/cockroachlabs"; 20 import { withTimeout } from "./api"; 21 22 export const COCKROACHLABS_ADDR = "https://register.cockroachdb.com"; 23 24 interface FetchConfig { 25 method?: string; 26 timeout?: moment.Duration; 27 } 28 29 // TODO(maxlang): might be possible to consolidate with Fetch in api.ts 30 function timeoutFetch<T extends BodyInit, R>(url: string, req?: T, config: FetchConfig = {}): Promise<R> { 31 return withTimeout( 32 fetch(url, { 33 method: config.method || (req ? "POST" : "GET"), 34 headers: { 35 "Accept": "application/json", 36 "Content-Type": "application/json", 37 }, 38 body: req, 39 }), 40 config.timeout, 41 ).then((res) => { 42 if (!res.ok) { 43 throw Error(res.statusText); 44 } 45 return res.json() as Promise<R>; 46 }); 47 } 48 49 /** 50 * COCKROACH LABS ENDPOINTS 51 */ 52 53 export function versionCheck(request: VersionCheckRequest, timeout?: moment.Duration): Promise<VersionList> { 54 return timeoutFetch(`${COCKROACHLABS_ADDR}/api/clusters/updates?uuid=${request.clusterID}&version=${request.buildtag}`, null, { timeout }); 55 }