mirror of
https://github.com/vercel/commerce.git
synced 2025-07-25 19:21:23 +00:00
Add Spree as allowed Framework
This commit is contained in:
43
framework/spree/utils/forceIsomorphicConfigValues.ts
Normal file
43
framework/spree/utils/forceIsomorphicConfigValues.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import type { NonUndefined, UnknownObjectValues } from '../types'
|
||||
import MisconfigurationError from '../errors/MisconfigurationError'
|
||||
import isServer from './isServer'
|
||||
|
||||
const generateMisconfigurationErrorMessage = (
|
||||
keys: Array<string | number | symbol>
|
||||
) => `${keys.join(', ')} must have values before running the Framework.`
|
||||
|
||||
const forceIsomorphicConfigValues = <
|
||||
X extends keyof T,
|
||||
T extends UnknownObjectValues,
|
||||
H extends Record<X, NonUndefined<T[X]>>
|
||||
>(
|
||||
config: T,
|
||||
requiredServerKeys: string[],
|
||||
requiredPublicKeys: X[]
|
||||
) => {
|
||||
if (isServer) {
|
||||
const missingServerConfigValues = requiredServerKeys.filter(
|
||||
(requiredServerKey) => typeof config[requiredServerKey] === 'undefined'
|
||||
)
|
||||
|
||||
if (missingServerConfigValues.length > 0) {
|
||||
throw new MisconfigurationError(
|
||||
generateMisconfigurationErrorMessage(missingServerConfigValues)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const missingPublicConfigValues = requiredPublicKeys.filter(
|
||||
(requiredPublicKey) => typeof config[requiredPublicKey] === 'undefined'
|
||||
)
|
||||
|
||||
if (missingPublicConfigValues.length > 0) {
|
||||
throw new MisconfigurationError(
|
||||
generateMisconfigurationErrorMessage(missingPublicConfigValues)
|
||||
)
|
||||
}
|
||||
|
||||
return config as T & H
|
||||
}
|
||||
|
||||
export default forceIsomorphicConfigValues
|
1
framework/spree/utils/isServer.ts
Normal file
1
framework/spree/utils/isServer.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default typeof window === 'undefined'
|
16
framework/spree/utils/requireConfig.ts
Normal file
16
framework/spree/utils/requireConfig.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import MissingConfigurationValueError from '../errors/MissingConfigurationValueError'
|
||||
import type { NonUndefined, ValueOf } from '../types'
|
||||
|
||||
const requireConfig = <T>(isomorphicConfig: T, key: keyof T) => {
|
||||
const valueUnderKey = isomorphicConfig[key]
|
||||
|
||||
if (typeof valueUnderKey === 'undefined') {
|
||||
throw new MissingConfigurationValueError(
|
||||
`Value for configuration key ${key} was undefined.`
|
||||
)
|
||||
}
|
||||
|
||||
return valueUnderKey as NonUndefined<ValueOf<T>>
|
||||
}
|
||||
|
||||
export default requireConfig
|
Reference in New Issue
Block a user