@coveo/shopify 0.1.0-pre.ed9ef9fb5e → 1.1.0-pre.5a73a68b8f

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.
@@ -0,0 +1,59 @@
1
+ import { CommerceEngineOptions } from '@coveo/headless/commerce';
2
+ import { CustomEnvironment } from '@coveo/relay';
3
+ export * from '@coveo/headless/commerce';
4
+ export * from './utilities';
5
+ export declare const SHOPIFY_COOKIE_KEY = "_shopify_y";
6
+ export interface AppProxyOptions {
7
+ appProxyUrl?: string;
8
+ marketId: string;
9
+ }
10
+ export interface AppProxyResponse {
11
+ accessToken: string;
12
+ organizationId: string;
13
+ environment: CommerceEngineOptions['configuration']['environment'];
14
+ trackingId: string;
15
+ }
16
+ /**
17
+ * Fetches configuration from the app proxy.
18
+ *
19
+ * Performs an HTTP GET request to retrieve the app proxy configuration using the provided marketId.
20
+ * The fetched response is parsed as JSON and returned.
21
+ *
22
+ * @param appProxyUrl - The URL template for the app proxy endpoint. Defaults to '/apps/coveo'.
23
+ * @param marketId - The unique market identifier used as a query parameter.
24
+ * @returns A promise that resolves to an object containing the access token, organization ID,
25
+ * environment, and tracking ID.
26
+ */
27
+ export declare function fetchAppProxyConfig({ appProxyUrl, marketId, }: AppProxyOptions): Promise<AppProxyResponse>;
28
+ interface BuildShopifyCommerceEngineOptions {
29
+ commerceEngineOptions: CommerceEngineOptions;
30
+ shopifyCookie?: string;
31
+ environment?: CustomEnvironment;
32
+ }
33
+ /**
34
+ * Builds the commerce engine for a Shopify store.
35
+ *
36
+ * This function initializes a commerce engine instance specific to a Shopify store by:
37
+ * - Creating or using an existing browser environment.
38
+ * - Retrieving the "_shopify_y" cookie to confirm it is running in a Shopify context.
39
+ * - Storing a client identifier derived from the shop and cookie value in the browser environment's storage.
40
+ * - Building and returning the commerce engine with the provided options.
41
+ *
42
+ * @param commerceEngineOptions - Options to configure the commerce engine.
43
+ * @param shopifyCookie - Optional value of the "_shopify_y" cookie. If not provided, it will attempt to retrieve it from the browser's cookies.
44
+ * @param environment - Optional browser environment; if not provided, a default one is created. Mainly useful when not running in a browser environment.
45
+ * @returns The constructed commerce engine instance.
46
+ * @throws Error if the required "_shopify_y" cookie is not found, ensuring the code runs within a Shopify store.
47
+ */
48
+ export declare function buildShopifyCommerceEngine({ commerceEngineOptions, shopifyCookie, environment, }: BuildShopifyCommerceEngineOptions): import("@coveo/headless/commerce").CommerceEngine<{}>;
49
+ /**
50
+ * Retrieves the value of a specified Shopify cookie by its name.
51
+ *
52
+ * @remarks
53
+ * This function is intended for use in browser environments only, as it relies on the `document.cookie` API.
54
+ * Attempting to use this function in non-browser environments will result in an error or undefined behavior.
55
+ *
56
+ * @param name - The name of the Shopify cookie to retrieve. Defaults to `'_shopify_y'`.
57
+ * @returns The value of the specified cookie, or `null` if the cookie is not found.
58
+ */
59
+ export declare function getShopifyCookie(name?: string): string | null;
@@ -0,0 +1,60 @@
1
+ import { buildCommerceEngine, } from '@coveo/headless/commerce';
2
+ import { buildBrowserEnvironment, clientIdKey, } from '@coveo/relay';
3
+ import { getClientId } from './utilities';
4
+ export * from '@coveo/headless/commerce';
5
+ export * from './utilities';
6
+ export const SHOPIFY_COOKIE_KEY = '_shopify_y';
7
+ /**
8
+ * Fetches configuration from the app proxy.
9
+ *
10
+ * Performs an HTTP GET request to retrieve the app proxy configuration using the provided marketId.
11
+ * The fetched response is parsed as JSON and returned.
12
+ *
13
+ * @param appProxyUrl - The URL template for the app proxy endpoint. Defaults to '/apps/coveo'.
14
+ * @param marketId - The unique market identifier used as a query parameter.
15
+ * @returns A promise that resolves to an object containing the access token, organization ID,
16
+ * environment, and tracking ID.
17
+ */
18
+ export async function fetchAppProxyConfig({ appProxyUrl = '/apps/coveo', marketId, }) {
19
+ const response = await fetch(`${appProxyUrl}?marketId=${marketId}]`);
20
+ return response.json();
21
+ }
22
+ /**
23
+ * Builds the commerce engine for a Shopify store.
24
+ *
25
+ * This function initializes a commerce engine instance specific to a Shopify store by:
26
+ * - Creating or using an existing browser environment.
27
+ * - Retrieving the "_shopify_y" cookie to confirm it is running in a Shopify context.
28
+ * - Storing a client identifier derived from the shop and cookie value in the browser environment's storage.
29
+ * - Building and returning the commerce engine with the provided options.
30
+ *
31
+ * @param commerceEngineOptions - Options to configure the commerce engine.
32
+ * @param shopifyCookie - Optional value of the "_shopify_y" cookie. If not provided, it will attempt to retrieve it from the browser's cookies.
33
+ * @param environment - Optional browser environment; if not provided, a default one is created. Mainly useful when not running in a browser environment.
34
+ * @returns The constructed commerce engine instance.
35
+ * @throws Error if the required "_shopify_y" cookie is not found, ensuring the code runs within a Shopify store.
36
+ */
37
+ export function buildShopifyCommerceEngine({ commerceEngineOptions, shopifyCookie, environment, }) {
38
+ const browserEnvironment = environment || buildBrowserEnvironment();
39
+ const cookie = shopifyCookie || getShopifyCookie();
40
+ if (!cookie) {
41
+ throw new Error('Unable to find the _shopify_y cookie. Please ensure you are running this code in a Shopify store.');
42
+ }
43
+ // TODO: Once headless is updated to support custom relay environments, we should instead pass in a generateUUID function
44
+ browserEnvironment.storage.setItem(clientIdKey, getClientId(cookie));
45
+ return buildCommerceEngine(commerceEngineOptions);
46
+ }
47
+ /**
48
+ * Retrieves the value of a specified Shopify cookie by its name.
49
+ *
50
+ * @remarks
51
+ * This function is intended for use in browser environments only, as it relies on the `document.cookie` API.
52
+ * Attempting to use this function in non-browser environments will result in an error or undefined behavior.
53
+ *
54
+ * @param name - The name of the Shopify cookie to retrieve. Defaults to `'_shopify_y'`.
55
+ * @returns The value of the specified cookie, or `null` if the cookie is not found.
56
+ */
57
+ export function getShopifyCookie(name = SHOPIFY_COOKIE_KEY) {
58
+ const match = document.cookie.match(new RegExp('(?:^|;\\s*)' + name + '=([^;]*)'));
59
+ return match ? decodeURIComponent(match[1]) : null;
60
+ }
@@ -0,0 +1,12 @@
1
+ export declare const UUID_NAMESPACE = "c2db3701-991e-424d-b117-03e30d43b651";
2
+ /**
3
+ * Generates a unique client identifier for the Shopify store.
4
+ *
5
+ * This function creates a version 5 UUID based on the provided
6
+ * shop-specific data and the Shopify cookie value.
7
+ *
8
+ * @param shop - The identifier of the Shopify store.
9
+ * @param shopifyCookie - The value of the Shopify _shopify_y cookie.
10
+ * @returns A version 5 UUID string uniquely representing the client.
11
+ */
12
+ export declare function getClientId(shopifyCookie: string): string;
@@ -0,0 +1,15 @@
1
+ import { v5 } from 'uuid';
2
+ export const UUID_NAMESPACE = 'c2db3701-991e-424d-b117-03e30d43b651';
3
+ /**
4
+ * Generates a unique client identifier for the Shopify store.
5
+ *
6
+ * This function creates a version 5 UUID based on the provided
7
+ * shop-specific data and the Shopify cookie value.
8
+ *
9
+ * @param shop - The identifier of the Shopify store.
10
+ * @param shopifyCookie - The value of the Shopify _shopify_y cookie.
11
+ * @returns A version 5 UUID string uniquely representing the client.
12
+ */
13
+ export function getClientId(shopifyCookie) {
14
+ return v5(shopifyCookie, UUID_NAMESPACE);
15
+ }
package/package.json CHANGED
@@ -7,23 +7,25 @@
7
7
  },
