|
| 1 | +import { LinkedApiAdmin, LinkedApiError } from '@linkedapi/node'; |
| 2 | + |
| 3 | +async function adminLimitsExample(): Promise<void> { |
| 4 | + const admin = new LinkedApiAdmin({ |
| 5 | + linkedApiToken: process.env.LINKED_API_TOKEN!, |
| 6 | + }); |
| 7 | + |
| 8 | + try { |
| 9 | + console.log('🚀 Linked API Admin Limits example starting...'); |
| 10 | + |
| 11 | + await getDefaultLimits(admin); |
| 12 | + await getAccountLimitsAndUsage(admin); |
| 13 | + |
| 14 | + } catch (error) { |
| 15 | + if (error instanceof LinkedApiError) { |
| 16 | + console.error('🚨 Linked API Error:', error.message); |
| 17 | + console.error('📝 Details:', error.details); |
| 18 | + } else { |
| 19 | + console.error('💥 Unknown error:', error); |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +async function getDefaultLimits(admin: LinkedApiAdmin): Promise<void> { |
| 25 | + console.log('\n📋 Getting default limits...'); |
| 26 | + |
| 27 | + const { limits } = await admin.limits.getDefaults(); |
| 28 | + console.log('✅ Default limits retrieved'); |
| 29 | + for (const limit of limits) { |
| 30 | + const status = limit.isEnabled ? '✅' : '⏸️'; |
| 31 | + console.log(` ${status} ${limit.category} (${limit.period}): max ${limit.maxValue}`); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +async function getAccountLimitsAndUsage(admin: LinkedApiAdmin): Promise<void> { |
| 36 | + const { accounts } = await admin.accounts.getAll(); |
| 37 | + if (accounts.length === 0) { |
| 38 | + console.log('\n⚠️ No accounts to check limits for'); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + const account = accounts[0]; |
| 43 | + if (!account) { |
| 44 | + console.log('\n⚠️ No accounts to check limits for'); |
| 45 | + return; |
| 46 | + } |
| 47 | + const accountId = account.id; |
| 48 | + |
| 49 | + console.log(`\n📊 Getting limits for ${account.name}...`); |
| 50 | + const { limits } = await admin.limits.get({ accountId }); |
| 51 | + console.log('✅ Account limits retrieved'); |
| 52 | + for (const limit of limits) { |
| 53 | + console.log(` ${limit.category} (${limit.period}): max ${limit.maxValue}`); |
| 54 | + } |
| 55 | + |
| 56 | + console.log(`\n📈 Getting usage for ${account.name}...`); |
| 57 | + const { usage } = await admin.limits.getUsage({ accountId }); |
| 58 | + console.log('✅ Usage retrieved'); |
| 59 | + for (const entry of usage) { |
| 60 | + const remaining = entry.maxValue - entry.currentUsage; |
| 61 | + const status = entry.isEnabled ? '✅' : '⏸️'; |
| 62 | + console.log(` ${status} ${entry.category} (${entry.period}): ${entry.currentUsage}/${entry.maxValue} (${remaining} remaining)`); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +async function setCustomLimits(admin: LinkedApiAdmin): Promise<void> { |
| 67 | + const { accounts } = await admin.accounts.getAll(); |
| 68 | + if (accounts.length === 0) { |
| 69 | + console.log('\n⚠️ No accounts to set limits for'); |
| 70 | + return; |
| 71 | + } |
| 72 | + const account = accounts[0]; |
| 73 | + if (!account) { |
| 74 | + console.log('\n⚠️ No accounts to check limits for'); |
| 75 | + return; |
| 76 | + } |
| 77 | + const accountId = account.id; |
| 78 | + |
| 79 | + console.log(`\n🔧 Setting custom limits for ${account.name}...`); |
| 80 | + await admin.limits.set({ |
| 81 | + accountId, |
| 82 | + limits: [ |
| 83 | + { |
| 84 | + category: 'stMessages', |
| 85 | + period: 'daily', |
| 86 | + maxValue: 25, |
| 87 | + isEnabled: true, |
| 88 | + }, |
| 89 | + { |
| 90 | + category: 'stConnectionRequests', |
| 91 | + period: 'weekly', |
| 92 | + maxValue: 30, |
| 93 | + }, |
| 94 | + ], |
| 95 | + }); |
| 96 | + console.log('✅ Custom limits set'); |
| 97 | + |
| 98 | + console.log('\n🗑️ Deleting specific limits...'); |
| 99 | + await admin.limits.delete({ |
| 100 | + accountId, |
| 101 | + limits: [ |
| 102 | + { category: 'stMessages', period: 'daily' }, |
| 103 | + ], |
| 104 | + }); |
| 105 | + console.log('✅ Limits deleted'); |
| 106 | + |
| 107 | + console.log('\n🔄 Resetting all limits to defaults...'); |
| 108 | + await admin.limits.resetToDefaults({ accountId }); |
| 109 | + console.log('✅ Limits reset to defaults'); |
| 110 | +} |
| 111 | + |
| 112 | +if (require.main === module) { |
| 113 | + adminLimitsExample(); |
| 114 | +} |
0 commit comments