dikript-react-identity-comparison-sdk
Version:
A Dikript's React SDK for identity comparison and liveness checks.
89 lines (79 loc) • 2.84 kB
JavaScript
/**
* API utility functions for Dikript Identity Comparison SDK
*/
const LIVENESS_ENDPOINT = '/livelinesscheck';
const IDENTITY_COMPARISON_ENDPOINT = '/identitycomparison';
/**
* Send an image for liveness check
* @param {string} dataUrl - Base64 encoded image data URL
* @param {string} apiKey - API key for authentication
* @param {string} apiUrl - Base URL for API requests
* @returns {Promise<Object>} - Response from the liveness check API
*/
export async function sendImageForLiveness(dataUrl, apiKey, apiUrl) {
try {
const base64String = dataUrl.split(',')[1];
const formData = new FormData();
formData.append('ImageBase64String', base64String);
const response = await fetch(`${apiUrl}${LIVENESS_ENDPOINT}`, {
method: 'POST',
headers: {
'x-api-key': apiKey,
},
body: formData,
});
if (!response.ok) {
throw new Error(`Server error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error in sendImageForLiveness:', error);
return handleApiError(error);
}
}
/**
* Send an image for identity comparison
* @param {string} dataUrl - Base64 encoded image data URL
* @param {string} idType - Type of ID (e.g., BVN, NIN)
* @param {string} idNumber - ID number
* @param {string} apiKey - API key for authentication
* @param {string} apiUrl - Base URL for API requests
* @returns {Promise<Object>} - Response from the identity comparison API
*/
export async function sendImageForIdentityComparison(dataUrl, idType, idNumber, apiKey, apiUrl) {
try {
const base64String = dataUrl.split(',')[1];
const formData = new FormData();
formData.append('ImageBase64String', base64String);
formData.append('idType', idType);
formData.append('idNumber', idNumber);
const response = await fetch(`${apiUrl}${IDENTITY_COMPARISON_ENDPOINT}`, {
method: 'POST',
headers: {
'x-api-key': apiKey,
},
body: formData,
});
if (!response.ok) {
throw new Error(`Server error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error in sendImageForIdentityComparison:', error);
return handleApiError(error);
}
}
/**
* Handle API errors
* @param {Error} error - Error object
* @throws {Error} - Throws an error with appropriate message
*/
function handleApiError(error) {
if (error.response) {
throw new Error(`Server error: ${error.response.status} - ${error.response.data?.message || 'Unknown error'}`);
} else if (error.request) {
throw new Error('Network error. Please check your internet connection and try again.');
} else {
throw new Error(`Error: ${error.message}`);
}
}