TSone

Request API

The request client is imported from a dedicated subpath. It uses native fetch and parses JSON by default.

import { createRequest } from '@geektech/tsone/request';

const api = createRequest({ baseURL: '/api' });

api.interceptors.request.use((config) => ({
  ...config,
  headers: { ...config.headers, Authorization: 'Bearer token' },
}));
api.interceptors.error.use(() => ({ offline: true }));

const health = await api.get('/health', { responseType: 'text' });

Client and Interceptors

NameSignatureDescription
createRequestcreateRequest(defaults?: RequestDefaults): RequestClientCreates an isolated request client.
requestRequestClientThe default request client instance.
interceptors.request.use(config: RequestConfig) => RequestConfig | Promise<RequestConfig>Runs before fetch and can replace the request config.
interceptors.response.use(value: unknown) => unknown | Promise<unknown>Runs after a successful response has been parsed.
interceptors.error.use(error: unknown) => unknown | Promise<unknown>Runs for request, transport, parsing, and non-2xx failures; returning a value recovers the request.

Response Types and Errors

responseType defaults to 'json'. Use 'text', 'blob', 'arrayBuffer', or 'response' for another result; 'response' leaves the native Response unconsumed. A non-2xx response throws RequestError unless an error interceptor returns a recovery value.

  • 'json': parsed JSON (default)
  • 'text': response text
  • 'blob': Blob data
  • 'arrayBuffer': ArrayBuffer data
  • 'response': the raw Response