-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use node-fetch instead of xmlhttprequest (#130)
* deps: Add node-fetch, remove xmlhttprequest * refactor: Use node-fetch instead of xmlhttprequest
- Loading branch information
Showing
5 changed files
with
104 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,34 @@ | ||
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; | ||
import fetch from 'node-fetch'; | ||
|
||
export default function getLatestVersion(): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
const xhr = new XMLHttpRequest(); | ||
const url: string = 'https://formulae.brew.sh/api/formula/hugo.json'; | ||
xhr.open('GET', url); | ||
xhr.send(); | ||
xhr.onreadystatechange = function() { | ||
if (xhr.readyState === 4 && xhr.status === 200) { | ||
const result = JSON.parse(xhr.responseText); | ||
const latestVersion: string = result.versions.stable; | ||
resolve(latestVersion); | ||
} else if (xhr.readyState === 4 && xhr.status !== 200) { | ||
reject(`ERROR: got status ${xhr.status} of ${url}`); | ||
} | ||
}; | ||
}); | ||
export function getURL(org: string, repo: string, api: string): string { | ||
let url: string = ''; | ||
|
||
if (api === 'brew') { | ||
url = `https://formulae.brew.sh/api/formula/${repo}.json`; | ||
} else if (api === 'github') { | ||
url = `https://api.github.com/repos/${org}/${repo}/releases/latest`; | ||
} | ||
|
||
return url; | ||
} | ||
|
||
export async function getLatestVersion( | ||
org: string, | ||
repo: string, | ||
api: string | ||
): Promise<string> { | ||
try { | ||
const url = getURL(org, repo, api); | ||
const response = await fetch(url); | ||
const json = await response.json(); | ||
let latestVersion: string = ''; | ||
if (api === 'brew') { | ||
latestVersion = json.versions.stable; | ||
} else if (api === 'github') { | ||
latestVersion = json.tag_name; | ||
} | ||
return latestVersion; | ||
} catch (e) { | ||
return e; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters