@vue-storefront/middleware
Advanced tools
Comparing version 3.7.1 to 3.8.0
@@ -214,5 +214,24 @@ 'use strict'; | ||
/** | ||
* Resolves the api function from the apiClient based on the extensionName and functionName parameters. | ||
* | ||
* @param apiClient | ||
* @param reqParams | ||
*/ | ||
const getApiFunction = (apiClient, functionName, extensionName) => { | ||
const apiFn = extensionName | ||
? apiClient?.api?.[extensionName]?.[functionName] | ||
: apiClient?.api?.[functionName]; | ||
if (!apiFn) { | ||
const errorMessage = extensionName | ||
? `Extension "${extensionName}" is not namespaced or the function "${functionName}" is not available in the namespace.` | ||
: `The function "${functionName}" is not registered.`; | ||
throw new Error(errorMessage); | ||
} | ||
return apiFn; | ||
}; | ||
function prepareApiFunction(integrations) { | ||
return (req, res, next) => { | ||
const { integrationName, functionName } = req.params; | ||
const { integrationName, extensionName, functionName } = req.params; | ||
if (!integrations || !integrations[integrationName]) { | ||
@@ -258,4 +277,11 @@ res.status(404); | ||
}); | ||
const apiFunction = apiClientInstance.api[functionName]; | ||
res.locals.apiFunction = apiFunction; | ||
// Pick the function from the namespaced if it exists, otherwise pick it from the shared integration | ||
try { | ||
res.locals.apiFunction = getApiFunction(apiClientInstance, functionName, extensionName); | ||
} | ||
catch (e) { | ||
res.status(404); | ||
res.send(e.message); | ||
return; | ||
} | ||
next(); | ||
@@ -322,4 +348,4 @@ }; | ||
consola__default["default"].success("Integrations loaded!"); | ||
app.post("/:integrationName/:functionName", prepareApiFunction(integrations), prepareErrorHandler(integrations), prepareArguments, callApiFunction); | ||
app.get("/:integrationName/:functionName", prepareApiFunction(integrations), prepareErrorHandler(integrations), prepareArguments, callApiFunction); | ||
app.post("/:integrationName/:extensionName?/:functionName", prepareApiFunction(integrations), prepareErrorHandler(integrations), prepareArguments, callApiFunction); | ||
app.get("/:integrationName/:extensionName?/:functionName", prepareApiFunction(integrations), prepareErrorHandler(integrations), prepareArguments, callApiFunction); | ||
app.get("/healthz", (_req, res) => { | ||
@@ -375,3 +401,2 @@ res.end("ok"); | ||
.map(({ hooks }) => hooks(this?.middleware?.req, this?.middleware?.res)); | ||
const extendedApis = rawExtensions.reduce((prev, { extendApiMethods }) => ({ ...prev, ...extendApiMethods }), customApi); | ||
const _config = lifecycles | ||
@@ -406,2 +431,20 @@ .filter((extension) => isFunction(extension?.beforeCreate)) | ||
const api = isApiFactory ? apiOrApiFactory(settings) : apiOrApiFactory; | ||
const namespacedExtensions = {}; | ||
let sharedExtensions = customApi; | ||
// If the extension is namespaced, we need to merge the extended api methods into the namespace | ||
// Otherwise, we can just merge the extended api methods into the api | ||
rawExtensions.forEach((extension) => { | ||
if (extension.isNamespaced) { | ||
namespacedExtensions[extension.name] = { | ||
...(namespacedExtensions?.[extension.name] ?? {}), | ||
...extension.extendApiMethods, | ||
}; | ||
} | ||
else { | ||
sharedExtensions = { | ||
...sharedExtensions, | ||
...extension.extendApiMethods, | ||
}; | ||
} | ||
}); | ||
/** | ||
@@ -418,10 +461,18 @@ * FIXME IN-3487 | ||
*/ | ||
const integrationApi = applyContextToApi(api, | ||
// @ts-expect-error see above | ||
const integrationApi = applyContextToApi(api, context, extensionHooks); | ||
const extensionsApi = applyContextToApi(extendedApis ?? {}, | ||
context, extensionHooks); | ||
const sharedExtensionsApi = applyContextToApi(sharedExtensions, | ||
// @ts-expect-error see above | ||
context, extensionHooks); | ||
const namespacedApi = {}; | ||
for (const [namespace, extension] of Object.entries(namespacedExtensions)) { | ||
namespacedApi[namespace] = applyContextToApi(extension, | ||
// @ts-expect-error see above | ||
context, extensionHooks); | ||
} | ||
const mergedApi = { | ||
...integrationApi, | ||
...extensionsApi, | ||
...sharedExtensionsApi, | ||
...namespacedApi, | ||
}; | ||
@@ -428,0 +479,0 @@ // api methods haven't been invoked yet, so we still have time to add them to the context |
@@ -202,5 +202,24 @@ import consola from 'consola'; | ||
/** | ||
* Resolves the api function from the apiClient based on the extensionName and functionName parameters. | ||
* | ||
* @param apiClient | ||
* @param reqParams | ||
*/ | ||
const getApiFunction = (apiClient, functionName, extensionName) => { | ||
const apiFn = extensionName | ||
? apiClient?.api?.[extensionName]?.[functionName] | ||
: apiClient?.api?.[functionName]; | ||
if (!apiFn) { | ||
const errorMessage = extensionName | ||
? `Extension "${extensionName}" is not namespaced or the function "${functionName}" is not available in the namespace.` | ||
: `The function "${functionName}" is not registered.`; | ||
throw new Error(errorMessage); | ||
} | ||
return apiFn; | ||
}; | ||
function prepareApiFunction(integrations) { | ||
return (req, res, next) => { | ||
const { integrationName, functionName } = req.params; | ||
const { integrationName, extensionName, functionName } = req.params; | ||
if (!integrations || !integrations[integrationName]) { | ||
@@ -246,4 +265,11 @@ res.status(404); | ||
}); | ||
const apiFunction = apiClientInstance.api[functionName]; | ||
res.locals.apiFunction = apiFunction; | ||
// Pick the function from the namespaced if it exists, otherwise pick it from the shared integration | ||
try { | ||
res.locals.apiFunction = getApiFunction(apiClientInstance, functionName, extensionName); | ||
} | ||
catch (e) { | ||
res.status(404); | ||
res.send(e.message); | ||
return; | ||
} | ||
next(); | ||
@@ -310,4 +336,4 @@ }; | ||
consola.success("Integrations loaded!"); | ||
app.post("/:integrationName/:functionName", prepareApiFunction(integrations), prepareErrorHandler(integrations), prepareArguments, callApiFunction); | ||
app.get("/:integrationName/:functionName", prepareApiFunction(integrations), prepareErrorHandler(integrations), prepareArguments, callApiFunction); | ||
app.post("/:integrationName/:extensionName?/:functionName", prepareApiFunction(integrations), prepareErrorHandler(integrations), prepareArguments, callApiFunction); | ||
app.get("/:integrationName/:extensionName?/:functionName", prepareApiFunction(integrations), prepareErrorHandler(integrations), prepareArguments, callApiFunction); | ||
app.get("/healthz", (_req, res) => { | ||
@@ -363,3 +389,2 @@ res.end("ok"); | ||
.map(({ hooks }) => hooks(this?.middleware?.req, this?.middleware?.res)); | ||
const extendedApis = rawExtensions.reduce((prev, { extendApiMethods }) => ({ ...prev, ...extendApiMethods }), customApi); | ||
const _config = lifecycles | ||
@@ -394,2 +419,20 @@ .filter((extension) => isFunction(extension?.beforeCreate)) | ||
const api = isApiFactory ? apiOrApiFactory(settings) : apiOrApiFactory; | ||
const namespacedExtensions = {}; | ||
let sharedExtensions = customApi; | ||
// If the extension is namespaced, we need to merge the extended api methods into the namespace | ||
// Otherwise, we can just merge the extended api methods into the api | ||
rawExtensions.forEach((extension) => { | ||
if (extension.isNamespaced) { | ||
namespacedExtensions[extension.name] = { | ||
...(namespacedExtensions?.[extension.name] ?? {}), | ||
...extension.extendApiMethods, | ||
}; | ||
} | ||
else { | ||
sharedExtensions = { | ||
...sharedExtensions, | ||
...extension.extendApiMethods, | ||
}; | ||
} | ||
}); | ||
/** | ||
@@ -406,10 +449,18 @@ * FIXME IN-3487 | ||
*/ | ||
const integrationApi = applyContextToApi(api, | ||
// @ts-expect-error see above | ||
const integrationApi = applyContextToApi(api, context, extensionHooks); | ||
const extensionsApi = applyContextToApi(extendedApis ?? {}, | ||
context, extensionHooks); | ||
const sharedExtensionsApi = applyContextToApi(sharedExtensions, | ||
// @ts-expect-error see above | ||
context, extensionHooks); | ||
const namespacedApi = {}; | ||
for (const [namespace, extension] of Object.entries(namespacedExtensions)) { | ||
namespacedApi[namespace] = applyContextToApi(extension, | ||
// @ts-expect-error see above | ||
context, extensionHooks); | ||
} | ||
const mergedApi = { | ||
...integrationApi, | ||
...extensionsApi, | ||
...sharedExtensionsApi, | ||
...namespacedApi, | ||
}; | ||
@@ -416,0 +467,0 @@ // api methods haven't been invoked yet, so we still have time to add them to the context |
@@ -36,2 +36,3 @@ import type { Express, Request, Response } from "express"; | ||
name: string; | ||
isNamespaced?: boolean; | ||
extendApiMethods?: ExtendApiMethod<API, CONTEXT>; | ||
@@ -38,0 +39,0 @@ extendApp?: ({ app, configuration, }: { |
{ | ||
"name": "@vue-storefront/middleware", | ||
"version": "3.7.1", | ||
"version": "3.8.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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
75945
62
1274