Skip to content

Commit

Permalink
docs: add initial examples (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbapapazes authored Oct 26, 2023
1 parent 3482a56 commit da64d78
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 6 deletions.
5 changes: 5 additions & 0 deletions examples/README.md
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). -->
11 changes: 11 additions & 0 deletions examples/body.mjs
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);
13 changes: 13 additions & 0 deletions examples/error-handling.mjs
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);
}
5 changes: 5 additions & 0 deletions examples/first-request.mjs
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);
19 changes: 19 additions & 0 deletions examples/headers.mjs
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);
7 changes: 7 additions & 0 deletions examples/methods.mjs
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);
9 changes: 9 additions & 0 deletions examples/query-string.mjs
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);
21 changes: 21 additions & 0 deletions examples/type-safety.ts
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);
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
"scripts": {
"build": "unbuild",
"dev": "vitest",
"lint": "eslint --ext .ts . && prettier -c src test playground",
"lint:fix": "eslint --fix --ext .ts . && prettier -w src test playground",
"lint": "eslint --ext .ts . && prettier -c src test playground examples",
"lint:fix": "eslint --fix --ext .ts . && prettier -w src test playground examples",
"prepack": "pnpm build",
"play": "jiti playground/index.ts",
"release": "pnpm test && changelogen --release && npm publish && git push --follow-tags",
Expand Down Expand Up @@ -91,5 +91,5 @@
"unbuild": "2.0.0",
"vitest": "^0.34.6"
},
"packageManager": "pnpm@8.8.0"
}
"packageManager": "pnpm@8.9.2"
}
6 changes: 4 additions & 2 deletions playground/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ async function main() {
await $fetch("http://google.com/404");
}

main().catch((err) => {
console.error(err);
// eslint-disable-next-line unicorn/prefer-top-level-await
main().catch((error) => {
console.error(error);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
});

0 comments on commit da64d78

Please sign in to comment.