diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..cd7fb420 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,5 @@ +# ofetch examples + +In this directory you can find some examples of how to use ofetch. + + diff --git a/examples/body.mjs b/examples/body.mjs new file mode 100644 index 00000000..5132c4d1 --- /dev/null +++ b/examples/body.mjs @@ -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); diff --git a/examples/error-handling.mjs b/examples/error-handling.mjs new file mode 100644 index 00000000..4a0fcfdf --- /dev/null +++ b/examples/error-handling.mjs @@ -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); +} diff --git a/examples/first-request.mjs b/examples/first-request.mjs new file mode 100644 index 00000000..29d3e300 --- /dev/null +++ b/examples/first-request.mjs @@ -0,0 +1,5 @@ +import { ofetch } from "ofetch"; + +const data = await ofetch("https://ungh.cc/repos/unjs/ofetch"); + +console.log(data); diff --git a/examples/headers.mjs b/examples/headers.mjs new file mode 100644 index 00000000..264eb285 --- /dev/null +++ b/examples/headers.mjs @@ -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); diff --git a/examples/methods.mjs b/examples/methods.mjs new file mode 100644 index 00000000..fdb3bac6 --- /dev/null +++ b/examples/methods.mjs @@ -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); diff --git a/examples/query-string.mjs b/examples/query-string.mjs new file mode 100644 index 00000000..5d3f1bc3 --- /dev/null +++ b/examples/query-string.mjs @@ -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); diff --git a/examples/type-safety.ts b/examples/type-safety.ts new file mode 100644 index 00000000..4057e55e --- /dev/null +++ b/examples/type-safety.ts @@ -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); diff --git a/package.json b/package.json index 690d4e54..d285a299 100644 --- a/package.json +++ b/package.json @@ -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", @@ -91,5 +91,5 @@ "unbuild": "2.0.0", "vitest": "^0.34.6" }, - "packageManager": "pnpm@8.8.0" -} \ No newline at end of file + "packageManager": "pnpm@8.9.2" +} diff --git a/playground/index.mjs b/playground/index.mjs index 038299d2..c304c9ad 100644 --- a/playground/index.mjs +++ b/playground/index.mjs @@ -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); });