Skip to content

Commit

Permalink
doc: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Bekacru committed Jul 25, 2024
1 parent 93d95b7 commit ef6d816
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion doc/content/docs/fetch-schema.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ import { z } from "zod";
const $fetch = createFetch({
baseUrl: "https://jsonplaceholder.typicode.com",
schema: createSchema({
"/@put/user": { // [!code highlight]
"@put/user": { // [!code highlight]
input: z.object({
title: z.string(),
completed: z.boolean(),
Expand Down
7 changes: 5 additions & 2 deletions doc/content/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ Learn more about handling errors [Handling Errors](/docs/handling-errors) sectio

### Fetch Schema

The fetch schema enables you to pre-define the URL path and the shape of request and response data. This makes it easy to document your API. With plugins, you can even share these schemas with other as a package.
Fetch schema enables you to pre-define the URL path and the shape of request and response data. This makes it easy to document your API.

<Callout type="info">
Plugins can also define fetch schemas. See [Plugins](/docs/plugins) section for more details.
</Callout>

The output of the scheam will be validated using zod and if the validation fails, it'll throw an error.

Expand Down Expand Up @@ -132,7 +136,6 @@ const $fetch = createFetch({
schema: schema // [!code highlight]
});


const { data, error } = await $fetch("/path", {
body: {
userId: "1",
Expand Down
4 changes: 1 addition & 3 deletions packages/better-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
"require": "./dist/index.cjs"
}
},
"files": [
"dist"
],
"files": ["dist"],
"type": "module"
}
41 changes: 40 additions & 1 deletion packages/better-fetch/src/test/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createApp, toNodeListener } from "h3";
import { type Listener, listen } from "listhen";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { BetterFetchError, betterFetch, createFetch } from "..";
import { router } from "./test-router";

Expand Down Expand Up @@ -278,6 +278,45 @@ describe("fetch-error", () => {
});
});

describe.only("hooks", () => {
it("should call onRequest and onResponse", async () => {
const onRequest = vi.fn();
const onResponse = vi.fn();
const f = createFetch({
baseURL: "http://localhost:4001",
customFetchImpl: async (req, init) => {
return new Response(JSON.stringify({ message: "ok" }));
},
onRequest,
onResponse,
});
await f("/ok");
expect(onRequest).toHaveBeenCalled();
expect(onResponse).toHaveBeenCalled();
});

it("should call onError", async () => {
const onError = vi.fn();
const onResponse = vi.fn();
const onSuccess = vi.fn();
const f = createFetch({
baseURL: "http://localhost:4001",
customFetchImpl: async (req, init) => {
return new Response(null, {
status: 500,
});
},
onError,
onResponse,
onSuccess,
});
await f("/ok");
expect(onError).toHaveBeenCalled();
expect(onResponse).toHaveBeenCalled();
expect(onSuccess).not.toHaveBeenCalled();
});
});

describe("fetch-error-throw", () => {
const f = createFetch({
baseURL: "http://localhost:4001",
Expand Down

0 comments on commit ef6d816

Please sign in to comment.