Skip to content

Repository files navigation

@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-authentication

Usage

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, merges Content-Type: application/json + defaultHeaders + headers.
    • getApiResponseObject(url, headers?) — GET with Cache-Control: no-cache added by default.
    • Failures normalize to error.response / error.request / the raw error (never thrown from HTTP errors).
  • createAuthRestApi(options) — wires createAuthSystem from @build-from-bits/jwt-authentication (uniform token policy: access→sessionStorage, refresh→localStorage, refresh only on 401) and returns { auth, api }. Optional onUnauthorized fires on any 401.

Publish

npm run build
npm run pack:check
npm run publish:npm
npm run publish:github

License

Licensed under the Apache License 2.0.

Copyright 2026 Build From Bits Pvt Ltd.

About

REST request helpers built on the JWT-authentication axios client, with response-object normalization (no thrown errors from HTTP failures).

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages