Fetch Package (@schwab/fetch)
Overview
The @schwab/fetch package serves as a centralized library of fetch operations and API integrations for the Charles Schwab monorepo. It provides standardized interfaces for communicating with external services including Drupal CMS, Bynder Digital Asset Management, CXP (Customer Experience Platform), Vercel Edge Config, and various third-party APIs.
Architecture
Package Configuration
Core Dependencies
{
"@schwab/schema": "workspace:*", // Type definitions and validation
"@schwab/transformer": "workspace:*", // Data transformation utilities
"@schwab/utilities": "workspace:*", // Shared utility functions
"react": "^19.2.0", // React cache integration
"zod": "^3.24.1" // Runtime type validation
}
Module Structure
{
"#drupalFetch": "./src/drupal/_drupalFetch.ts",
"#drupal/*": "./src/drupal/*.ts",
"#bynderFetch": "./src/bynder/_bynderFetch.ts",
"#bynder/*": "./src/bynder/*.ts",
"#cxpFetch": "./src/cxp/_cxpFetch.ts",
"#cxp/*": "./src/cxp/*.ts",
"#utilities/*": "./src/utilities/*.ts",
"#types": "./src/types/index.ts"
}
Core Service Integrations
1. Drupal CMS Integration
Location: src/drupal/
Comprehensive Drupal API integration with content management capabilities:
// src/drupal/cmap-api/getDrupalAsset.ts
import { FetchedDrupalAssetSchema } from '@schwab/schema/fetched/cmap-api/FetchedDrupalAssetSchema';
import { EDrupalSource } from '@schwab/schema/native-enums/EDrupalSource';
import { transformDrupalAsset } from '@schwab/transformer/transformDrupalAsset';
async function fetchAsset(
nodeId: string,
datasource: EDrupalSource = EDrupalSource.Education,
role: ERole,
): Promise<TResponse<TDrupalAsset | null>> {
const dataSourceUrl: string = getDataSourceUrl(datasource);
const { status } = await getModerationState();
const cacheTags = [`${datasource}_node:${nodeId}`];
const fetchedResponse = await drupalFetch<TFetchedDrupalAsset[]>(
`${dataSourceUrl}/api/cmap/asset/${status}/en/${nodeId}`,
datasource,
cacheTags,
true,
true,
);
if (!fetchedResponse.isOk) {
return responseCreate(false, null, fetchedResponse.error);
}
// Validate with Zod schema
const schema = z.array(FetchedDrupalAssetSchema).nonempty();
const zodResponse = schema.safeParse(fetchedResponse.data);
return zodResponse.success
? responseCreate(true, transformDrupalAsset(zodResponse.data[0], role))
: responseCreate(false, null, zodResponse.error);
}
Drupal API Functions:
getDrupalAsset- Fetch individual content assetsgetRelatedContent- Content relationship queriesgetPersonRelatedContent- Person-specific contentgetTopic- Topic content retrievalgetCollection- Content collection managementgetRedirectsFromDrupal- URL redirect managementgetTaxonomyList- Taxonomy and categorization
2. Bynder Digital Asset Management
Location: src/bynder/
Integration with Bynder DAM for digital asset management:
// Branch and consultant image fetching
export { getBranchImage } from './src/bynder/getBranchImage.ts';
export { getConsultantImage } from './src/bynder/getConsultantImage.ts';
Features:
- Branch location image retrieval
- Consultant profile images
- Digital asset metadata management
- Brand asset compliance
3. CXP (Customer Experience Platform)
Location: src/cxp/
Customer experience and authentication services:
// Authentication and navigation
export { keepAlive } from './src/cxp/keepAlive.ts';
export { getCxpNavigationData } from './src/cxp/getCxpNavigationData.ts';
export { isRequestAuthenticated } from './src/cxp/isRequestAuthenticated.ts';
export { getAltoDataContext } from './src/cxp/getAltoDataContext.ts';
Capabilities:
- User session management
- Authentication validation
- Navigation data retrieval
- Alto platform integration
4. Vercel Edge Config Integration
Location: src/edge-config-db/
High-performance edge configuration management:
// Redirect management
export { postRedirectsToEdge } from './src/edge-config-db/postRedirectsToEdge.ts';
export { getRedirectFromEdge } from './src/edge-config-db/getRedirectFromEdge.ts';
export { getAllRedirectsFromEdge } from './src/edge-config-db/getAllRedirectsFromEdge.ts';
export { syncRedirectsToEdge } from './src/edge-config-db/syncRedirectsToEdge.ts';
// Feature flag management
export { getFeatureFlagFromEdge } from './src/edge-config-db/getFeatureFlagFromEdge.ts';
export { getAllFlagsFromEdge } from './src/edge-config-db/getAllFlagsFromEdge.ts';
Advanced Features
1. Cache Management
React Cache Integration:
import { cache } from 'react';
// Automatic caching for server components
const fetchAsset = cache(async (nodeId: string) => {
// Fetch implementation with automatic deduplication
});
Cache Strategies:
- React Server Component cache deduplication
- Drupal cache tag invalidation
- Edge config cache synchronization
- Feature flag cache warming
2. Error Handling and Resilience
import { responseCreate } from '@schwab/utilities/functions/responseCreate';
import { runtimeError } from '@schwab/utilities/functions/runtimeError';
import { SchLogger } from '@schwab/utilities/SchLogger';
// Standardized error response pattern
if (!fetchedResponse.isOk) {
SchLogger.error('API fetch failed', {
service: 'drupal',
endpoint: url,
error: fetchedResponse.error
});
return responseCreate(false, null, fetchedResponse.error);
}
3. Type Safety with Zod Validation
import { z } from 'zod';
import { FetchedDrupalAssetSchema } from '@schwab/schema/fetched/cmap-api/FetchedDrupalAssetSchema';
const schema = z.array(FetchedDrupalAssetSchema).nonempty();
const zodResponse = schema.safeParse(fetchedResponse.data);
if (!zodResponse.success) {
return responseCreate(false, null, zodResponse.error);
}
Service-Specific Implementations
Salesforce Marketing Cloud (SFMC)
Location: src/sfmc/
export { postLeadData } from './src/sfmc/postLeadData.ts';
Features:
- Lead data synchronization
- Marketing campaign integration
- Customer journey tracking
Google Services Integration
Location: src/google/
- Analytics data retrieval
- Tag Manager integration
- Custom dimension tracking
RRBUS Content Platform
Location: src/rrbus/
- Content delivery network integration
- Asset optimization
- Performance monitoring
Utility Functions
1. Cache Management Utilities
Location: src/utilities/
// Cache revalidation and tag management
export { revalidateByTag } from './src/utilities/revalidateByTag.ts';
export { revalidateByPath } from './src/utilities/revalidateByPath.ts';
export { getCacheKey } from './src/utilities/getCacheKey.ts';
2. Response Standardization
export type TResponse<T> = {
isOk: boolean;
data: T;
error?: Error | string;
cacheTags?: string[];
revalidate?: number;
};
3. Data Source Management
import { getDataSourceUrl } from '@schwab/utilities/functions/getDataSourceUrl';
// Environment-aware API endpoint resolution
const dataSourceUrl = getDataSourceUrl(EDrupalSource.Education);
Development Patterns
1. Service Factory Pattern
// Base fetch function for service-specific implementations
function createServiceFetcher<T>(
baseUrl: string,
defaultOptions: RequestInit = {}
) {
return async function<R>(
endpoint: string,
options?: RequestInit
): Promise<TResponse<R>> {
// Implementation with error handling, caching, validation
};
}
export const drupalFetch = createServiceFetcher(process.env.DRUPAL_BASE_URL);
export const bynderFetch = createServiceFetcher(process.env.BYNDER_BASE_URL);
2. Transformer Integration
import { transformDrupalAsset } from '@schwab/transformer/transformDrupalAsset';
// Fetch → Validate → Transform → Return
const rawData = await drupalFetch(endpoint);
const validatedData = schema.parse(rawData);
const transformedData = transformDrupalAsset(validatedData, role);
return responseCreate(true, transformedData);
Performance Optimization
1. Request Deduplication
- React cache integration for automatic deduplication
- Request coalescing for identical API calls
- Smart cache invalidation strategies
2. Edge Computing Integration
- Vercel Edge Config for ultra-low latency
- Geographic distribution of cached data
- Smart cache warming algorithms
3. Parallel Processing
// Concurrent API calls with Promise.all
const [asset, relatedContent, taxonomy] = await Promise.all([
getDrupalAsset(nodeId),
getRelatedContent(nodeId),
getTaxonomyList(categoryId)
]);
Testing and Mocking
Mock Data Integration
import { createMockResponse } from '@schwab/mock-data';
// Development and testing mock responses
export const getMockDrupalAsset = () =>
createMockResponse(mockDrupalAssetData);
Security and Compliance
1. Authentication Management
- API key rotation and management
- OAuth token handling
- Service-to-service authentication
2. Data Privacy
- PII handling compliance
- GDPR data processing
- Audit logging for sensitive operations
3. Rate Limiting
// Built-in rate limiting and retry logic
const rateLimitedFetch = withRateLimit(baseFetch, {
maxRequests: 100,
windowMs: 60000,
retryAfter: 1000
});
Monitoring and Observability
1. Logging Integration
import { SchLogger } from '@schwab/utilities/SchLogger';
SchLogger.info('API request initiated', {
service: 'drupal',
endpoint: '/api/cmap/asset',
nodeId,
duration: requestTime
});
2. Performance Metrics
- API response time tracking
- Cache hit/miss ratios
- Error rate monitoring
- Data freshness metrics
Future Enhancements
Planned Improvements
- GraphQL Integration: Unified GraphQL layer over REST APIs
- Real-time Updates: WebSocket integration for live data
- Advanced Caching: Intelligent cache prefetching and warming
- Multi-region Support: Geographic data distribution
- Enhanced Security: Advanced authentication and authorization
Integration Roadmap
- Microservices: Service mesh integration
- Event Streaming: Kafka/Pub-Sub integration
- AI/ML: Intelligent content recommendations
- Analytics: Enhanced data analytics and reporting
The Fetch package provides a robust, type-safe, and performant foundation for all external API integrations across the Charles Schwab monorepo. Its standardized approach ensures consistency, reliability, and maintainability across all consuming applications.