-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3482a56
commit da64d78
Showing
10 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# ofetch examples | ||
|
||
In this directory you can find some examples of how to use ofetch. | ||
|
||
<!-- To learn more, you can read the [ofetch first hand tutorial on unjs.io](https://unjs.io/resources/learn/ofetch-101-first-hand). --> |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ofetch } from "ofetch"; | ||
|
||
const response = await ofetch("https://api.github.com/markdown", { | ||
method: "POST", | ||
// To provide a body, we need to use the `body` option and just use an object. | ||
body: { | ||
text: "UnJS is **awesome**!\n\nCheck out their [website](https://unjs.io).", | ||
}, | ||
}); | ||
|
||
console.log(response); |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ofetch } from "ofetch"; | ||
|
||
try { | ||
await ofetch("https://api.github.com", { | ||
method: "POST", | ||
}); | ||
} catch (error) { | ||
// Error will be pretty printed | ||
console.error(error); | ||
|
||
// This allow us to access the error body | ||
console.log(error.data); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ofetch } from "ofetch"; | ||
|
||
const data = await ofetch("https://ungh.cc/repos/unjs/ofetch"); | ||
|
||
console.log(data); |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ofetch } from "ofetch"; | ||
|
||
const response = await ofetch("https://api.github.com/gists", { | ||
method: "POST", | ||
headers: { | ||
Authorization: `token ${process.env.GH_TOKEN}`, | ||
}, | ||
body: { | ||
description: "This is a gist created by ofetch.", | ||
public: true, | ||
files: { | ||
"unjs.txt": { | ||
content: "UnJS is awesome!", | ||
}, | ||
}, | ||
}, | ||
}); // Be careful, we use the GitHub API directly. | ||
|
||
console.log(response.url); |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ofetch } from "ofetch"; | ||
|
||
const response = await ofetch("https://api.github.com/gists", { | ||
method: "POST", | ||
}); // Be careful, we use the GitHub API directly. | ||
|
||
console.log(response); |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ofetch } from "ofetch"; | ||
|
||
const response = await ofetch("https://api.github.com/repos/unjs/ofetch/tags", { | ||
query: { | ||
per_page: 2, | ||
}, | ||
}); // Be careful, we use the GitHub API directly. | ||
|
||
console.log(response); |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// @ts-ignore | ||
import { ofetch } from "ofetch"; | ||
|
||
interface Repo { | ||
id: number; | ||
name: string; | ||
repo: string; | ||
description: string; | ||
stars: number; | ||
} | ||
|
||
async function main() { | ||
const { repo } = await ofetch<{ repo: Repo }>( | ||
"https://ungh.cc/repos/unjs/ofetch" | ||
); | ||
|
||
console.log(`The repo ${repo.name} has ${repo.stars} stars.`); // The repo object is now strongly typed. | ||
} | ||
|
||
// eslint-disable-next-line unicorn/prefer-top-level-await | ||
main().catch(console.error); |
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