@askdialog/dialog-sdk 2.0.1 → 2.1.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 +8 -0
- package/dist/Dialog.d.ts +2 -1
- package/dist/Dialog.d.ts.map +1 -1
- package/dist/Dialog.js +6 -4
- package/dist/types/constructor.d.ts +6 -0
- package/dist/types/constructor.d.ts.map +1 -1
- package/dist/utils/localization.d.ts +16 -1
- package/dist/utils/localization.d.ts.map +1 -1
- package/dist/utils/localization.js +29 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -52,6 +52,7 @@ import { Dialog } from '@dialog/dialog-sdk';
|
|
|
52
52
|
const client = new Dialog({
|
|
53
53
|
apiKey: 'YOUR_API_KEY', // required
|
|
54
54
|
locale: 'TARGETED_LOCALE', // required
|
|
55
|
+
countryCode: 'FR', // optional, ISO 3166 alpha-2
|
|
55
56
|
callbacks: {
|
|
56
57
|
addToCart: async ({
|
|
57
58
|
productId,
|
|
@@ -76,6 +77,13 @@ const client = new Dialog({
|
|
|
76
77
|
|
|
77
78
|
The apiKey is required to authenticate with our API and interact with our assistant.
|
|
78
79
|
The locale specifies the language you want to use.
|
|
80
|
+
|
|
81
|
+
The optional `countryCode` (ISO 3166 alpha-2, e.g. `'FR'`, `'US'`) sets the region used to format prices (separators, symbol placement) and the language variant. It does not change the currency itself, which comes from each product. The effective region is resolved in this order:
|
|
82
|
+
|
|
83
|
+
1. the `countryCode` parameter, when provided;
|
|
84
|
+
2. the region embedded in `locale` (e.g. `'en-US'` → `US`);
|
|
85
|
+
3. the region guessed from the language (e.g. `'fr'` → `FR`).
|
|
86
|
+
|
|
79
87
|
The addToCart function is triggered when a user clicks the AddToCart button.
|
|
80
88
|
The getProduct function is used to display product information in the assistant.
|
|
81
89
|
|
package/dist/Dialog.d.ts
CHANGED
|
@@ -10,11 +10,12 @@ export declare class Dialog {
|
|
|
10
10
|
static readonly VERSION: string;
|
|
11
11
|
private _apiKey;
|
|
12
12
|
private _locale;
|
|
13
|
+
private _countryCode?;
|
|
13
14
|
private _callbacks;
|
|
14
15
|
private _theme;
|
|
15
16
|
private _userId;
|
|
16
17
|
private _eventsHandler;
|
|
17
|
-
constructor({ apiKey, locale, callbacks, theme, userId }: DialogConstructor);
|
|
18
|
+
constructor({ apiKey, locale, countryCode, callbacks, theme, userId, }: DialogConstructor);
|
|
18
19
|
get apiKey(): string;
|
|
19
20
|
get theme(): Theme;
|
|
20
21
|
get userId(): string;
|
package/dist/Dialog.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Dialog.d.ts","sourceRoot":"","sources":["../src/Dialog.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EACL,kBAAkB,EAEnB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAEL,sBAAsB,EACtB,oBAAoB,EACpB,sBAAsB,EACvB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGhD,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,qBAAa,MAAM;IACjB,gBAAuB,OAAO,SAAuB;IAErD,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,OAAO,CAAS;
|
|
1
|
+
{"version":3,"file":"Dialog.d.ts","sourceRoot":"","sources":["../src/Dialog.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EACL,kBAAkB,EAEnB,MAAM,sBAAsB,CAAC;AAE9B,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sBAAsB,CAAC,KAAK,EAAE,cAAc,GAAG,IAAI;IAInD,UAAU,CACf,SAAS,EAAE,MAAM,EACjB,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,iBAAiB,CAAC;IAIhB,SAAS,CAAC,EACrB,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,MAAM,CAAC;QAClB,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,IAAI,CAAC;IAmBV,sBAAsB,CAAC,EAC5B,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,MAAM,CAAC;QAClB,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,GAAG,IAAI;IAWD,2BAA2B,CAAC,EACjC,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,MAAM,CAAC;QAClB,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,GAAG,IAAI;IAWR,OAAO,CAAC,cAAc;CA4BvB"}
|
package/dist/Dialog.js
CHANGED
|
@@ -12,13 +12,15 @@ export class Dialog {
|
|
|
12
12
|
static VERSION = packageJson.version;
|
|
13
13
|
_apiKey;
|
|
14
14
|
_locale;
|
|
15
|
+
_countryCode;
|
|
15
16
|
_callbacks;
|
|
16
17
|
_theme;
|
|
17
18
|
_userId;
|
|
18
19
|
_eventsHandler;
|
|
19
|
-
constructor({ apiKey, locale, callbacks, theme, userId }) {
|
|
20
|
+
constructor({ apiKey, locale, countryCode, callbacks, theme, userId, }) {
|
|
20
21
|
this._apiKey = apiKey;
|
|
21
22
|
this._locale = locale;
|
|
23
|
+
this._countryCode = countryCode;
|
|
22
24
|
this._callbacks = callbacks;
|
|
23
25
|
this._theme = { ...defaultTheme, ...theme };
|
|
24
26
|
this._userId = this._createOrRetrieveUserId(userId);
|
|
@@ -45,7 +47,7 @@ export class Dialog {
|
|
|
45
47
|
return this._eventsHandler;
|
|
46
48
|
}
|
|
47
49
|
getLocalizationInformations() {
|
|
48
|
-
return getDetailedLocaleInfo(this._locale);
|
|
50
|
+
return getDetailedLocaleInfo(this._locale, this._countryCode);
|
|
49
51
|
}
|
|
50
52
|
_createOrRetrieveUserId(userId) {
|
|
51
53
|
if (userId !== undefined) {
|
|
@@ -124,14 +126,14 @@ export class Dialog {
|
|
|
124
126
|
});
|
|
125
127
|
}
|
|
126
128
|
_loadAssistant() {
|
|
127
|
-
const localeInfo = getDetailedLocaleInfo(this._locale);
|
|
129
|
+
const localeInfo = getDetailedLocaleInfo(this._locale, this._countryCode);
|
|
128
130
|
if (localeInfo === null) {
|
|
129
131
|
console.error("Missing locale information");
|
|
130
132
|
return;
|
|
131
133
|
}
|
|
132
134
|
const div = document.createElement("div");
|
|
133
135
|
div.id = "dialog-shopify-ai";
|
|
134
|
-
div.dataset.shopIsoCode =
|
|
136
|
+
div.dataset.shopIsoCode = localeInfo.languageCode;
|
|
135
137
|
div.dataset.apiKey = this._apiKey;
|
|
136
138
|
div.dataset.userId = this._userId;
|
|
137
139
|
div.dataset.countryCode = localeInfo.countryCode;
|
|
@@ -3,6 +3,12 @@ import { Theme } from "./theme";
|
|
|
3
3
|
export interface DialogConstructor {
|
|
4
4
|
apiKey: string;
|
|
5
5
|
locale: string;
|
|
6
|
+
/**
|
|
7
|
+
* Optional ISO 3166 country code used to format prices (e.g. 'FR', 'US').
|
|
8
|
+
* When provided it takes precedence over the country derived from `locale`.
|
|
9
|
+
* Omit it to keep deriving the country from the locale (backward compatible).
|
|
10
|
+
*/
|
|
11
|
+
countryCode?: string;
|
|
6
12
|
callbacks: {
|
|
7
13
|
addToCart: ({ productId, quantity, price, variantId, currency, }: {
|
|
8
14
|
productId: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constructor.d.ts","sourceRoot":"","sources":["../../src/types/constructor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE;QACT,SAAS,EAAE,CAAC,EACV,SAAS,EACT,QAAQ,EACR,KAAK,EACL,SAAS,EACT,QAAQ,GACT,EAAE;YACD,SAAS,EAAE,MAAM,CAAC;YAClB,QAAQ,EAAE,MAAM,CAAC;YACjB,KAAK,CAAC,EAAE,MAAM,CAAC;YACf,SAAS,CAAC,EAAE,MAAM,CAAC;YACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;SACnB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;QACpB,UAAU,EAAE,CACV,SAAS,EAAE,MAAM,EACjB,SAAS,CAAC,EAAE,MAAM,KACf,OAAO,CAAC,iBAAiB,CAAC,CAAC;KACjC,CAAC;IACF,KAAK,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB"}
|
|
1
|
+
{"version":3,"file":"constructor.d.ts","sourceRoot":"","sources":["../../src/types/constructor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE;QACT,SAAS,EAAE,CAAC,EACV,SAAS,EACT,QAAQ,EACR,KAAK,EACL,SAAS,EACT,QAAQ,GACT,EAAE;YACD,SAAS,EAAE,MAAM,CAAC;YAClB,QAAQ,EAAE,MAAM,CAAC;YACjB,KAAK,CAAC,EAAE,MAAM,CAAC;YACf,SAAS,CAAC,EAAE,MAAM,CAAC;YACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;SACnB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;QACpB,UAAU,EAAE,CACV,SAAS,EAAE,MAAM,EACjB,SAAS,CAAC,EAAE,MAAM,KACf,OAAO,CAAC,iBAAiB,CAAC,CAAC;KACjC,CAAC;IACF,KAAK,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB"}
|
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
export interface DetailedLocaleInfo {
|
|
2
2
|
language: string;
|
|
3
|
+
languageCode: string;
|
|
3
4
|
countryCode: string;
|
|
4
5
|
formatted: string;
|
|
5
6
|
locale: string;
|
|
6
7
|
}
|
|
7
|
-
export declare const
|
|
8
|
+
export declare const isValidCountryCode: (countryCode: string | undefined) => countryCode is string;
|
|
9
|
+
/**
|
|
10
|
+
* Resolves the detailed locale information used by the assistant.
|
|
11
|
+
*
|
|
12
|
+
* The effective region follows this precedence:
|
|
13
|
+
* 1. `countryCodeOverride` (explicit SDK parameter),
|
|
14
|
+
* 2. the region embedded in `locale` (e.g. `en-US` -> `US`),
|
|
15
|
+
* 3. the region guessed from the language via `Intl.Locale.maximize()`.
|
|
16
|
+
*
|
|
17
|
+
* The override (1) and the locale region (2) are considered "explicit" and also
|
|
18
|
+
* drive the language display name (e.g. `en-GB` -> "British English"). The
|
|
19
|
+
* `maximize()` fallback (3) only feeds the country code: a bare language code
|
|
20
|
+
* therefore keeps a generic language name ("French", not "French (France)").
|
|
21
|
+
*/
|
|
22
|
+
export declare const getDetailedLocaleInfo: (locale: string, countryCodeOverride?: string) => DetailedLocaleInfo | null;
|
|
8
23
|
//# 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,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,eAAO,MAAM,qBAAqB,GAChC,QAAQ,MAAM,
|
|
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;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,qBAAqB,GAChC,QAAQ,MAAM,EACd,sBAAsB,MAAM,KAC3B,kBAAkB,GAAG,IAoCvB,CAAC"}
|
|
@@ -1,15 +1,40 @@
|
|
|
1
|
-
export const
|
|
1
|
+
export const isValidCountryCode = (countryCode) => countryCode !== undefined && /^[A-Za-z]{2}$/.test(countryCode);
|
|
2
|
+
/**
|
|
3
|
+
* Resolves the detailed locale information used by the assistant.
|
|
4
|
+
*
|
|
5
|
+
* The effective region follows this precedence:
|
|
6
|
+
* 1. `countryCodeOverride` (explicit SDK parameter),
|
|
7
|
+
* 2. the region embedded in `locale` (e.g. `en-US` -> `US`),
|
|
8
|
+
* 3. the region guessed from the language via `Intl.Locale.maximize()`.
|
|
9
|
+
*
|
|
10
|
+
* The override (1) and the locale region (2) are considered "explicit" and also
|
|
11
|
+
* drive the language display name (e.g. `en-GB` -> "British English"). The
|
|
12
|
+
* `maximize()` fallback (3) only feeds the country code: a bare language code
|
|
13
|
+
* therefore keeps a generic language name ("French", not "French (France)").
|
|
14
|
+
*/
|
|
15
|
+
export const getDetailedLocaleInfo = (locale, countryCodeOverride) => {
|
|
2
16
|
try {
|
|
3
17
|
const localeObj = new Intl.Locale(locale);
|
|
4
18
|
const language = localeObj.language;
|
|
5
|
-
const
|
|
19
|
+
const overrideRegion = isValidCountryCode(countryCodeOverride)
|
|
20
|
+
? countryCodeOverride.toUpperCase()
|
|
21
|
+
: undefined;
|
|
22
|
+
const explicitRegion = overrideRegion ?? localeObj.region;
|
|
23
|
+
const countryCode = explicitRegion ?? localeObj.maximize().region;
|
|
24
|
+
if (countryCode === undefined) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
const baseNameForLanguage = explicitRegion !== undefined
|
|
28
|
+
? `${language}-${explicitRegion}`
|
|
29
|
+
: localeObj.baseName;
|
|
6
30
|
const languageNames = new Intl.DisplayNames(["en"], { type: "language" });
|
|
7
|
-
const languageName = languageNames.of(
|
|
8
|
-
if (languageName === undefined
|
|
31
|
+
const languageName = languageNames.of(baseNameForLanguage);
|
|
32
|
+
if (languageName === undefined) {
|
|
9
33
|
return null;
|
|
10
34
|
}
|
|
11
35
|
return {
|
|
12
36
|
language: languageName,
|
|
37
|
+
languageCode: language,
|
|
13
38
|
countryCode,
|
|
14
39
|
formatted: `${language}-${countryCode}`,
|
|
15
40
|
locale,
|