wineater-bot 4.5.11 → 4.7.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/package.json +1 -1
- package/src/App.svelte +2 -0
- package/src/services/api/api.ts +21 -6
package/package.json
CHANGED
package/src/App.svelte
CHANGED
|
@@ -98,6 +98,7 @@
|
|
|
98
98
|
try {
|
|
99
99
|
results = await wineaterApi.getInstantRecommendations();
|
|
100
100
|
} catch (error) {
|
|
101
|
+
console.error('[wineater] instant recommendations error:', error);
|
|
101
102
|
setSearchError();
|
|
102
103
|
} finally {
|
|
103
104
|
isLoading = false;
|
|
@@ -155,6 +156,7 @@
|
|
|
155
156
|
}
|
|
156
157
|
} catch (error) {
|
|
157
158
|
results = [];
|
|
159
|
+
console.error('[wineater] search error:', error);
|
|
158
160
|
trackWidgetEvent(WIDGET_EVENTS.ERROR_OCCURRED, storeId, {
|
|
159
161
|
error_message: error?.message,
|
|
160
162
|
query
|
package/src/services/api/api.ts
CHANGED
|
@@ -36,14 +36,19 @@ export type RecommendedWine = Omit<Wine, "shortDescription" | "producer" | "pair
|
|
|
36
36
|
class WineaterApi {
|
|
37
37
|
#baseUrl = 'http://localhost:3000/';
|
|
38
38
|
#token: string;
|
|
39
|
+
#apiVersion: string;
|
|
39
40
|
|
|
40
|
-
constructor(token: string) {
|
|
41
|
+
constructor(token: string, apiVersion: string = '1') {
|
|
41
42
|
this.#token = token;
|
|
43
|
+
this.#apiVersion = apiVersion;
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
async getInstantRecommendations(): Promise<Wine[]> {
|
|
45
47
|
try {
|
|
46
|
-
const
|
|
48
|
+
const endpoint = this.#apiVersion === '2'
|
|
49
|
+
? 'dish/recommendations/instant/v2'
|
|
50
|
+
: 'dish/recommendations/instant';
|
|
51
|
+
const response = await fetch(`${this.#baseUrl}${endpoint}`, {
|
|
47
52
|
method: 'POST',
|
|
48
53
|
headers: {
|
|
49
54
|
'Content-Type': 'application/json',
|
|
@@ -57,7 +62,7 @@ class WineaterApi {
|
|
|
57
62
|
throw new Error(`API call failed with status ${response.status}: ${data.message || 'No additional error information'}`);
|
|
58
63
|
}
|
|
59
64
|
|
|
60
|
-
const deserializedData: Wine[] = data.map(deserializeInstantRecommendation);
|
|
65
|
+
const deserializedData: Wine[] = data.filter(Boolean).map(deserializeInstantRecommendation);
|
|
61
66
|
return deserializedData;
|
|
62
67
|
} catch (error) {
|
|
63
68
|
throw new Error(`Error in getInstantRecommendations: ${error.message}`);
|
|
@@ -66,7 +71,10 @@ class WineaterApi {
|
|
|
66
71
|
|
|
67
72
|
async getWineRecommendationsSearch(text): Promise<RecommendedWine[]> {
|
|
68
73
|
try {
|
|
69
|
-
const
|
|
74
|
+
const endpoint = this.#apiVersion === '2'
|
|
75
|
+
? 'dish/recommendations/search/v2'
|
|
76
|
+
: 'dish/recommendations/search';
|
|
77
|
+
const response = await fetch(`${this.#baseUrl}${endpoint}`, {
|
|
70
78
|
method: 'POST',
|
|
71
79
|
headers: {
|
|
72
80
|
'Content-Type': 'application/json',
|
|
@@ -81,7 +89,7 @@ class WineaterApi {
|
|
|
81
89
|
throw new Error(`API call failed with status ${response.status}: ${data.message || 'No additional error information'}`);
|
|
82
90
|
}
|
|
83
91
|
|
|
84
|
-
const deserializedData: RecommendedWine[] = data.map(deserializeRecommendedWine);
|
|
92
|
+
const deserializedData: RecommendedWine[] = data.filter(Boolean).map(deserializeRecommendedWine);
|
|
85
93
|
return deserializedData;
|
|
86
94
|
} catch (error) {
|
|
87
95
|
throw new Error(`Error in getWineRecommendations: ${error.message}`);
|
|
@@ -107,6 +115,13 @@ class WineaterApi {
|
|
|
107
115
|
}
|
|
108
116
|
}
|
|
109
117
|
|
|
118
|
+
function getApiVersion(): string {
|
|
119
|
+
const scriptTag = (document.currentScript as HTMLScriptElement | null)
|
|
120
|
+
|| document.querySelector('script[src*="embed.js"]');
|
|
121
|
+
return scriptTag?.getAttribute('api-version') || '1';
|
|
122
|
+
}
|
|
123
|
+
|
|
110
124
|
const DEMO_TOKEN = 'HeUw6IXuhvz6tRuXdPEGEbj3SGQ3Q2YtEc1Z8jd41Kg9LtyPb4Epu0to31SYIadG';
|
|
111
125
|
const token = window?.wineaterData?.CLIENT_TOKEN || DEMO_TOKEN;
|
|
112
|
-
|
|
126
|
+
const apiVersion = getApiVersion();
|
|
127
|
+
export const wineaterApi = new WineaterApi(token, apiVersion)
|