@build-from-bits/rest-utils
v1.0.0
Published
REST request helpers built on the JWT-authentication axios client, with response-object normalization.
Downloads
97
Readme
@build-from-bits/rest-utils
REST request helpers built on the JWT-authentication axios client, with response-object normalization (no thrown errors from HTTP failures).
Install
npm install @build-from-bits/rest-utils @build-from-bits/jwt-authenticationUsage
Bind to an existing hardened auth client
import { createAuthSystem } from '@build-from-bits/jwt-authentication';
import { createRestApi } from '@build-from-bits/rest-utils';
const auth = createAuthSystem({ baseUrl: import.meta.env.VITE_API_BASE_URL, env: import.meta.env });
const api = createRestApi({
client: auth.client,
defaultHeaders: {
'api-key': import.meta.env.VITE_API_KEY,
'api-secret': import.meta.env.VITE_API_SECRET_KEY,
},
});
await auth.session.initialize();
// HTTP errors come back as { status, data } instead of throwing
const response = await api.postWithResponseObject('/v2/order/', { qty: 1 });
if (response.status === 200) {
// success
}
const products = await api.getApiResponseObject('/v2/products/');One-factory convenience
import { createAuthRestApi } from '@build-from-bits/rest-utils';
const { auth, api } = createAuthRestApi({
baseUrl: import.meta.env.VITE_API_BASE_URL,
env: import.meta.env,
defaultHeaders: {
'api-key': import.meta.env.VITE_API_KEY,
'api-secret': import.meta.env.VITE_API_SECRET_KEY,
},
onUnauthorized: () => window.location.assign('/signin'),
});API
createRestApi({ client, defaultHeaders })— returns{ postWithResponseObject, getApiResponseObject }.postWithResponseObject(url, data?, headers?)— POST, mergesContent-Type: application/json+defaultHeaders+headers.getApiResponseObject(url, headers?)— GET withCache-Control: no-cacheadded by default.- Failures normalize to
error.response/error.request/ the raw error (never thrown from HTTP errors).
createAuthRestApi(options)— wirescreateAuthSystemfrom@build-from-bits/jwt-authentication(uniform token policy: access→sessionStorage, refresh→localStorage, refresh only on 401) and returns{ auth, api }. OptionalonUnauthorizedfires on any 401.
Publish
npm run build
npm run pack:check
npm run publish:npm
npm run publish:githubLicense
Licensed under the Apache License 2.0.
Copyright 2026 Build From Bits Pvt Ltd.