8
8
  "main": "./dist/headless.esm.js",
9
9
  "module": "./dist/headless.esm.js",
10
- "types": "./dist/definitions/index.d.ts",
11
10
  "license": "Apache-2.0",
12
- "version": "0.1.0-pre.ed9ef9fb5e",
11
+ "version": "1.1.0-pre.5a73a68b8f",
13
12
  "files": [
14
13
  "dist/"
15
14
  ],
16
15
  "type": "module",
17
16
  "exports": {
18
17
  ".": {
18
+ "types": "./dist/headless.d.ts",
19
19
  "import": "./dist/headless.esm.js",
20
20
  "default": "./dist/headless.esm.js"
21
21
  },
22
22
  "./headless": {
23
+ "types": "./dist/headless.d.ts",
23
24
  "import": "./dist/headless.esm.js",
24
25
  "default": "./dist/headless.esm.js"
25
26
  },
26
27
  "./utilities": {
28
+ "types": "./dist/utilities.d.ts",
27
29
  "import": "./dist/utilities.esm.js",
28
30
  "default": "./dist/utilities.esm.js"
29
31
  }
@@ -32,6 +34,7 @@
32
34
  "dev": "npm run build:bundles -- dev",
33
35
  "build": "nx build",
34
36
  "build:bundles": "node esbuild.mjs",
37
+ "build:definitions": "tsc -p src/tsconfig.build.json && node ./scripts/copy-types.mjs",
35
38
  "test": "vitest run",
36
39
  "clean": "rimraf -rf dist/* && rimraf -rf cdn/",
37
40
  "publish:npm": "npm run-script -w=@coveo/ci npm-publish",
@@ -43,7 +46,7 @@
43
46
  "vitest": "3.1.2"
44
47
  },
45
48
  "dependencies": {
46
- "@coveo/headless": "3.23.1-pre.ed9ef9fb5e",
49
+ "@coveo/headless": "3.23.1-pre.5a73a68b8f",
47
50
  "@coveo/relay": "1.1.3",
48
51
  "uuid": "^11.0.0"
49
52
  },