@vue-storefront/middleware
Advanced tools
Comparing version 3.4.0 to 3.5.0
@@ -193,2 +193,70 @@ 'use strict'; | ||
async function callApiFunction(req, res) { | ||
const { apiFunction, args, errorHandler } = res.locals; | ||
try { | ||
const platformResponse = await apiFunction(...args); | ||
res.send(platformResponse); | ||
} | ||
catch (error) { | ||
errorHandler(error, req, res); | ||
} | ||
} | ||
function prepareApiFunction(integrations) { | ||
return (req, res, next) => { | ||
const { integrationName, functionName } = req.params; | ||
if (!integrations || !integrations[integrationName]) { | ||
res.status(404); | ||
res.send(`"${integrationName}" integration is not configured. Please, check the request path or integration configuration.`); | ||
return; | ||
} | ||
const { apiClient, configuration, extensions, customQueries = {}, initConfig } = integrations[integrationName]; | ||
const middlewareContext = { | ||
req, | ||
res, | ||
extensions, | ||
customQueries, | ||
}; | ||
const createApiClient = apiClient.createApiClient.bind({ | ||
middleware: middlewareContext, | ||
}); | ||
const apiClientInstance = createApiClient({ | ||
...configuration, | ||
...initConfig, | ||
}); | ||
const apiFunction = apiClientInstance.api[functionName]; | ||
res.locals.apiFunction = apiFunction; | ||
next(); | ||
}; | ||
} | ||
function prepareErrorHandler(integrations) { | ||
return (req, res, next) => { | ||
const { integrationName } = req.params; | ||
const { errorHandler } = integrations[integrationName]; | ||
res.locals.errorHandler = errorHandler; | ||
next(); | ||
}; | ||
} | ||
function prepareArguments(req, res, next) { | ||
const { method, query, body } = req; | ||
let args; | ||
if (method === 'GET') { | ||
/** | ||
* Falling back to empty object to mimic | ||
* the behavior of express.json() middleware | ||
* when no POST body is provided. | ||
*/ | ||
const { body: queryBody = '{}' } = query; | ||
args = JSON.parse(queryBody); | ||
} | ||
else { | ||
args = body; | ||
} | ||
const argsArray = Symbol.iterator in Object(args) ? args : [args]; | ||
res.locals.args = argsArray; | ||
next(); | ||
} | ||
const app = express__default["default"](); | ||
@@ -221,36 +289,5 @@ app.use(express__default["default"].json()); | ||
consola__default["default"].success('Integrations loaded!'); | ||
app.post('/:integrationName/:functionName', async (req, res) => { | ||
const { integrationName, functionName } = req.params; | ||
if (!integrations || !integrations[integrationName]) { | ||
res.status(404); | ||
res.send(`"${integrationName}" integration is not configured. Please, check the request path or integration configuration.`); | ||
return; | ||
} | ||
const { apiClient, configuration, extensions, customQueries = {}, initConfig, errorHandler, } = integrations[integrationName]; | ||
const middlewareContext = { | ||
req, | ||
res, | ||
extensions, | ||
customQueries, | ||
}; | ||
const createApiClient = apiClient.createApiClient.bind({ | ||
middleware: middlewareContext, | ||
}); | ||
const apiClientInstance = createApiClient({ | ||
...configuration, | ||
...initConfig, | ||
}); | ||
const apiFunction = apiClientInstance.api[functionName]; | ||
try { | ||
if (!(Symbol.iterator in Object(req.body))) { | ||
req.body = [req.body]; | ||
} | ||
const platformResponse = await apiFunction(...req.body); | ||
res.send(platformResponse); | ||
} | ||
catch (error) { | ||
errorHandler(error, req, res); | ||
} | ||
}); | ||
app.get('/healthz', (req, res) => { | ||
app.post('/:integrationName/:functionName', prepareApiFunction(integrations), prepareErrorHandler(integrations), prepareArguments, callApiFunction); | ||
app.get('/:integrationName/:functionName', prepareApiFunction(integrations), prepareErrorHandler(integrations), prepareArguments, callApiFunction); | ||
app.get('/healthz', (_req, res) => { | ||
res.end('ok'); | ||
@@ -257,0 +294,0 @@ }); |
@@ -181,2 +181,70 @@ import consola from 'consola'; | ||
async function callApiFunction(req, res) { | ||
const { apiFunction, args, errorHandler } = res.locals; | ||
try { | ||
const platformResponse = await apiFunction(...args); | ||
res.send(platformResponse); | ||
} | ||
catch (error) { | ||
errorHandler(error, req, res); | ||
} | ||
} | ||
function prepareApiFunction(integrations) { | ||
return (req, res, next) => { | ||
const { integrationName, functionName } = req.params; | ||
if (!integrations || !integrations[integrationName]) { | ||
res.status(404); | ||
res.send(`"${integrationName}" integration is not configured. Please, check the request path or integration configuration.`); | ||
return; | ||
} | ||
const { apiClient, configuration, extensions, customQueries = {}, initConfig } = integrations[integrationName]; | ||
const middlewareContext = { | ||
req, | ||
res, | ||
extensions, | ||
customQueries, | ||
}; | ||
const createApiClient = apiClient.createApiClient.bind({ | ||
middleware: middlewareContext, | ||
}); | ||
const apiClientInstance = createApiClient({ | ||
...configuration, | ||
...initConfig, | ||
}); | ||
const apiFunction = apiClientInstance.api[functionName]; | ||
res.locals.apiFunction = apiFunction; | ||
next(); | ||
}; | ||
} | ||
function prepareErrorHandler(integrations) { | ||
return (req, res, next) => { | ||
const { integrationName } = req.params; | ||
const { errorHandler } = integrations[integrationName]; | ||
res.locals.errorHandler = errorHandler; | ||
next(); | ||
}; | ||
} | ||
function prepareArguments(req, res, next) { | ||
const { method, query, body } = req; | ||
let args; | ||
if (method === 'GET') { | ||
/** | ||
* Falling back to empty object to mimic | ||
* the behavior of express.json() middleware | ||
* when no POST body is provided. | ||
*/ | ||
const { body: queryBody = '{}' } = query; | ||
args = JSON.parse(queryBody); | ||
} | ||
else { | ||
args = body; | ||
} | ||
const argsArray = Symbol.iterator in Object(args) ? args : [args]; | ||
res.locals.args = argsArray; | ||
next(); | ||
} | ||
const app = express(); | ||
@@ -209,36 +277,5 @@ app.use(express.json()); | ||
consola.success('Integrations loaded!'); | ||
app.post('/:integrationName/:functionName', async (req, res) => { | ||
const { integrationName, functionName } = req.params; | ||
if (!integrations || !integrations[integrationName]) { | ||
res.status(404); | ||
res.send(`"${integrationName}" integration is not configured. Please, check the request path or integration configuration.`); | ||
return; | ||
} | ||
const { apiClient, configuration, extensions, customQueries = {}, initConfig, errorHandler, } = integrations[integrationName]; | ||
const middlewareContext = { | ||
req, | ||
res, | ||
extensions, | ||
customQueries, | ||
}; | ||
const createApiClient = apiClient.createApiClient.bind({ | ||
middleware: middlewareContext, | ||
}); | ||
const apiClientInstance = createApiClient({ | ||
...configuration, | ||
...initConfig, | ||
}); | ||
const apiFunction = apiClientInstance.api[functionName]; | ||
try { | ||
if (!(Symbol.iterator in Object(req.body))) { | ||
req.body = [req.body]; | ||
} | ||
const platformResponse = await apiFunction(...req.body); | ||
res.send(platformResponse); | ||
} | ||
catch (error) { | ||
errorHandler(error, req, res); | ||
} | ||
}); | ||
app.get('/healthz', (req, res) => { | ||
app.post('/:integrationName/:functionName', prepareApiFunction(integrations), prepareErrorHandler(integrations), prepareArguments, callApiFunction); | ||
app.get('/:integrationName/:functionName', prepareApiFunction(integrations), prepareErrorHandler(integrations), prepareArguments, callApiFunction); | ||
app.get('/healthz', (_req, res) => { | ||
res.end('ok'); | ||
@@ -245,0 +282,0 @@ }); |
{ | ||
"name": "@vue-storefront/middleware", | ||
"version": "3.4.0", | ||
"version": "3.5.0", | ||
"main": "lib/index.cjs.js", | ||
@@ -5,0 +5,0 @@ "module": "lib/index.es.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
65986
58
1057
12143