-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathutils.ts
More file actions
70 lines (63 loc) · 2.33 KB
/
utils.ts
File metadata and controls
70 lines (63 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Copyright (c) 2023, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {ApiClients} from '../../hooks/types'
import {DEVELOPMENT_ORIGIN, getParentOrigin, isOriginTrusted} from '../../utils'
const LOCAL_BUNDLE_PATH = `/mobify/bundle/development`
/** Detects whether the storefront is running in an iframe as part of Storefront Preview.
* @private
*/
export const detectStorefrontPreview = () => {
const parentOrigin = getParentOrigin()
return isOriginTrusted(parentOrigin)
}
/**
* Returns the URL to load the Storefront Preview client script from the parent origin.
* The client script is served from Runtime Admin and is not a part of the PWA Retail React App bundle.
* @private
*/
export const getClientScript = () => {
const parentOrigin = getParentOrigin() ?? 'https://runtime.commercecloud.com'
return parentOrigin === DEVELOPMENT_ORIGIN
? `${parentOrigin}${LOCAL_BUNDLE_PATH}/static/storefront-preview.js`
: `${parentOrigin}/cc/b2c/preview/preview.client.js`
}
// Custom Prop Types.
export const CustomPropTypes = {
/**
* This custom PropType ensures that the prop is only required when the known prop
* "enabled" is set to "true".
*
* @param props
* @param propName
* @param componentName
* @returns
*/
requiredFunctionWhenEnabled: (props: any, propName: any, componentName: any) => {
if (
props['enabled'] === true &&
(props[propName] === undefined || typeof props[propName] !== 'function')
) {
return new Error(
`${String(propName)} is a required function for ${String(
componentName
)} when enabled is true`
)
}
}
}
/**
* Via the built-in Proxy object, modify the behaviour of each request for the given SCAPI clients
* @private
*/
export const proxyRequests = (clients: ApiClients, handlers: ProxyHandler<any>) => {
Object.values(clients).forEach((client: Record<string, any>) => {
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(client))
methods.forEach((method) => {
client[method] = new Proxy(client[method], handlers)
})
})
}