-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathbasic-usage.ts
More file actions
125 lines (104 loc) · 3.39 KB
/
basic-usage.ts
File metadata and controls
125 lines (104 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* NFE.io SDK v3 - Basic Usage Example
*
* Demonstrates core functionality of the SDK
*/
import { NfeClient } from '../src/index.js';
async function basicExample() {
// Create client
const nfe = new NfeClient({
apiKey: 'your-api-key-here',
environment: 'development', // or 'production'
timeout: 30000
});
try {
// Test client configuration
console.log('✅ Client created successfully');
console.log('Client info:', nfe.getClientInfo());
// Health check
const health = await nfe.healthCheck();
console.log('Health check:', health);
// List companies (should work with any valid API key)
const companies = await nfe.companies.list({ pageCount: 5 });
console.log(`Found ${companies.data.length} companies`);
if (companies.data.length > 0) {
const firstCompany = companies.data[0];
console.log('First company:', firstCompany.name);
// List service invoices for first company
const invoices = await nfe.serviceInvoices.list(firstCompany.id!, { pageCount: 5 });
console.log(`Found ${invoices.data.length} invoices for ${firstCompany.name}`);
}
} catch (error) {
console.error('❌ Error:', error);
}
}
async function createInvoiceExample() {
const nfe = new NfeClient({
apiKey: 'your-api-key-here',
environment: 'development'
});
try {
// Example invoice data
const invoiceData = {
cityServiceCode: '2690',
description: 'Consultoria em desenvolvimento de software',
servicesAmount: 1500.00,
borrower: {
type: 'LegalEntity' as const,
federalTaxNumber: 12345678000123,
name: 'Empresa Cliente LTDA',
address: {
country: 'BRA',
postalCode: '01234-567',
street: 'Rua Exemplo, 123',
district: 'Centro',
city: {
code: '3550308',
name: 'São Paulo'
},
state: 'SP'
}
}
};
// Create invoice with automatic wait for completion
const invoice = await nfe.serviceInvoices.createAndWait('company-id', invoiceData);
console.log('✅ Invoice created:', invoice.id, invoice.number);
} catch (error) {
console.error('❌ Error creating invoice:', error);
}
}
// Environment check
function checkEnvironment() {
console.log('=== NFE.io SDK v3 Environment Check ===');
const envCheck = {
nodeVersion: process.version,
hasFetch: typeof fetch !== 'undefined',
hasAbortController: typeof AbortController !== 'undefined',
platform: process.platform,
arch: process.arch
};
console.log('Environment:', envCheck);
if (!envCheck.hasFetch) {
console.error('❌ Fetch API not available - requires Node.js 18+');
return false;
}
const majorVersion = parseInt(process.version.slice(1).split('.')[0]);
if (majorVersion < 18) {
console.error(`❌ Node.js ${majorVersion} not supported - requires Node.js 18+`);
return false;
}
console.log('✅ Environment is compatible');
return true;
}
// Run examples
if (import.meta.url === `file://${process.argv[1]}`) {
console.log('NFE.io SDK v3 - Basic Example\n');
if (checkEnvironment()) {
console.log('\n=== Basic Usage ===');
await basicExample();
console.log('\n=== Invoice Creation ===');
// Uncomment to test invoice creation (requires valid API key and company ID)
// await createInvoiceExample();
}
}