Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adobe/plugin-hooks",
"version": "0.3.3",
"version": "0.3.4",
"publishConfig": {
"access": "public"
},
Expand Down
17 changes: 15 additions & 2 deletions src/handleBeforeAllHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,40 @@ governing permissions and limitations under the License.
*/

const {
importFn,
isModuleFn,
isRemoteFn,
invokeLocalFunction,
invokeLocalModuleFunction,
invokeRemoteFunction,
importFn,
} = require("./utils");

const handleBeforeAllHooks = (fnBuildConfig) => async (fnExecConfig) => {
try {
const { memoizedFns, baseDir, logger, beforeAll } = fnBuildConfig;
const { payload, updateContext } = fnExecConfig;
let beforeAllFn = null;
let beforeAllFn;

if (!memoizedFns.beforeAll) {
if (isRemoteFn(beforeAll.composer)) {
// Invoke remote endpoint
beforeAllFn = await invokeRemoteFunction(beforeAll.composer, {
baseDir,
importFn,
logger,
blocking: beforeAll.blocking,
});
} else if (isModuleFn(beforeAll)) {
// Invoke function from imported module. This handles bundled scenarios such as local development where the
// module needs to be known statically at build time.
beforeAllFn = await invokeLocalModuleFunction(beforeAll.module, beforeAll.fn, {
baseDir,
importFn,
logger,
blocking: beforeAll.blocking,
});
} else {
// Invoke local function at runtime
beforeAllFn = await invokeLocalFunction(beforeAll.composer, {
baseDir,
importFn,
Expand Down
105 changes: 105 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,100 @@ const invokeRemoteFunction = async (url, metaConfig) => (data) => {
}
};

/**
* Invoke local module function. Used in situations where the module needs to be imported at build time such as in
* bundled use cases like local development.
* @param mod Imported/required JavaScript module.
* @param functionName Function name.
* @param metaConfig Meta configuration
* @returns {Promise<function(*): Promise<unknown>>}
*/
const invokeLocalModuleFunction = async (mod, functionName, metaConfig) => {
const { logger, blocking } = metaConfig;

const exportName = functionName || 'default'

let composerFn = null;

try {
composerFn = mod[exportName] || (mod.default && mod.default[exportName]) || mod.default || mod;
} catch (err) {
logger.error("error while invoking local function %s", composerFn);
logger.error(err);

return Promise.reject({
status: "ERROR",
message:
err.message || `Unable to invoke local function ${composerFn}`,
});
}

return (data) => {
return new Promise((resolve, reject) => {
try {
if (!composerFn) {
reject({
status: "ERROR",
message: `Unable to invoke local function ${composerFn}`,
});
}

logger.debug("Invoking local fn %o", composerFn);

const result = composerFn(data);

if (blocking) {
if (result instanceof Promise) {
timedPromise(result)
.then((res) => {
if (res.status.toUpperCase() === "SUCCESS") {
resolve(res);
} else {
reject(res);
}
})
.catch((error) => {
logger.error(
"error while invoking local function %o",
composerFn
);
logger.error(error);

reject({
status: "ERROR",
message:
error.message ||
`Error while invoking local function ${composerFn}`,
});
});
} else {
if (result.status.toUpperCase() === "SUCCESS") {
resolve(result);
} else {
reject(result);
}
}
} else {
resolve({
status: "SUCCESS",
message: "Local function invoked successfully",
});
}
} catch (error) {
logger.error("error while invoking local function %o", composerFn);
logger.error(error);

reject({
status: "ERROR",
message:
error.message ||
`Error while invoking local function ${composerFn}`,
});
}
});
};
};

const invokeLocalFunction = async (composerFnPath, metaConfig) => {
const { baseDir, logger, importFn, blocking } = metaConfig;

Expand Down Expand Up @@ -267,9 +361,20 @@ const isRemoteFn = (composer) => {
return urlRegex.test(composer);
};

/**
* Whether beforeAll has module reference.
* @param beforeAll
* @returns {boolean}
*/
const isModuleFn = (beforeAll) => {
return !!(beforeAll.module && beforeAll.fn);
}

module.exports = {
invokeRemoteFunction,
invokeLocalModuleFunction,
invokeLocalFunction,
isRemoteFn,
isModuleFn,
importFn,
};
Loading