@trustrails/sdk 0.1.1 → 0.2.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/README.md +16 -17
- package/dist/index.d.mts +13 -9
- package/dist/index.d.ts +13 -9
- package/dist/index.js +14 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +11 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,16 +11,13 @@ npm install @trustrails/sdk
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
|
-
import
|
|
14
|
+
import TrustRails from '@trustrails/sdk';
|
|
15
15
|
|
|
16
|
-
// Initialize the
|
|
17
|
-
const
|
|
18
|
-
baseUrl: 'https://www.trustrails.app',
|
|
19
|
-
apiKey: 'your-api-key'
|
|
20
|
-
});
|
|
16
|
+
// Initialize the SDK (baseUrl defaults to production)
|
|
17
|
+
const trustrails = new TrustRails('your-api-key');
|
|
21
18
|
|
|
22
19
|
// Search for products
|
|
23
|
-
const results = await
|
|
20
|
+
const results = await trustrails.search({
|
|
24
21
|
query: 'USB-C charger',
|
|
25
22
|
maxPrice: 50
|
|
26
23
|
});
|
|
@@ -31,29 +28,29 @@ results.products.forEach(product => {
|
|
|
31
28
|
});
|
|
32
29
|
|
|
33
30
|
// Get a specific product
|
|
34
|
-
const product = await
|
|
31
|
+
const product = await trustrails.getProduct('product-id');
|
|
35
32
|
console.log(product);
|
|
36
33
|
```
|
|
37
34
|
|
|
38
35
|
## API Reference
|
|
39
36
|
|
|
40
|
-
### `
|
|
37
|
+
### `TrustRails`
|
|
41
38
|
|
|
42
39
|
#### Constructor
|
|
43
40
|
|
|
44
41
|
```typescript
|
|
45
|
-
new
|
|
42
|
+
new TrustRails(apiKey: string)
|
|
46
43
|
```
|
|
47
44
|
|
|
48
|
-
Or use a config object:
|
|
45
|
+
Or use a config object for custom baseUrl:
|
|
49
46
|
|
|
50
47
|
```typescript
|
|
51
|
-
new
|
|
48
|
+
new TrustRails({ apiKey: string, baseUrl?: string })
|
|
52
49
|
```
|
|
53
50
|
|
|
54
51
|
**Parameters:**
|
|
55
|
-
- `baseUrl` - The base URL of the TrustRails API
|
|
56
52
|
- `apiKey` - Your TrustRails API key
|
|
53
|
+
- `baseUrl` (optional) - Custom base URL (defaults to `https://www.trustrails.app`)
|
|
57
54
|
|
|
58
55
|
#### Methods
|
|
59
56
|
|
|
@@ -69,7 +66,7 @@ Search for products.
|
|
|
69
66
|
|
|
70
67
|
**Example:**
|
|
71
68
|
```typescript
|
|
72
|
-
const results = await
|
|
69
|
+
const results = await trustrails.search({
|
|
73
70
|
query: 'laptop',
|
|
74
71
|
maxPrice: 1000
|
|
75
72
|
});
|
|
@@ -86,7 +83,7 @@ Get a specific product by ID.
|
|
|
86
83
|
|
|
87
84
|
**Example:**
|
|
88
85
|
```typescript
|
|
89
|
-
const product = await
|
|
86
|
+
const product = await trustrails.getProduct('prod_123');
|
|
90
87
|
```
|
|
91
88
|
|
|
92
89
|
## Types
|
|
@@ -138,10 +135,12 @@ interface SearchResponse {
|
|
|
138
135
|
The SDK throws `TrustRailsError` for API-related errors:
|
|
139
136
|
|
|
140
137
|
```typescript
|
|
141
|
-
import {
|
|
138
|
+
import TrustRails, { TrustRailsError } from '@trustrails/sdk';
|
|
139
|
+
|
|
140
|
+
const trustrails = new TrustRails('your-api-key');
|
|
142
141
|
|
|
143
142
|
try {
|
|
144
|
-
const product = await
|
|
143
|
+
const product = await trustrails.getProduct('invalid-id');
|
|
145
144
|
} catch (error) {
|
|
146
145
|
if (error instanceof TrustRailsError) {
|
|
147
146
|
console.error(`API Error: ${error.message}`);
|
package/dist/index.d.mts
CHANGED
|
@@ -42,29 +42,33 @@ interface SearchResponse {
|
|
|
42
42
|
* Configuration options for TrustRails client
|
|
43
43
|
*/
|
|
44
44
|
interface TrustRailsConfig {
|
|
45
|
-
baseUrl: string;
|
|
46
45
|
apiKey: string;
|
|
46
|
+
baseUrl?: string;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
|
-
* TrustRails
|
|
50
|
+
* TrustRails SDK
|
|
51
51
|
*
|
|
52
52
|
* @example
|
|
53
53
|
* ```typescript
|
|
54
|
-
* const
|
|
55
|
-
* const results = await
|
|
54
|
+
* const trustrails = new TrustRails('your-api-key');
|
|
55
|
+
* const results = await trustrails.search({ query: 'laptop', maxPrice: 1000 });
|
|
56
56
|
* ```
|
|
57
57
|
*/
|
|
58
|
-
declare class
|
|
58
|
+
declare class TrustRails {
|
|
59
59
|
private readonly baseUrl;
|
|
60
60
|
private readonly apiKey;
|
|
61
61
|
/**
|
|
62
|
-
* Creates a new TrustRails
|
|
62
|
+
* Creates a new TrustRails SDK instance
|
|
63
63
|
*
|
|
64
|
-
* @param baseUrl - The base URL of the TrustRails API
|
|
65
64
|
* @param apiKey - Your TrustRails API key
|
|
66
65
|
*/
|
|
67
|
-
constructor(
|
|
66
|
+
constructor(apiKey: string);
|
|
67
|
+
/**
|
|
68
|
+
* Creates a new TrustRails SDK instance with custom configuration
|
|
69
|
+
*
|
|
70
|
+
* @param config - Configuration object with apiKey and optional baseUrl
|
|
71
|
+
*/
|
|
68
72
|
constructor(config: TrustRailsConfig);
|
|
69
73
|
/**
|
|
70
74
|
* Search for products
|
|
@@ -104,4 +108,4 @@ declare class TrustRailsError extends Error {
|
|
|
104
108
|
constructor(message: string, statusCode?: number | undefined, response?: any | undefined);
|
|
105
109
|
}
|
|
106
110
|
|
|
107
|
-
export { type Product, type SearchOptions, type SearchResponse,
|
|
111
|
+
export { type Product, type SearchOptions, type SearchResponse, TrustRails, type TrustRailsConfig, TrustRailsError, TrustRails as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -42,29 +42,33 @@ interface SearchResponse {
|
|
|
42
42
|
* Configuration options for TrustRails client
|
|
43
43
|
*/
|
|
44
44
|
interface TrustRailsConfig {
|
|
45
|
-
baseUrl: string;
|
|
46
45
|
apiKey: string;
|
|
46
|
+
baseUrl?: string;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
|
-
* TrustRails
|
|
50
|
+
* TrustRails SDK
|
|
51
51
|
*
|
|
52
52
|
* @example
|
|
53
53
|
* ```typescript
|
|
54
|
-
* const
|
|
55
|
-
* const results = await
|
|
54
|
+
* const trustrails = new TrustRails('your-api-key');
|
|
55
|
+
* const results = await trustrails.search({ query: 'laptop', maxPrice: 1000 });
|
|
56
56
|
* ```
|
|
57
57
|
*/
|
|
58
|
-
declare class
|
|
58
|
+
declare class TrustRails {
|
|
59
59
|
private readonly baseUrl;
|
|
60
60
|
private readonly apiKey;
|
|
61
61
|
/**
|
|
62
|
-
* Creates a new TrustRails
|
|
62
|
+
* Creates a new TrustRails SDK instance
|
|
63
63
|
*
|
|
64
|
-
* @param baseUrl - The base URL of the TrustRails API
|
|
65
64
|
* @param apiKey - Your TrustRails API key
|
|
66
65
|
*/
|
|
67
|
-
constructor(
|
|
66
|
+
constructor(apiKey: string);
|
|
67
|
+
/**
|
|
68
|
+
* Creates a new TrustRails SDK instance with custom configuration
|
|
69
|
+
*
|
|
70
|
+
* @param config - Configuration object with apiKey and optional baseUrl
|
|
71
|
+
*/
|
|
68
72
|
constructor(config: TrustRailsConfig);
|
|
69
73
|
/**
|
|
70
74
|
* Search for products
|
|
@@ -104,4 +108,4 @@ declare class TrustRailsError extends Error {
|
|
|
104
108
|
constructor(message: string, statusCode?: number | undefined, response?: any | undefined);
|
|
105
109
|
}
|
|
106
110
|
|
|
107
|
-
export { type Product, type SearchOptions, type SearchResponse,
|
|
111
|
+
export { type Product, type SearchOptions, type SearchResponse, TrustRails, type TrustRailsConfig, TrustRailsError, TrustRails as default };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
3
5
|
// src/errors.ts
|
|
4
6
|
var TrustRailsError = class _TrustRailsError extends Error {
|
|
5
7
|
constructor(message, statusCode, response) {
|
|
@@ -14,17 +16,18 @@ var TrustRailsError = class _TrustRailsError extends Error {
|
|
|
14
16
|
};
|
|
15
17
|
|
|
16
18
|
// src/client.ts
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
this.apiKey =
|
|
19
|
+
var DEFAULT_BASE_URL = "https://www.trustrails.app";
|
|
20
|
+
var TrustRails = class {
|
|
21
|
+
constructor(apiKeyOrConfig) {
|
|
22
|
+
if (typeof apiKeyOrConfig === "string") {
|
|
23
|
+
this.apiKey = apiKeyOrConfig;
|
|
24
|
+
this.baseUrl = DEFAULT_BASE_URL;
|
|
22
25
|
} else {
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
26
|
+
this.apiKey = apiKeyOrConfig.apiKey;
|
|
27
|
+
this.baseUrl = apiKeyOrConfig.baseUrl || DEFAULT_BASE_URL;
|
|
25
28
|
}
|
|
26
|
-
if (
|
|
27
|
-
|
|
29
|
+
if (this.baseUrl.endsWith("/")) {
|
|
30
|
+
this.baseUrl = this.baseUrl.slice(0, -1);
|
|
28
31
|
}
|
|
29
32
|
if (!this.apiKey) {
|
|
30
33
|
throw new Error("apiKey is required");
|
|
@@ -125,7 +128,8 @@ var TrustRailsClient = class {
|
|
|
125
128
|
}
|
|
126
129
|
};
|
|
127
130
|
|
|
128
|
-
exports.
|
|
131
|
+
exports.TrustRails = TrustRails;
|
|
129
132
|
exports.TrustRailsError = TrustRailsError;
|
|
133
|
+
exports.default = TrustRails;
|
|
130
134
|
//# sourceMappingURL=index.js.map
|
|
131
135
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/errors.ts","../src/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts","../src/client.ts"],"names":[],"mappings":";;;;;AAGO,IAAM,eAAA,GAA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A8B;AAC7C,IAAA,IAAI,CAAC,EAAA,EAAI;AACP,MAAA,MAAM,IAAI,MAAM,wBAAwB,CAAA;AAAA,IAC1C;AAEA,IAAA,MAAM,MAAM,CAAA,EAAG,IAAA,CAAK,OAAO,CAAA,cAAA,EAAiB,kBAAA,CAAmB,EAAE,CAAC,CAAA,CAAA;AAElE,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.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://www.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\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 * maxPrice: 50\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.maxPrice) {\n url.searchParams.set('max_price', options.maxPrice.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 a specific product by ID\n *\n * @param id - The product ID\n * @returns Promise resolving to product details\n *\n * @example\n * ```typescript\n * const product = await client.getProduct('prod_123');\n * ```\n */\n async getProduct(id: string): Promise<Product> {\n if (!id) {\n throw new Error('Product ID is required');\n }\n\n const url = `${this.baseUrl}/api/products/${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,17 +12,18 @@ var TrustRailsError = class _TrustRailsError extends Error {
|
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
// src/client.ts
|
|
15
|
-
var
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this.apiKey =
|
|
15
|
+
var DEFAULT_BASE_URL = "https://www.trustrails.app";
|
|
16
|
+
var TrustRails = class {
|
|
17
|
+
constructor(apiKeyOrConfig) {
|
|
18
|
+
if (typeof apiKeyOrConfig === "string") {
|
|
19
|
+
this.apiKey = apiKeyOrConfig;
|
|
20
|
+
this.baseUrl = DEFAULT_BASE_URL;
|
|
20
21
|
} else {
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
22
|
+
this.apiKey = apiKeyOrConfig.apiKey;
|
|
23
|
+
this.baseUrl = apiKeyOrConfig.baseUrl || DEFAULT_BASE_URL;
|
|
23
24
|
}
|
|
24
|
-
if (
|
|
25
|
-
|
|
25
|
+
if (this.baseUrl.endsWith("/")) {
|
|
26
|
+
this.baseUrl = this.baseUrl.slice(0, -1);
|
|
26
27
|
}
|
|
27
28
|
if (!this.apiKey) {
|
|
28
29
|
throw new Error("apiKey is required");
|
|
@@ -123,6 +124,6 @@ var TrustRailsClient = class {
|
|
|
123
124
|
}
|
|
124
125
|
};
|
|
125
126
|
|
|
126
|
-
export {
|
|
127
|
+
export { TrustRails, TrustRailsError, TrustRails as default };
|
|
127
128
|
//# sourceMappingURL=index.mjs.map
|
|
128
129
|
//# sourceMappingURL=index.mjs.map
|
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;AACA,IAAA,IAAA,CAAA,QAAA,GAAA,QAAA;AAGP,IAAA,IAAA,CAAK,IAAA,GAAO,iBAAA;AAGZ,IAAA,IAAI,MAAM,iBAAA,EAAmB;AAC3B,MAAA,KAAA,CAAM,iBAAA,CAAkB,MAAM,gBAAe,CAAA;AAAA,IAC/C;AAAA,EACF;AACF;;;
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts","../src/client.ts"],"names":[],"mappings":";AAGO,IAAM,eAAA,GAAN,MAAM,gBAAA,SAAwB,KAAA,CAAM;AAAA,EAC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ACP,MAAA,MAAM,IAAI,MAAM,wBAAwB,CAAA;AAAA,IAC1C;AAEA,IAAA,MAAM,MAAM,CAAA,EAAG,IAAA,CAAK,OAAO,CAAA,cAAA,EAAiB,kBAAA,CAAmB,EAAE,CAAC,CAAA,CAAA;AAElE,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://www.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\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 * maxPrice: 50\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.maxPrice) {\n url.searchParams.set('max_price', options.maxPrice.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 a specific product by ID\n *\n * @param id - The product ID\n * @returns Promise resolving to product details\n *\n * @example\n * ```typescript\n * const product = await client.getProduct('prod_123');\n * ```\n */\n async getProduct(id: string): Promise<Product> {\n if (!id) {\n throw new Error('Product ID is required');\n }\n\n const url = `${this.baseUrl}/api/products/${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"]}
|