Repository Data Utility
The Repository Data utility provides functions for fetching detailed repository information from the AWS S3 Bucket. It serves as a data provider for the DataContext, enabling the application to access and display repository-specific information with filtering capabilities.
Core Functionality
The utility exports a primary function:
fetchRepositoryData
export const fetchRepositoryData = async (
repositories,
date = null,
archived = null
) => {
// Implementation details
}
This function:
- Fetches detailed data for specific repositories
- Supports filtering by date and archived status
- Handles environment-specific URLs (development vs production)
- Implements error handling with user feedback
Parameters
The function accepts the following parameters:
Parameter | Type | Default | Description |
---|---|---|---|
repositories |
string[] | Required | Array of repository names to fetch data for |
date |
string | null |
ISO date string to filter repositories by last commit date |
archived |
string | null |
'true'/'false' to filter archived repositories |
Implementation Details
The function follows a structured request pattern:
-
Parameter Validation: Checks if repositories array is valid
- Returns
null
if repositories array is empty or undefined
- Returns
-
URL Parameter Construction: Builds query parameters
- Converts repository array to comma-separated string
- Adds optional date and archived parameters when provided
-
Environment-Specific URL: Determines the correct API endpoint
- Uses
localhost:5001/api/repository/project/json
in development - Uses
/api/repository/project/json
in production
- Uses
-
Request Execution: Fetches data with constructed URL and parameters
- Validates response status
- Parses JSON response
-
Error Handling: Manages request failures
- Displays error toast notification
- Returns
null
to indicate failure
Integration with DataContext
The DataContext uses this utility to:
- Fetch repository data when needed for specific views
- Apply filtering based on user selections (date ranges, archived status)
- Cache the returned data to minimize redundant API calls
Example usage within DataContext:
const getRepositoryData = useCallback(
async (repositories, date = null, archived = null, forceRefresh = false) => {
// Create cache key based on parameters
const cacheKey = `${repositories.join(",")}:${date || ""}:${
archived || ""
}`;
// Check cache first unless forceRefresh is true
if (repositoryData.has(cacheKey) && !forceRefresh) {
return repositoryData.get(cacheKey);
}
// Check for pending request
if (pendingRequests.repository?.[cacheKey]) {
return pendingRequests.repository[cacheKey];
}
// Initialize repository requests object if needed
if (!pendingRequests.repository) {
pendingRequests.repository = {};
}
// Create new request
const request = fetchRepositoryData(repositories, date, archived);
pendingRequests.repository[cacheKey] = request;
try {
const data = await request;
// Update cache with new data
setRepositoryData(new Map(repositoryData.set(cacheKey, data)));
return data;
} finally {
// Clean up pending request
pendingRequests.repository[cacheKey] = null;
}
},
[repositoryData, pendingRequests]
);
Error Handling
The utility implements error handling:
- Catches and logs network errors
- Provides user feedback via toast notifications
- Returns
null
when data is unavailable