@ha_tecno/react-native-sdk
Version:
React Native SDK for biometric authentication, liveness detection, and fingerprint recognition
65 lines (64 loc) • 1.84 kB
JavaScript
;
import { getToken } from "../auth/tokenManager.js";
const BASE_URL = 'https://api.liveid.app.br';
class ApiError extends Error {
constructor(status, message) {
super(message);
this.status = status;
this.name = 'ApiError';
}
}
const request = async (endpoint, options = {}) => {
const token = getToken();
if (!token) throw new Error('[HA-TECNO-SDK] Token não configurado.');
const res = await fetch(`${BASE_URL}${endpoint}`, {
...options,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
...options.headers
}
});
if (!res.ok) {
let errorMessage = '';
try {
const errorBody = await res.json();
errorMessage = errorBody?.message || JSON.stringify(errorBody);
} catch (e) {
errorMessage = await res.text();
}
throw new ApiError(res.status, errorMessage || 'Erro desconhecido');
}
return res.json();
};
const requestFormData = async (endpoint, formData) => {
const token = getToken();
if (!token) throw new Error('[HA-TECNO-SDK] Token não configurado.');
const res = await fetch(`${BASE_URL}${endpoint}`, {
method: 'POST',
body: formData,
headers: {
Authorization: `Bearer ${token}`
}
});
if (!res.ok) {
let errorMessage = '';
try {
const errorBody = await res.json();
errorMessage = errorBody?.message || JSON.stringify(errorBody);
} catch (e) {
errorMessage = await res.text();
}
throw new ApiError(res.status, errorMessage || 'Erro desconhecido');
}
return res.json();
};
export const api = {
get: url => request(url),
post: (url, body) => request(url, {
method: 'POST',
body: JSON.stringify(body)
}),
postFormData: (url, formData) => requestFormData(url, formData)
};
//# sourceMappingURL=api.js.map