s4kit 0.0.1 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +188 -0
- package/dist/cli.cjs +211 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +188 -0
- package/dist/index.cjs +1661 -0
- package/dist/index.d.cts +1175 -0
- package/dist/index.d.ts +1175 -0
- package/dist/index.js +1600 -0
- package/package.json +60 -6
- package/index.js +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Michal Majer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# S4Kit
|
|
2
|
+
|
|
3
|
+
The simplest way to integrate with SAP S/4HANA.
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
const customers = await client.Customers.list({ top: 10 });
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install s4kit
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { S4Kit } from 's4kit';
|
|
19
|
+
|
|
20
|
+
const client = S4Kit({ apiKey: 'sk_live_...' });
|
|
21
|
+
|
|
22
|
+
// List
|
|
23
|
+
const customers = await client.Customers.list({ top: 10 });
|
|
24
|
+
|
|
25
|
+
// Get
|
|
26
|
+
const customer = await client.Customers.get('ALFKI');
|
|
27
|
+
|
|
28
|
+
// Create
|
|
29
|
+
const created = await client.Customers.create({
|
|
30
|
+
CustomerID: 'NEWCO',
|
|
31
|
+
CompanyName: 'New Company'
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Update
|
|
35
|
+
await client.Customers.update('NEWCO', { CompanyName: 'Updated Name' });
|
|
36
|
+
|
|
37
|
+
// Delete
|
|
38
|
+
await client.Customers.delete('NEWCO');
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Type Safety
|
|
42
|
+
|
|
43
|
+
Generate types for full autocomplete and type checking:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npx s4kit generate-types --api-key sk_live_... --output ./types
|
|
47
|
+
|
|
48
|
+
# Self-hosted
|
|
49
|
+
npx s4kit generate-types --api-key sk_live_... --base-url http://localhost:3002 --output ./types
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { S4Kit } from 's4kit';
|
|
54
|
+
import './types'; // Enable type inference
|
|
55
|
+
|
|
56
|
+
const client = S4Kit({ apiKey: 'sk_live_...' });
|
|
57
|
+
|
|
58
|
+
// Full autocomplete on entity names
|
|
59
|
+
const customers = await client.Customers.list({
|
|
60
|
+
select: ['CustomerID', 'CompanyName', 'City'], // Type-safe fields
|
|
61
|
+
filter: { City: 'Berlin' },
|
|
62
|
+
top: 10
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// customers is Customer[], not any[]
|
|
66
|
+
customers.forEach(c => {
|
|
67
|
+
console.log(c.CompanyName); // Autocomplete works!
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Query Options
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
await client.Products.list({
|
|
75
|
+
// Select fields
|
|
76
|
+
select: ['ProductID', 'ProductName', 'UnitPrice'],
|
|
77
|
+
|
|
78
|
+
// Filter - multiple formats
|
|
79
|
+
filter: { Category: 'Beverages' }, // Simple equality
|
|
80
|
+
filter: { UnitPrice: { gt: 20 } }, // Operators: gt, lt, ge, le, ne
|
|
81
|
+
filter: { ProductName: { contains: 'Ch' } }, // String: contains, startswith, endswith
|
|
82
|
+
filter: "UnitPrice gt 20 and Category eq 'Beverages'", // Raw OData
|
|
83
|
+
|
|
84
|
+
// Pagination
|
|
85
|
+
top: 10,
|
|
86
|
+
skip: 20,
|
|
87
|
+
|
|
88
|
+
// Sorting
|
|
89
|
+
orderBy: { UnitPrice: 'desc' },
|
|
90
|
+
orderBy: [{ Category: 'asc' }, { ProductName: 'asc' }],
|
|
91
|
+
|
|
92
|
+
// Expand relations
|
|
93
|
+
expand: ['Category', 'Supplier'],
|
|
94
|
+
expand: {
|
|
95
|
+
Category: true,
|
|
96
|
+
Supplier: { select: ['CompanyName'] }
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Count & Pagination
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
// Get count
|
|
105
|
+
const total = await client.Products.count();
|
|
106
|
+
const filtered = await client.Products.count({ filter: { Discontinued: false } });
|
|
107
|
+
|
|
108
|
+
// List with count
|
|
109
|
+
const { value, count } = await client.Products.listWithCount({ top: 10 });
|
|
110
|
+
console.log(`Showing ${value.length} of ${count}`);
|
|
111
|
+
|
|
112
|
+
// Iterate all pages
|
|
113
|
+
for await (const page of client.Products.paginate({ pageSize: 100 })) {
|
|
114
|
+
console.log(`Processing ${page.value.length} items`);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Get all (auto-pagination)
|
|
118
|
+
const all = await client.Products.all();
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Navigation Properties
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
// Access related entities
|
|
125
|
+
const orderItems = await client.Orders.nav(10248, 'Order_Details').list();
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Batch Operations
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
// Multiple operations in one request
|
|
132
|
+
const results = await client.batch([
|
|
133
|
+
{ method: 'GET', entity: 'Products', id: 1 },
|
|
134
|
+
{ method: 'POST', entity: 'Products', data: { ProductName: 'New' } },
|
|
135
|
+
{ method: 'PATCH', entity: 'Products', id: 2, data: { UnitPrice: 29.99 } },
|
|
136
|
+
{ method: 'DELETE', entity: 'Products', id: 3 },
|
|
137
|
+
]);
|
|
138
|
+
|
|
139
|
+
// Atomic transaction (all succeed or all fail)
|
|
140
|
+
await client.changeset({
|
|
141
|
+
operations: [
|
|
142
|
+
{ method: 'POST', entity: 'Orders', data: orderData },
|
|
143
|
+
{ method: 'POST', entity: 'Order_Details', data: item1 },
|
|
144
|
+
{ method: 'POST', entity: 'Order_Details', data: item2 },
|
|
145
|
+
]
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Error Handling
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
import { S4KitError, NotFoundError, ValidationError } from 's4kit';
|
|
153
|
+
|
|
154
|
+
try {
|
|
155
|
+
await client.Products.get(99999);
|
|
156
|
+
} catch (error) {
|
|
157
|
+
if (error instanceof NotFoundError) {
|
|
158
|
+
console.log('Product not found');
|
|
159
|
+
} else if (error instanceof ValidationError) {
|
|
160
|
+
console.log('Invalid data:', error.details);
|
|
161
|
+
} else if (error instanceof S4KitError) {
|
|
162
|
+
console.log(error.friendlyMessage);
|
|
163
|
+
console.log(error.help);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Configuration
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
const client = S4Kit({
|
|
172
|
+
apiKey: 'sk_live_...', // Required
|
|
173
|
+
baseUrl: 'https://api.s4kit.com/api/proxy', // Default (or self-hosted)
|
|
174
|
+
connection: 'prod', // Default SAP instance
|
|
175
|
+
timeout: 30000, // Request timeout (ms)
|
|
176
|
+
retries: 3, // Retry on failure
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// Override per request
|
|
180
|
+
await client.Products.list({
|
|
181
|
+
connection: 'sandbox', // Use different instance
|
|
182
|
+
service: 'API_PRODUCT' // Specify service
|
|
183
|
+
});
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
MIT
|
package/dist/cli.cjs
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
18
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
19
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
20
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
21
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
+
mod
|
|
24
|
+
));
|
|
25
|
+
|
|
26
|
+
// src/cli.ts
|
|
27
|
+
var VERSION = "0.1.0";
|
|
28
|
+
function parseArgs(args) {
|
|
29
|
+
const options = {};
|
|
30
|
+
const positional = [];
|
|
31
|
+
let command = "help";
|
|
32
|
+
for (let i = 0; i < args.length; i++) {
|
|
33
|
+
const arg = args[i];
|
|
34
|
+
if (arg === "generate-types" || arg === "init" || arg === "help" || arg === "version") {
|
|
35
|
+
command = arg;
|
|
36
|
+
} else if (arg === "--api-key" || arg === "-k") {
|
|
37
|
+
options.apiKey = args[++i] ?? "";
|
|
38
|
+
} else if (arg === "--base-url" || arg === "-u") {
|
|
39
|
+
options.baseUrl = args[++i] ?? "";
|
|
40
|
+
} else if (arg === "--connection" || arg === "-c") {
|
|
41
|
+
options.connection = args[++i] ?? "";
|
|
42
|
+
} else if (arg === "--output" || arg === "-o") {
|
|
43
|
+
options.output = args[++i] ?? "";
|
|
44
|
+
} else if (arg === "--help" || arg === "-h") {
|
|
45
|
+
command = "help";
|
|
46
|
+
} else if (arg === "--version" || arg === "-v") {
|
|
47
|
+
command = "version";
|
|
48
|
+
} else if (!arg.startsWith("-")) {
|
|
49
|
+
positional.push(arg);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return { command, options, positional };
|
|
53
|
+
}
|
|
54
|
+
function showHelp() {
|
|
55
|
+
console.log(`
|
|
56
|
+
s4kit - The lightweight, type-safe SDK for SAP S/4HANA
|
|
57
|
+
|
|
58
|
+
Usage:
|
|
59
|
+
s4kit <command> [options]
|
|
60
|
+
|
|
61
|
+
Commands:
|
|
62
|
+
generate-types Generate TypeScript types from SAP metadata
|
|
63
|
+
init Initialize S4Kit in your project
|
|
64
|
+
help Show this help message
|
|
65
|
+
version Show version number
|
|
66
|
+
|
|
67
|
+
Options:
|
|
68
|
+
-k, --api-key API key for S4Kit platform
|
|
69
|
+
-u, --base-url Proxy URL (default: https://api.s4kit.com/api/proxy)
|
|
70
|
+
-c, --connection SAP connection alias
|
|
71
|
+
-o, --output Output directory for generated files
|
|
72
|
+
|
|
73
|
+
Examples:
|
|
74
|
+
# Generate types for all entities accessible by your API key
|
|
75
|
+
s4kit generate-types --api-key sk_live_xxx --output ./types
|
|
76
|
+
|
|
77
|
+
# Generate types for a specific connection
|
|
78
|
+
s4kit generate-types -k sk_live_xxx -c erp-prod -o ./types
|
|
79
|
+
|
|
80
|
+
# Initialize S4Kit in your project
|
|
81
|
+
s4kit init
|
|
82
|
+
|
|
83
|
+
Environment Variables:
|
|
84
|
+
S4KIT_API_KEY Default API key (can be overridden with --api-key)
|
|
85
|
+
S4KIT_BASE_URL Default base URL
|
|
86
|
+
|
|
87
|
+
Documentation: https://github.com/michal-majer/s4kit
|
|
88
|
+
`);
|
|
89
|
+
}
|
|
90
|
+
function showVersion() {
|
|
91
|
+
console.log(`s4kit v${VERSION}`);
|
|
92
|
+
}
|
|
93
|
+
async function generateTypes(options) {
|
|
94
|
+
const apiKey = options.apiKey || process.env.S4KIT_API_KEY;
|
|
95
|
+
const baseUrl = options.baseUrl || process.env.S4KIT_BASE_URL || "https://api.s4kit.com/api/proxy";
|
|
96
|
+
const output = options.output || "./s4kit-types";
|
|
97
|
+
if (!apiKey) {
|
|
98
|
+
console.error("Error: API key is required");
|
|
99
|
+
console.error(" Use --api-key or set S4KIT_API_KEY environment variable");
|
|
100
|
+
process.exit(1);
|
|
101
|
+
}
|
|
102
|
+
console.log("Generating TypeScript types...");
|
|
103
|
+
console.log(` API: ${baseUrl}`);
|
|
104
|
+
console.log(` Output: ${output}`);
|
|
105
|
+
try {
|
|
106
|
+
const response = await fetch(`${baseUrl}/$types`, {
|
|
107
|
+
headers: {
|
|
108
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
109
|
+
"Accept": "application/typescript"
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
if (!response.ok) {
|
|
113
|
+
if (response.status === 401) {
|
|
114
|
+
console.error("Error: Invalid API key");
|
|
115
|
+
process.exit(1);
|
|
116
|
+
}
|
|
117
|
+
if (response.status === 404) {
|
|
118
|
+
console.error("Error: Types endpoint not found. Make sure you have the correct base URL.");
|
|
119
|
+
process.exit(1);
|
|
120
|
+
}
|
|
121
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
122
|
+
}
|
|
123
|
+
const types = await response.text();
|
|
124
|
+
const fs = await import("fs");
|
|
125
|
+
const path = await import("path");
|
|
126
|
+
const outputDir = path.resolve(output);
|
|
127
|
+
if (!fs.existsSync(outputDir)) {
|
|
128
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
129
|
+
}
|
|
130
|
+
const typesFile = path.join(outputDir, "index.d.ts");
|
|
131
|
+
fs.writeFileSync(typesFile, types);
|
|
132
|
+
console.log(`
|
|
133
|
+
Types generated successfully!`);
|
|
134
|
+
console.log(` ${typesFile}`);
|
|
135
|
+
console.log(`
|
|
136
|
+
Usage:`);
|
|
137
|
+
console.log(` import type { A_BusinessPartner } from '${output}';`);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
console.error("Failed to generate types:", error instanceof Error ? error.message : error);
|
|
140
|
+
process.exit(1);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function init() {
|
|
144
|
+
console.log("Initializing S4Kit...\n");
|
|
145
|
+
const fs = await import("fs");
|
|
146
|
+
const configFile = "s4kit.config.ts";
|
|
147
|
+
if (fs.existsSync(configFile)) {
|
|
148
|
+
console.log(`Config file already exists: ${configFile}`);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const config = `// S4Kit Configuration
|
|
152
|
+
// See: https://github.com/michal-majer/s4kit
|
|
153
|
+
|
|
154
|
+
export default {
|
|
155
|
+
// Your S4Kit API key (use environment variable in production)
|
|
156
|
+
apiKey: process.env.S4KIT_API_KEY || '',
|
|
157
|
+
|
|
158
|
+
// Default SAP connection alias
|
|
159
|
+
connection: 'erp-dev',
|
|
160
|
+
|
|
161
|
+
// Optional: Base URL (default: https://api.s4kit.com)
|
|
162
|
+
// baseUrl: 'https://api.s4kit.com',
|
|
163
|
+
};
|
|
164
|
+
`;
|
|
165
|
+
fs.writeFileSync(configFile, config);
|
|
166
|
+
console.log(`Created ${configFile}`);
|
|
167
|
+
const typesDir = "./s4kit-types";
|
|
168
|
+
if (!fs.existsSync(typesDir)) {
|
|
169
|
+
fs.mkdirSync(typesDir, { recursive: true });
|
|
170
|
+
console.log(`Created ${typesDir}/`);
|
|
171
|
+
}
|
|
172
|
+
const gitignore = ".gitignore";
|
|
173
|
+
if (fs.existsSync(gitignore)) {
|
|
174
|
+
const content = fs.readFileSync(gitignore, "utf-8");
|
|
175
|
+
if (!content.includes("s4kit-types")) {
|
|
176
|
+
fs.appendFileSync(gitignore, "\n# S4Kit generated types\ns4kit-types/\n");
|
|
177
|
+
console.log(`Updated ${gitignore}`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
console.log(`
|
|
181
|
+
Next steps:
|
|
182
|
+
1. Set your API key in S4KIT_API_KEY environment variable
|
|
183
|
+
2. Run: npx s4kit generate-types
|
|
184
|
+
3. Import types in your code:
|
|
185
|
+
import { S4Kit } from 's4kit';
|
|
186
|
+
import type { A_BusinessPartner } from './s4kit-types';
|
|
187
|
+
`);
|
|
188
|
+
}
|
|
189
|
+
async function main() {
|
|
190
|
+
const args = process.argv.slice(2);
|
|
191
|
+
const { command, options } = parseArgs(args);
|
|
192
|
+
switch (command) {
|
|
193
|
+
case "generate-types":
|
|
194
|
+
await generateTypes(options);
|
|
195
|
+
break;
|
|
196
|
+
case "init":
|
|
197
|
+
await init();
|
|
198
|
+
break;
|
|
199
|
+
case "version":
|
|
200
|
+
showVersion();
|
|
201
|
+
break;
|
|
202
|
+
case "help":
|
|
203
|
+
default:
|
|
204
|
+
showHelp();
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
main().catch((error) => {
|
|
209
|
+
console.error("Unexpected error:", error);
|
|
210
|
+
process.exit(1);
|
|
211
|
+
});
|
package/dist/cli.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/cli.ts
|
|
4
|
+
var VERSION = "0.1.0";
|
|
5
|
+
function parseArgs(args) {
|
|
6
|
+
const options = {};
|
|
7
|
+
const positional = [];
|
|
8
|
+
let command = "help";
|
|
9
|
+
for (let i = 0; i < args.length; i++) {
|
|
10
|
+
const arg = args[i];
|
|
11
|
+
if (arg === "generate-types" || arg === "init" || arg === "help" || arg === "version") {
|
|
12
|
+
command = arg;
|
|
13
|
+
} else if (arg === "--api-key" || arg === "-k") {
|
|
14
|
+
options.apiKey = args[++i] ?? "";
|
|
15
|
+
} else if (arg === "--base-url" || arg === "-u") {
|
|
16
|
+
options.baseUrl = args[++i] ?? "";
|
|
17
|
+
} else if (arg === "--connection" || arg === "-c") {
|
|
18
|
+
options.connection = args[++i] ?? "";
|
|
19
|
+
} else if (arg === "--output" || arg === "-o") {
|
|
20
|
+
options.output = args[++i] ?? "";
|
|
21
|
+
} else if (arg === "--help" || arg === "-h") {
|
|
22
|
+
command = "help";
|
|
23
|
+
} else if (arg === "--version" || arg === "-v") {
|
|
24
|
+
command = "version";
|
|
25
|
+
} else if (!arg.startsWith("-")) {
|
|
26
|
+
positional.push(arg);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return { command, options, positional };
|
|
30
|
+
}
|
|
31
|
+
function showHelp() {
|
|
32
|
+
console.log(`
|
|
33
|
+
s4kit - The lightweight, type-safe SDK for SAP S/4HANA
|
|
34
|
+
|
|
35
|
+
Usage:
|
|
36
|
+
s4kit <command> [options]
|
|
37
|
+
|
|
38
|
+
Commands:
|
|
39
|
+
generate-types Generate TypeScript types from SAP metadata
|
|
40
|
+
init Initialize S4Kit in your project
|
|
41
|
+
help Show this help message
|
|
42
|
+
version Show version number
|
|
43
|
+
|
|
44
|
+
Options:
|
|
45
|
+
-k, --api-key API key for S4Kit platform
|
|
46
|
+
-u, --base-url Proxy URL (default: https://api.s4kit.com/api/proxy)
|
|
47
|
+
-c, --connection SAP connection alias
|
|
48
|
+
-o, --output Output directory for generated files
|
|
49
|
+
|
|
50
|
+
Examples:
|
|
51
|
+
# Generate types for all entities accessible by your API key
|
|
52
|
+
s4kit generate-types --api-key sk_live_xxx --output ./types
|
|
53
|
+
|
|
54
|
+
# Generate types for a specific connection
|
|
55
|
+
s4kit generate-types -k sk_live_xxx -c erp-prod -o ./types
|
|
56
|
+
|
|
57
|
+
# Initialize S4Kit in your project
|
|
58
|
+
s4kit init
|
|
59
|
+
|
|
60
|
+
Environment Variables:
|
|
61
|
+
S4KIT_API_KEY Default API key (can be overridden with --api-key)
|
|
62
|
+
S4KIT_BASE_URL Default base URL
|
|
63
|
+
|
|
64
|
+
Documentation: https://github.com/michal-majer/s4kit
|
|
65
|
+
`);
|
|
66
|
+
}
|
|
67
|
+
function showVersion() {
|
|
68
|
+
console.log(`s4kit v${VERSION}`);
|
|
69
|
+
}
|
|
70
|
+
async function generateTypes(options) {
|
|
71
|
+
const apiKey = options.apiKey || process.env.S4KIT_API_KEY;
|
|
72
|
+
const baseUrl = options.baseUrl || process.env.S4KIT_BASE_URL || "https://api.s4kit.com/api/proxy";
|
|
73
|
+
const output = options.output || "./s4kit-types";
|
|
74
|
+
if (!apiKey) {
|
|
75
|
+
console.error("Error: API key is required");
|
|
76
|
+
console.error(" Use --api-key or set S4KIT_API_KEY environment variable");
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
79
|
+
console.log("Generating TypeScript types...");
|
|
80
|
+
console.log(` API: ${baseUrl}`);
|
|
81
|
+
console.log(` Output: ${output}`);
|
|
82
|
+
try {
|
|
83
|
+
const response = await fetch(`${baseUrl}/$types`, {
|
|
84
|
+
headers: {
|
|
85
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
86
|
+
"Accept": "application/typescript"
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
if (!response.ok) {
|
|
90
|
+
if (response.status === 401) {
|
|
91
|
+
console.error("Error: Invalid API key");
|
|
92
|
+
process.exit(1);
|
|
93
|
+
}
|
|
94
|
+
if (response.status === 404) {
|
|
95
|
+
console.error("Error: Types endpoint not found. Make sure you have the correct base URL.");
|
|
96
|
+
process.exit(1);
|
|
97
|
+
}
|
|
98
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
99
|
+
}
|
|
100
|
+
const types = await response.text();
|
|
101
|
+
const fs = await import("fs");
|
|
102
|
+
const path = await import("path");
|
|
103
|
+
const outputDir = path.resolve(output);
|
|
104
|
+
if (!fs.existsSync(outputDir)) {
|
|
105
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
106
|
+
}
|
|
107
|
+
const typesFile = path.join(outputDir, "index.d.ts");
|
|
108
|
+
fs.writeFileSync(typesFile, types);
|
|
109
|
+
console.log(`
|
|
110
|
+
Types generated successfully!`);
|
|
111
|
+
console.log(` ${typesFile}`);
|
|
112
|
+
console.log(`
|
|
113
|
+
Usage:`);
|
|
114
|
+
console.log(` import type { A_BusinessPartner } from '${output}';`);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.error("Failed to generate types:", error instanceof Error ? error.message : error);
|
|
117
|
+
process.exit(1);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async function init() {
|
|
121
|
+
console.log("Initializing S4Kit...\n");
|
|
122
|
+
const fs = await import("fs");
|
|
123
|
+
const configFile = "s4kit.config.ts";
|
|
124
|
+
if (fs.existsSync(configFile)) {
|
|
125
|
+
console.log(`Config file already exists: ${configFile}`);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const config = `// S4Kit Configuration
|
|
129
|
+
// See: https://github.com/michal-majer/s4kit
|
|
130
|
+
|
|
131
|
+
export default {
|
|
132
|
+
// Your S4Kit API key (use environment variable in production)
|
|
133
|
+
apiKey: process.env.S4KIT_API_KEY || '',
|
|
134
|
+
|
|
135
|
+
// Default SAP connection alias
|
|
136
|
+
connection: 'erp-dev',
|
|
137
|
+
|
|
138
|
+
// Optional: Base URL (default: https://api.s4kit.com)
|
|
139
|
+
// baseUrl: 'https://api.s4kit.com',
|
|
140
|
+
};
|
|
141
|
+
`;
|
|
142
|
+
fs.writeFileSync(configFile, config);
|
|
143
|
+
console.log(`Created ${configFile}`);
|
|
144
|
+
const typesDir = "./s4kit-types";
|
|
145
|
+
if (!fs.existsSync(typesDir)) {
|
|
146
|
+
fs.mkdirSync(typesDir, { recursive: true });
|
|
147
|
+
console.log(`Created ${typesDir}/`);
|
|
148
|
+
}
|
|
149
|
+
const gitignore = ".gitignore";
|
|
150
|
+
if (fs.existsSync(gitignore)) {
|
|
151
|
+
const content = fs.readFileSync(gitignore, "utf-8");
|
|
152
|
+
if (!content.includes("s4kit-types")) {
|
|
153
|
+
fs.appendFileSync(gitignore, "\n# S4Kit generated types\ns4kit-types/\n");
|
|
154
|
+
console.log(`Updated ${gitignore}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
console.log(`
|
|
158
|
+
Next steps:
|
|
159
|
+
1. Set your API key in S4KIT_API_KEY environment variable
|
|
160
|
+
2. Run: npx s4kit generate-types
|
|
161
|
+
3. Import types in your code:
|
|
162
|
+
import { S4Kit } from 's4kit';
|
|
163
|
+
import type { A_BusinessPartner } from './s4kit-types';
|
|
164
|
+
`);
|
|
165
|
+
}
|
|
166
|
+
async function main() {
|
|
167
|
+
const args = process.argv.slice(2);
|
|
168
|
+
const { command, options } = parseArgs(args);
|
|
169
|
+
switch (command) {
|
|
170
|
+
case "generate-types":
|
|
171
|
+
await generateTypes(options);
|
|
172
|
+
break;
|
|
173
|
+
case "init":
|
|
174
|
+
await init();
|
|
175
|
+
break;
|
|
176
|
+
case "version":
|
|
177
|
+
showVersion();
|
|
178
|
+
break;
|
|
179
|
+
case "help":
|
|
180
|
+
default:
|
|
181
|
+
showHelp();
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
main().catch((error) => {
|
|
186
|
+
console.error("Unexpected error:", error);
|
|
187
|
+
process.exit(1);
|
|
188
|
+
});
|