@salesforce/storefront-next-runtime 0.4.2 → 1.0.0-alpha.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 +9 -3
- package/dist/config.d.ts +33 -221
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +34 -116
- package/dist/config.js.map +1 -1
- package/dist/data-store.d.ts +185 -15
- package/dist/data-store.d.ts.map +1 -1
- package/dist/data-store.js +412 -10
- package/dist/data-store.js.map +1 -1
- package/dist/design-data.d.ts +266 -62
- package/dist/design-data.d.ts.map +1 -1
- package/dist/design-data.js +399 -14
- package/dist/design-data.js.map +1 -1
- package/dist/design-mode.d.ts +3 -2
- package/dist/design-mode.d.ts.map +1 -1
- package/dist/design-react-core.d.ts +2 -2
- package/dist/events.d.ts +32 -6
- package/dist/events.d.ts.map +1 -1
- package/dist/i18n-client.d.ts.map +1 -1
- package/dist/i18n-client.js.map +1 -1
- package/dist/i18n.d.ts +1 -2
- package/dist/i18n.d.ts.map +1 -1
- package/dist/modeDetection.js +0 -18
- package/dist/modeDetection.js.map +1 -1
- package/dist/scapi.d.ts +2185 -466
- package/dist/scapi.d.ts.map +1 -1
- package/dist/scapi.js +1 -1
- package/dist/scapi.js.map +1 -1
- package/dist/schema.d.ts +17 -15
- package/dist/schema.d.ts.map +1 -1
- package/dist/site-context.d.ts +43 -27
- package/dist/site-context.d.ts.map +1 -1
- package/dist/site-context.js +2 -2
- package/dist/site-context2.js +41 -31
- package/dist/site-context2.js.map +1 -1
- package/dist/types.d.ts +19 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/types2.d.ts +89 -63
- package/dist/types2.d.ts.map +1 -1
- package/package.json +1 -19
- package/dist/custom-global-preferences.d.ts +0 -20
- package/dist/custom-global-preferences.d.ts.map +0 -1
- package/dist/custom-global-preferences.js +0 -31
- package/dist/custom-global-preferences.js.map +0 -1
- package/dist/custom-site-preferences.d.ts +0 -20
- package/dist/custom-site-preferences.d.ts.map +0 -1
- package/dist/custom-site-preferences.js +0 -31
- package/dist/custom-site-preferences.js.map +0 -1
- package/dist/data-store-custom-global-preferences.d.ts +0 -2
- package/dist/data-store-custom-global-preferences.js +0 -6
- package/dist/data-store-custom-site-preferences.d.ts +0 -2
- package/dist/data-store-custom-site-preferences.js +0 -6
- package/dist/data-store-gcp-preferences.d.ts +0 -2
- package/dist/data-store-gcp-preferences.js +0 -6
- package/dist/gcp-preferences.d.ts +0 -52
- package/dist/gcp-preferences.d.ts.map +0 -1
- package/dist/gcp-preferences.js +0 -64
- package/dist/gcp-preferences.js.map +0 -1
- package/dist/utils.js +0 -90
- package/dist/utils.js.map +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"custom-site-preferences.js","names":[],"sources":["../src/data-store/middleware/custom-site-preferences.ts"],"sourcesContent":["/**\n * Copyright 2026 Salesforce, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { RouterContextProvider } from 'react-router';\nimport { createDataStoreContext, createDataStoreMiddleware, prefixWithSiteId } from '../utils';\n\nexport type SitePreferences = Record<string, unknown>;\n\nexport const DEFAULT_SITE_PREFERENCES_KEY = 'site-preferences';\nexport const sitePreferencesContext = createDataStoreContext<SitePreferences>();\nconst DATA_STORE_UNAVAILABLE_MODE = process.env.SFNEXT_DATA_STORE_UNAVAILABLE_MODE;\n\n/**\n * Read site preferences from router context.\n *\n * @param context - Router context provider\n * @returns Site preferences data stored by data-store middleware\n * @throws Error when the data-store context is not available\n */\nexport function getSitePreferences(context: Readonly<RouterContextProvider>): SitePreferences {\n const data = context.get(sitePreferencesContext);\n if (!data) {\n // eslint-disable-next-line no-console\n console.warn(\n 'Data store context not found. Ensure data-store middleware runs before loaders and the required env vars are set.'\n );\n return {};\n }\n return data;\n}\n\nexport const customSitePreferencesMiddleware = createDataStoreMiddleware({\n entryKey: prefixWithSiteId('custom-site-preferences'),\n context: sitePreferencesContext,\n onUnavailable: DATA_STORE_UNAVAILABLE_MODE === 'fallback' ? 'fallback' : 'throw',\n fallbackValue: {},\n});\n"],"mappings":";;;AAqBA,MAAa,+BAA+B;AAC5C,MAAa,yBAAyB,wBAAyC;AAC/E,MAAM,8BAA8B,QAAQ,IAAI;;;;;;;;AAShD,SAAgB,mBAAmB,SAA2D;CAC1F,MAAM,OAAO,QAAQ,IAAI,uBAAuB;AAChD,KAAI,CAAC,MAAM;AAEP,UAAQ,KACJ,oHACH;AACD,SAAO,EAAE;;AAEb,QAAO;;AAGX,MAAa,kCAAkC,0BAA0B;CACrE,UAAU,iBAAiB,0BAA0B;CACrD,SAAS;CACT,eAAe,gCAAgC,aAAa,aAAa;CACzE,eAAe,EAAE;CACpB,CAAC"}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import { a as getCustomGlobalPreferences, i as customGlobalPreferencesMiddleware, n as DEFAULT_CUSTOM_GLOBAL_PREFERENCES_KEY, r as customGlobalPreferencesContext, t as CustomGlobalPreferences } from "./custom-global-preferences.js";
|
|
2
|
-
export { CustomGlobalPreferences, DEFAULT_CUSTOM_GLOBAL_PREFERENCES_KEY, customGlobalPreferencesContext, customGlobalPreferencesMiddleware, getCustomGlobalPreferences };
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import "./site-context2.js";
|
|
2
|
-
import "./apply-url-config.js";
|
|
3
|
-
import "./utils.js";
|
|
4
|
-
import { i as getCustomGlobalPreferences, n as customGlobalPreferencesContext, r as customGlobalPreferencesMiddleware, t as DEFAULT_CUSTOM_GLOBAL_PREFERENCES_KEY } from "./custom-global-preferences.js";
|
|
5
|
-
|
|
6
|
-
export { DEFAULT_CUSTOM_GLOBAL_PREFERENCES_KEY, customGlobalPreferencesContext, customGlobalPreferencesMiddleware, getCustomGlobalPreferences };
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import { a as sitePreferencesContext, i as getSitePreferences, n as SitePreferences, r as customSitePreferencesMiddleware, t as DEFAULT_SITE_PREFERENCES_KEY } from "./custom-site-preferences.js";
|
|
2
|
-
export { DEFAULT_SITE_PREFERENCES_KEY, SitePreferences, customSitePreferencesMiddleware, getSitePreferences, sitePreferencesContext };
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import "./site-context2.js";
|
|
2
|
-
import "./apply-url-config.js";
|
|
3
|
-
import "./utils.js";
|
|
4
|
-
import { i as sitePreferencesContext, n as customSitePreferencesMiddleware, r as getSitePreferences, t as DEFAULT_SITE_PREFERENCES_KEY } from "./custom-site-preferences.js";
|
|
5
|
-
|
|
6
|
-
export { DEFAULT_SITE_PREFERENCES_KEY, customSitePreferencesMiddleware, getSitePreferences, sitePreferencesContext };
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import { a as getGcpApiKey, i as gcpPreferencesMiddleware, n as GcpPreferences, o as getGcpPreferences, r as gcpPreferencesContext, t as DEFAULT_GCP_PREFERENCES_KEY } from "./gcp-preferences.js";
|
|
2
|
-
export { DEFAULT_GCP_PREFERENCES_KEY, GcpPreferences, gcpPreferencesContext, gcpPreferencesMiddleware, getGcpApiKey, getGcpPreferences };
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import "./site-context2.js";
|
|
2
|
-
import "./apply-url-config.js";
|
|
3
|
-
import "./utils.js";
|
|
4
|
-
import { a as getGcpPreferences, i as getGcpApiKey, n as gcpPreferencesContext, r as gcpPreferencesMiddleware, t as DEFAULT_GCP_PREFERENCES_KEY } from "./gcp-preferences.js";
|
|
5
|
-
|
|
6
|
-
export { DEFAULT_GCP_PREFERENCES_KEY, gcpPreferencesContext, gcpPreferencesMiddleware, getGcpApiKey, getGcpPreferences };
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import * as react_router1 from "react-router";
|
|
2
|
-
import { RouterContextProvider } from "react-router";
|
|
3
|
-
|
|
4
|
-
//#region src/data-store/middleware/gcp-preferences.d.ts
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* OOTB Google Cloud Platform preferences sourced from the MRT data store.
|
|
8
|
-
*
|
|
9
|
-
* Additional fields (e.g. `projectId`, `region`) may be added here as the
|
|
10
|
-
* ECOM MRT sync job expands the `gcp` entry. Consumers should read the
|
|
11
|
-
* object as a whole via `getGcpPreferences`, or use a specific convenience
|
|
12
|
-
* getter like `getGcpApiKey` for a single field.
|
|
13
|
-
*/
|
|
14
|
-
type GcpPreferences = {
|
|
15
|
-
apiKey: string;
|
|
16
|
-
};
|
|
17
|
-
declare const DEFAULT_GCP_PREFERENCES_KEY = "gcp";
|
|
18
|
-
declare const gcpPreferencesContext: react_router1.RouterContext<GcpPreferences | null>;
|
|
19
|
-
/**
|
|
20
|
-
* Read the GCP (Google Cloud Platform) preferences object from router context.
|
|
21
|
-
*
|
|
22
|
-
* The preferences are sourced from the MRT data store entry `gcp`, which is
|
|
23
|
-
* populated only for storefronts connecting to production ECOM instances.
|
|
24
|
-
* In non-production environments, or when the entry is missing, returns an
|
|
25
|
-
* object whose fields are all empty/default.
|
|
26
|
-
*
|
|
27
|
-
* @param context - Router context provider
|
|
28
|
-
* @returns GCP preferences object; fields are empty/default when the entry is unavailable
|
|
29
|
-
*/
|
|
30
|
-
declare function getGcpPreferences(context: Readonly<RouterContextProvider>): GcpPreferences;
|
|
31
|
-
/**
|
|
32
|
-
* Convenience getter for the Google Cloud API key alone.
|
|
33
|
-
*
|
|
34
|
-
* Equivalent to `getGcpPreferences(context).apiKey`.
|
|
35
|
-
*
|
|
36
|
-
* @param context - Router context provider
|
|
37
|
-
* @returns The GCP API key, or an empty string when unavailable
|
|
38
|
-
*/
|
|
39
|
-
declare function getGcpApiKey(context: Readonly<RouterContextProvider>): string;
|
|
40
|
-
/**
|
|
41
|
-
* Middleware that reads the OOTB GCP preferences from the MRT data store and
|
|
42
|
-
* stores them in the router context. The entry shape is `{ "api-key": string, ... }`
|
|
43
|
-
* under data store key `gcp`. Missing/invalid fields coerce to empty/default values.
|
|
44
|
-
*
|
|
45
|
-
* Only available for storefronts connecting to production ECOM instances.
|
|
46
|
-
* Must run before any loader/middleware that reads `getGcpPreferences(context)`
|
|
47
|
-
* or `getGcpApiKey(context)`.
|
|
48
|
-
*/
|
|
49
|
-
declare const gcpPreferencesMiddleware: react_router1.MiddlewareFunction<Response>;
|
|
50
|
-
//#endregion
|
|
51
|
-
export { getGcpApiKey as a, gcpPreferencesMiddleware as i, GcpPreferences as n, getGcpPreferences as o, gcpPreferencesContext as r, DEFAULT_GCP_PREFERENCES_KEY as t };
|
|
52
|
-
//# sourceMappingURL=gcp-preferences.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"gcp-preferences.d.ts","names":[],"sources":["../src/data-store/middleware/gcp-preferences.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;;;KA2BY,cAAA;;;cAIC,2BAAA;cASA,uBAAqB,aAAA,CAAA,cAAA;;;;;;;;;;;;iBAalB,iBAAA,UAA2B,SAAS,yBAAyB;;;;;;;;;iBAoB7D,YAAA,UAAsB,SAAS;;;;;;;;;;cAalC,0BAAwB,aAAA,CAAA,mBAAA"}
|
package/dist/gcp-preferences.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { n as createDataStoreMiddleware, t as createDataStoreContext } from "./utils.js";
|
|
2
|
-
|
|
3
|
-
//#region src/data-store/middleware/gcp-preferences.ts
|
|
4
|
-
const DEFAULT_GCP_PREFERENCES_KEY = "gcp";
|
|
5
|
-
const DATA_STORE_UNAVAILABLE_MODE = process.env.SFNEXT_DATA_STORE_UNAVAILABLE_MODE;
|
|
6
|
-
/**
|
|
7
|
-
* Map keys inside the `gcp` data store entry. The ECOM MRT sync job writes
|
|
8
|
-
* to these exact keys; keep in sync with the sync job contract.
|
|
9
|
-
*/
|
|
10
|
-
const API_KEY_MAP_KEY = "api-key";
|
|
11
|
-
const gcpPreferencesContext = createDataStoreContext();
|
|
12
|
-
/**
|
|
13
|
-
* Read the GCP (Google Cloud Platform) preferences object from router context.
|
|
14
|
-
*
|
|
15
|
-
* The preferences are sourced from the MRT data store entry `gcp`, which is
|
|
16
|
-
* populated only for storefronts connecting to production ECOM instances.
|
|
17
|
-
* In non-production environments, or when the entry is missing, returns an
|
|
18
|
-
* object whose fields are all empty/default.
|
|
19
|
-
*
|
|
20
|
-
* @param context - Router context provider
|
|
21
|
-
* @returns GCP preferences object; fields are empty/default when the entry is unavailable
|
|
22
|
-
*/
|
|
23
|
-
function getGcpPreferences(context) {
|
|
24
|
-
const data = context.get(gcpPreferencesContext);
|
|
25
|
-
if (data === null) {
|
|
26
|
-
console.warn("GCP preferences context not found. Ensure gcpPreferencesMiddleware runs before loaders, or expect empty values in environments without the MRT data store entry.");
|
|
27
|
-
return { apiKey: "" };
|
|
28
|
-
}
|
|
29
|
-
return data;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Convenience getter for the Google Cloud API key alone.
|
|
33
|
-
*
|
|
34
|
-
* Equivalent to `getGcpPreferences(context).apiKey`.
|
|
35
|
-
*
|
|
36
|
-
* @param context - Router context provider
|
|
37
|
-
* @returns The GCP API key, or an empty string when unavailable
|
|
38
|
-
*/
|
|
39
|
-
function getGcpApiKey(context) {
|
|
40
|
-
return getGcpPreferences(context).apiKey;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Middleware that reads the OOTB GCP preferences from the MRT data store and
|
|
44
|
-
* stores them in the router context. The entry shape is `{ "api-key": string, ... }`
|
|
45
|
-
* under data store key `gcp`. Missing/invalid fields coerce to empty/default values.
|
|
46
|
-
*
|
|
47
|
-
* Only available for storefronts connecting to production ECOM instances.
|
|
48
|
-
* Must run before any loader/middleware that reads `getGcpPreferences(context)`
|
|
49
|
-
* or `getGcpApiKey(context)`.
|
|
50
|
-
*/
|
|
51
|
-
const gcpPreferencesMiddleware = createDataStoreMiddleware({
|
|
52
|
-
entryKey: DEFAULT_GCP_PREFERENCES_KEY,
|
|
53
|
-
context: gcpPreferencesContext,
|
|
54
|
-
onUnavailable: DATA_STORE_UNAVAILABLE_MODE === "fallback" ? "fallback" : "throw",
|
|
55
|
-
fallbackValue: { apiKey: "" },
|
|
56
|
-
transform: (value) => {
|
|
57
|
-
const rawKey = value[API_KEY_MAP_KEY];
|
|
58
|
-
return { apiKey: typeof rawKey === "string" ? rawKey : "" };
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
//#endregion
|
|
63
|
-
export { getGcpPreferences as a, getGcpApiKey as i, gcpPreferencesContext as n, gcpPreferencesMiddleware as r, DEFAULT_GCP_PREFERENCES_KEY as t };
|
|
64
|
-
//# sourceMappingURL=gcp-preferences.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"gcp-preferences.js","names":[],"sources":["../src/data-store/middleware/gcp-preferences.ts"],"sourcesContent":["/**\n * Copyright 2026 Salesforce, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { RouterContextProvider } from 'react-router';\nimport { createDataStoreContext, createDataStoreMiddleware } from '../utils';\n\n/**\n * OOTB Google Cloud Platform preferences sourced from the MRT data store.\n *\n * Additional fields (e.g. `projectId`, `region`) may be added here as the\n * ECOM MRT sync job expands the `gcp` entry. Consumers should read the\n * object as a whole via `getGcpPreferences`, or use a specific convenience\n * getter like `getGcpApiKey` for a single field.\n */\nexport type GcpPreferences = {\n apiKey: string;\n};\n\nexport const DEFAULT_GCP_PREFERENCES_KEY = 'gcp';\nconst DATA_STORE_UNAVAILABLE_MODE = process.env.SFNEXT_DATA_STORE_UNAVAILABLE_MODE;\n\n/**\n * Map keys inside the `gcp` data store entry. The ECOM MRT sync job writes\n * to these exact keys; keep in sync with the sync job contract.\n */\nconst API_KEY_MAP_KEY = 'api-key';\n\nexport const gcpPreferencesContext = createDataStoreContext<GcpPreferences>();\n\n/**\n * Read the GCP (Google Cloud Platform) preferences object from router context.\n *\n * The preferences are sourced from the MRT data store entry `gcp`, which is\n * populated only for storefronts connecting to production ECOM instances.\n * In non-production environments, or when the entry is missing, returns an\n * object whose fields are all empty/default.\n *\n * @param context - Router context provider\n * @returns GCP preferences object; fields are empty/default when the entry is unavailable\n */\nexport function getGcpPreferences(context: Readonly<RouterContextProvider>): GcpPreferences {\n const data = context.get(gcpPreferencesContext);\n if (data === null) {\n // eslint-disable-next-line no-console\n console.warn(\n 'GCP preferences context not found. Ensure gcpPreferencesMiddleware runs before loaders, or expect empty values in environments without the MRT data store entry.'\n );\n return { apiKey: '' };\n }\n return data;\n}\n\n/**\n * Convenience getter for the Google Cloud API key alone.\n *\n * Equivalent to `getGcpPreferences(context).apiKey`.\n *\n * @param context - Router context provider\n * @returns The GCP API key, or an empty string when unavailable\n */\nexport function getGcpApiKey(context: Readonly<RouterContextProvider>): string {\n return getGcpPreferences(context).apiKey;\n}\n\n/**\n * Middleware that reads the OOTB GCP preferences from the MRT data store and\n * stores them in the router context. The entry shape is `{ \"api-key\": string, ... }`\n * under data store key `gcp`. Missing/invalid fields coerce to empty/default values.\n *\n * Only available for storefronts connecting to production ECOM instances.\n * Must run before any loader/middleware that reads `getGcpPreferences(context)`\n * or `getGcpApiKey(context)`.\n */\nexport const gcpPreferencesMiddleware = createDataStoreMiddleware<GcpPreferences>({\n entryKey: DEFAULT_GCP_PREFERENCES_KEY,\n context: gcpPreferencesContext,\n onUnavailable: DATA_STORE_UNAVAILABLE_MODE === 'fallback' ? 'fallback' : 'throw',\n fallbackValue: { apiKey: '' },\n transform: (value) => {\n const rawKey = value[API_KEY_MAP_KEY];\n return { apiKey: typeof rawKey === 'string' ? rawKey : '' };\n },\n});\n"],"mappings":";;;AA+BA,MAAa,8BAA8B;AAC3C,MAAM,8BAA8B,QAAQ,IAAI;;;;;AAMhD,MAAM,kBAAkB;AAExB,MAAa,wBAAwB,wBAAwC;;;;;;;;;;;;AAa7E,SAAgB,kBAAkB,SAA0D;CACxF,MAAM,OAAO,QAAQ,IAAI,sBAAsB;AAC/C,KAAI,SAAS,MAAM;AAEf,UAAQ,KACJ,mKACH;AACD,SAAO,EAAE,QAAQ,IAAI;;AAEzB,QAAO;;;;;;;;;;AAWX,SAAgB,aAAa,SAAkD;AAC3E,QAAO,kBAAkB,QAAQ,CAAC;;;;;;;;;;;AAYtC,MAAa,2BAA2B,0BAA0C;CAC9E,UAAU;CACV,SAAS;CACT,eAAe,gCAAgC,aAAa,aAAa;CACzE,eAAe,EAAE,QAAQ,IAAI;CAC7B,YAAY,UAAU;EAClB,MAAM,SAAS,MAAM;AACrB,SAAO,EAAE,QAAQ,OAAO,WAAW,WAAW,SAAS,IAAI;;CAElE,CAAC"}
|
package/dist/utils.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { i as siteContext } from "./site-context2.js";
|
|
2
|
-
import { createContext } from "react-router";
|
|
3
|
-
import { DataStore, DataStoreNotFoundError, DataStoreServiceError, DataStoreUnavailableError } from "@salesforce/mrt-utilities/data-store";
|
|
4
|
-
|
|
5
|
-
//#region src/data-store/utils.ts
|
|
6
|
-
/**
|
|
7
|
-
* Creates a typed React Router context for data store entries.
|
|
8
|
-
*
|
|
9
|
-
* Initializes the context with `null` so middleware can populate it during requests.
|
|
10
|
-
*
|
|
11
|
-
* @returns React Router context key for data store values
|
|
12
|
-
*/
|
|
13
|
-
function createDataStoreContext() {
|
|
14
|
-
return createContext(null);
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Creates a data-store middleware that fetches site preferences from MRT data access layer
|
|
18
|
-
* and stores them in the router context.
|
|
19
|
-
*
|
|
20
|
-
* Environment variables:
|
|
21
|
-
* - `AWS_REGION` (required): AWS region for the data store table (e.g., "us-east-1")
|
|
22
|
-
* - `MOBIFY_PROPERTY_ID` (required): MRT property identifier (e.g., "abcd1234")
|
|
23
|
-
* - `DEPLOY_TARGET` (required): MRT deploy target (e.g., "production")
|
|
24
|
-
*
|
|
25
|
-
* @param options - Middleware options for data store entry and context
|
|
26
|
-
* @returns React Router middleware for server requests
|
|
27
|
-
*/
|
|
28
|
-
function createDataStoreMiddleware(options) {
|
|
29
|
-
const { entryKey, context: contextKey, onUnavailable = "throw", fallbackValue } = options;
|
|
30
|
-
const transform = options.transform ?? ((value) => value);
|
|
31
|
-
const dataStoreMiddleware = async ({ context }, next) => {
|
|
32
|
-
const resolvedEntryKey = typeof entryKey === "function" ? entryKey(context) : entryKey;
|
|
33
|
-
try {
|
|
34
|
-
const entry = await getDataStoreEntry(resolvedEntryKey);
|
|
35
|
-
if (!entry?.value || typeof entry.value !== "object") {
|
|
36
|
-
console.warn(`Data store entry '${resolvedEntryKey}' not found or invalid.`);
|
|
37
|
-
return next();
|
|
38
|
-
}
|
|
39
|
-
context.set(contextKey, transform(entry.value));
|
|
40
|
-
} catch (error) {
|
|
41
|
-
if (error instanceof DataStoreUnavailableError) {
|
|
42
|
-
if (onUnavailable === "fallback" && typeof fallbackValue !== "undefined") {
|
|
43
|
-
const resolvedFallbackValue = typeof fallbackValue === "function" ? fallbackValue(context) : fallbackValue;
|
|
44
|
-
context.set(contextKey, resolvedFallbackValue);
|
|
45
|
-
console.warn(`Data store unavailable for '${resolvedEntryKey}'. Using configured fallback value.`);
|
|
46
|
-
return next();
|
|
47
|
-
}
|
|
48
|
-
throw new Error("Data store is unavailable. Ensure AWS_REGION, MOBIFY_PROPERTY_ID, and DEPLOY_TARGET are set.");
|
|
49
|
-
}
|
|
50
|
-
if (error instanceof DataStoreNotFoundError) {
|
|
51
|
-
console.warn(`Data store entry '${resolvedEntryKey}' not found.`);
|
|
52
|
-
return next();
|
|
53
|
-
}
|
|
54
|
-
if (error instanceof DataStoreServiceError) throw new Error(`Data store request failed for '${resolvedEntryKey}'.`);
|
|
55
|
-
throw error;
|
|
56
|
-
}
|
|
57
|
-
return next();
|
|
58
|
-
};
|
|
59
|
-
return dataStoreMiddleware;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Read a data-store entry through the singleton MRT utilities API.
|
|
63
|
-
* The underlying implementation (production DynamoDB vs development pseudo store)
|
|
64
|
-
* is resolved by `@salesforce/mrt-utilities/data-store` export conditions.
|
|
65
|
-
*
|
|
66
|
-
* @param key - Data-store entry key
|
|
67
|
-
* @returns Data-store entry or null when missing/invalid shape
|
|
68
|
-
*/
|
|
69
|
-
async function getDataStoreEntry(key) {
|
|
70
|
-
const entry = await DataStore.getDataStore().getEntry(key);
|
|
71
|
-
if (!entry || typeof entry !== "object") return null;
|
|
72
|
-
return entry;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Creates an entryKey function that prefixes the given suffix with the current site ID.
|
|
76
|
-
*
|
|
77
|
-
* @param suffix - The entry key suffix (e.g., "custom-site-preferences")
|
|
78
|
-
* @returns A function compatible with `DataStoreMiddlewareOptions.entryKey`
|
|
79
|
-
*/
|
|
80
|
-
function prefixWithSiteId(suffix) {
|
|
81
|
-
return (context) => {
|
|
82
|
-
const siteId = context.get(siteContext)?.site?.id;
|
|
83
|
-
if (!siteId) throw new Error("Site id not found. Ensure site context middleware runs before data-store middleware.");
|
|
84
|
-
return `${siteId}-${suffix}`;
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
//#endregion
|
|
89
|
-
export { prefixWithSiteId as i, createDataStoreMiddleware as n, getDataStoreEntry as r, createDataStoreContext as t };
|
|
90
|
-
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","names":["dataStoreMiddleware: MiddlewareFunction<Response>"],"sources":["../src/data-store/utils.ts"],"sourcesContent":["/**\n * Copyright 2026 Salesforce, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { type MiddlewareFunction, type RouterContextProvider, createContext } from 'react-router';\nimport {\n DataStore,\n DataStoreNotFoundError,\n DataStoreServiceError,\n DataStoreUnavailableError,\n} from '@salesforce/mrt-utilities/data-store';\nimport { siteContext } from '../site-context';\n\nexport type DataStoreContextKey<T> = ReturnType<typeof createContext<T | null>>;\n\nexport type DataStoreEntryKey = string | ((context: Readonly<RouterContextProvider>) => string);\n\nexport type DataStoreEntry<TValue = unknown> = {\n value?: TValue;\n};\n\nexport type DataStoreMiddlewareOptions<T> = {\n entryKey: DataStoreEntryKey;\n context: DataStoreContextKey<T>;\n transform?: (value: Record<string, unknown>) => T;\n onUnavailable?: 'throw' | 'fallback';\n fallbackValue?: T | ((context: Readonly<RouterContextProvider>) => T);\n};\n\n/**\n * Creates a typed React Router context for data store entries.\n *\n * Initializes the context with `null` so middleware can populate it during requests.\n *\n * @returns React Router context key for data store values\n */\nexport function createDataStoreContext<T>(): DataStoreContextKey<T> {\n return createContext<T | null>(null);\n}\n\n/**\n * Creates a data-store middleware that fetches site preferences from MRT data access layer\n * and stores them in the router context.\n *\n * Environment variables:\n * - `AWS_REGION` (required): AWS region for the data store table (e.g., \"us-east-1\")\n * - `MOBIFY_PROPERTY_ID` (required): MRT property identifier (e.g., \"abcd1234\")\n * - `DEPLOY_TARGET` (required): MRT deploy target (e.g., \"production\")\n *\n * @param options - Middleware options for data store entry and context\n * @returns React Router middleware for server requests\n */\nexport function createDataStoreMiddleware<T>(options: DataStoreMiddlewareOptions<T>): MiddlewareFunction<Response> {\n const { entryKey, context: contextKey, onUnavailable = 'throw', fallbackValue } = options;\n const transform = options.transform ?? ((value: Record<string, unknown>) => value as T);\n\n const dataStoreMiddleware: MiddlewareFunction<Response> = async ({ context }, next) => {\n const resolvedEntryKey = typeof entryKey === 'function' ? entryKey(context) : entryKey;\n try {\n const entry = await getDataStoreEntry(resolvedEntryKey);\n\n if (!entry?.value || typeof entry.value !== 'object') {\n // eslint-disable-next-line no-console\n console.warn(`Data store entry '${resolvedEntryKey}' not found or invalid.`);\n return next();\n }\n context.set(contextKey, transform(entry.value as Record<string, unknown>));\n } catch (error) {\n if (error instanceof DataStoreUnavailableError) {\n if (onUnavailable === 'fallback' && typeof fallbackValue !== 'undefined') {\n const resolvedFallbackValue =\n typeof fallbackValue === 'function'\n ? (fallbackValue as (ctx: Readonly<RouterContextProvider>) => T)(context)\n : fallbackValue;\n context.set(contextKey, resolvedFallbackValue);\n // eslint-disable-next-line no-console\n console.warn(`Data store unavailable for '${resolvedEntryKey}'. Using configured fallback value.`);\n return next();\n }\n throw new Error(\n 'Data store is unavailable. Ensure AWS_REGION, MOBIFY_PROPERTY_ID, and DEPLOY_TARGET are set.'\n );\n }\n if (error instanceof DataStoreNotFoundError) {\n // eslint-disable-next-line no-console\n console.warn(`Data store entry '${resolvedEntryKey}' not found.`);\n return next();\n }\n if (error instanceof DataStoreServiceError) {\n throw new Error(`Data store request failed for '${resolvedEntryKey}'.`);\n }\n throw error;\n }\n\n return next();\n };\n\n return dataStoreMiddleware;\n}\n\n/**\n * Read a data-store entry through the singleton MRT utilities API.\n * The underlying implementation (production DynamoDB vs development pseudo store)\n * is resolved by `@salesforce/mrt-utilities/data-store` export conditions.\n *\n * @param key - Data-store entry key\n * @returns Data-store entry or null when missing/invalid shape\n */\nexport async function getDataStoreEntry<TValue = unknown>(key: string): Promise<DataStoreEntry<TValue> | null> {\n const entry = (await DataStore.getDataStore().getEntry(key)) as DataStoreEntry<TValue> | undefined;\n if (!entry || typeof entry !== 'object') {\n return null;\n }\n return entry;\n}\n\n/**\n * Creates an entryKey function that prefixes the given suffix with the current site ID.\n *\n * @param suffix - The entry key suffix (e.g., \"custom-site-preferences\")\n * @returns A function compatible with `DataStoreMiddlewareOptions.entryKey`\n */\nexport function prefixWithSiteId(suffix: string): (context: Readonly<RouterContextProvider>) => string {\n return (context) => {\n const siteId = context.get(siteContext)?.site?.id;\n if (!siteId)\n throw new Error('Site id not found. Ensure site context middleware runs before data-store middleware.');\n return `${siteId}-${suffix}`;\n };\n}\n"],"mappings":";;;;;;;;;;;;AAgDA,SAAgB,yBAAoD;AAChE,QAAO,cAAwB,KAAK;;;;;;;;;;;;;;AAexC,SAAgB,0BAA6B,SAAsE;CAC/G,MAAM,EAAE,UAAU,SAAS,YAAY,gBAAgB,SAAS,kBAAkB;CAClF,MAAM,YAAY,QAAQ,eAAe,UAAmC;CAE5E,MAAMA,sBAAoD,OAAO,EAAE,WAAW,SAAS;EACnF,MAAM,mBAAmB,OAAO,aAAa,aAAa,SAAS,QAAQ,GAAG;AAC9E,MAAI;GACA,MAAM,QAAQ,MAAM,kBAAkB,iBAAiB;AAEvD,OAAI,CAAC,OAAO,SAAS,OAAO,MAAM,UAAU,UAAU;AAElD,YAAQ,KAAK,qBAAqB,iBAAiB,yBAAyB;AAC5E,WAAO,MAAM;;AAEjB,WAAQ,IAAI,YAAY,UAAU,MAAM,MAAiC,CAAC;WACrE,OAAO;AACZ,OAAI,iBAAiB,2BAA2B;AAC5C,QAAI,kBAAkB,cAAc,OAAO,kBAAkB,aAAa;KACtE,MAAM,wBACF,OAAO,kBAAkB,aAClB,cAA8D,QAAQ,GACvE;AACV,aAAQ,IAAI,YAAY,sBAAsB;AAE9C,aAAQ,KAAK,+BAA+B,iBAAiB,qCAAqC;AAClG,YAAO,MAAM;;AAEjB,UAAM,IAAI,MACN,+FACH;;AAEL,OAAI,iBAAiB,wBAAwB;AAEzC,YAAQ,KAAK,qBAAqB,iBAAiB,cAAc;AACjE,WAAO,MAAM;;AAEjB,OAAI,iBAAiB,sBACjB,OAAM,IAAI,MAAM,kCAAkC,iBAAiB,IAAI;AAE3E,SAAM;;AAGV,SAAO,MAAM;;AAGjB,QAAO;;;;;;;;;;AAWX,eAAsB,kBAAoC,KAAqD;CAC3G,MAAM,QAAS,MAAM,UAAU,cAAc,CAAC,SAAS,IAAI;AAC3D,KAAI,CAAC,SAAS,OAAO,UAAU,SAC3B,QAAO;AAEX,QAAO;;;;;;;;AASX,SAAgB,iBAAiB,QAAsE;AACnG,SAAQ,YAAY;EAChB,MAAM,SAAS,QAAQ,IAAI,YAAY,EAAE,MAAM;AAC/C,MAAI,CAAC,OACD,OAAM,IAAI,MAAM,uFAAuF;AAC3G,SAAO,GAAG,OAAO,GAAG"}
|