@drttix/drt-sdk 0.6.0 → 0.6.2
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/demo/test.html +240 -0
- package/dist/cjs/src/functions/sdkConfig.d.ts +55 -0
- package/dist/cjs/src/functions/sdkConfig.js +111 -0
- package/dist/cjs/src/generated/portal/core/OpenAPI.js +1 -1
- package/dist/esm/src/functions/sdkConfig.d.ts +55 -0
- package/dist/esm/src/functions/sdkConfig.js +103 -0
- package/dist/esm/src/generated/portal/core/OpenAPI.js +1 -1
- package/package.json +1 -1
- package/src/functions/sdkConfig.ts +132 -0
- package/src/generated/portal/core/OpenAPI.ts +1 -1
package/demo/test.html
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<title>DRT SDK Test Page</title>
|
|
6
|
+
<style>
|
|
7
|
+
body {
|
|
8
|
+
font-family: system-ui, sans-serif;
|
|
9
|
+
max-width: 900px;
|
|
10
|
+
margin: 2rem auto;
|
|
11
|
+
padding: 0 1rem;
|
|
12
|
+
}
|
|
13
|
+
h1 {
|
|
14
|
+
color: #333;
|
|
15
|
+
}
|
|
16
|
+
h2 {
|
|
17
|
+
color: #555;
|
|
18
|
+
margin-top: 2rem;
|
|
19
|
+
}
|
|
20
|
+
button {
|
|
21
|
+
padding: 0.5rem 1rem;
|
|
22
|
+
font-size: 1rem;
|
|
23
|
+
cursor: pointer;
|
|
24
|
+
margin: 0.25rem;
|
|
25
|
+
}
|
|
26
|
+
pre {
|
|
27
|
+
background: #f4f4f4;
|
|
28
|
+
padding: 1rem;
|
|
29
|
+
overflow: auto;
|
|
30
|
+
border-radius: 4px;
|
|
31
|
+
max-height: 400px;
|
|
32
|
+
}
|
|
33
|
+
.success {
|
|
34
|
+
color: green;
|
|
35
|
+
}
|
|
36
|
+
.error {
|
|
37
|
+
color: red;
|
|
38
|
+
}
|
|
39
|
+
#status {
|
|
40
|
+
margin: 1rem 0;
|
|
41
|
+
font-weight: bold;
|
|
42
|
+
}
|
|
43
|
+
.config-form {
|
|
44
|
+
background: #f9f9f9;
|
|
45
|
+
padding: 1rem;
|
|
46
|
+
border-radius: 4px;
|
|
47
|
+
margin-bottom: 1rem;
|
|
48
|
+
}
|
|
49
|
+
.config-form label {
|
|
50
|
+
display: block;
|
|
51
|
+
margin: 0.5rem 0 0.25rem;
|
|
52
|
+
}
|
|
53
|
+
.config-form input {
|
|
54
|
+
width: 100%;
|
|
55
|
+
padding: 0.5rem;
|
|
56
|
+
font-size: 1rem;
|
|
57
|
+
box-sizing: border-box;
|
|
58
|
+
}
|
|
59
|
+
</style>
|
|
60
|
+
</head>
|
|
61
|
+
<body>
|
|
62
|
+
<h1>DRT SDK Test Page</h1>
|
|
63
|
+
|
|
64
|
+
<h2>1. Initialize SDK</h2>
|
|
65
|
+
<div class="config-form">
|
|
66
|
+
<label for="shopperGuid">Shopper GUID:</label>
|
|
67
|
+
<input
|
|
68
|
+
type="text"
|
|
69
|
+
id="shopperGuid"
|
|
70
|
+
placeholder="e.g., F1199DCD-BB45-4BD0-8DED600A3A234C75"
|
|
71
|
+
/>
|
|
72
|
+
|
|
73
|
+
<label for="accountId">Account ID:</label>
|
|
74
|
+
<input type="text" id="accountId" placeholder="e.g., 508" />
|
|
75
|
+
|
|
76
|
+
<label for="apiKey">API Key:</label>
|
|
77
|
+
<input type="text" id="apiKey" placeholder="e.g., your-api-key" />
|
|
78
|
+
|
|
79
|
+
<button id="btn-init" style="margin-top: 1rem">
|
|
80
|
+
Initialize SDK
|
|
81
|
+
</button>
|
|
82
|
+
</div>
|
|
83
|
+
|
|
84
|
+
<h2>2. Test API Calls</h2>
|
|
85
|
+
<p>
|
|
86
|
+
After initializing, these calls will automatically use your
|
|
87
|
+
credentials.
|
|
88
|
+
</p>
|
|
89
|
+
<div>
|
|
90
|
+
<button id="btn-check-sdk">Check SDK Status</button>
|
|
91
|
+
<button id="btn-strings">Get Scanner Strings (no auth)</button>
|
|
92
|
+
<button id="btn-theme">Get Theme (requires auth)</button>
|
|
93
|
+
</div>
|
|
94
|
+
|
|
95
|
+
<div id="status"></div>
|
|
96
|
+
<pre id="output">
|
|
97
|
+
Initialize the SDK above, then click a button to test...</pre
|
|
98
|
+
>
|
|
99
|
+
|
|
100
|
+
<script type="module">
|
|
101
|
+
import { DRT } from '../dist/esm/index.js';
|
|
102
|
+
|
|
103
|
+
const out = document.getElementById('output');
|
|
104
|
+
const status = document.getElementById('status');
|
|
105
|
+
|
|
106
|
+
function setStatus(msg, isError = false) {
|
|
107
|
+
status.textContent = msg;
|
|
108
|
+
status.className = isError ? 'error' : 'success';
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function setOutput(data) {
|
|
112
|
+
out.textContent =
|
|
113
|
+
typeof data === 'string'
|
|
114
|
+
? data
|
|
115
|
+
: JSON.stringify(data, null, 2);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Expose DRT globally for console testing
|
|
119
|
+
window.DRT = DRT;
|
|
120
|
+
|
|
121
|
+
// Initialize SDK
|
|
122
|
+
document
|
|
123
|
+
.getElementById('btn-init')
|
|
124
|
+
.addEventListener('click', () => {
|
|
125
|
+
const shopperGuid = document
|
|
126
|
+
.getElementById('shopperGuid')
|
|
127
|
+
.value.trim();
|
|
128
|
+
const accountId = document
|
|
129
|
+
.getElementById('accountId')
|
|
130
|
+
.value.trim();
|
|
131
|
+
const apiKey = document
|
|
132
|
+
.getElementById('apiKey')
|
|
133
|
+
.value.trim();
|
|
134
|
+
|
|
135
|
+
if (!shopperGuid || !accountId || !apiKey) {
|
|
136
|
+
setStatus('Please fill in all fields', true);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
DRT.init({
|
|
142
|
+
shopperGuid,
|
|
143
|
+
accountId,
|
|
144
|
+
apiKey,
|
|
145
|
+
});
|
|
146
|
+
setStatus('SDK Initialized!', false);
|
|
147
|
+
setOutput({
|
|
148
|
+
message: 'SDK initialized successfully!',
|
|
149
|
+
config: {
|
|
150
|
+
shopperGuid,
|
|
151
|
+
accountId,
|
|
152
|
+
apiKey: '***', // Don't show full key
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
} catch (err) {
|
|
156
|
+
setStatus('Init failed: ' + err.message, true);
|
|
157
|
+
setOutput({ error: err.message });
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// Check SDK status
|
|
162
|
+
document
|
|
163
|
+
.getElementById('btn-check-sdk')
|
|
164
|
+
.addEventListener('click', () => {
|
|
165
|
+
setStatus('SDK Status', false);
|
|
166
|
+
const config = DRT.getConfig();
|
|
167
|
+
setOutput({
|
|
168
|
+
isReady: DRT.isReady(),
|
|
169
|
+
config: config
|
|
170
|
+
? {
|
|
171
|
+
shopperGuid: config.shopperGuid,
|
|
172
|
+
accountId: config.accountId,
|
|
173
|
+
apiKey: '***',
|
|
174
|
+
}
|
|
175
|
+
: null,
|
|
176
|
+
availableServices: {
|
|
177
|
+
shopper: Object.keys(DRT.shopper),
|
|
178
|
+
scanner: Object.keys(DRT.scanner),
|
|
179
|
+
portal: Object.keys(DRT.portal),
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// Get scanner strings (no auth required)
|
|
185
|
+
document
|
|
186
|
+
.getElementById('btn-strings')
|
|
187
|
+
.addEventListener('click', async () => {
|
|
188
|
+
setStatus('Fetching...', false);
|
|
189
|
+
try {
|
|
190
|
+
const strings =
|
|
191
|
+
await DRT.scanner.scanning.appControllerGetStrings();
|
|
192
|
+
setStatus('Success!', false);
|
|
193
|
+
setOutput(strings);
|
|
194
|
+
} catch (err) {
|
|
195
|
+
setStatus('Error: ' + err.message, true);
|
|
196
|
+
setOutput({
|
|
197
|
+
error: err.message,
|
|
198
|
+
details: err.toString(),
|
|
199
|
+
});
|
|
200
|
+
console.error(err);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// Get theme (requires auth - credentials auto-injected after init!)
|
|
205
|
+
document
|
|
206
|
+
.getElementById('btn-theme')
|
|
207
|
+
.addEventListener('click', async () => {
|
|
208
|
+
if (!DRT.isReady()) {
|
|
209
|
+
setStatus(
|
|
210
|
+
'SDK not initialized! Please initialize first.',
|
|
211
|
+
true,
|
|
212
|
+
);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
setStatus('Fetching theme...', false);
|
|
217
|
+
try {
|
|
218
|
+
// After DRT.init(), credentials are automatically injected - no params needed!
|
|
219
|
+
const theme = await DRT.shopper.account.getTheme();
|
|
220
|
+
setStatus('Success!', false);
|
|
221
|
+
setOutput(theme);
|
|
222
|
+
} catch (err) {
|
|
223
|
+
setStatus('Error: ' + err.message, true);
|
|
224
|
+
setOutput({
|
|
225
|
+
error: err.message,
|
|
226
|
+
details: err.toString(),
|
|
227
|
+
});
|
|
228
|
+
console.error(err);
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// Auto-check on load
|
|
233
|
+
setStatus(
|
|
234
|
+
'SDK loaded! Initialize with your credentials above.',
|
|
235
|
+
false,
|
|
236
|
+
);
|
|
237
|
+
console.log('DRT SDK available at window.DRT for console testing');
|
|
238
|
+
</script>
|
|
239
|
+
</body>
|
|
240
|
+
</html>
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export interface SDKConfig {
|
|
2
|
+
shopperGuid: string;
|
|
3
|
+
accountId: string;
|
|
4
|
+
apiKey: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Initialize the DRT SDK with credentials.
|
|
8
|
+
* Once initialized, these credentials will be automatically included
|
|
9
|
+
* in all API requests as headers.
|
|
10
|
+
*
|
|
11
|
+
* @param config - The SDK configuration containing shopperGuid, accountId, and apiKey
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```javascript
|
|
15
|
+
* import { DRT } from '@drttix/drt-sdk';
|
|
16
|
+
*
|
|
17
|
+
* // Initialize once
|
|
18
|
+
* DRT.init({
|
|
19
|
+
* shopperGuid: 'your-shopper-guid',
|
|
20
|
+
* accountId: 'your-account-id',
|
|
21
|
+
* apiKey: 'your-api-key'
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Now make calls without passing credentials
|
|
25
|
+
* const theme = await DRT.shopper.account.getTheme();
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function init(config: SDKConfig): void;
|
|
29
|
+
/**
|
|
30
|
+
* Check if the SDK has been initialized with credentials.
|
|
31
|
+
*/
|
|
32
|
+
export declare function isReady(): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Get the current SDK configuration (if initialized).
|
|
35
|
+
*/
|
|
36
|
+
export declare function getConfig(): SDKConfig | null;
|
|
37
|
+
/**
|
|
38
|
+
* Reset the SDK configuration.
|
|
39
|
+
*/
|
|
40
|
+
export declare function reset(): void;
|
|
41
|
+
/**
|
|
42
|
+
* Get credentials from config, throwing if not initialized.
|
|
43
|
+
* Used internally by wrapped services.
|
|
44
|
+
*/
|
|
45
|
+
export declare function getCredentials(): {
|
|
46
|
+
shopperGuid: string;
|
|
47
|
+
accountId: string;
|
|
48
|
+
apiKey: string;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Creates a proxy that auto-injects credentials for service methods.
|
|
52
|
+
* Methods that take (shopperguid, accountid, apikey, ...rest) as first 3 params
|
|
53
|
+
* will have those auto-filled from the SDK config.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createAutoCredentialsProxy<T extends object>(service: T): T;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.init = init;
|
|
4
|
+
exports.isReady = isReady;
|
|
5
|
+
exports.getConfig = getConfig;
|
|
6
|
+
exports.reset = reset;
|
|
7
|
+
exports.getCredentials = getCredentials;
|
|
8
|
+
exports.createAutoCredentialsProxy = createAutoCredentialsProxy;
|
|
9
|
+
// SDK Configuration - allows initializing credentials once
|
|
10
|
+
const OpenAPI_1 = require("../generated/shopper/core/OpenAPI");
|
|
11
|
+
const OpenAPI_2 = require("../generated/scanner/core/OpenAPI");
|
|
12
|
+
const OpenAPI_3 = require("../generated/portal/core/OpenAPI");
|
|
13
|
+
let isInitialized = false;
|
|
14
|
+
let currentConfig = null;
|
|
15
|
+
/**
|
|
16
|
+
* Initialize the DRT SDK with credentials.
|
|
17
|
+
* Once initialized, these credentials will be automatically included
|
|
18
|
+
* in all API requests as headers.
|
|
19
|
+
*
|
|
20
|
+
* @param config - The SDK configuration containing shopperGuid, accountId, and apiKey
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```javascript
|
|
24
|
+
* import { DRT } from '@drttix/drt-sdk';
|
|
25
|
+
*
|
|
26
|
+
* // Initialize once
|
|
27
|
+
* DRT.init({
|
|
28
|
+
* shopperGuid: 'your-shopper-guid',
|
|
29
|
+
* accountId: 'your-account-id',
|
|
30
|
+
* apiKey: 'your-api-key'
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* // Now make calls without passing credentials
|
|
34
|
+
* const theme = await DRT.shopper.account.getTheme();
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
function init(config) {
|
|
38
|
+
if (!config.shopperGuid || !config.accountId || !config.apiKey) {
|
|
39
|
+
throw new Error('DRT SDK init requires shopperGuid, accountId, and apiKey');
|
|
40
|
+
}
|
|
41
|
+
currentConfig = config;
|
|
42
|
+
const headers = {
|
|
43
|
+
shopperguid: config.shopperGuid,
|
|
44
|
+
accountid: config.accountId,
|
|
45
|
+
apikey: config.apiKey,
|
|
46
|
+
};
|
|
47
|
+
// Set headers on all API clients
|
|
48
|
+
OpenAPI_1.OpenAPI.HEADERS = headers;
|
|
49
|
+
OpenAPI_2.OpenAPI.HEADERS = headers;
|
|
50
|
+
OpenAPI_3.OpenAPI.HEADERS = headers;
|
|
51
|
+
isInitialized = true;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Check if the SDK has been initialized with credentials.
|
|
55
|
+
*/
|
|
56
|
+
function isReady() {
|
|
57
|
+
return isInitialized;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Get the current SDK configuration (if initialized).
|
|
61
|
+
*/
|
|
62
|
+
function getConfig() {
|
|
63
|
+
return currentConfig;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Reset the SDK configuration.
|
|
67
|
+
*/
|
|
68
|
+
function reset() {
|
|
69
|
+
currentConfig = null;
|
|
70
|
+
isInitialized = false;
|
|
71
|
+
OpenAPI_1.OpenAPI.HEADERS = undefined;
|
|
72
|
+
OpenAPI_2.OpenAPI.HEADERS = undefined;
|
|
73
|
+
OpenAPI_3.OpenAPI.HEADERS = undefined;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Get credentials from config, throwing if not initialized.
|
|
77
|
+
* Used internally by wrapped services.
|
|
78
|
+
*/
|
|
79
|
+
function getCredentials() {
|
|
80
|
+
if (!currentConfig) {
|
|
81
|
+
throw new Error('DRT SDK not initialized. Call DRT.init() first.');
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
shopperGuid: currentConfig.shopperGuid,
|
|
85
|
+
accountId: currentConfig.accountId,
|
|
86
|
+
apiKey: currentConfig.apiKey,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Creates a proxy that auto-injects credentials for service methods.
|
|
91
|
+
* Methods that take (shopperguid, accountid, apikey, ...rest) as first 3 params
|
|
92
|
+
* will have those auto-filled from the SDK config.
|
|
93
|
+
*/
|
|
94
|
+
function createAutoCredentialsProxy(service) {
|
|
95
|
+
return new Proxy(service, {
|
|
96
|
+
get(target, prop) {
|
|
97
|
+
const value = target[prop];
|
|
98
|
+
if (typeof value === 'function') {
|
|
99
|
+
return (...args) => {
|
|
100
|
+
// If SDK is initialized, prepend credentials
|
|
101
|
+
if (isInitialized && currentConfig) {
|
|
102
|
+
return value.call(target, currentConfig.shopperGuid, currentConfig.accountId, currentConfig.apiKey, ...args);
|
|
103
|
+
}
|
|
104
|
+
// Otherwise call with original args
|
|
105
|
+
return value.apply(target, args);
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return value;
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export interface SDKConfig {
|
|
2
|
+
shopperGuid: string;
|
|
3
|
+
accountId: string;
|
|
4
|
+
apiKey: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Initialize the DRT SDK with credentials.
|
|
8
|
+
* Once initialized, these credentials will be automatically included
|
|
9
|
+
* in all API requests as headers.
|
|
10
|
+
*
|
|
11
|
+
* @param config - The SDK configuration containing shopperGuid, accountId, and apiKey
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```javascript
|
|
15
|
+
* import { DRT } from '@drttix/drt-sdk';
|
|
16
|
+
*
|
|
17
|
+
* // Initialize once
|
|
18
|
+
* DRT.init({
|
|
19
|
+
* shopperGuid: 'your-shopper-guid',
|
|
20
|
+
* accountId: 'your-account-id',
|
|
21
|
+
* apiKey: 'your-api-key'
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Now make calls without passing credentials
|
|
25
|
+
* const theme = await DRT.shopper.account.getTheme();
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function init(config: SDKConfig): void;
|
|
29
|
+
/**
|
|
30
|
+
* Check if the SDK has been initialized with credentials.
|
|
31
|
+
*/
|
|
32
|
+
export declare function isReady(): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Get the current SDK configuration (if initialized).
|
|
35
|
+
*/
|
|
36
|
+
export declare function getConfig(): SDKConfig | null;
|
|
37
|
+
/**
|
|
38
|
+
* Reset the SDK configuration.
|
|
39
|
+
*/
|
|
40
|
+
export declare function reset(): void;
|
|
41
|
+
/**
|
|
42
|
+
* Get credentials from config, throwing if not initialized.
|
|
43
|
+
* Used internally by wrapped services.
|
|
44
|
+
*/
|
|
45
|
+
export declare function getCredentials(): {
|
|
46
|
+
shopperGuid: string;
|
|
47
|
+
accountId: string;
|
|
48
|
+
apiKey: string;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Creates a proxy that auto-injects credentials for service methods.
|
|
52
|
+
* Methods that take (shopperguid, accountid, apikey, ...rest) as first 3 params
|
|
53
|
+
* will have those auto-filled from the SDK config.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createAutoCredentialsProxy<T extends object>(service: T): T;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// SDK Configuration - allows initializing credentials once
|
|
2
|
+
import { OpenAPI as ShopperAPI } from '../generated/shopper/core/OpenAPI';
|
|
3
|
+
import { OpenAPI as ScannerAPI } from '../generated/scanner/core/OpenAPI';
|
|
4
|
+
import { OpenAPI as PortalAPI } from '../generated/portal/core/OpenAPI';
|
|
5
|
+
let isInitialized = false;
|
|
6
|
+
let currentConfig = null;
|
|
7
|
+
/**
|
|
8
|
+
* Initialize the DRT SDK with credentials.
|
|
9
|
+
* Once initialized, these credentials will be automatically included
|
|
10
|
+
* in all API requests as headers.
|
|
11
|
+
*
|
|
12
|
+
* @param config - The SDK configuration containing shopperGuid, accountId, and apiKey
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```javascript
|
|
16
|
+
* import { DRT } from '@drttix/drt-sdk';
|
|
17
|
+
*
|
|
18
|
+
* // Initialize once
|
|
19
|
+
* DRT.init({
|
|
20
|
+
* shopperGuid: 'your-shopper-guid',
|
|
21
|
+
* accountId: 'your-account-id',
|
|
22
|
+
* apiKey: 'your-api-key'
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Now make calls without passing credentials
|
|
26
|
+
* const theme = await DRT.shopper.account.getTheme();
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function init(config) {
|
|
30
|
+
if (!config.shopperGuid || !config.accountId || !config.apiKey) {
|
|
31
|
+
throw new Error('DRT SDK init requires shopperGuid, accountId, and apiKey');
|
|
32
|
+
}
|
|
33
|
+
currentConfig = config;
|
|
34
|
+
const headers = {
|
|
35
|
+
shopperguid: config.shopperGuid,
|
|
36
|
+
accountid: config.accountId,
|
|
37
|
+
apikey: config.apiKey,
|
|
38
|
+
};
|
|
39
|
+
// Set headers on all API clients
|
|
40
|
+
ShopperAPI.HEADERS = headers;
|
|
41
|
+
ScannerAPI.HEADERS = headers;
|
|
42
|
+
PortalAPI.HEADERS = headers;
|
|
43
|
+
isInitialized = true;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Check if the SDK has been initialized with credentials.
|
|
47
|
+
*/
|
|
48
|
+
export function isReady() {
|
|
49
|
+
return isInitialized;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Get the current SDK configuration (if initialized).
|
|
53
|
+
*/
|
|
54
|
+
export function getConfig() {
|
|
55
|
+
return currentConfig;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Reset the SDK configuration.
|
|
59
|
+
*/
|
|
60
|
+
export function reset() {
|
|
61
|
+
currentConfig = null;
|
|
62
|
+
isInitialized = false;
|
|
63
|
+
ShopperAPI.HEADERS = undefined;
|
|
64
|
+
ScannerAPI.HEADERS = undefined;
|
|
65
|
+
PortalAPI.HEADERS = undefined;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Get credentials from config, throwing if not initialized.
|
|
69
|
+
* Used internally by wrapped services.
|
|
70
|
+
*/
|
|
71
|
+
export function getCredentials() {
|
|
72
|
+
if (!currentConfig) {
|
|
73
|
+
throw new Error('DRT SDK not initialized. Call DRT.init() first.');
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
shopperGuid: currentConfig.shopperGuid,
|
|
77
|
+
accountId: currentConfig.accountId,
|
|
78
|
+
apiKey: currentConfig.apiKey,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Creates a proxy that auto-injects credentials for service methods.
|
|
83
|
+
* Methods that take (shopperguid, accountid, apikey, ...rest) as first 3 params
|
|
84
|
+
* will have those auto-filled from the SDK config.
|
|
85
|
+
*/
|
|
86
|
+
export function createAutoCredentialsProxy(service) {
|
|
87
|
+
return new Proxy(service, {
|
|
88
|
+
get(target, prop) {
|
|
89
|
+
const value = target[prop];
|
|
90
|
+
if (typeof value === 'function') {
|
|
91
|
+
return (...args) => {
|
|
92
|
+
// If SDK is initialized, prepend credentials
|
|
93
|
+
if (isInitialized && currentConfig) {
|
|
94
|
+
return value.call(target, currentConfig.shopperGuid, currentConfig.accountId, currentConfig.apiKey, ...args);
|
|
95
|
+
}
|
|
96
|
+
// Otherwise call with original args
|
|
97
|
+
return value.apply(target, args);
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
return value;
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// SDK Configuration - allows initializing credentials once
|
|
2
|
+
import { OpenAPI as ShopperAPI } from '../generated/shopper/core/OpenAPI';
|
|
3
|
+
import { OpenAPI as ScannerAPI } from '../generated/scanner/core/OpenAPI';
|
|
4
|
+
import { OpenAPI as PortalAPI } from '../generated/portal/core/OpenAPI';
|
|
5
|
+
|
|
6
|
+
export interface SDKConfig {
|
|
7
|
+
shopperGuid: string;
|
|
8
|
+
accountId: string;
|
|
9
|
+
apiKey: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let isInitialized = false;
|
|
13
|
+
let currentConfig: SDKConfig | null = null;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Initialize the DRT SDK with credentials.
|
|
17
|
+
* Once initialized, these credentials will be automatically included
|
|
18
|
+
* in all API requests as headers.
|
|
19
|
+
*
|
|
20
|
+
* @param config - The SDK configuration containing shopperGuid, accountId, and apiKey
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```javascript
|
|
24
|
+
* import { DRT } from '@drttix/drt-sdk';
|
|
25
|
+
*
|
|
26
|
+
* // Initialize once
|
|
27
|
+
* DRT.init({
|
|
28
|
+
* shopperGuid: 'your-shopper-guid',
|
|
29
|
+
* accountId: 'your-account-id',
|
|
30
|
+
* apiKey: 'your-api-key'
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* // Now make calls without passing credentials
|
|
34
|
+
* const theme = await DRT.shopper.account.getTheme();
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function init(config: SDKConfig): void {
|
|
38
|
+
if (!config.shopperGuid || !config.accountId || !config.apiKey) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
'DRT SDK init requires shopperGuid, accountId, and apiKey',
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
currentConfig = config;
|
|
45
|
+
|
|
46
|
+
const headers = {
|
|
47
|
+
shopperguid: config.shopperGuid,
|
|
48
|
+
accountid: config.accountId,
|
|
49
|
+
apikey: config.apiKey,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// Set headers on all API clients
|
|
53
|
+
ShopperAPI.HEADERS = headers;
|
|
54
|
+
ScannerAPI.HEADERS = headers;
|
|
55
|
+
PortalAPI.HEADERS = headers;
|
|
56
|
+
|
|
57
|
+
isInitialized = true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Check if the SDK has been initialized with credentials.
|
|
62
|
+
*/
|
|
63
|
+
export function isReady(): boolean {
|
|
64
|
+
return isInitialized;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Get the current SDK configuration (if initialized).
|
|
69
|
+
*/
|
|
70
|
+
export function getConfig(): SDKConfig | null {
|
|
71
|
+
return currentConfig;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Reset the SDK configuration.
|
|
76
|
+
*/
|
|
77
|
+
export function reset(): void {
|
|
78
|
+
currentConfig = null;
|
|
79
|
+
isInitialized = false;
|
|
80
|
+
ShopperAPI.HEADERS = undefined;
|
|
81
|
+
ScannerAPI.HEADERS = undefined;
|
|
82
|
+
PortalAPI.HEADERS = undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Get credentials from config, throwing if not initialized.
|
|
87
|
+
* Used internally by wrapped services.
|
|
88
|
+
*/
|
|
89
|
+
export function getCredentials(): {
|
|
90
|
+
shopperGuid: string;
|
|
91
|
+
accountId: string;
|
|
92
|
+
apiKey: string;
|
|
93
|
+
} {
|
|
94
|
+
if (!currentConfig) {
|
|
95
|
+
throw new Error('DRT SDK not initialized. Call DRT.init() first.');
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
shopperGuid: currentConfig.shopperGuid,
|
|
99
|
+
accountId: currentConfig.accountId,
|
|
100
|
+
apiKey: currentConfig.apiKey,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Creates a proxy that auto-injects credentials for service methods.
|
|
106
|
+
* Methods that take (shopperguid, accountid, apikey, ...rest) as first 3 params
|
|
107
|
+
* will have those auto-filled from the SDK config.
|
|
108
|
+
*/
|
|
109
|
+
export function createAutoCredentialsProxy<T extends object>(service: T): T {
|
|
110
|
+
return new Proxy(service, {
|
|
111
|
+
get(target, prop) {
|
|
112
|
+
const value = (target as any)[prop];
|
|
113
|
+
if (typeof value === 'function') {
|
|
114
|
+
return (...args: any[]) => {
|
|
115
|
+
// If SDK is initialized, prepend credentials
|
|
116
|
+
if (isInitialized && currentConfig) {
|
|
117
|
+
return value.call(
|
|
118
|
+
target,
|
|
119
|
+
currentConfig.shopperGuid,
|
|
120
|
+
currentConfig.accountId,
|
|
121
|
+
currentConfig.apiKey,
|
|
122
|
+
...args,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
// Otherwise call with original args
|
|
126
|
+
return value.apply(target, args);
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
return value;
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
}
|