@winnermarket/api-client
v1.0.0
Published
Typed HTTP client for the Transiqo REST API — isomorphic core plus optional React Query bindings (subpath `/react`).
Downloads
150
Maintainers
Readme
@winnermarket/api-client
Typed HTTP client for the Transiqo REST API. Isomorphic core plus optional React Query bindings.
Proprietary software. See LICENSE. Use requires an active commercial agreement with Techassistia.
Install
pnpm add @winnermarket/api-client
# React bindings also need:
pnpm add react @tanstack/react-queryNode >= 18 required.
Two entrypoints
| Entry | Peer deps | Purpose |
|-------|-----------|---------|
| @winnermarket/api-client | none | ApiClient + ApiError + envelope types |
| @winnermarket/api-client/react | react, @tanstack/react-query | ApiClientProvider, useApiClient, 22 domain hooks |
Core client
import { ApiClient, ApiError } from "@winnermarket/api-client";
const api = new ApiClient("https://api.example.com/v1", {
unwrapEnvelope: true, // `{ success, data }` → return `data` (default: true)
credentials: "include", // fetch credentials mode (default: "include")
onAuthError: (err) => { // called on HTTP 401 before throwing
console.warn("Token expired, redirecting…");
location.href = "/login";
},
defaultHeaders: { "X-Tenant": "acme" },
});
api.setToken(accessToken);
// Typed responses
const me = await api.get<User>("/auth/me");
const widget = await api.post<Widget>("/widgets", { name: "X" });
try {
await api.delete("/widgets/99");
} catch (err) {
if (err instanceof ApiError && err.isNotFound) {
// handle
}
}Options
| Option | Default | Description |
|--------|---------|-------------|
| unwrapEnvelope | true | Unwrap { success, data } envelope from NestJS-style APIs |
| credentials | "include" | fetch credentials mode |
| onAuthError | – | Callback invoked on HTTP 401 responses |
| defaultHeaders | {} | Extra headers sent on every request |
| fetch | globalThis.fetch | Custom fetch impl (SSR polyfill, tests…) |
ApiError
Thrown on any non-2xx response. Exposes statusCode, code, message,
optional details, plus helpers: isUnauthorized, isForbidden, isNotFound,
isValidationError.
React bindings
import {
ApiClientProvider,
useApiClient,
useMe,
useLogin,
} from "@winnermarket/api-client/react";
function Root() {
return (
<ApiClientProvider
baseUrl={process.env.NEXT_PUBLIC_API_URL}
clientOptions={{
onAuthError: () => window.location.assign("/login"),
}}
>
<App />
</ApiClientProvider>
);
}
function Profile() {
const { data, isLoading } = useMe();
const login = useLogin();
// …
}Passing an existing ApiClient
const api = new ApiClient("/api/v1", { onAuthError });
<ApiClientProvider apiClient={api}>
<App />
</ApiClientProvider>;Domain hooks included
auth, fleet, rental, vtc, transit, finance, crm, maintenance,
settings, documents, reporting, shift, ai, tracking, planning,
ticketing, intermodal, payroll, training, ml, pricing.
See the d.ts in your editor for the exact signatures.
On-prem / third-party backends
The client is not hardcoded to Transiqo URLs. Point it at any NestJS-compatible
API by setting baseUrl; flip unwrapEnvelope: false if your backend returns
raw JSON instead of the NestJS interceptor envelope.
© Techassistia. All rights reserved.
