Created
July 22, 2020 10:54
-
-
Save jbergant/acbc680fda0f02439627ae6beb319091 to your computer and use it in GitHub Desktop.
Deno fetch examples
This file contains hidden or 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
| // Fetch Watson Tone Analyzer. Use basic key authentocation | |
| // Get key and url here: https://www.ibm.com/watson/services/tone-analyzer/ | |
| const sentimentAnalysis = async (text: string): Promise<any> => { | |
| const env = Deno.env.toObject(); | |
| const SENTIMENT_KEY = env.SENTIMENT_KEY || "sentish"; | |
| const SENTIMENT_URI = env.SENTIMENT_URI || "localhost"; | |
| const response = await fetch( | |
| `${SENTIMENT_URI}&text=${encodeURI(text)}`, | |
| { | |
| method: "GET", | |
| headers: { | |
| "Authorization": `Basic ${btoa("apikey:" + SENTIMENT_KEY)}`, | |
| }, | |
| }, | |
| ) | |
| .then((response) => response.json()) | |
| .then((data) => { | |
| return data; | |
| }) | |
| .catch((error) => { | |
| console.error("Error:", error); | |
| }); | |
| return response; | |
| }; | |
| console.log(await sentimentAnalysis("I love you baby")); | |
| // FETCH repositories from github | |
| const githubResponse = async (): Promise<any> => { | |
| const response = await fetch( | |
| "https://api.github.com/search/repositories?q=deno", | |
| { | |
| method: "GET", | |
| }, | |
| ) | |
| .then((response) => response.json()) | |
| .then((data) => { | |
| return data; | |
| }) | |
| .catch((error) => { | |
| console.error("Error:", error); | |
| }); | |
| return response; | |
| }; | |
| console.log(await githubResponse()); | |
| // fetch Dummy users | |
| const getUser = async (url: string): Promise<void> => { | |
| return await (await fetch(url)).json(); | |
| }; | |
| const users = await getUser("https://jsonplaceholder.typicode.com/users"); | |
| console.log(users); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment