@dracoonghost/trndup-sdk
v1.5.4
Published
Official TypeScript SDK for TrndUp API with Firebase authentication
Maintainers
Readme
TrndUp TypeScript SDK
Official TypeScript SDK for the TrndUp API with Firebase authentication support.
Installation
# In your mobile app
npm install file:../trndup-service/src/sdk
# or when published
npm install @dracoonghost/trndup-sdkQuick Start
import { TrndUpSDK } from '@dracoonghost/trndup-sdk';
import auth from '@react-native-firebase/auth';
// Initialize SDK
const sdk = new TrndUpSDK({
baseUrl: 'https://api.trndup.app', // or 'http://localhost:3000' for dev
getToken: async () => {
const user = auth().currentUser;
return user ? await user.getIdToken() : null;
},
onAuthFailure: () => {
// Handle auth failure - navigate to login
console.log('Authentication failed - please log in again');
},
debug: __DEV__, // Enable debug logging in development
});
export default sdk;Usage
Authentication
// Login with Firebase ID token
const result = await sdk.auth.login(firebaseIdToken);
console.log('User:', result.user);
// Get current user
const { user, socialAccounts } = await sdk.auth.getCurrentUser();
// Update profile
await sdk.auth.updateProfile({
username: 'New Name',
picture: 'https://...',
});
// Logout
await sdk.auth.logout();YouTube Analytics
// Check initialization status
const status = await sdk.youtube.getInitStatus();
console.log('Needs init:', status.needsInit);
// Initialize YouTube data
if (status.needsInit) {
await sdk.youtube.initialize();
}
// Get videos
const { videos, total } = await sdk.youtube.getVideos({
limit: 20,
sortBy: 'views',
});
// Get channel metrics
const metrics = await sdk.youtube.getChannelMetrics();
console.log('Subscribers:', metrics.subscriberCount);
// Get health score
const health = await sdk.youtube.getHealthScore({ range: '30d' });
console.log('Score:', health.score, 'Grade:', health.grade);Instagram Analytics
// Check initialization status
const status = await sdk.instagram.getInitStatus();
// Get posts
const { posts } = await sdk.instagram.getPosts({ limit: 20 });
// Get account metrics
const metrics = await sdk.instagram.getAccountMetrics();
console.log('Followers:', metrics.followersCount);Social Account Linking
// Link YouTube account
await sdk.social.linkYouTube({
accessToken: 'ya29.a0...',
refreshToken: '1//0g...',
});
// Link Instagram account
await sdk.social.linkInstagram({
accessToken: 'IGQVJ...',
});
// Get connected accounts
const accounts = await sdk.social.getConnectedAccounts();
// Unlink account
await sdk.social.unlinkAccount('youtube');Cross-Platform Insights
// Get overview
const overview = await sdk.insights.getOverview();
console.log('Total followers:', overview.totalFollowers);
// Get trending content
const trending = await sdk.insights.getTrending();Error Handling
import { TrndUpApiError, TrndUpNetworkError } from '@dracoonghost/trndup-sdk';
try {
await sdk.youtube.getVideos();
} catch (error) {
if (error instanceof TrndUpApiError) {
console.error('API Error:', error.message);
console.error('Status:', error.status);
console.error('Code:', error.code);
if (error.isAuthError()) {
// Handle authentication error
console.log('Please log in again');
}
if (error.isRateLimitError()) {
// Handle rate limiting
console.log('Too many requests, please wait');
}
} else if (error instanceof TrndUpNetworkError) {
console.error('Network Error:', error.message);
} else {
console.error('Unknown error:', error);
}
}Configuration Options
interface TrndUpClientConfig {
/** Base URL for the API */
baseUrl: string;
/** Function to get the current Firebase ID token */
getToken: () => string | null | Promise<string | null>;
/** Called when auth completely fails */
onAuthFailure?: () => void | Promise<void>;
/** Called on any API error */
onError?: (error: ApiErrorResponse, endpoint: string) => void;
/** Custom headers to include in all requests */
defaultHeaders?: Record<string, string>;
/** Request timeout in milliseconds (default: 30000) */
timeout?: number;
/** Enable debug logging (default: false) */
debug?: boolean;
}Type Safety
The SDK is fully typed with TypeScript. All request/response types are available:
import type { Auth, YouTube, Instagram, Social, Insights } from '@dracoonghost/trndup-sdk';
// Use types in your code
const user: Auth.User = await sdk.auth.getCurrentUser();
const videos: YouTube.Video[] = await sdk.youtube.getVideos();Development
# Build SDK
cd src/sdk
npm install
npm run build
# Watch mode for development
npm run dev
# Type check
npm run type-check
# Link for local development
npm link
# In your mobile app
npm link @dracoonghost/trndup-sdkLicense
MIT
