optropic 1.0.0

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.
@@ -0,0 +1,478 @@
1
+ /**
2
+ * optropic - Public Types
3
+ *
4
+ * Configuration and error types for the Optropic SDK.
5
+ *
6
+ * @module optropic
7
+ */
8
+ /**
9
+ * Configuration for the Optropic SDK client.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const config: OptropicConfig = {
14
+ * apiKey: 'optr_live_xxxxxxxxxxxx',
15
+ * };
16
+ * ```
17
+ */
18
+ interface OptropicConfig {
19
+ /**
20
+ * API key for authentication.
21
+ * Format: optr_live_xxx (production) or optr_test_xxx (test)
22
+ */
23
+ readonly apiKey: string;
24
+ /**
25
+ * Base URL override (for self-hosted deployments).
26
+ * If not provided, uses Optropic's managed infrastructure.
27
+ */
28
+ readonly baseUrl?: string;
29
+ /**
30
+ * Request timeout in milliseconds.
31
+ * @default 30000
32
+ */
33
+ readonly timeout?: number;
34
+ /**
35
+ * Custom headers to include in all requests.
36
+ */
37
+ readonly headers?: Record<string, string>;
38
+ /**
39
+ * Retry configuration for failed requests.
40
+ */
41
+ readonly retry?: RetryConfig;
42
+ }
43
+ /**
44
+ * Retry configuration for network requests.
45
+ */
46
+ interface RetryConfig {
47
+ /**
48
+ * Maximum number of retry attempts.
49
+ * @default 3
50
+ */
51
+ readonly maxRetries?: number;
52
+ /**
53
+ * Base delay between retries in milliseconds.
54
+ * Uses exponential backoff: delay * 2^attempt
55
+ * @default 1000
56
+ */
57
+ readonly baseDelay?: number;
58
+ /**
59
+ * Maximum delay between retries in milliseconds.
60
+ * @default 10000
61
+ */
62
+ readonly maxDelay?: number;
63
+ }
64
+ /**
65
+ * Error codes returned by the SDK.
66
+ * Use these for programmatic error handling.
67
+ */
68
+ type ErrorCode = 'INVALID_API_KEY' | 'EXPIRED_API_KEY' | 'INSUFFICIENT_PERMISSIONS' | 'INVALID_GTIN' | 'INVALID_SERIAL' | 'INVALID_CODE_FORMAT' | 'INVALID_BATCH_CONFIG' | 'CODE_NOT_FOUND' | 'BATCH_NOT_FOUND' | 'PRODUCT_NOT_FOUND' | 'CODE_REVOKED' | 'CODE_EXPIRED' | 'BATCH_ALREADY_EXISTS' | 'RATE_LIMITED' | 'QUOTA_EXCEEDED' | 'NETWORK_ERROR' | 'TIMEOUT' | 'SERVICE_UNAVAILABLE' | 'INTERNAL_ERROR' | 'UNKNOWN_ERROR';
69
+
70
+ interface Asset {
71
+ readonly id: string;
72
+ readonly short_id: string;
73
+ readonly gtin: string;
74
+ readonly serial: string;
75
+ readonly status: 'active' | 'revoked';
76
+ readonly security_level: 'signed' | 'provenance';
77
+ readonly metadata?: Record<string, string>;
78
+ readonly created_at: string;
79
+ readonly revoked_at?: string;
80
+ }
81
+ interface CreateAssetParams {
82
+ readonly gtin: string;
83
+ readonly serial: string;
84
+ readonly metadata?: Record<string, string>;
85
+ }
86
+ interface ListAssetsParams {
87
+ readonly limit?: number;
88
+ readonly offset?: number;
89
+ readonly status?: 'active' | 'revoked';
90
+ readonly gtin?: string;
91
+ }
92
+ interface VerifyResult {
93
+ readonly id: string;
94
+ readonly status: string;
95
+ readonly security_level: string;
96
+ readonly signature_valid: boolean;
97
+ readonly verification_count: number;
98
+ readonly last_verified_at: string;
99
+ readonly revocation_status: 'active' | 'revoked';
100
+ readonly verification_mode: 'online' | 'offline';
101
+ readonly provenance_valid?: boolean;
102
+ readonly evidence?: VerificationEvidence;
103
+ }
104
+ interface VerificationEvidence {
105
+ readonly signature_valid: boolean;
106
+ readonly revocation_status: string;
107
+ readonly verification_mode: string;
108
+ readonly provenance_valid?: boolean;
109
+ readonly security_level?: string;
110
+ readonly verification_count?: number;
111
+ }
112
+ interface BatchCreateParams {
113
+ readonly assets: CreateAssetParams[];
114
+ }
115
+ interface BatchCreateResult {
116
+ readonly created: number;
117
+ readonly asset_ids: string[];
118
+ }
119
+ declare class AssetsResource {
120
+ private readonly request;
121
+ constructor(request: RequestFn);
122
+ create(params: CreateAssetParams): Promise<Asset>;
123
+ list(params?: ListAssetsParams): Promise<Asset[]>;
124
+ get(assetId: string): Promise<Asset>;
125
+ verify(assetId: string): Promise<VerifyResult>;
126
+ revoke(assetId: string, reason?: string): Promise<Asset>;
127
+ batchCreate(params: BatchCreateParams): Promise<BatchCreateResult>;
128
+ private buildQuery;
129
+ }
130
+
131
+ interface ApiKey {
132
+ readonly id: string;
133
+ readonly name: string;
134
+ readonly key_prefix: string;
135
+ readonly environment: string;
136
+ readonly permissions: string[];
137
+ readonly created_at: string;
138
+ readonly last_used_at?: string;
139
+ }
140
+ interface CreateKeyParams {
141
+ readonly name?: string;
142
+ readonly environment?: 'production' | 'test';
143
+ }
144
+ interface CreateKeyResult extends ApiKey {
145
+ readonly key: string;
146
+ }
147
+ declare class KeysResource {
148
+ private readonly request;
149
+ constructor(request: RequestFn);
150
+ create(params?: CreateKeyParams): Promise<CreateKeyResult>;
151
+ list(): Promise<ApiKey[]>;
152
+ revoke(keyId: string): Promise<void>;
153
+ }
154
+
155
+ /**
156
+ * optropic - OptropicClient
157
+ *
158
+ * The SDK interface for integrations with the Optropic API.
159
+ *
160
+ * @module optropic
161
+ */
162
+
163
+ interface RequestOptions {
164
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
165
+ path: string;
166
+ body?: unknown;
167
+ headers?: Record<string, string>;
168
+ timeout?: number;
169
+ }
170
+ type RequestFn = <T>(options: RequestOptions) => Promise<T>;
171
+ /**
172
+ * OptropicClient - The SDK interface for Optropic API integrations.
173
+ *
174
+ * @example
175
+ * ```typescript
176
+ * import { OptropicClient } from 'optropic';
177
+ *
178
+ * const client = new OptropicClient({
179
+ * apiKey: 'optr_live_xxxxxxxxxxxx',
180
+ * });
181
+ *
182
+ * // Create an asset
183
+ * const asset = await client.assets.create({
184
+ * gtin: '01234567890123',
185
+ * serial: 'SN001',
186
+ * });
187
+ *
188
+ * // Verify an asset
189
+ * const result = await client.assets.verify(asset.id);
190
+ * ```
191
+ */
192
+ declare class OptropicClient {
193
+ private readonly config;
194
+ private readonly baseUrl;
195
+ private readonly retryConfig;
196
+ readonly assets: AssetsResource;
197
+ readonly keys: KeysResource;
198
+ constructor(config: OptropicConfig);
199
+ private isValidApiKey;
200
+ private request;
201
+ private executeRequest;
202
+ private sleep;
203
+ }
204
+ /**
205
+ * Create an OptropicClient instance.
206
+ *
207
+ * @param config - Client configuration
208
+ * @returns Configured OptropicClient
209
+ *
210
+ * @example
211
+ * ```typescript
212
+ * import { createClient } from 'optropic';
213
+ *
214
+ * const client = createClient({
215
+ * apiKey: process.env.OPTROPIC_API_KEY!,
216
+ * });
217
+ * ```
218
+ */
219
+ declare function createClient(config: OptropicConfig): OptropicClient;
220
+
221
+ /**
222
+ * optropic - Error Classes
223
+ *
224
+ * Clear, actionable errors for API integrations.
225
+ * Each error includes a code for programmatic handling
226
+ * and a human-readable message.
227
+ *
228
+ * @module optropic
229
+ */
230
+
231
+ /**
232
+ * Base error class for all Optropic SDK errors.
233
+ *
234
+ * @example
235
+ * ```typescript
236
+ * try {
237
+ * await client.verify(scanData);
238
+ * } catch (error) {
239
+ * if (error instanceof OptropicError) {
240
+ * console.log('Error code:', error.code);
241
+ * console.log('Message:', error.message);
242
+ * if (error.retryable) {
243
+ * // Implement retry logic
244
+ * }
245
+ * }
246
+ * }
247
+ * ```
248
+ */
249
+ declare class OptropicError extends Error {
250
+ /**
251
+ * Error code for programmatic handling.
252
+ */
253
+ readonly code: ErrorCode;
254
+ /**
255
+ * HTTP status code (if applicable).
256
+ */
257
+ readonly statusCode?: number;
258
+ /**
259
+ * Whether this error can be retried.
260
+ */
261
+ readonly retryable: boolean;
262
+ /**
263
+ * Additional details about the error.
264
+ */
265
+ readonly details?: Record<string, unknown>;
266
+ /**
267
+ * Request ID for support reference.
268
+ */
269
+ readonly requestId?: string;
270
+ constructor(code: ErrorCode, message: string, options?: {
271
+ statusCode?: number;
272
+ retryable?: boolean;
273
+ details?: Record<string, unknown>;
274
+ requestId?: string;
275
+ cause?: Error;
276
+ });
277
+ /**
278
+ * Create a JSON-serializable representation.
279
+ */
280
+ toJSON(): Record<string, unknown>;
281
+ }
282
+ /**
283
+ * Error thrown when API key authentication fails.
284
+ *
285
+ * Common causes:
286
+ * - Invalid API key format
287
+ * - Expired API key
288
+ * - Revoked API key
289
+ * - Wrong environment (using test key in production)
290
+ */
291
+ declare class AuthenticationError extends OptropicError {
292
+ constructor(message?: string, options?: {
293
+ code?: ErrorCode;
294
+ details?: Record<string, unknown>;
295
+ requestId?: string;
296
+ });
297
+ }
298
+ /**
299
+ * Error thrown when a serial number is invalid.
300
+ *
301
+ * Serial numbers must:
302
+ * - Be non-empty
303
+ * - Not exceed 20 characters (GS1 limit)
304
+ * - Contain only allowed characters
305
+ */
306
+ declare class InvalidSerialError extends OptropicError {
307
+ /**
308
+ * The invalid serial that was provided.
309
+ */
310
+ readonly serial: string;
311
+ constructor(serial: string, message?: string, options?: {
312
+ details?: Record<string, unknown>;
313
+ requestId?: string;
314
+ });
315
+ }
316
+ /**
317
+ * Error thrown when a GTIN is invalid.
318
+ */
319
+ declare class InvalidGTINError extends OptropicError {
320
+ /**
321
+ * The invalid GTIN that was provided.
322
+ */
323
+ readonly gtin: string;
324
+ constructor(gtin: string, message?: string, options?: {
325
+ details?: Record<string, unknown>;
326
+ requestId?: string;
327
+ });
328
+ }
329
+ /**
330
+ * Error thrown when a code format is invalid.
331
+ */
332
+ declare class InvalidCodeError extends OptropicError {
333
+ /**
334
+ * The invalid code that was provided.
335
+ */
336
+ readonly invalidCode: string;
337
+ constructor(invalidCode: string, message?: string, options?: {
338
+ details?: Record<string, unknown>;
339
+ requestId?: string;
340
+ });
341
+ }
342
+ /**
343
+ * Error thrown when a code has been revoked.
344
+ *
345
+ * Revocation reasons include:
346
+ * - Confirmed counterfeit
347
+ * - Product recall
348
+ * - Duplicate detection
349
+ * - Manual revocation by brand
350
+ */
351
+ declare class RevokedCodeError extends OptropicError {
352
+ /**
353
+ * The revoked code.
354
+ */
355
+ readonly revokedCode: string;
356
+ /**
357
+ * Reason for revocation.
358
+ */
359
+ readonly reason: string;
360
+ /**
361
+ * When the code was revoked.
362
+ */
363
+ readonly revokedAt: string;
364
+ constructor(code: string, reason: string, revokedAt: string, options?: {
365
+ details?: Record<string, unknown>;
366
+ requestId?: string;
367
+ });
368
+ }
369
+ /**
370
+ * Error thrown when a code is not found.
371
+ */
372
+ declare class CodeNotFoundError extends OptropicError {
373
+ /**
374
+ * The code that was not found.
375
+ */
376
+ readonly searchedCode: string;
377
+ constructor(code: string, options?: {
378
+ details?: Record<string, unknown>;
379
+ requestId?: string;
380
+ });
381
+ }
382
+ /**
383
+ * Error thrown when a batch is not found.
384
+ */
385
+ declare class BatchNotFoundError extends OptropicError {
386
+ /**
387
+ * The batch ID that was not found.
388
+ */
389
+ readonly batchId: string;
390
+ constructor(batchId: string, options?: {
391
+ details?: Record<string, unknown>;
392
+ requestId?: string;
393
+ });
394
+ }
395
+ /**
396
+ * Error thrown when rate limit is exceeded.
397
+ *
398
+ * Rate limits are per-client and may vary by vertical policy.
399
+ * Check `retryAfter` for when to retry.
400
+ */
401
+ declare class RateLimitedError extends OptropicError {
402
+ /**
403
+ * Seconds until rate limit resets.
404
+ */
405
+ readonly retryAfter: number;
406
+ /**
407
+ * Current rate limit (requests per period).
408
+ */
409
+ readonly limit: number;
410
+ /**
411
+ * Remaining requests in current period.
412
+ */
413
+ readonly remaining: number;
414
+ constructor(retryAfter: number, options?: {
415
+ limit?: number;
416
+ remaining?: number;
417
+ details?: Record<string, unknown>;
418
+ requestId?: string;
419
+ });
420
+ }
421
+ /**
422
+ * Error thrown when quota is exceeded.
423
+ */
424
+ declare class QuotaExceededError extends OptropicError {
425
+ /**
426
+ * Current quota limit.
427
+ */
428
+ readonly quotaLimit: number;
429
+ /**
430
+ * Quota used.
431
+ */
432
+ readonly quotaUsed: number;
433
+ /**
434
+ * When quota resets (ISO 8601).
435
+ */
436
+ readonly resetsAt: string;
437
+ constructor(quotaLimit: number, quotaUsed: number, resetsAt: string, options?: {
438
+ details?: Record<string, unknown>;
439
+ requestId?: string;
440
+ });
441
+ }
442
+ /**
443
+ * Error thrown when a network error occurs.
444
+ */
445
+ declare class NetworkError extends OptropicError {
446
+ constructor(message?: string, options?: {
447
+ cause?: Error;
448
+ details?: Record<string, unknown>;
449
+ requestId?: string;
450
+ });
451
+ }
452
+ /**
453
+ * Error thrown when a request times out.
454
+ */
455
+ declare class TimeoutError extends OptropicError {
456
+ /**
457
+ * Timeout duration in milliseconds.
458
+ */
459
+ readonly timeoutMs: number;
460
+ constructor(timeoutMs: number, options?: {
461
+ details?: Record<string, unknown>;
462
+ requestId?: string;
463
+ });
464
+ }
465
+ /**
466
+ * Error thrown when service is unavailable.
467
+ */
468
+ declare class ServiceUnavailableError extends OptropicError {
469
+ constructor(message?: string, options?: {
470
+ retryAfter?: number;
471
+ details?: Record<string, unknown>;
472
+ requestId?: string;
473
+ });
474
+ }
475
+
476
+ declare const SDK_VERSION = "1.0.0";
477
+
478
+ export { type ApiKey, type Asset, AssetsResource, AuthenticationError, type BatchCreateParams, type BatchCreateResult, BatchNotFoundError, CodeNotFoundError, type CreateAssetParams, type CreateKeyParams, type CreateKeyResult, type ErrorCode, InvalidCodeError, InvalidGTINError, InvalidSerialError, KeysResource, type ListAssetsParams, NetworkError, OptropicClient, type OptropicConfig, OptropicError, QuotaExceededError, RateLimitedError, type RequestFn, type RetryConfig, RevokedCodeError, SDK_VERSION, ServiceUnavailableError, TimeoutError, type VerificationEvidence, type VerifyResult, createClient };