-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtyped-usage.example.ts
More file actions
88 lines (74 loc) · 3.07 KB
/
typed-usage.example.ts
File metadata and controls
88 lines (74 loc) · 3.07 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
/**
* Example: Using generated TypeScript types with S4Kit SDK
*
* After generating types from your API key:
* 1. Download: GET /admin/api-keys/:id/types
* 2. Save as: ./types/s4kit-types.d.ts
* 3. Import and use as shown below
*
* Note: Replace EntityName, CreateEntityNameRequest, UpdateEntityNameRequest
* with actual types from your generated s4kit-types.d.ts file
*/
import { S4Kit } from '../src';
// Import your generated types - replace with actual entity names from your API key
import type {
A_BusinessPartnerType,
CreateA_BusinessPartnerTypeRequest,
UpdateA_BusinessPartnerTypeRequest
// Note: SelectFields is no longer needed with Generic QueryOptions!
} from './types/s4kit-types';
async function example() {
const client = new S4Kit({
baseUrl: 'https://api.s4kit.com/api/proxy',
apiKey: 'your-api-key-here'
});
// ✅ Type-safe list query with autocomplete for select fields
// Now with Generic QueryOptions - no need for `as SelectFields<T>`!
const entities: A_BusinessPartnerType[] = await client.A_BusinessPartnerType.list({
select: [
'BusinessPartner',
'BusinessPartnerFullName',
'BusinessPartnerName',
// ✅ TypeScript automatically provides autocomplete here!
// Just start typing and IDE will suggest field names
// No need for `as SelectFields<A_BusinessPartnerType>` anymore
],
top: 5,
});
// ✅ TypeScript knows the properties
// entities.forEach(entity => {
// console.log(entity.Field1); // ✅ Typed correctly
// console.log(entity.Field2); // ✅ Typed correctly
// // console.log(entity.InvalidProp); // ❌ TypeScript error!
// });
// ✅ Type-safe get by ID
const entity: A_BusinessPartnerType = await client.A_BusinessPartnerType.get('BP001');
console.log('Retrieved entity:', entity.BusinessPartnerFullName);
// ✅ Type-safe create (POST)
// CreateA_BusinessPartnerTypeRequest automatically excludes key fields (BusinessPartner)
// and makes all fields optional - perfect for POST requests
const newEntity: CreateA_BusinessPartnerTypeRequest = {
BusinessPartnerFullName: 'Acme Corporation',
BusinessPartnerName: 'Acme Corp',
BusinessPartnerCategory: '1', // Customer category
FirstName: 'John',
LastName: 'Doe',
// BusinessPartner is NOT in this type (it's a key field, auto-generated)
// All other fields are optional
};
const created: A_BusinessPartnerType = await client.A_BusinessPartnerType.create(newEntity);
console.log('Created entity:', created.BusinessPartner, created.BusinessPartnerFullName);
// ✅ Type-safe update (PATCH)
// UpdateA_BusinessPartnerTypeRequest has all fields optional
const updates: UpdateA_BusinessPartnerTypeRequest = {
BusinessPartnerFullName: 'Acme Corporation Updated',
// Only include fields you want to update
};
const updated: A_BusinessPartnerType = await client.A_BusinessPartnerType.update(
created.BusinessPartner,
updates
);
console.log('Updated entity:', updated.BusinessPartnerFullName);
}
// Run the example
example().catch(console.error);