EasyPack Work in Progress!
An EasyPack
is tidy way to create a feature or group of features in your EasyApp. An EasyPack
can contain:
Feature | Description |
---|---|
Actions | Actions are the main way to interact with your app |
Middleware | Middleware is used to perform tasks such as authentication, logging, or modifying the request or response |
Entities | Entities are used to define the structure of the data that is stored in the database |
Realtime Rooms | Realtime rooms are used to group together clients that are connected to the realtime server |
Creating an EasyPack
import { EasyPack } from "@vef/easy-app";
const myEasyPack = new EasyPack("myPackage", {
description: "This is my easy pack",
version: "1.0.0",
});
// Now add it to your app
app.addEasyPack(myEasyPack);
Adding stuff to your EasyPack
The EasyPack
class has a number of methods that you can use to add features to it:
Adding Entities
Entities are used to define the structure of the data that is stored in the database.
Check out the Entities page for more information.
There are two ways to add entities to your EasyPack
:
defineEntity
Define an entity directly in the EasyPack
.
Example
myEasyPack.defineEntity("user", {
label: "User",
fields: [
{
key: "username",
fieldType: "DataField",
label: "Username",
},
],
});
addEntity
Add an entity to the EasyPack. This is an alternative to defineEntity
that allows you to add an entity that you may have created elsewhere.
Typically this would be the case if you have many entities and want to organize their definitions in other files and import them here.
import { defineEntity } from "@vef/easy-orm";
import { EasyPack } from "@vef/easy-app";
const userEntity = defineEntity("user", {
label: "User",
fields: [
{
key: "username",
fieldType: "DataField",
label: "User Name",
},
],
});
const easyPack = new EasyPack("myPackage", {
description: "This is my easy pack",
version: "1.0.0",
});
easyPack.addEntity(userEntity);
Adding Actions
There are a number of methods that you can use to add actions to your EasyPack
. Check out the Actions page for more information.
Note: If the group does not exist, it will be created.
addAction
Add an action to the EasyPack and assign it to a group
easyPack.addAction({
groupName: "auth",
actionName: "login",
description: "Login to the app",
action: (app, { username, password }) => {
// your action code here
},
params: {
username: "DataField",
password: "PasswordField",
},
});
addActionToGroup
Add an action to a group. This is an alternative to addAction that allows you to add an action to a group that you may have created elsewhere.
Note
Typically this would be the case if you have many actions and want to organize their declarations in other files and import them here.
Define your action in a separate file and export it:
// someFile.ts
import { createAction } from "@vef/easy-app";
export const actionA = createAction("actionA", {
description: "Action A",
action: (app) => {
// your action code here
},
});
And then in your main.ts
file:
// main.ts
import { actionA } from "./someFile";
// Add the action to the group
easyPack.addActionToGroup("groupA", actionA);
addActionGroup
Add multiple actions to a group. This is useful if you want to add multiple actions to a group at once instead of callingaddAction multiple times.
import { createAction } from "@vef/easy-app";
// Create an array of actions
const actions = [
createAction("actionA", {
description: "Action A",
action: (app) => {
return "Action A";
},
response: "string",
}),
createAction("actionB", {
description: "Action B",
action: (app) => {
return "Action B";
},
response: "string",
}),
];
// Add both actions to the group
easyPack.addActionGroup("groupA", actions);
Adding Middleware
Add a middleware to the EasyPack. This middleware will be run before the api handler for the action.
Check out the Middleware page for more information.
There are two types of middleware:
- Middleware that gets handed the
EasyRequest
object (along with theEasyApp
object) - Middleware that gets handed the
EasyRequest
andEasyResponse
objects (along with theEasyApp
object) and returns theEasyResponse
object
easyPack.addMiddleware((app:EasyApp, easyRequest:EasyRequest) void => {
// your middleware code here
});
easyPack.addMiddleware((app:EasyApp, easyRequest:EasyRequest,
easyResponse:EasyResponse):EasyResponse => {
// your middleware code here
easyResponse.setCookie("myCookie", "someValue");
return easyResponse;
});
Adding Realtime Rooms
Add a realtime room to the EasyPack. Realtime rooms are used to group together clients that are connected to the realtime server.
Check out the Realtime Rooms page for more information.
easyPack.addRealtimeRoom({
roomName: "chat",
description: "A chat room",
events: ["message", "join", "leave"],
});