HttpService provides a convenient wrapper around Axios for HTTP requests with automatic Bearer token injection and error handling.
import { HttpService } from 'authsafe-ts';
const httpService = new HttpService(token?: string);token (optional) - Access token for authentication. Automatically adds Authorization: Bearer <token> header// GET request
await httpService.get<T>(path?: string, config?: AxiosRequestConfig): Promise<T>
// POST request
await httpService.post<T>(path: string, data?: any, config?: AxiosRequestConfig): Promise<T>
// PUT request
await httpService.put<T>(path: string, data?: any, config?: AxiosRequestConfig): Promise<T>
// PATCH request
await httpService.patch<T>(path: string, data?: any, config?: AxiosRequestConfig): Promise<T>
// DELETE request
await httpService.delete<T>(path: string, config?: AxiosRequestConfig): Promise<T>import { HttpService } from 'authsafe-ts';
const user = await oauthService.getUser();
const httpService = new HttpService(user?.access_token);
// Make authenticated requests
const userInfo = await httpService.get('/auth/userinfo');
const data = await httpService.post('/api/resource', { key: 'value' });import { HttpService, MfaApi, UserApi } from 'authsafe-ts';
const user = await oauthService.getUser();
const httpService = new HttpService(user?.access_token);
// Create API clients
const mfaApi = new MfaApi(httpService);
const userApi = new UserApi(httpService);
// Use API clients
const methods = await mfaApi.listMethods();
await userApi.updateUser(userId, { name: 'John Doe' });import { HttpService, BrandingApi } from 'authsafe-ts';
// No token needed for public endpoints
const httpService = new HttpService();
const brandingApi = new BrandingApi(httpService);
const branding = await brandingApi.getBranding(clientId);"Login error".
try {
const data = await httpService.get('/protected/resource');
} catch (error) {
if (error.message === 'Login error') {
// Redirect to login
await oauthService.signinRedirect();
}
}try {
await httpService.post('/api/action', data);
} catch (error) {
console.error('Request failed:', error.message);
}import.meta.env.VITE_AUTHORITYContent-Type: application/jsonCache-Control: no-cachePragma: no-cacheAuthorization: Bearer <token> (if provided)