102 lines
3.1 KiB
TypeScript
102 lines
3.1 KiB
TypeScript
import { pennyzoApi } from '../services';
|
|
|
|
/**
|
|
* This file contains examples of how to use the PennyzoApiService
|
|
* These are meant to be reference examples and not actual code to run.
|
|
*/
|
|
|
|
// Example 1: System health check (public endpoint)
|
|
async function checkSystemHealth() {
|
|
try {
|
|
const health = await pennyzoApi.checkHealth();
|
|
console.log('System status:', health.status);
|
|
console.log('Service:', health.service);
|
|
console.log('Version:', health.version);
|
|
return health;
|
|
} catch (error) {
|
|
console.error('Error checking health:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// Example 2: User registration (public endpoint)
|
|
async function registerNewUser(userData: any) {
|
|
try {
|
|
const registeredUser = await pennyzoApi.registerUser({
|
|
first_name: userData.firstName,
|
|
last_name: userData.lastName,
|
|
email: userData.email,
|
|
company_name: userData.companyName,
|
|
company_type: userData.companyType,
|
|
gst_registered: userData.isGstRegistered,
|
|
gst_number: userData.gstNumber,
|
|
industry: userData.industry,
|
|
// Other optional fields
|
|
});
|
|
|
|
console.log('User registered successfully:', registeredUser);
|
|
return registeredUser;
|
|
} catch (error) {
|
|
console.error('Registration failed:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// Example 3: Login and authentication flow
|
|
async function loginUser(email: string, password: string) {
|
|
try {
|
|
const loginResult = await pennyzoApi.login(email, password);
|
|
console.log('Login successful, token type:', loginResult.token_type);
|
|
console.log('Token expiration:', loginResult.expires_in);
|
|
|
|
// Token is automatically stored and will be used in subsequent API calls
|
|
|
|
// Get the current user's details (authenticated call)
|
|
const currentUser = await pennyzoApi.getCurrentUser();
|
|
console.log('Current user:', currentUser);
|
|
|
|
return { loginResult, currentUser };
|
|
} catch (error) {
|
|
console.error('Login failed:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// Example 4: Update profile (authenticated call)
|
|
async function updateProfile(profileData: any) {
|
|
try {
|
|
const updatedProfile = await pennyzoApi.updateUserProfile({
|
|
first_name: profileData.firstName,
|
|
last_name: profileData.lastName,
|
|
mobile: profileData.mobile,
|
|
industry: profileData.industry
|
|
});
|
|
|
|
console.log('Profile updated successfully:', updatedProfile);
|
|
return updatedProfile;
|
|
} catch (error) {
|
|
console.error('Profile update failed:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// Example 5: Logout
|
|
function logout() {
|
|
pennyzoApi.logout();
|
|
console.log('User logged out successfully');
|
|
}
|
|
|
|
// Example 6: Error handling with the service
|
|
async function demonstrateErrorHandling() {
|
|
try {
|
|
// Trying to access a protected endpoint without authentication
|
|
await pennyzoApi.logout(); // Clear any existing token
|
|
await pennyzoApi.getUserProfile(); // This should fail with 401
|
|
} catch (error: any) {
|
|
console.error('Expected error occurred:');
|
|
console.error('Status:', error.status);
|
|
console.error('Message:', error.message);
|
|
console.error('Error code:', error.code);
|
|
console.error('Details:', error.details);
|
|
}
|
|
} |