@askdialog/dialog-sdk 2.9.0 → 2.9.1
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.
|
@@ -48,6 +48,20 @@ describe("searchProducts", () => {
|
|
|
48
48
|
queryId: "0198c3f2-0000-7000-8000-000000000000",
|
|
49
49
|
});
|
|
50
50
|
});
|
|
51
|
+
it.each([
|
|
52
|
+
["fr-FR", "fr"],
|
|
53
|
+
["pt-BR", "pt"],
|
|
54
|
+
["fr", "fr"],
|
|
55
|
+
["not a locale", "not a locale"],
|
|
56
|
+
])("sends the locale %s as its bare ISO 639-1 language %s", async (locale, expected) => {
|
|
57
|
+
fetchMock.mockResolvedValue(jsonResponse(emptyResponse));
|
|
58
|
+
await searchProducts(API_KEY, { query: "running shoes", locale });
|
|
59
|
+
const [, init] = fetchMock.mock.calls[0];
|
|
60
|
+
expect(JSON.parse(init.body)).toEqual({
|
|
61
|
+
query: "running shoes",
|
|
62
|
+
locale: expected,
|
|
63
|
+
});
|
|
64
|
+
});
|
|
51
65
|
it("omits pagination fields and queryId the caller did not provide", async () => {
|
|
52
66
|
fetchMock.mockResolvedValue(jsonResponse(emptyResponse));
|
|
53
67
|
await searchProducts(API_KEY, { query: "running shoes" });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../src/services/search.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../src/services/search.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAgC/E;;;GAGG;AACH,eAAO,MAAM,cAAc,GACzB,QAAQ,MAAM,EACd,SAAS,aAAa,EACtB,UAAU,aAAa,KACtB,OAAO,CAAC,cAAc,CAgBxB,CAAC"}
|
package/dist/services/search.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { config } from "../config";
|
|
2
2
|
import { DialogSearchError } from "../DialogSearchError";
|
|
3
|
+
import { toIso639LanguageCode } from "../utils/localization";
|
|
3
4
|
const SEARCH_PATH = "/public/search";
|
|
4
5
|
const API_KEY_HEADER = "x-dialog-api-key";
|
|
5
6
|
const toSearchError = async (response) => {
|
|
@@ -16,6 +17,12 @@ const toSearchError = async (response) => {
|
|
|
16
17
|
: response.statusText;
|
|
17
18
|
return new DialogSearchError({ status: response.status, code, message });
|
|
18
19
|
};
|
|
20
|
+
// Every search path (Dialog instance, search controller, direct transport)
|
|
21
|
+
// funnels through here, so the locale reaches the wire as the bare ISO 639-1
|
|
22
|
+
// language whatever tag the caller holds.
|
|
23
|
+
const withNormalizedLocale = (request) => request.locale === undefined
|
|
24
|
+
? request
|
|
25
|
+
: { ...request, locale: toIso639LanguageCode(request.locale) };
|
|
19
26
|
/**
|
|
20
27
|
* One POST per invocation — no debounce, cache, retry or request state; the
|
|
21
28
|
* caller owns cancellation through `options.signal`.
|
|
@@ -27,7 +34,7 @@ export const searchProducts = async (apiKey, request, options) => {
|
|
|
27
34
|
"Content-Type": "application/json",
|
|
28
35
|
[API_KEY_HEADER]: apiKey,
|
|
29
36
|
},
|
|
30
|
-
body: JSON.stringify(request),
|
|
37
|
+
body: JSON.stringify(withNormalizedLocale(request)),
|
|
31
38
|
signal: options?.signal,
|
|
32
39
|
});
|
|
33
40
|
if (!response.ok) {
|
|
@@ -23,4 +23,5 @@ export declare const resolveTextDirection: (code?: string) => TextDirection;
|
|
|
23
23
|
* therefore keeps a generic language name ("French", not "French (France)").
|
|
24
24
|
*/
|
|
25
25
|
export declare const getDetailedLocaleInfo: (locale: string, countryCodeOverride?: string) => DetailedLocaleInfo | null;
|
|
26
|
+
export declare const toIso639LanguageCode: (locale: string) => string;
|
|
26
27
|
//# sourceMappingURL=localization.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localization.d.ts","sourceRoot":"","sources":["../../src/utils/localization.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,eAAO,MAAM,kBAAkB,GAC7B,aAAa,MAAM,GAAG,SAAS,KAC9B,WAAW,IAAI,MAC8C,CAAC;AAEjE,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG,KAAK,CAAC;AAQ1C,eAAO,MAAM,iBAAiB,GAAI,OAAO,MAAM,KAAG,OAEa,CAAC;AAEhE,eAAO,MAAM,oBAAoB,GAAI,OAAO,MAAM,KAAG,aACZ,CAAC;AAE1C;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,qBAAqB,GAChC,QAAQ,MAAM,EACd,sBAAsB,MAAM,KAC3B,kBAAkB,GAAG,IAoCvB,CAAC"}
|
|
1
|
+
{"version":3,"file":"localization.d.ts","sourceRoot":"","sources":["../../src/utils/localization.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,eAAO,MAAM,kBAAkB,GAC7B,aAAa,MAAM,GAAG,SAAS,KAC9B,WAAW,IAAI,MAC8C,CAAC;AAEjE,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG,KAAK,CAAC;AAQ1C,eAAO,MAAM,iBAAiB,GAAI,OAAO,MAAM,KAAG,OAEa,CAAC;AAEhE,eAAO,MAAM,oBAAoB,GAAI,OAAO,MAAM,KAAG,aACZ,CAAC;AAE1C;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,qBAAqB,GAChC,QAAQ,MAAM,EACd,sBAAsB,MAAM,KAC3B,kBAAkB,GAAG,IAoCvB,CAAC;AAKF,eAAO,MAAM,oBAAoB,GAAI,QAAQ,MAAM,KAAG,MAMrD,CAAC"}
|
|
@@ -52,3 +52,14 @@ export const getDetailedLocaleInfo = (locale, countryCodeOverride) => {
|
|
|
52
52
|
return null;
|
|
53
53
|
}
|
|
54
54
|
};
|
|
55
|
+
// The search backend keys its indexes on the bare ISO 639-1 language, while
|
|
56
|
+
// storefronts usually hold full BCP-47 tags ("fr-FR", "pt-BR"). An unparsable
|
|
57
|
+
// value is returned as-is so a search is never dropped over its locale.
|
|
58
|
+
export const toIso639LanguageCode = (locale) => {
|
|
59
|
+
try {
|
|
60
|
+
return new Intl.Locale(locale).language;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return locale;
|
|
64
|
+
}
|
|
65
|
+
};
|