|
| 1 | +import { LicenseChainClient } from '../src/client'; |
| 2 | +import { WebhookHandler, WebhookEvents } from '../src/webhook-handler'; |
| 3 | +import { |
| 4 | + validateEmail, |
| 5 | + validateLicenseKey, |
| 6 | + generateLicenseKey, |
| 7 | + generateUuid, |
| 8 | + formatBytes, |
| 9 | + formatDuration, |
| 10 | + capitalizeFirst, |
| 11 | + toSnakeCase, |
| 12 | + toPascalCase, |
| 13 | + slugify, |
| 14 | + jsonSerialize |
| 15 | +} from '../src/utils'; |
| 16 | + |
| 17 | +// Configure the SDK |
| 18 | +const client = LicenseChainClient.create('your-api-key-here', 'https://api.licensechain.app'); |
| 19 | + |
| 20 | +async function basicUsageExample() { |
| 21 | + console.log('🚀 LicenseChain JavaScript SDK - Basic Usage Example\n'); |
| 22 | + |
| 23 | + try { |
| 24 | + // 1. License Management |
| 25 | + console.log('🔑 License Management:'); |
| 26 | + |
| 27 | + // Create a license |
| 28 | + const metadata = { |
| 29 | + platform: 'javascript', |
| 30 | + version: '1.0.0', |
| 31 | + features: ['validation', 'webhooks'] |
| 32 | + }; |
| 33 | + |
| 34 | + const license = await client.getLicenses().create({ |
| 35 | + user_id: 'user123', |
| 36 | + product_id: 'product456', |
| 37 | + metadata |
| 38 | + }); |
| 39 | + console.log(`✅ License created: ${license.id}`); |
| 40 | + console.log(` License Key: ${license.license_key}`); |
| 41 | + console.log(` Status: ${license.status}`); |
| 42 | + |
| 43 | + // Validate a license |
| 44 | + const licenseKey = generateLicenseKey(); |
| 45 | + console.log(`\n🔍 Validating license key: ${licenseKey}`); |
| 46 | + |
| 47 | + const isValid = await client.getLicenses().validate(licenseKey); |
| 48 | + if (isValid) { |
| 49 | + console.log('✅ License is valid'); |
| 50 | + } else { |
| 51 | + console.log('❌ License is invalid'); |
| 52 | + } |
| 53 | + |
| 54 | + // Get license stats |
| 55 | + const stats = await client.getLicenses().stats(); |
| 56 | + console.log('\n📊 License Statistics:'); |
| 57 | + console.log(` Total: ${stats.total}`); |
| 58 | + console.log(` Active: ${stats.active}`); |
| 59 | + console.log(` Expired: ${stats.expired}`); |
| 60 | + console.log(` Revenue: $${stats.revenue}`); |
| 61 | + |
| 62 | + // 2. User Management |
| 63 | + console.log('\n👤 User Management:'); |
| 64 | + |
| 65 | + // Create a user |
| 66 | + const userMetadata = { |
| 67 | + source: 'javascript-sdk', |
| 68 | + plan: 'premium' |
| 69 | + }; |
| 70 | + |
| 71 | + const user = await client.getUsers().create({ |
| 72 | + email: 'user@example.com', |
| 73 | + name: 'John Doe', |
| 74 | + metadata: userMetadata |
| 75 | + }); |
| 76 | + console.log(`✅ User created: ${user.id}`); |
| 77 | + console.log(` Email: ${user.email}`); |
| 78 | + console.log(` Name: ${user.name}`); |
| 79 | + |
| 80 | + // Get user stats |
| 81 | + const userStats = await client.getUsers().stats(); |
| 82 | + console.log('\n📊 User Statistics:'); |
| 83 | + console.log(` Total: ${userStats.total}`); |
| 84 | + console.log(` Active: ${userStats.active}`); |
| 85 | + console.log(` Inactive: ${userStats.inactive}`); |
| 86 | + |
| 87 | + // 3. Product Management |
| 88 | + console.log('\n📦 Product Management:'); |
| 89 | + |
| 90 | + // Create a product |
| 91 | + const productMetadata = { |
| 92 | + category: 'software', |
| 93 | + tags: ['premium', 'enterprise'] |
| 94 | + }; |
| 95 | + |
| 96 | + const product = await client.getProducts().create({ |
| 97 | + name: 'My Software Product', |
| 98 | + description: 'A great software product', |
| 99 | + price: 99.99, |
| 100 | + currency: 'USD', |
| 101 | + metadata: productMetadata |
| 102 | + }); |
| 103 | + console.log(`✅ Product created: ${product.id}`); |
| 104 | + console.log(` Name: ${product.name}`); |
| 105 | + console.log(` Price: $${product.price} ${product.currency}`); |
| 106 | + |
| 107 | + // Get product stats |
| 108 | + const productStats = await client.getProducts().stats(); |
| 109 | + console.log('\n📊 Product Statistics:'); |
| 110 | + console.log(` Total: ${productStats.total}`); |
| 111 | + console.log(` Active: ${productStats.active}`); |
| 112 | + console.log(` Revenue: $${productStats.revenue}`); |
| 113 | + |
| 114 | + // 4. Webhook Management |
| 115 | + console.log('\n🔗 Webhook Management:'); |
| 116 | + |
| 117 | + // Create a webhook |
| 118 | + const events = [ |
| 119 | + 'license.created', |
| 120 | + 'license.updated', |
| 121 | + 'user.created' |
| 122 | + ]; |
| 123 | + |
| 124 | + const webhook = await client.getWebhooks().create({ |
| 125 | + url: 'https://example.com/webhook', |
| 126 | + events, |
| 127 | + secret: 'webhook-secret' |
| 128 | + }); |
| 129 | + console.log(`✅ Webhook created: ${webhook.id}`); |
| 130 | + console.log(` URL: ${webhook.url}`); |
| 131 | + console.log(` Events: ${webhook.events.join(', ')}`); |
| 132 | + |
| 133 | + // 5. Webhook Processing |
| 134 | + console.log('\n🔄 Webhook Processing:'); |
| 135 | + |
| 136 | + const webhookHandler = new WebhookHandler('webhook-secret'); |
| 137 | + |
| 138 | + // Simulate a webhook event |
| 139 | + const webhookEvent = { |
| 140 | + id: 'evt_123', |
| 141 | + type: 'license.created', |
| 142 | + data: { |
| 143 | + id: 'lic_123', |
| 144 | + user_id: 'user_123', |
| 145 | + product_id: 'prod_123', |
| 146 | + license_key: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345', |
| 147 | + status: 'active', |
| 148 | + created_at: '2023-01-01T00:00:00Z' |
| 149 | + }, |
| 150 | + timestamp: '2023-01-01T00:00:00Z', |
| 151 | + signature: 'signature_here' |
| 152 | + }; |
| 153 | + |
| 154 | + webhookHandler.processEvent(webhookEvent); |
| 155 | + console.log('✅ Webhook event processed successfully'); |
| 156 | + |
| 157 | + // 6. Utility Functions |
| 158 | + console.log('\n🛠️ Utility Functions:'); |
| 159 | + |
| 160 | + // Email validation |
| 161 | + const email = 'test@example.com'; |
| 162 | + console.log(`Email '${email}' is valid: ${validateEmail(email)}`); |
| 163 | + |
| 164 | + // License key validation |
| 165 | + const licenseKey = generateLicenseKey(); |
| 166 | + console.log(`License key '${licenseKey}' is valid: ${validateLicenseKey(licenseKey)}`); |
| 167 | + |
| 168 | + // Generate UUID |
| 169 | + const uuid = generateUuid(); |
| 170 | + console.log(`Generated UUID: ${uuid}`); |
| 171 | + |
| 172 | + // Format bytes |
| 173 | + const bytes = 1024 * 1024; |
| 174 | + console.log(`${bytes} bytes = ${formatBytes(bytes)}`); |
| 175 | + |
| 176 | + // Format duration |
| 177 | + const seconds = 3661; |
| 178 | + console.log(`Duration: ${formatDuration(seconds)}`); |
| 179 | + |
| 180 | + // String utilities |
| 181 | + const text = 'Hello World'; |
| 182 | + console.log(`Capitalize first: ${capitalizeFirst(text)}`); |
| 183 | + console.log(`To snake_case: ${toSnakeCase('HelloWorld')}`); |
| 184 | + console.log(`To PascalCase: ${toPascalCase('hello_world')}`); |
| 185 | + console.log(`Slugify: ${slugify('Hello World!')}`); |
| 186 | + |
| 187 | + // 7. Error Handling |
| 188 | + console.log('\n🛡️ Error Handling:'); |
| 189 | + |
| 190 | + try { |
| 191 | + await client.getLicenses().get('invalid-id'); |
| 192 | + } catch (error) { |
| 193 | + console.log(`✅ Caught expected error: ${error}`); |
| 194 | + } |
| 195 | + |
| 196 | + try { |
| 197 | + await client.getUsers().create({ |
| 198 | + email: 'invalid-email', |
| 199 | + name: 'John Doe' |
| 200 | + }); |
| 201 | + } catch (error) { |
| 202 | + console.log(`✅ Caught expected error: ${error}`); |
| 203 | + } |
| 204 | + |
| 205 | + // 8. API Health Check |
| 206 | + console.log('\n🏥 API Health Check:'); |
| 207 | + |
| 208 | + const ping = await client.ping(); |
| 209 | + console.log(`Ping response: ${jsonSerialize(ping)}`); |
| 210 | + |
| 211 | + const health = await client.health(); |
| 212 | + console.log(`Health response: ${jsonSerialize(health)}`); |
| 213 | + |
| 214 | + console.log('\n✅ Basic usage example completed successfully!'); |
| 215 | + |
| 216 | + } catch (error) { |
| 217 | + console.error('❌ Error:', error); |
| 218 | + if (process.env.DEBUG) { |
| 219 | + console.error('Stack trace:', error); |
| 220 | + } |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +// Run the example |
| 225 | +if (require.main === module) { |
| 226 | + basicUsageExample(); |
| 227 | +} |
| 228 | + |
| 229 | +export { basicUsageExample }; |
0 commit comments