@chat-ads/chatads-sdk 0.1.10 → 0.1.11
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 +13 -15
- package/dist/client.js +5 -2
- package/dist/models.d.ts +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,8 +29,8 @@ const response = await client.analyze({
|
|
|
29
29
|
ip: "8.8.8.8",
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
-
if (response.
|
|
33
|
-
console.log(response.data.
|
|
32
|
+
if (response.data?.offers?.length) {
|
|
33
|
+
console.log(response.data.offers[0]);
|
|
34
34
|
} else {
|
|
35
35
|
console.error(response.error);
|
|
36
36
|
}
|
|
@@ -45,7 +45,7 @@ const result = await client.analyzeMessage("Need scheduling ideas", {
|
|
|
45
45
|
```
|
|
46
46
|
|
|
47
47
|
- Reserved payload keys (like `message`, `country`, etc.) cannot be overwritten inside `extraFields`; the client throws if it detects a collision.
|
|
48
|
-
- Pass `raiseOnFailure: true` to throw `ChatAdsAPIError` when the API returns
|
|
48
|
+
- Pass `raiseOnFailure: true` to throw `ChatAdsAPIError` when the API returns an error with HTTP 200 responses.
|
|
49
49
|
- Retries honor `Retry-After` headers and exponential backoff (`maxRetries` + `retryBackoffFactorMs`).
|
|
50
50
|
|
|
51
51
|
## Error handling
|
|
@@ -66,7 +66,7 @@ try {
|
|
|
66
66
|
}
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
`ChatAdsAPIError` wraps non-2xx API responses (and optionally
|
|
69
|
+
`ChatAdsAPIError` wraps non-2xx API responses (and optionally error responses). `ChatAdsSDKError` covers local issues like invalid payloads, timeouts, or missing `fetch`.
|
|
70
70
|
|
|
71
71
|
## Configuration options
|
|
72
72
|
|
|
@@ -77,9 +77,9 @@ try {
|
|
|
77
77
|
| `endpoint` | Defaults to `/v1/chatads/messages`. Override if you host multiple versions. |
|
|
78
78
|
| `timeoutMs` | Request timeout (default 10s). |
|
|
79
79
|
| `maxRetries` | How many automatic retries to attempt for retryable HTTP statuses. |
|
|
80
|
-
| `retryStatuses` | Array of status codes treated as retryable (defaults to `408, 429,
|
|
80
|
+
| `retryStatuses` | Array of status codes treated as retryable (defaults to `408, 429, 500, 502, 503, 504`). |
|
|
81
81
|
| `retryBackoffFactorMs` | Base backoff delay in milliseconds (default 500). |
|
|
82
|
-
| `raiseOnFailure` | Throw when the API returns
|
|
82
|
+
| `raiseOnFailure` | Throw when the API returns an error even if HTTP 200. |
|
|
83
83
|
| `fetchImplementation` | Provide a custom `fetch` for Node < 18 or custom runtimes. |
|
|
84
84
|
| `logger` | Optional logger (any object with `debug`). Useful for redacted request logs. |
|
|
85
85
|
|
|
@@ -87,19 +87,17 @@ try {
|
|
|
87
87
|
|
|
88
88
|
```json
|
|
89
89
|
{
|
|
90
|
-
"success": true,
|
|
91
90
|
"data": {
|
|
92
|
-
"
|
|
93
|
-
"
|
|
91
|
+
"status": "filled",
|
|
92
|
+
"offers": [
|
|
94
93
|
{
|
|
95
|
-
"
|
|
96
|
-
"
|
|
97
|
-
"
|
|
98
|
-
"Category": "Software"
|
|
94
|
+
"link_text": "CRM tools",
|
|
95
|
+
"confidence_level": "high",
|
|
96
|
+
"url": "https://amazon.com/dp/example?tag=chatads-20"
|
|
99
97
|
}
|
|
100
98
|
],
|
|
101
|
-
"
|
|
102
|
-
"
|
|
99
|
+
"requested": 1,
|
|
100
|
+
"returned": 1
|
|
103
101
|
},
|
|
104
102
|
"error": null,
|
|
105
103
|
"meta": {
|
package/dist/client.js
CHANGED
|
@@ -3,7 +3,7 @@ import { RESERVED_PAYLOAD_KEYS } from "./models.js";
|
|
|
3
3
|
const DEFAULT_ENDPOINT = "/v1/chatads/messages";
|
|
4
4
|
const DEFAULT_TIMEOUT_MS = 10000;
|
|
5
5
|
const DEFAULT_BACKOFF_FACTOR = 500; // ms
|
|
6
|
-
const DEFAULT_RETRYABLE_STATUSES = new Set([408,
|
|
6
|
+
const DEFAULT_RETRYABLE_STATUSES = new Set([408, 429, 500, 502, 503, 504]);
|
|
7
7
|
/**
|
|
8
8
|
* Field aliases for normalizing optional field names.
|
|
9
9
|
* Only includes the 3 optional fields from the OpenAPI spec.
|
|
@@ -48,9 +48,9 @@ export class ChatAdsClient {
|
|
|
48
48
|
async post(body, options) {
|
|
49
49
|
const url = `${this.baseUrl}${this.endpoint}`;
|
|
50
50
|
const headers = {
|
|
51
|
+
...lowercaseHeaders(options?.headers),
|
|
51
52
|
"content-type": "application/json",
|
|
52
53
|
"x-api-key": this.apiKey,
|
|
53
|
-
...lowercaseHeaders(options?.headers),
|
|
54
54
|
};
|
|
55
55
|
let attempt = 0;
|
|
56
56
|
// eslint-disable-next-line no-constant-condition
|
|
@@ -127,6 +127,8 @@ function normalizeBaseUrl(raw) {
|
|
|
127
127
|
return url.toString().replace(/\/$/, "");
|
|
128
128
|
}
|
|
129
129
|
catch (error) {
|
|
130
|
+
if (error instanceof ChatAdsSDKError)
|
|
131
|
+
throw error;
|
|
130
132
|
throw new ChatAdsSDKError(`Invalid baseUrl: ${raw}`, error);
|
|
131
133
|
}
|
|
132
134
|
}
|
|
@@ -184,6 +186,7 @@ async function parseResponse(response) {
|
|
|
184
186
|
returned: data.returned ?? 0,
|
|
185
187
|
extraction_source: data.extraction_source,
|
|
186
188
|
extraction_debug: data.extraction_debug,
|
|
189
|
+
resolution_debug: data.resolution_debug,
|
|
187
190
|
},
|
|
188
191
|
error: raw.error,
|
|
189
192
|
meta: raw.meta ?? { request_id: "unknown" },
|
package/dist/models.d.ts
CHANGED
|
@@ -49,8 +49,6 @@ export interface Offer {
|
|
|
49
49
|
url: string;
|
|
50
50
|
/** Source of the URL resolution (e.g., amazon, serper, vector) (verbose mode only) */
|
|
51
51
|
resolution_source?: string;
|
|
52
|
-
/** Detected product category */
|
|
53
|
-
category?: string;
|
|
54
52
|
/** Product metadata from resolution */
|
|
55
53
|
product?: Product;
|
|
56
54
|
}
|
|
@@ -70,6 +68,8 @@ export interface AnalyzeData {
|
|
|
70
68
|
extraction_source?: string;
|
|
71
69
|
/** Extraction debug information (verbose mode only) */
|
|
72
70
|
extraction_debug?: unknown[];
|
|
71
|
+
/** Resolution debug information (verbose mode only) */
|
|
72
|
+
resolution_debug?: unknown[];
|
|
73
73
|
}
|
|
74
74
|
export interface ChatAdsError {
|
|
75
75
|
code: string;
|