@trustrails/sdk 0.4.1 → 0.4.3
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/README.md +16 -3
- package/dist/index.d.mts +15 -7
- package/dist/index.d.ts +15 -7
- package/dist/index.js +13 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +13 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,7 +59,7 @@ new TrustRails({ apiKey: string, baseUrl?: string })
|
|
|
59
59
|
|
|
60
60
|
##### `search(options?: SearchOptions): Promise<SearchResponse>`
|
|
61
61
|
|
|
62
|
-
Search for products.
|
|
62
|
+
Search for products. Returns summary data (title, price, availability, category). For full technical specs, call `product(id)` with the product ID.
|
|
63
63
|
|
|
64
64
|
**Parameters:**
|
|
65
65
|
- `options.query?: string` - Search query string (searches title, brand, category)
|
|
@@ -67,6 +67,7 @@ Search for products.
|
|
|
67
67
|
- `options.category?: string` - Filter by product category (e.g., 'Laptops', 'Headphones')
|
|
68
68
|
- `options.minPrice?: number` - Minimum price filter in GBP
|
|
69
69
|
- `options.maxPrice?: number` - Maximum price filter in GBP
|
|
70
|
+
- `options.lite?: boolean` - Return trimmed product objects with only essential fields (reduces payload by ~80%)
|
|
70
71
|
- `options.limit?: number` - Maximum number of results (default: 20, max: 100)
|
|
71
72
|
|
|
72
73
|
**Returns:** Promise resolving to `SearchResponse` containing `products` array and `total` count.
|
|
@@ -83,14 +84,23 @@ const results = await trustrails.search({
|
|
|
83
84
|
});
|
|
84
85
|
```
|
|
85
86
|
|
|
87
|
+
**Lite mode (faster responses, smaller payloads):**
|
|
88
|
+
```typescript
|
|
89
|
+
const results = await trustrails.search({
|
|
90
|
+
query: 'wireless mouse',
|
|
91
|
+
maxPrice: 50,
|
|
92
|
+
lite: true // Returns only: id, title, brand, price, availability, image_url, purchase_url
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
86
96
|
##### `product(id: string): Promise<Product>`
|
|
87
97
|
|
|
88
|
-
Get a
|
|
98
|
+
Get full details for a single product. Returns complete technical specifications, full description, stock level, delivery time, and retailer source. Use this after `search()` to get detailed specs for comparison or recommendations.
|
|
89
99
|
|
|
90
100
|
**Parameters:**
|
|
91
101
|
- `id: string` - The product ID
|
|
92
102
|
|
|
93
|
-
**Returns:** Promise resolving to `Product` object
|
|
103
|
+
**Returns:** Promise resolving to `Product` object with all fields populated
|
|
94
104
|
|
|
95
105
|
**Example:**
|
|
96
106
|
```typescript
|
|
@@ -112,6 +122,8 @@ interface Product {
|
|
|
112
122
|
stock: number;
|
|
113
123
|
delivery_time: string;
|
|
114
124
|
image_url?: string;
|
|
125
|
+
category: string;
|
|
126
|
+
product_type: "product" | "accessory";
|
|
115
127
|
specs: {
|
|
116
128
|
[key: string]: any;
|
|
117
129
|
};
|
|
@@ -132,6 +144,7 @@ interface SearchOptions {
|
|
|
132
144
|
category?: string;
|
|
133
145
|
minPrice?: number;
|
|
134
146
|
maxPrice?: number;
|
|
147
|
+
lite?: boolean;
|
|
135
148
|
limit?: number;
|
|
136
149
|
}
|
|
137
150
|
```
|
package/dist/index.d.mts
CHANGED
|
@@ -15,11 +15,12 @@ interface Product {
|
|
|
15
15
|
stock: number;
|
|
16
16
|
delivery_time: string;
|
|
17
17
|
image_url?: string;
|
|
18
|
+
category: string;
|
|
19
|
+
product_type: 'product' | 'accessory';
|
|
18
20
|
specs: {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
pd_version?: string;
|
|
21
|
+
raw_category?: string;
|
|
22
|
+
model_number?: string;
|
|
23
|
+
dimensions?: string;
|
|
23
24
|
[key: string]: any;
|
|
24
25
|
};
|
|
25
26
|
provenance: {
|
|
@@ -37,6 +38,10 @@ interface SearchOptions {
|
|
|
37
38
|
maxPrice?: number;
|
|
38
39
|
brand?: string;
|
|
39
40
|
category?: string;
|
|
41
|
+
/** Return trimmed product objects with only essential fields. Reduces payload size by ~80%. */
|
|
42
|
+
lite?: boolean;
|
|
43
|
+
/** Maximum number of products to return (default 20, max 100) */
|
|
44
|
+
limit?: number;
|
|
40
45
|
}
|
|
41
46
|
/**
|
|
42
47
|
* Response from product search endpoint
|
|
@@ -78,7 +83,8 @@ declare class TrustRails {
|
|
|
78
83
|
*/
|
|
79
84
|
constructor(config: TrustRailsConfig);
|
|
80
85
|
/**
|
|
81
|
-
* Search for products
|
|
86
|
+
* Search for products. Returns summary data (title, price, availability, category).
|
|
87
|
+
* For full technical specs, call product(id) with the product ID.
|
|
82
88
|
*
|
|
83
89
|
* @param options - Search parameters
|
|
84
90
|
* @returns Promise resolving to search results
|
|
@@ -96,10 +102,12 @@ declare class TrustRails {
|
|
|
96
102
|
*/
|
|
97
103
|
search(options?: SearchOptions): Promise<SearchResponse>;
|
|
98
104
|
/**
|
|
99
|
-
* Get a
|
|
105
|
+
* Get full details for a single product. Returns complete specs, description,
|
|
106
|
+
* stock level, delivery time, and retailer source. Use after search() for
|
|
107
|
+
* detailed comparison or recommendations.
|
|
100
108
|
*
|
|
101
109
|
* @param id - The product ID
|
|
102
|
-
* @returns Promise resolving to product details
|
|
110
|
+
* @returns Promise resolving to complete product details
|
|
103
111
|
*
|
|
104
112
|
* @example
|
|
105
113
|
* ```typescript
|
package/dist/index.d.ts
CHANGED
|
@@ -15,11 +15,12 @@ interface Product {
|
|
|
15
15
|
stock: number;
|
|
16
16
|
delivery_time: string;
|
|
17
17
|
image_url?: string;
|
|
18
|
+
category: string;
|
|
19
|
+
product_type: 'product' | 'accessory';
|
|
18
20
|
specs: {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
pd_version?: string;
|
|
21
|
+
raw_category?: string;
|
|
22
|
+
model_number?: string;
|
|
23
|
+
dimensions?: string;
|
|
23
24
|
[key: string]: any;
|
|
24
25
|
};
|
|
25
26
|
provenance: {
|
|
@@ -37,6 +38,10 @@ interface SearchOptions {
|
|
|
37
38
|
maxPrice?: number;
|
|
38
39
|
brand?: string;
|
|
39
40
|
category?: string;
|
|
41
|
+
/** Return trimmed product objects with only essential fields. Reduces payload size by ~80%. */
|
|
42
|
+
lite?: boolean;
|
|
43
|
+
/** Maximum number of products to return (default 20, max 100) */
|
|
44
|
+
limit?: number;
|
|
40
45
|
}
|
|
41
46
|
/**
|
|
42
47
|
* Response from product search endpoint
|
|
@@ -78,7 +83,8 @@ declare class TrustRails {
|
|
|
78
83
|
*/
|
|
79
84
|
constructor(config: TrustRailsConfig);
|
|
80
85
|
/**
|
|
81
|
-
* Search for products
|
|
86
|
+
* Search for products. Returns summary data (title, price, availability, category).
|
|
87
|
+
* For full technical specs, call product(id) with the product ID.
|
|
82
88
|
*
|
|
83
89
|
* @param options - Search parameters
|
|
84
90
|
* @returns Promise resolving to search results
|
|
@@ -96,10 +102,12 @@ declare class TrustRails {
|
|
|
96
102
|
*/
|
|
97
103
|
search(options?: SearchOptions): Promise<SearchResponse>;
|
|
98
104
|
/**
|
|
99
|
-
* Get a
|
|
105
|
+
* Get full details for a single product. Returns complete specs, description,
|
|
106
|
+
* stock level, delivery time, and retailer source. Use after search() for
|
|
107
|
+
* detailed comparison or recommendations.
|
|
100
108
|
*
|
|
101
109
|
* @param id - The product ID
|
|
102
|
-
* @returns Promise resolving to product details
|
|
110
|
+
* @returns Promise resolving to complete product details
|
|
103
111
|
*
|
|
104
112
|
* @example
|
|
105
113
|
* ```typescript
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,7 @@ var TrustRailsError = class _TrustRailsError extends Error {
|
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
// src/client.ts
|
|
19
|
-
var DEFAULT_BASE_URL = "https://
|
|
19
|
+
var DEFAULT_BASE_URL = "https://trustrails.app";
|
|
20
20
|
var TrustRails = class {
|
|
21
21
|
constructor(apiKeyOrConfig) {
|
|
22
22
|
if (typeof apiKeyOrConfig === "string") {
|
|
@@ -34,7 +34,8 @@ var TrustRails = class {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
|
-
* Search for products
|
|
37
|
+
* Search for products. Returns summary data (title, price, availability, category).
|
|
38
|
+
* For full technical specs, call product(id) with the product ID.
|
|
38
39
|
*
|
|
39
40
|
* @param options - Search parameters
|
|
40
41
|
* @returns Promise resolving to search results
|
|
@@ -67,6 +68,12 @@ var TrustRails = class {
|
|
|
67
68
|
if (options.category) {
|
|
68
69
|
url.searchParams.set("category", options.category);
|
|
69
70
|
}
|
|
71
|
+
if (options.lite) {
|
|
72
|
+
url.searchParams.set("lite", "true");
|
|
73
|
+
}
|
|
74
|
+
if (options.limit) {
|
|
75
|
+
url.searchParams.set("limit", options.limit.toString());
|
|
76
|
+
}
|
|
70
77
|
try {
|
|
71
78
|
const response = await fetch(url.toString(), {
|
|
72
79
|
method: "GET",
|
|
@@ -94,10 +101,12 @@ var TrustRails = class {
|
|
|
94
101
|
}
|
|
95
102
|
}
|
|
96
103
|
/**
|
|
97
|
-
* Get a
|
|
104
|
+
* Get full details for a single product. Returns complete specs, description,
|
|
105
|
+
* stock level, delivery time, and retailer source. Use after search() for
|
|
106
|
+
* detailed comparison or recommendations.
|
|
98
107
|
*
|
|
99
108
|
* @param id - The product ID
|
|
100
|
-
* @returns Promise resolving to product details
|
|
109
|
+
* @returns Promise resolving to complete product details
|
|
101
110
|
*
|
|
102
111
|
* @example
|
|
103
112
|
* ```typescript
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/errors.ts","../src/client.ts"],"names":[],"mappings":";;;;;AAGO,IAAM,eAAA,GAAN,MAAM,gBAAA,SAAwB,KAAA,CAAM;AAAA,EACzC,WAAA,CACE,OAAA,EACO,UAAA,EACA,QAAA,EACP;AACA,IAAA,KAAA,CAAM,OAAO,CAAA;AAHN,IAAA,IAAA,CAAA,UAAA,GAAA,UAAA;AACA,IAAA,IAAA,CAAA,QAAA,GAAA,QAAA;AAGP,IAAA,IAAA,CAAK,IAAA,GAAO,iBAA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CAAM,GAAA,EAAK;AAAA,QAChC,MAAA,EAAQ,KAAA;AAAA,QACR,OAAA,EAAS;AAAA,UACP,eAAA,EAAiB,CAAA,OAAA,EAAU,IAAA,CAAK,MAAM,CAAA,CAAA;AAAA,UACtC,cAAA,EAAgB;AAAA;AAClB,OACD,CAAA;AAED,MAAA,IAAI,CAAC,SAAS,EAAA,EAAI;AAChB,QAAA,IAAI,QAAA,CAAS,WAAW,GAAA,EAAK;AAC3B,UAAA,MAAM,IAAI,eAAA,CAAgB,CAAA,mBAAA,EAAsB,EAAE,IAAI,GAAG,CAAA;AAAA,QAC3D;AACA,QAAA,MAAM,IAAI,eAAA;AAAA,UACR,CAAA,oBAAA,EAAuB,SAAS,UAAU,CAAA,CAAA;AAAA,UAC1C,QAAA,CAAS,MAAA;AAAA,UACT,MAAM,SAAS,IAAA;AAAK,SACtB;AAAA,MACF;AAEA,MAAA,MAAM,OAAA,GAAU,MAAM,QAAA,CAAS,IAAA,EAAK;AACpC,MAAA,OAAO,OAAA;AAAA,IACT,SAAS,KAAA,EAAO;AACd,MAAA,IAAI,iBAAiB,eAAA,EAAiB;AACpC,QAAA,MAAM,KAAA;AAAA,MACR;AACA,MAAA,MAAM,IAAI,eAAA;AAAA,QACR,CAAA,eAAA,EAAkB,KAAA,YAAiB,KAAA,GAAQ,KAAA,CAAM,UAAU,eAAe,CAAA;AAAA,OAC5E;AAAA,IACF;AAAA,EACF;AACF","file":"index.js","sourcesContent":["/**\n * Custom error class for TrustRails API errors\n */\nexport class TrustRailsError extends Error {\n constructor(\n message: string,\n public statusCode?: number,\n public response?: any\n ) {\n super(message);\n this.name = 'TrustRailsError';\n\n // Maintains proper stack trace for where our error was thrown (only available on V8)\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, TrustRailsError);\n }\n }\n}\n","import { TrustRailsError } from './errors';\nimport type { Product, SearchOptions, SearchResponse, TrustRailsConfig } from './types';\n\nconst DEFAULT_BASE_URL = 'https://trustrails.app';\n\n/**\n * TrustRails SDK\n *\n * @example\n * ```typescript\n * const trustrails = new TrustRails('your-api-key');\n * const results = await trustrails.search({ query: 'laptop', maxPrice: 1000 });\n * ```\n */\nexport class TrustRails {\n private readonly baseUrl: string;\n private readonly apiKey: string;\n\n /**\n * Creates a new TrustRails SDK instance\n *\n * @param apiKey - Your TrustRails API key\n */\n constructor(apiKey: string);\n /**\n * Creates a new TrustRails SDK instance with custom configuration\n *\n * @param config - Configuration object with apiKey and optional baseUrl\n */\n constructor(config: TrustRailsConfig);\n constructor(apiKeyOrConfig: string | TrustRailsConfig) {\n if (typeof apiKeyOrConfig === 'string') {\n this.apiKey = apiKeyOrConfig;\n this.baseUrl = DEFAULT_BASE_URL;\n } else {\n this.apiKey = apiKeyOrConfig.apiKey;\n this.baseUrl = apiKeyOrConfig.baseUrl || DEFAULT_BASE_URL;\n }\n\n // Clean up baseUrl - remove trailing slash\n if (this.baseUrl.endsWith('/')) {\n this.baseUrl = this.baseUrl.slice(0, -1);\n }\n\n if (!this.apiKey) {\n throw new Error('apiKey is required');\n }\n }\n\n /**\n * Search for products. Returns summary data (title, price, availability, category).\n * For full technical specs, call product(id) with the product ID.\n *\n * @param options - Search parameters\n * @returns Promise resolving to search results\n *\n * @example\n * ```typescript\n * const results = await client.search({\n * query: 'USB-C charger',\n * brand: 'Anker',\n * minPrice: 20,\n * maxPrice: 50,\n * category: 'Chargers'\n * });\n * ```\n */\n async search(options: SearchOptions = {}): Promise<SearchResponse> {\n const url = new URL(`${this.baseUrl}/api/search`);\n\n if (options.query) {\n url.searchParams.set('query', options.query);\n }\n\n if (options.minPrice) {\n url.searchParams.set('min_price', options.minPrice.toString());\n }\n\n if (options.maxPrice) {\n url.searchParams.set('max_price', options.maxPrice.toString());\n }\n\n if (options.brand) {\n url.searchParams.set('brand', options.brand);\n }\n\n if (options.category) {\n url.searchParams.set('category', options.category);\n }\n\n if (options.lite) {\n url.searchParams.set('lite', 'true');\n }\n\n if (options.limit) {\n url.searchParams.set('limit', options.limit.toString());\n }\n\n try {\n const response = await fetch(url.toString(), {\n method: 'GET',\n headers: {\n 'Authorization': `Bearer ${this.apiKey}`,\n 'Content-Type': 'application/json',\n },\n });\n\n if (!response.ok) {\n throw new TrustRailsError(\n `Search failed: ${response.statusText}`,\n response.status,\n await response.text()\n );\n }\n\n const data = await response.json() as SearchResponse;\n return data;\n } catch (error) {\n if (error instanceof TrustRailsError) {\n throw error;\n }\n throw new TrustRailsError(\n `Network error: ${error instanceof Error ? error.message : 'Unknown error'}`\n );\n }\n }\n\n /**\n * Get full details for a single product. Returns complete specs, description,\n * stock level, delivery time, and retailer source. Use after search() for\n * detailed comparison or recommendations.\n *\n * @param id - The product ID\n * @returns Promise resolving to complete product details\n *\n * @example\n * ```typescript\n * const product = await client.product('prod_123');\n * ```\n */\n async product(id: string): Promise<Product> {\n if (!id) {\n throw new Error('Product ID is required');\n }\n\n const url = `${this.baseUrl}/api/product/${encodeURIComponent(id)}`;\n\n try {\n const response = await fetch(url, {\n method: 'GET',\n headers: {\n 'Authorization': `Bearer ${this.apiKey}`,\n 'Content-Type': 'application/json',\n },\n });\n\n if (!response.ok) {\n if (response.status === 404) {\n throw new TrustRailsError(`Product not found: ${id}`, 404);\n }\n throw new TrustRailsError(\n `Get product failed: ${response.statusText}`,\n response.status,\n await response.text()\n );\n }\n\n const product = await response.json() as Product;\n return product;\n } catch (error) {\n if (error instanceof TrustRailsError) {\n throw error;\n }\n throw new TrustRailsError(\n `Network error: ${error instanceof Error ? error.message : 'Unknown error'}`\n );\n }\n }\n}\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -12,7 +12,7 @@ var TrustRailsError = class _TrustRailsError extends Error {
|
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
// src/client.ts
|
|
15
|
-
var DEFAULT_BASE_URL = "https://
|
|
15
|
+
var DEFAULT_BASE_URL = "https://trustrails.app";
|
|
16
16
|
var TrustRails = class {
|
|
17
17
|
constructor(apiKeyOrConfig) {
|
|
18
18
|
if (typeof apiKeyOrConfig === "string") {
|
|
@@ -30,7 +30,8 @@ var TrustRails = class {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
|
-
* Search for products
|
|
33
|
+
* Search for products. Returns summary data (title, price, availability, category).
|
|
34
|
+
* For full technical specs, call product(id) with the product ID.
|
|
34
35
|
*
|
|
35
36
|
* @param options - Search parameters
|
|
36
37
|
* @returns Promise resolving to search results
|
|
@@ -63,6 +64,12 @@ var TrustRails = class {
|
|
|
63
64
|
if (options.category) {
|
|
64
65
|
url.searchParams.set("category", options.category);
|
|
65
66
|
}
|
|
67
|
+
if (options.lite) {
|
|
68
|
+
url.searchParams.set("lite", "true");
|
|
69
|
+
}
|
|
70
|
+
if (options.limit) {
|
|
71
|
+
url.searchParams.set("limit", options.limit.toString());
|
|
72
|
+
}
|
|
66
73
|
try {
|
|
67
74
|
const response = await fetch(url.toString(), {
|
|
68
75
|
method: "GET",
|
|
@@ -90,10 +97,12 @@ var TrustRails = class {
|
|
|
90
97
|
}
|
|
91
98
|
}
|
|
92
99
|
/**
|
|
93
|
-
* Get a
|
|
100
|
+
* Get full details for a single product. Returns complete specs, description,
|
|
101
|
+
* stock level, delivery time, and retailer source. Use after search() for
|
|
102
|
+
* detailed comparison or recommendations.
|
|
94
103
|
*
|
|
95
104
|
* @param id - The product ID
|
|
96
|
-
* @returns Promise resolving to product details
|
|
105
|
+
* @returns Promise resolving to complete product details
|
|
97
106
|
*
|
|
98
107
|
* @example
|
|
99
108
|
* ```typescript
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/errors.ts","../src/client.ts"],"names":[],"mappings":";AAGO,IAAM,eAAA,GAAN,MAAM,gBAAA,SAAwB,KAAA,CAAM;AAAA,EACzC,WAAA,CACE,OAAA,EACO,UAAA,EACA,QAAA,EACP;AACA,IAAA,KAAA,CAAM,OAAO,CAAA;AAHN,IAAA,IAAA,CAAA,UAAA,GAAA,UAAA;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AAC,CAAA,CAAA;AAEjE,IAAA,IAAI;AACF,MAAA,MAAM,QAAA,GAAW,MAAM,KAAA,CAAM,GAAA,EAAK;AAAA,QAChC,MAAA,EAAQ,KAAA;AAAA,QACR,OAAA,EAAS;AAAA,UACP,eAAA,EAAiB,CAAA,OAAA,EAAU,IAAA,CAAK,MAAM,CAAA,CAAA;AAAA,UACtC,cAAA,EAAgB;AAAA;AAClB,OACD,CAAA;AAED,MAAA,IAAI,CAAC,SAAS,EAAA,EAAI;AAChB,QAAA,IAAI,QAAA,CAAS,WAAW,GAAA,EAAK;AAC3B,UAAA,MAAM,IAAI,eAAA,CAAgB,CAAA,mBAAA,EAAsB,EAAE,IAAI,GAAG,CAAA;AAAA,QAC3D;AACA,QAAA,MAAM,IAAI,eAAA;AAAA,UACR,CAAA,oBAAA,EAAuB,SAAS,UAAU,CAAA,CAAA;AAAA,UAC1C,QAAA,CAAS,MAAA;AAAA,UACT,MAAM,SAAS,IAAA;AAAK,SACtB;AAAA,MACF;AAEA,MAAA,MAAM,OAAA,GAAU,MAAM,QAAA,CAAS,IAAA,EAAK;AACpC,MAAA,OAAO,OAAA;AAAA,IACT,SAAS,KAAA,EAAO;AACd,MAAA,IAAI,iBAAiB,eAAA,EAAiB;AACpC,QAAA,MAAM,KAAA;AAAA,MACR;AACA,MAAA,MAAM,IAAI,eAAA;AAAA,QACR,CAAA,eAAA,EAAkB,KAAA,YAAiB,KAAA,GAAQ,KAAA,CAAM,UAAU,eAAe,CAAA;AAAA,OAC5E;AAAA,IACF;AAAA,EACF;AACF","file":"index.mjs","sourcesContent":["/**\n * Custom error class for TrustRails API errors\n */\nexport class TrustRailsError extends Error {\n constructor(\n message: string,\n public statusCode?: number,\n public response?: any\n ) {\n super(message);\n this.name = 'TrustRailsError';\n\n // Maintains proper stack trace for where our error was thrown (only available on V8)\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, TrustRailsError);\n }\n }\n}\n","import { TrustRailsError } from './errors';\nimport type { Product, SearchOptions, SearchResponse, TrustRailsConfig } from './types';\n\nconst DEFAULT_BASE_URL = 'https://trustrails.app';\n\n/**\n * TrustRails SDK\n *\n * @example\n * ```typescript\n * const trustrails = new TrustRails('your-api-key');\n * const results = await trustrails.search({ query: 'laptop', maxPrice: 1000 });\n * ```\n */\nexport class TrustRails {\n private readonly baseUrl: string;\n private readonly apiKey: string;\n\n /**\n * Creates a new TrustRails SDK instance\n *\n * @param apiKey - Your TrustRails API key\n */\n constructor(apiKey: string);\n /**\n * Creates a new TrustRails SDK instance with custom configuration\n *\n * @param config - Configuration object with apiKey and optional baseUrl\n */\n constructor(config: TrustRailsConfig);\n constructor(apiKeyOrConfig: string | TrustRailsConfig) {\n if (typeof apiKeyOrConfig === 'string') {\n this.apiKey = apiKeyOrConfig;\n this.baseUrl = DEFAULT_BASE_URL;\n } else {\n this.apiKey = apiKeyOrConfig.apiKey;\n this.baseUrl = apiKeyOrConfig.baseUrl || DEFAULT_BASE_URL;\n }\n\n // Clean up baseUrl - remove trailing slash\n if (this.baseUrl.endsWith('/')) {\n this.baseUrl = this.baseUrl.slice(0, -1);\n }\n\n if (!this.apiKey) {\n throw new Error('apiKey is required');\n }\n }\n\n /**\n * Search for products. Returns summary data (title, price, availability, category).\n * For full technical specs, call product(id) with the product ID.\n *\n * @param options - Search parameters\n * @returns Promise resolving to search results\n *\n * @example\n * ```typescript\n * const results = await client.search({\n * query: 'USB-C charger',\n * brand: 'Anker',\n * minPrice: 20,\n * maxPrice: 50,\n * category: 'Chargers'\n * });\n * ```\n */\n async search(options: SearchOptions = {}): Promise<SearchResponse> {\n const url = new URL(`${this.baseUrl}/api/search`);\n\n if (options.query) {\n url.searchParams.set('query', options.query);\n }\n\n if (options.minPrice) {\n url.searchParams.set('min_price', options.minPrice.toString());\n }\n\n if (options.maxPrice) {\n url.searchParams.set('max_price', options.maxPrice.toString());\n }\n\n if (options.brand) {\n url.searchParams.set('brand', options.brand);\n }\n\n if (options.category) {\n url.searchParams.set('category', options.category);\n }\n\n if (options.lite) {\n url.searchParams.set('lite', 'true');\n }\n\n if (options.limit) {\n url.searchParams.set('limit', options.limit.toString());\n }\n\n try {\n const response = await fetch(url.toString(), {\n method: 'GET',\n headers: {\n 'Authorization': `Bearer ${this.apiKey}`,\n 'Content-Type': 'application/json',\n },\n });\n\n if (!response.ok) {\n throw new TrustRailsError(\n `Search failed: ${response.statusText}`,\n response.status,\n await response.text()\n );\n }\n\n const data = await response.json() as SearchResponse;\n return data;\n } catch (error) {\n if (error instanceof TrustRailsError) {\n throw error;\n }\n throw new TrustRailsError(\n `Network error: ${error instanceof Error ? error.message : 'Unknown error'}`\n );\n }\n }\n\n /**\n * Get full details for a single product. Returns complete specs, description,\n * stock level, delivery time, and retailer source. Use after search() for\n * detailed comparison or recommendations.\n *\n * @param id - The product ID\n * @returns Promise resolving to complete product details\n *\n * @example\n * ```typescript\n * const product = await client.product('prod_123');\n * ```\n */\n async product(id: string): Promise<Product> {\n if (!id) {\n throw new Error('Product ID is required');\n }\n\n const url = `${this.baseUrl}/api/product/${encodeURIComponent(id)}`;\n\n try {\n const response = await fetch(url, {\n method: 'GET',\n headers: {\n 'Authorization': `Bearer ${this.apiKey}`,\n 'Content-Type': 'application/json',\n },\n });\n\n if (!response.ok) {\n if (response.status === 404) {\n throw new TrustRailsError(`Product not found: ${id}`, 404);\n }\n throw new TrustRailsError(\n `Get product failed: ${response.statusText}`,\n response.status,\n await response.text()\n );\n }\n\n const product = await response.json() as Product;\n return product;\n } catch (error) {\n if (error instanceof TrustRailsError) {\n throw error;\n }\n throw new TrustRailsError(\n `Network error: ${error instanceof Error ? error.message : 'Unknown error'}`\n );\n }\n }\n}\n"]}
|