API reference / @backpack/rest-client
@backpack/rest-client
️ 🌍
A pre-configured REST client built on top of Wretch, enhanced with additional middleware for extended functionality.
Installing
First, ensure you have set up Nexus as private registry needed to use Backpack.
Then, install the package using npm:
bash
npm install @backpack/rest-client
Example usage
Wretch is basically a thin wrapper around the Fetch API:
ts
import { createRestClient } from "@backpack/rest-client";
const books = await createRestClient({ baseUrl: "https://my-book-store.com" })
.url("/api/v1/books")
.get()
.json();
You can check out the Wretch documentation to learn more.
Advanced usage
ts
import { createRestClient } from "@backpack/rest-client";
const restClient = createRestClient({
// additional options:
baseUrl: "https://api.bookstore.com/",
timeoutMilliseconds: 2_000,
apiManagementSubscriptionKey: "...",
userAgent: "my-app/v2"
});
// additional methods:
const result = await restClient
// set or override subscription key:
.apiManagementSubscriptionKey("...")
// set or override user agent header
// shorthand for `.headers({ "User-Agent", "..." })`:
.userAgent("my-app")
// set or override timeout:
.setTimeout(2_000)
// shorthand for `.headers({ "Accept-Language", "..." })`:
.acceptLanguage("my-app")
.url("/api/v1/books")
.get()
// shorthand for `.json().then(it => MyZodSchema.parse(it)`
.validatedJson(MyZodSchema)
Adding additional logging
Optionally, you can add logging using the powertoolsLoggingMiddleware()
.
This middleware uses Powertools Logger to add logging fro requests and responses:
ts
import { createRestClient, powertoolsLoggingMiddleware } from "@backpack/rest-client";
const restClient = createRestClient()
.middlewares([
powertoolsLoggingMiddleware({
logger: myLogger
// additional options
})
]);
Do you have ideas for additional middleware?
Please consider making a contribution by submitting a pull request.