action-kit
v1.0.0
Published
The Action Helper Library provides a framework for defining and handling actions in a Node.js environment. It enables you to encapsulate actions as functions and easily manage their invocation and error handling.
Downloads
3
Readme
Action Helper Library
The Action Helper Library provides a framework for defining and handling actions in a Node.js environment. It enables you to encapsulate actions as functions and easily manage their invocation and error handling.
Installation
To install the Action Helper Library, you can use npm:
npm install action-helper-library
Usage
Importing and Setting Up
import { ActionHelper } from "action-helper-library";
import { getClientAction } from "./path/actions/getClientAction.js";
import { withErrorsAction } from "./path/actions/withErrorsAction.js";
const actionHelper = new ActionHelper();
actionHelper.registerAction(getClientAction);
actionHelper.registerAction(withErrorsAction);
export function handler(event, context) {
return actionHelper.handle(event, context);
}
Defining Actions
withErrorsAction
import { ActionError, Action } from "action-helper-library";
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const withErrorsAction = new Action("withErrors", async function (
event,
context,
addError
) {
await sleep(2000);
addError(
new ActionError({ message: "clientnotfound" }, 404, "User not found")
);
});
export default withErrorsAction;
getClientAction
import { ActionResponse, Action } from "action-helper-library";
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const getClientAction = new Action(
"getClient",
async (event, context, addError) => {
await sleep(2000);
return new ActionResponse({ message: "OK" });
}
);
export default getClientAction;
Invoking the Handler
import { handler } from "./index.js";
const event = { body: { action: "getClient" } };
// const event = { body: { action: "withErrors" } };
// const event = { body: { action: "notFound" } };
const run = async () => {
const result = await handler(event, {});
console.log(result);
};
run();