colacloud 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.
- package/LICENSE +21 -0
- package/README.md +340 -0
- package/dist/index.d.mts +580 -0
- package/dist/index.d.ts +580 -0
- package/dist/index.js +671 -0
- package/dist/index.mjs +632 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 COLA Cloud
|
|
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,340 @@
|
|
|
1
|
+
# COLA Cloud JavaScript/TypeScript SDK
|
|
2
|
+
|
|
3
|
+
Official JavaScript/TypeScript SDK for the [COLA Cloud API](https://colacloud.us) - access the TTB COLA Registry of alcohol product label approvals.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Full TypeScript support with comprehensive type definitions
|
|
8
|
+
- Works in Node.js 18+ and modern browsers
|
|
9
|
+
- Async/await API with async iterators for pagination
|
|
10
|
+
- Custom error classes for precise error handling
|
|
11
|
+
- Rate limit information accessible from responses
|
|
12
|
+
- Zero dependencies (uses native fetch)
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install colacloud
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { ColaCloud } from 'colacloud';
|
|
24
|
+
|
|
25
|
+
const client = new ColaCloud({ apiKey: 'your-api-key' });
|
|
26
|
+
|
|
27
|
+
// Search COLAs
|
|
28
|
+
const { data, pagination } = await client.colas.list({
|
|
29
|
+
q: 'bourbon',
|
|
30
|
+
productType: 'distilled spirits',
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
console.log(`Found ${pagination.total} results`);
|
|
34
|
+
for (const cola of data) {
|
|
35
|
+
console.log(`${cola.brand_name}: ${cola.product_name}`);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
### Configuration
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { ColaCloud } from 'colacloud';
|
|
45
|
+
|
|
46
|
+
const client = new ColaCloud({
|
|
47
|
+
apiKey: 'your-api-key',
|
|
48
|
+
// Optional: custom base URL (for testing)
|
|
49
|
+
baseUrl: 'https://app.colacloud.us/api/v1',
|
|
50
|
+
// Optional: request timeout in milliseconds (default: 30000)
|
|
51
|
+
timeout: 30000,
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Searching COLAs
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
// Basic search
|
|
59
|
+
const results = await client.colas.list({ q: 'whiskey' });
|
|
60
|
+
|
|
61
|
+
// Advanced search with filters
|
|
62
|
+
const filtered = await client.colas.list({
|
|
63
|
+
q: 'bourbon',
|
|
64
|
+
productType: 'distilled spirits',
|
|
65
|
+
origin: 'united states',
|
|
66
|
+
brandName: 'maker',
|
|
67
|
+
approvalDateFrom: '2023-01-01',
|
|
68
|
+
approvalDateTo: '2023-12-31',
|
|
69
|
+
abvMin: 40,
|
|
70
|
+
abvMax: 50,
|
|
71
|
+
page: 1,
|
|
72
|
+
perPage: 50,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Access pagination info
|
|
76
|
+
console.log(`Page ${filtered.pagination.page} of ${filtered.pagination.pages}`);
|
|
77
|
+
console.log(`Total: ${filtered.pagination.total} COLAs`);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Getting a Single COLA
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
const cola = await client.colas.get('12345678');
|
|
84
|
+
|
|
85
|
+
console.log(cola.brand_name);
|
|
86
|
+
console.log(cola.product_name);
|
|
87
|
+
console.log(cola.abv);
|
|
88
|
+
|
|
89
|
+
// Access images
|
|
90
|
+
for (const image of cola.images) {
|
|
91
|
+
console.log(image.url);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Access barcodes
|
|
95
|
+
for (const barcode of cola.barcodes) {
|
|
96
|
+
console.log(`${barcode.barcode_type}: ${barcode.barcode_value}`);
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Async Iterator for Pagination
|
|
101
|
+
|
|
102
|
+
Use async iterators to automatically page through all results:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
// Iterate through all matching COLAs
|
|
106
|
+
for await (const cola of client.colas.iterate({ q: 'vodka' })) {
|
|
107
|
+
console.log(cola.ttb_id, cola.brand_name);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Collect into an array (use with caution on large datasets)
|
|
111
|
+
import { collectAll, take } from 'colacloud';
|
|
112
|
+
|
|
113
|
+
const allColas = await collectAll(
|
|
114
|
+
client.colas.iterate({ q: 'rare whiskey' }),
|
|
115
|
+
1000 // Optional: safety limit
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
// Take first N results
|
|
119
|
+
const firstTen = await take(client.colas.iterate({ q: 'gin' }), 10);
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Searching Permittees
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
// List permittees
|
|
126
|
+
const { data: permittees } = await client.permittees.list({
|
|
127
|
+
q: 'distillery',
|
|
128
|
+
state: 'KY',
|
|
129
|
+
isActive: true,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Get a specific permittee
|
|
133
|
+
const permittee = await client.permittees.get('KY-12345');
|
|
134
|
+
console.log(permittee.company_name);
|
|
135
|
+
console.log(`${permittee.colas} total COLAs`);
|
|
136
|
+
|
|
137
|
+
// Access recent COLAs
|
|
138
|
+
for (const cola of permittee.recent_colas) {
|
|
139
|
+
console.log(cola.brand_name);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Iterate through permittees
|
|
143
|
+
for await (const p of client.permittees.iterate({ state: 'CA' })) {
|
|
144
|
+
console.log(p.company_name);
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Barcode Lookup
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
const result = await client.barcodes.lookup('012345678905');
|
|
152
|
+
|
|
153
|
+
console.log(`Barcode: ${result.barcode_value}`);
|
|
154
|
+
console.log(`Type: ${result.barcode_type}`);
|
|
155
|
+
console.log(`Found ${result.total_colas} COLAs`);
|
|
156
|
+
|
|
157
|
+
for (const cola of result.colas) {
|
|
158
|
+
console.log(cola.brand_name);
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Usage Statistics
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
const usage = await client.usage.get();
|
|
166
|
+
|
|
167
|
+
console.log(`Tier: ${usage.tier}`);
|
|
168
|
+
console.log(`Used: ${usage.requests_used} / ${usage.monthly_limit}`);
|
|
169
|
+
console.log(`Remaining: ${usage.requests_remaining}`);
|
|
170
|
+
console.log(`Rate limit: ${usage.per_minute_limit} req/min`);
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Rate Limit Information
|
|
174
|
+
|
|
175
|
+
Access rate limit headers from any request:
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
const { data, rateLimit } = await client.colas.listWithRateLimit({ q: 'wine' });
|
|
179
|
+
|
|
180
|
+
if (rateLimit) {
|
|
181
|
+
console.log(`Limit: ${rateLimit.limit}`);
|
|
182
|
+
console.log(`Remaining: ${rateLimit.remaining}`);
|
|
183
|
+
console.log(`Resets at: ${new Date(rateLimit.reset * 1000)}`);
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Error Handling
|
|
188
|
+
|
|
189
|
+
The SDK provides specific error classes for different error types:
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
import {
|
|
193
|
+
ColaCloud,
|
|
194
|
+
ColaCloudError,
|
|
195
|
+
AuthenticationError,
|
|
196
|
+
RateLimitError,
|
|
197
|
+
NotFoundError,
|
|
198
|
+
ValidationError,
|
|
199
|
+
ServerError,
|
|
200
|
+
TimeoutError,
|
|
201
|
+
NetworkError,
|
|
202
|
+
} from 'colacloud';
|
|
203
|
+
|
|
204
|
+
const client = new ColaCloud({ apiKey: 'your-api-key' });
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
const cola = await client.colas.get('invalid-id');
|
|
208
|
+
} catch (error) {
|
|
209
|
+
if (error instanceof NotFoundError) {
|
|
210
|
+
console.log(`${error.resourceType} not found: ${error.resourceId}`);
|
|
211
|
+
} else if (error instanceof AuthenticationError) {
|
|
212
|
+
console.log('Invalid API key');
|
|
213
|
+
} else if (error instanceof RateLimitError) {
|
|
214
|
+
console.log(`Rate limited. Retry after ${error.retryAfter} seconds`);
|
|
215
|
+
console.log(`Remaining: ${error.rateLimit?.remaining}`);
|
|
216
|
+
} else if (error instanceof ValidationError) {
|
|
217
|
+
console.log('Invalid request:', error.message);
|
|
218
|
+
console.log('Field errors:', error.fieldErrors);
|
|
219
|
+
} else if (error instanceof ServerError) {
|
|
220
|
+
console.log(`Server error (${error.statusCode}): ${error.message}`);
|
|
221
|
+
} else if (error instanceof TimeoutError) {
|
|
222
|
+
console.log(`Request timed out after ${error.timeoutMs}ms`);
|
|
223
|
+
} else if (error instanceof NetworkError) {
|
|
224
|
+
console.log('Network error:', error.message);
|
|
225
|
+
} else if (error instanceof ColaCloudError) {
|
|
226
|
+
// Generic API error
|
|
227
|
+
console.log(`Error (${error.statusCode}): ${error.message}`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## TypeScript Types
|
|
233
|
+
|
|
234
|
+
All types are exported for use in your TypeScript code:
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
import type {
|
|
238
|
+
// Configuration
|
|
239
|
+
ColaCloudConfig,
|
|
240
|
+
|
|
241
|
+
// Pagination
|
|
242
|
+
Pagination,
|
|
243
|
+
PaginatedResponse,
|
|
244
|
+
SingleResponse,
|
|
245
|
+
|
|
246
|
+
// Rate limiting
|
|
247
|
+
RateLimitInfo,
|
|
248
|
+
|
|
249
|
+
// COLA types
|
|
250
|
+
ColaSummary,
|
|
251
|
+
ColaDetail,
|
|
252
|
+
ColaImage,
|
|
253
|
+
ColaBarcode,
|
|
254
|
+
ColaListParams,
|
|
255
|
+
|
|
256
|
+
// Permittee types
|
|
257
|
+
PermitteeSummary,
|
|
258
|
+
PermitteeDetail,
|
|
259
|
+
PermitteeListParams,
|
|
260
|
+
|
|
261
|
+
// Barcode types
|
|
262
|
+
BarcodeLookupResult,
|
|
263
|
+
|
|
264
|
+
// Usage types
|
|
265
|
+
UsageStats,
|
|
266
|
+
} from 'colacloud';
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## API Reference
|
|
270
|
+
|
|
271
|
+
### ColaCloud
|
|
272
|
+
|
|
273
|
+
The main client class.
|
|
274
|
+
|
|
275
|
+
#### Constructor
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
new ColaCloud(config: ColaCloudConfig)
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
- `config.apiKey` (required): Your COLA Cloud API key
|
|
282
|
+
- `config.baseUrl` (optional): Base URL for the API
|
|
283
|
+
- `config.timeout` (optional): Request timeout in milliseconds
|
|
284
|
+
|
|
285
|
+
#### Resources
|
|
286
|
+
|
|
287
|
+
- `client.colas` - COLA endpoints
|
|
288
|
+
- `client.permittees` - Permittee endpoints
|
|
289
|
+
- `client.barcodes` - Barcode lookup endpoint
|
|
290
|
+
- `client.usage` - Usage statistics endpoint
|
|
291
|
+
|
|
292
|
+
### Colas Resource
|
|
293
|
+
|
|
294
|
+
- `list(params?)` - List/search COLAs
|
|
295
|
+
- `listWithRateLimit(params?)` - List with rate limit info
|
|
296
|
+
- `get(ttbId)` - Get a single COLA by TTB ID
|
|
297
|
+
- `getWithRateLimit(ttbId)` - Get with rate limit info
|
|
298
|
+
- `iterate(params?)` - Async iterator for all results
|
|
299
|
+
|
|
300
|
+
### Permittees Resource
|
|
301
|
+
|
|
302
|
+
- `list(params?)` - List/search permittees
|
|
303
|
+
- `listWithRateLimit(params?)` - List with rate limit info
|
|
304
|
+
- `get(permitNumber)` - Get a single permittee
|
|
305
|
+
- `getWithRateLimit(permitNumber)` - Get with rate limit info
|
|
306
|
+
- `iterate(params?)` - Async iterator for all results
|
|
307
|
+
|
|
308
|
+
### Barcodes Resource
|
|
309
|
+
|
|
310
|
+
- `lookup(barcodeValue)` - Look up COLAs by barcode
|
|
311
|
+
- `lookupWithRateLimit(barcodeValue)` - Lookup with rate limit info
|
|
312
|
+
|
|
313
|
+
### Usage Resource
|
|
314
|
+
|
|
315
|
+
- `get()` - Get API usage statistics
|
|
316
|
+
- `getWithRateLimit()` - Get with rate limit info
|
|
317
|
+
|
|
318
|
+
## Browser Usage
|
|
319
|
+
|
|
320
|
+
The SDK works in modern browsers that support the Fetch API:
|
|
321
|
+
|
|
322
|
+
```html
|
|
323
|
+
<script type="module">
|
|
324
|
+
import { ColaCloud } from 'https://unpkg.com/colacloud/dist/index.mjs';
|
|
325
|
+
|
|
326
|
+
const client = new ColaCloud({ apiKey: 'your-api-key' });
|
|
327
|
+
const results = await client.colas.list({ q: 'wine' });
|
|
328
|
+
console.log(results);
|
|
329
|
+
</script>
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
## License
|
|
333
|
+
|
|
334
|
+
MIT
|
|
335
|
+
|
|
336
|
+
## Support
|
|
337
|
+
|
|
338
|
+
- Documentation: https://colacloud.us/docs/api
|
|
339
|
+
- Issues: https://github.com/cola-cloud-us/colacloud-js/issues
|
|
340
|
+
- Email: support@colacloud.us
|