Handle a response
ts
import { createHttpClient } from "@duplojs/http/client";
import { type Routes } from "./types";
const client = createHttpClient<Routes>({
baseUrl: "http://localhost:1506",
});
const response = await client
.get(
"/posts",
{ query: { page: "3" } },
)
.whenInformation(
"post.findMany",
({ body }) => {
},
)
.whenCode(
"422",
({ information }) => {
},
);
const eitherResponse = await client
.post(
"/users/{userId}/posts",
{
params: { userId: "1" },
body: {
title: "Super article",
content: "Super content of article",
},
},
)
.iWantInformationOrThrow("post.created");The object returned by the get, post, patch, put, delete, or request methods is called PromiseRequest. This object has many methods to handle responses depending on your needs:
- Methods starting with
whenare simple callbacks based on their configuration. - Methods starting with
iWantare promises that return anEitherof the requested result. - Methods ending with
OrThrowreturn only the successful result, but throw an error otherwise.
INFO
The PromiseRequest object cannot throw an error. By default, it will always return an Either.
