Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add initial examples #288

Merged
merged 12 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"extends": ["eslint-config-unjs"],
"extends": [
"eslint-config-unjs"
],
"rules": {
"no-undef": 0,
"unicorn/consistent-destructuring": 0,
"unicorn/no-await-expression-member": 0
"unicorn/no-await-expression-member": 0,
"unicorn/prefer-top-level-await": 0
}
}
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-firs-hand).
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To learn more, you can read the [ofetch first hand tutorial on unjs.io](https://unjs.io/resources/learn/ofetch-101-firs-hand).
To learn more, you can read the [ofetch first hand tutorial on unjs.io](https://unjs.io/resources/learn/ofetch-101-first-hand).

15 changes: 15 additions & 0 deletions examples/body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { $fetch } from "ofetch";

async function main() {
const response = await $fetch<string>("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).",
},
}); // Be careful, we use the GitHub API.

console.log(response);
}

main().catch(console.error);
13 changes: 13 additions & 0 deletions examples/error-handling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { $fetch } from "ofetch";

async function main() {
try {
await $fetch("https://api.github.com", {
method: "POST",
});
} catch (error) {
console.log(error.data); // This allow us to get the error body.
}
}

main().catch(console.error);
9 changes: 9 additions & 0 deletions examples/first-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { $fetch } from "ofetch";

async function main() {
const data = await $fetch("https://ungh.cc/repos/unjs/ofetch");

console.log(data);
}

main().catch(console.error);
23 changes: 23 additions & 0 deletions examples/headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { $fetch } from "ofetch";

async function main() {
const response = await $fetch("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);
}

main().catch(console.error);
11 changes: 11 additions & 0 deletions examples/methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { $fetch } from "ofetch";

async function main() {
const response = await $fetch("https://api.github.com/gists", {
method: "POST",
}); // Be careful, we use the GitHub API directly.

console.log(response);
}

main().catch(console.error);
7 changes: 7 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "examples",
"dependencies": {
"jiti": "^1.19.3",
"ofetch": "workspace:../src"
}
}
16 changes: 16 additions & 0 deletions examples/query-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { $fetch } from "ofetch";

async function main() {
const response = await $fetch(
"https://api.github.com/repos/unjs/ofetch/tags",
{
query: {
per_page: 2,
},
}
); // Be careful, we use the GitHub API directly.

console.log(response);
}

main().catch(console.error);
19 changes: 19 additions & 0 deletions examples/type-safety.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { $fetch } from "ofetch";

interface Repo {
id: number;
name: string;
repo: string;
description: string;
stars: number;
}

async function main() {
const { repo } = await $fetch<{ 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.
}

main().catch(console.error);
6 changes: 3 additions & 3 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 @@ -92,4 +92,4 @@
"vitest": "^0.34.3"
},
"packageManager": "[email protected]"
}
}
2 changes: 1 addition & 1 deletion playground/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $fetch } from "../src/node";
import { $fetch } from "ofetch";

async function main() {
// const r = await $fetch<string>('http://google.com/404')
Expand Down
6 changes: 6 additions & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "playground",
"dependencies": {
"ofetch": "workspace:../src"
}
}
132 changes: 74 additions & 58 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
packages:
- "src"
- "playground"
- "examples"