106 lines
3.0 KiB
JavaScript
106 lines
3.0 KiB
JavaScript
var plugins = [{
|
|
name: 'gatsby-plugin-react-helmet',
|
|
plugin: require('/Users/oliverjuhl/Desktop/medusa/core/www/reference/node_modules/gatsby-plugin-react-helmet/gatsby-ssr'),
|
|
options: {"plugins":[]},
|
|
},{
|
|
name: 'gatsby-remark-autolink-headers',
|
|
plugin: require('/Users/oliverjuhl/Desktop/medusa/core/www/reference/node_modules/gatsby-remark-autolink-headers/gatsby-ssr'),
|
|
options: {"plugins":[],"elements":["h2","h3","h4"],"offsetY":0,"className":"anchor"},
|
|
},{
|
|
name: 'gatsby-plugin-theme-ui',
|
|
plugin: require('/Users/oliverjuhl/Desktop/medusa/core/www/reference/node_modules/gatsby-plugin-theme-ui/gatsby-ssr'),
|
|
options: {"plugins":[]},
|
|
},{
|
|
name: 'default-site-plugin',
|
|
plugin: require('/Users/oliverjuhl/Desktop/medusa/core/www/reference/gatsby-ssr'),
|
|
options: {"plugins":[]},
|
|
}]
|
|
/* global plugins */
|
|
// During bootstrap, we write requires at top of this file which looks like:
|
|
// var plugins = [
|
|
// {
|
|
// plugin: require("/path/to/plugin1/gatsby-ssr.js"),
|
|
// options: { ... },
|
|
// },
|
|
// {
|
|
// plugin: require("/path/to/plugin2/gatsby-ssr.js"),
|
|
// options: { ... },
|
|
// },
|
|
// ]
|
|
|
|
const apis = require(`./api-ssr-docs`)
|
|
|
|
function augmentErrorWithPlugin(plugin, err) {
|
|
if (plugin.name !== `default-site-plugin`) {
|
|
// default-site-plugin is user code and will print proper stack trace,
|
|
// so no point in annotating error message pointing out which plugin is root of the problem
|
|
err.message += ` (from plugin: ${plugin.name})`
|
|
}
|
|
|
|
throw err
|
|
}
|
|
|
|
export function apiRunner(api, args, defaultReturn, argTransform) {
|
|
if (!apis[api]) {
|
|
console.log(`This API doesn't exist`, api)
|
|
}
|
|
|
|
const results = []
|
|
plugins.forEach(plugin => {
|
|
const apiFn = plugin.plugin[api]
|
|
if (!apiFn) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
const result = apiFn(args, plugin.options)
|
|
|
|
if (result && argTransform) {
|
|
args = argTransform({ args, result })
|
|
}
|
|
|
|
// This if case keeps behaviour as before, we should allow undefined here as the api is defined
|
|
// TODO V4
|
|
if (typeof result !== `undefined`) {
|
|
results.push(result)
|
|
}
|
|
} catch (e) {
|
|
augmentErrorWithPlugin(plugin, e)
|
|
}
|
|
})
|
|
|
|
return results.length ? results : [defaultReturn]
|
|
}
|
|
|
|
export async function apiRunnerAsync(api, args, defaultReturn, argTransform) {
|
|
if (!apis[api]) {
|
|
console.log(`This API doesn't exist`, api)
|
|
}
|
|
|
|
const results = []
|
|
for (const plugin of plugins) {
|
|
const apiFn = plugin.plugin[api]
|
|
if (!apiFn) {
|
|
continue
|
|
}
|
|
|
|
try {
|
|
const result = await apiFn(args, plugin.options)
|
|
|
|
if (result && argTransform) {
|
|
args = argTransform({ args, result })
|
|
}
|
|
|
|
// This if case keeps behaviour as before, we should allow undefined here as the api is defined
|
|
// TODO V4
|
|
if (typeof result !== `undefined`) {
|
|
results.push(result)
|
|
}
|
|
} catch (e) {
|
|
augmentErrorWithPlugin(plugin, e)
|
|
}
|
|
}
|
|
|
|
return results.length ? results : [defaultReturn]
|
|
}
|