@chec/commercerays-plugin
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -185,3 +185,45 @@ var Commerce = require('@chec/commerce.js'); | ||
function createGetStaticProps(schema) { | ||
var addMissingValues = (function (context, schema, defaults) { | ||
var missingAttributes = {}; // Function that generates a loop. This exists because it's a recursive loop | ||
var createLoop = function createLoop(prefix) { | ||
if (prefix === void 0) { | ||
prefix = ''; | ||
} | ||
return function (object) { | ||
var key = "" + prefix + object.key; // Deal with sub-schema | ||
if (Object.hasOwnProperty.call(object, 'schema')) { | ||
// Reduce the sub-schema (with this reducer), using the current accumulator as a base | ||
return object.schema.forEach(createLoop(key + ".")); | ||
} // Check if the value exists in context | ||
var value = get__default['default'](context, key); // Skip if the value exists and it's not an image | ||
if (object.type !== 'image' && value !== undefined) { | ||
return; | ||
} // Skip images if they have an ID (ie. has come from chec assets API) | ||
if (object.type === 'image' && value && typeof value === 'object' && value.id) { | ||
return; | ||
} // Grab a value from defaults to use in-place | ||
set__default['default'](missingAttributes, key, get__default['default'](defaults, key)); | ||
}; | ||
}; | ||
schema.schema.forEach(createLoop()); // Apply the new values to our context and return that | ||
return defaultsDeep__default['default'](missingAttributes, context); | ||
}); | ||
function createGetStaticProps(schema, defaults) { | ||
if (defaults === void 0) { | ||
defaults = {}; | ||
} | ||
return function () { | ||
@@ -202,6 +244,10 @@ try { | ||
return preview ? _temp(null) : Promise.resolve(getSavedContext(process.env.COMMERCEJS_RAY_ID, process.env.CHEC_SECRET_KEY, domain).then(function (context) { | ||
return preview ? _temp(null) : Promise.resolve(getSavedContext(process.env.COMMERCEJS_RAY_ID, process.env.CHEC_SECRET_KEY, domain) // Convert IDs from the API into actual resources using Commerce.js | ||
.then(function (context) { | ||
return context && resolveResourcesInContext(new Commerce__default['default'](process.env.NEXT_PUBLIC_CHEC_PUBLIC_KEY, false, { | ||
url: domain | ||
}), schema, context); | ||
}) // Add in any values that are defined in schema but missing from the loaded context, falling back to the defaults | ||
.then(function (context) { | ||
return addMissingValues(context, schema, defaults); | ||
})).then(_temp); | ||
@@ -232,3 +278,7 @@ }; | ||
var augmentStaticProps = function augmentStaticProps(schema) { | ||
var augmentStaticProps = function augmentStaticProps(schema, defaults) { | ||
if (defaults === void 0) { | ||
defaults = {}; | ||
} | ||
return function (staticProps) { | ||
@@ -240,6 +290,6 @@ if (staticProps === void 0) { | ||
try { | ||
var getStaticProps = createGetStaticProps(schema); | ||
return Promise.resolve(getStaticProps()).then(function (defaults) { | ||
return _extends({}, staticProps, defaults, { | ||
props: _extends({}, staticProps.props || {}, defaults.props) | ||
var getStaticProps = createGetStaticProps(schema, defaults); | ||
return Promise.resolve(getStaticProps()).then(function (parentProps) { | ||
return _extends({}, staticProps, parentProps, { | ||
props: _extends({}, staticProps.props || {}, parentProps.props) | ||
}); | ||
@@ -246,0 +296,0 @@ }); |
@@ -136,3 +136,35 @@ import Commerce from '@chec/commerce.js'; | ||
function createGetStaticProps(schema) { | ||
var addMissingValues = ((context, schema, defaults) => { | ||
const missingAttributes = {}; // Function that generates a loop. This exists because it's a recursive loop | ||
const createLoop = (prefix = '') => object => { | ||
const key = `${prefix}${object.key}`; // Deal with sub-schema | ||
if (Object.hasOwnProperty.call(object, 'schema')) { | ||
// Reduce the sub-schema (with this reducer), using the current accumulator as a base | ||
return object.schema.forEach(createLoop(`${key}.`)); | ||
} // Check if the value exists in context | ||
const value = get(context, key); // Skip if the value exists and it's not an image | ||
if (object.type !== 'image' && value !== undefined) { | ||
return; | ||
} // Skip images if they have an ID (ie. has come from chec assets API) | ||
if (object.type === 'image' && value && typeof value === 'object' && value.id) { | ||
return; | ||
} // Grab a value from defaults to use in-place | ||
set(missingAttributes, key, get(defaults, key)); | ||
}; | ||
schema.schema.forEach(createLoop()); // Apply the new values to our context and return that | ||
return defaultsDeep(missingAttributes, context); | ||
}); | ||
function createGetStaticProps(schema, defaults = {}) { | ||
return async () => { | ||
@@ -150,5 +182,7 @@ const preview = process.env.COMMERCEJS_RAYS_MODE === 'preview'; | ||
const rayContext = preview ? null : await getSavedContext(process.env.COMMERCEJS_RAY_ID, process.env.CHEC_SECRET_KEY, domain).then(context => context && resolveResourcesInContext(new Commerce(process.env.NEXT_PUBLIC_CHEC_PUBLIC_KEY, false, { | ||
const rayContext = preview ? null : await getSavedContext(process.env.COMMERCEJS_RAY_ID, process.env.CHEC_SECRET_KEY, domain) // Convert IDs from the API into actual resources using Commerce.js | ||
.then(context => context && resolveResourcesInContext(new Commerce(process.env.NEXT_PUBLIC_CHEC_PUBLIC_KEY, false, { | ||
url: domain | ||
}), schema, context)); | ||
}), schema, context)) // Add in any values that are defined in schema but missing from the loaded context, falling back to the defaults | ||
.then(context => addMissingValues(context, schema, defaults)); | ||
return { | ||
@@ -166,7 +200,7 @@ props: { | ||
const augmentStaticProps = schema => async (staticProps = {}) => { | ||
const getStaticProps = createGetStaticProps(schema); | ||
const defaults = await getStaticProps(); | ||
return _extends({}, staticProps, defaults, { | ||
props: _extends({}, staticProps.props || {}, defaults.props) | ||
const augmentStaticProps = (schema, defaults = {}) => async (staticProps = {}) => { | ||
const getStaticProps = createGetStaticProps(schema, defaults); | ||
const parentProps = await getStaticProps(); | ||
return _extends({}, staticProps, parentProps, { | ||
props: _extends({}, staticProps.props || {}, parentProps.props) | ||
}); | ||
@@ -173,0 +207,0 @@ }; |
@@ -175,3 +175,45 @@ import Commerce from '@chec/commerce.js'; | ||
function createGetStaticProps(schema) { | ||
var addMissingValues = (function (context, schema, defaults) { | ||
var missingAttributes = {}; // Function that generates a loop. This exists because it's a recursive loop | ||
var createLoop = function createLoop(prefix) { | ||
if (prefix === void 0) { | ||
prefix = ''; | ||
} | ||
return function (object) { | ||
var key = "" + prefix + object.key; // Deal with sub-schema | ||
if (Object.hasOwnProperty.call(object, 'schema')) { | ||
// Reduce the sub-schema (with this reducer), using the current accumulator as a base | ||
return object.schema.forEach(createLoop(key + ".")); | ||
} // Check if the value exists in context | ||
var value = get(context, key); // Skip if the value exists and it's not an image | ||
if (object.type !== 'image' && value !== undefined) { | ||
return; | ||
} // Skip images if they have an ID (ie. has come from chec assets API) | ||
if (object.type === 'image' && value && typeof value === 'object' && value.id) { | ||
return; | ||
} // Grab a value from defaults to use in-place | ||
set(missingAttributes, key, get(defaults, key)); | ||
}; | ||
}; | ||
schema.schema.forEach(createLoop()); // Apply the new values to our context and return that | ||
return defaultsDeep(missingAttributes, context); | ||
}); | ||
function createGetStaticProps(schema, defaults) { | ||
if (defaults === void 0) { | ||
defaults = {}; | ||
} | ||
return function () { | ||
@@ -192,6 +234,10 @@ try { | ||
return preview ? _temp(null) : Promise.resolve(getSavedContext(process.env.COMMERCEJS_RAY_ID, process.env.CHEC_SECRET_KEY, domain).then(function (context) { | ||
return preview ? _temp(null) : Promise.resolve(getSavedContext(process.env.COMMERCEJS_RAY_ID, process.env.CHEC_SECRET_KEY, domain) // Convert IDs from the API into actual resources using Commerce.js | ||
.then(function (context) { | ||
return context && resolveResourcesInContext(new Commerce(process.env.NEXT_PUBLIC_CHEC_PUBLIC_KEY, false, { | ||
url: domain | ||
}), schema, context); | ||
}) // Add in any values that are defined in schema but missing from the loaded context, falling back to the defaults | ||
.then(function (context) { | ||
return addMissingValues(context, schema, defaults); | ||
})).then(_temp); | ||
@@ -222,3 +268,7 @@ }; | ||
var augmentStaticProps = function augmentStaticProps(schema) { | ||
var augmentStaticProps = function augmentStaticProps(schema, defaults) { | ||
if (defaults === void 0) { | ||
defaults = {}; | ||
} | ||
return function (staticProps) { | ||
@@ -230,6 +280,6 @@ if (staticProps === void 0) { | ||
try { | ||
var getStaticProps = createGetStaticProps(schema); | ||
return Promise.resolve(getStaticProps()).then(function (defaults) { | ||
return _extends({}, staticProps, defaults, { | ||
props: _extends({}, staticProps.props || {}, defaults.props) | ||
var getStaticProps = createGetStaticProps(schema, defaults); | ||
return Promise.resolve(getStaticProps()).then(function (parentProps) { | ||
return _extends({}, staticProps, parentProps, { | ||
props: _extends({}, staticProps.props || {}, parentProps.props) | ||
}); | ||
@@ -236,0 +286,0 @@ }); |
@@ -181,3 +181,45 @@ (function (global, factory) { | ||
function createGetStaticProps(schema) { | ||
var addMissingValues = (function (context, schema, defaults) { | ||
var missingAttributes = {}; // Function that generates a loop. This exists because it's a recursive loop | ||
var createLoop = function createLoop(prefix) { | ||
if (prefix === void 0) { | ||
prefix = ''; | ||
} | ||
return function (object) { | ||
var key = "" + prefix + object.key; // Deal with sub-schema | ||
if (Object.hasOwnProperty.call(object, 'schema')) { | ||
// Reduce the sub-schema (with this reducer), using the current accumulator as a base | ||
return object.schema.forEach(createLoop(key + ".")); | ||
} // Check if the value exists in context | ||
var value = get__default['default'](context, key); // Skip if the value exists and it's not an image | ||
if (object.type !== 'image' && value !== undefined) { | ||
return; | ||
} // Skip images if they have an ID (ie. has come from chec assets API) | ||
if (object.type === 'image' && value && typeof value === 'object' && value.id) { | ||
return; | ||
} // Grab a value from defaults to use in-place | ||
set__default['default'](missingAttributes, key, get__default['default'](defaults, key)); | ||
}; | ||
}; | ||
schema.schema.forEach(createLoop()); // Apply the new values to our context and return that | ||
return defaultsDeep__default['default'](missingAttributes, context); | ||
}); | ||
function createGetStaticProps(schema, defaults) { | ||
if (defaults === void 0) { | ||
defaults = {}; | ||
} | ||
return function () { | ||
@@ -198,6 +240,10 @@ try { | ||
return preview ? _temp(null) : Promise.resolve(getSavedContext(process.env.COMMERCEJS_RAY_ID, process.env.CHEC_SECRET_KEY, domain).then(function (context) { | ||
return preview ? _temp(null) : Promise.resolve(getSavedContext(process.env.COMMERCEJS_RAY_ID, process.env.CHEC_SECRET_KEY, domain) // Convert IDs from the API into actual resources using Commerce.js | ||
.then(function (context) { | ||
return context && resolveResourcesInContext(new Commerce__default['default'](process.env.NEXT_PUBLIC_CHEC_PUBLIC_KEY, false, { | ||
url: domain | ||
}), schema, context); | ||
}) // Add in any values that are defined in schema but missing from the loaded context, falling back to the defaults | ||
.then(function (context) { | ||
return addMissingValues(context, schema, defaults); | ||
})).then(_temp); | ||
@@ -228,3 +274,7 @@ }; | ||
var augmentStaticProps = function augmentStaticProps(schema) { | ||
var augmentStaticProps = function augmentStaticProps(schema, defaults) { | ||
if (defaults === void 0) { | ||
defaults = {}; | ||
} | ||
return function (staticProps) { | ||
@@ -236,6 +286,6 @@ if (staticProps === void 0) { | ||
try { | ||
var getStaticProps = createGetStaticProps(schema); | ||
return Promise.resolve(getStaticProps()).then(function (defaults) { | ||
return _extends({}, staticProps, defaults, { | ||
props: _extends({}, staticProps.props || {}, defaults.props) | ||
var getStaticProps = createGetStaticProps(schema, defaults); | ||
return Promise.resolve(getStaticProps()).then(function (parentProps) { | ||
return _extends({}, staticProps, parentProps, { | ||
props: _extends({}, staticProps.props || {}, parentProps.props) | ||
}); | ||
@@ -242,0 +292,0 @@ }); |
{ | ||
"name": "@chec/commercerays-plugin", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Integrate directly with an editor within the Chec Dashboard - allowing customisation of your project by the merchant", | ||
@@ -5,0 +5,0 @@ "main": "dist/rays-plugin.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
182260
1475
4