Spaces:
Sleeping
Sleeping
File size: 6,657 Bytes
e327f0d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | /**
* Axios HTTP client with:
* - JWT Bearer auth (token loaded from Tauri Store via `auth-store.ts`)
* - request interceptor injects `Authorization: Bearer <access>`
* - response interceptor on 401: attempts a single refresh, replays the request,
* and falls back to a logout callback on hard failure
* - base URL & legacy X-API-Key bridged from `settings.ts`
*
* The interceptor avoids infinite refresh loops by guarding `_retry` on the request config.
*/
import axios, { AxiosError, AxiosInstance, InternalAxiosRequestConfig } from 'axios';
import type {
AuthTokens,
HealthResponse,
InspectionCreateResponse,
InspectionStatusResponse,
SyncInspectionResponse,
InspectionListResponse,
LoginRequest,
LoginResponse,
RegisterRequest,
RefreshTokenResponse,
User,
} from '@arac-hasar/types';
const DEFAULT_BASE_URL = import.meta.env.VITE_API_URL ?? 'http://localhost:8000';
interface RetryableConfig extends InternalAxiosRequestConfig {
_retry?: boolean;
}
type TokenGetter = () => AuthTokens | null;
type TokensSetter = (t: AuthTokens) => void | Promise<void>;
type LogoutHandler = () => void | Promise<void>;
class ApiClient {
private client: AxiosInstance;
private getTokens: TokenGetter = () => null;
private setTokens: TokensSetter = () => undefined;
private onLogout: LogoutHandler = () => undefined;
private refreshInFlight: Promise<AuthTokens | null> | null = null;
constructor(baseURL: string = DEFAULT_BASE_URL, apiKey?: string) {
this.client = axios.create({
baseURL,
timeout: 60_000,
headers: apiKey ? { 'X-API-Key': apiKey } : {},
});
this.client.interceptors.request.use((cfg) => {
const tk = this.getTokens();
if (tk?.access_token) {
cfg.headers = cfg.headers ?? {};
cfg.headers.Authorization = `Bearer ${tk.access_token}`;
}
return cfg;
});
this.client.interceptors.response.use(
(r) => r,
async (err: AxiosError) => {
const original = err.config as RetryableConfig | undefined;
if (err.response?.status === 401 && original && !original._retry) {
original._retry = true;
const refreshed = await this.tryRefresh();
if (refreshed) {
original.headers = original.headers ?? {};
original.headers.Authorization = `Bearer ${refreshed.access_token}`;
return this.client.request(original);
}
await this.onLogout();
}
return Promise.reject(err);
},
);
}
bindAuth(opts: { getTokens: TokenGetter; setTokens: TokensSetter; onLogout: LogoutHandler }) {
this.getTokens = opts.getTokens;
this.setTokens = opts.setTokens;
this.onLogout = opts.onLogout;
}
setApiKey(apiKey: string | null) {
if (apiKey) this.client.defaults.headers.common['X-API-Key'] = apiKey;
else delete this.client.defaults.headers.common['X-API-Key'];
}
setBaseUrl(url: string) {
this.client.defaults.baseURL = url;
}
private async tryRefresh(): Promise<AuthTokens | null> {
if (this.refreshInFlight) return this.refreshInFlight;
const cur = this.getTokens();
if (!cur?.refresh_token) return null;
this.refreshInFlight = (async () => {
try {
const { data } = await axios.post<RefreshTokenResponse>(
`${this.client.defaults.baseURL}/api/v1/auth/refresh`,
{ refresh_token: cur.refresh_token },
);
const next: AuthTokens = {
access_token: data.access_token,
refresh_token: data.refresh_token ?? cur.refresh_token,
};
await this.setTokens(next);
return next;
} catch {
return null;
} finally {
this.refreshInFlight = null;
}
})();
return this.refreshInFlight;
}
// βββββ Auth βββββ
async login(payload: LoginRequest): Promise<LoginResponse> {
const { data } = await this.client.post<LoginResponse>('/api/v1/auth/login', payload);
return data;
}
async register(payload: RegisterRequest): Promise<LoginResponse> {
const { data } = await this.client.post<LoginResponse>('/api/v1/auth/register', payload);
return data;
}
async me(): Promise<User> {
const { data } = await this.client.get<User>('/api/v1/auth/me');
return data;
}
async logout(): Promise<void> {
try {
await this.client.post('/api/v1/auth/logout');
} catch {
// server-side logout best-effort; client clears regardless
}
}
// βββββ System βββββ
async health(): Promise<HealthResponse> {
const { data } = await this.client.get<HealthResponse>('/health');
return data;
}
// βββββ Inspections βββββ
async createInspection(
files: File[] | Blob[],
mode: 'sync' | 'async' = 'async',
onProgress?: (pct: number) => void,
): Promise<InspectionCreateResponse | SyncInspectionResponse> {
const form = new FormData();
files.forEach((f, i) => {
const name = f instanceof File ? f.name : `image_${i}.jpg`;
form.append('files', f, name);
});
const { data } = await this.client.post(`/api/v1/inspect?mode=${mode}`, form, {
headers: { 'Content-Type': 'multipart/form-data' },
onUploadProgress: (e) => {
if (onProgress && e.total) onProgress(Math.round((e.loaded / e.total) * 100));
},
});
return data;
}
async getInspection(id: string): Promise<InspectionStatusResponse> {
const { data } = await this.client.get<InspectionStatusResponse>(`/api/v1/inspect/${id}`);
return data;
}
async listInspections(page = 1, pageSize = 20): Promise<InspectionListResponse> {
const { data } = await this.client.get<InspectionListResponse>('/api/v1/inspect', {
params: { page, page_size: pageSize },
});
return data;
}
async deleteInspection(id: string): Promise<void> {
await this.client.delete(`/api/v1/inspect/${id}`);
}
/** Server-rendered PDF report (returned as base64 to forward to `save_report`). */
async exportInspectionPdf(id: string): Promise<string> {
const { data } = await this.client.get<ArrayBuffer>(`/api/v1/inspect/${id}/report.pdf`, {
responseType: 'arraybuffer',
});
return arrayBufferToBase64(data);
}
}
function arrayBufferToBase64(buf: ArrayBuffer): string {
const bytes = new Uint8Array(buf);
let bin = '';
for (let i = 0; i < bytes.byteLength; i++) bin += String.fromCharCode(bytes[i] ?? 0);
return typeof btoa !== 'undefined' ? btoa(bin) : Buffer.from(bin, 'binary').toString('base64');
}
export const api = new ApiClient();
export default ApiClient;
|