IContext
IContext
is an important interface that holds many data during the HTTP Request-Response life cycle.
interface IContext {
api: IAPI;
database: Knex | Knex.Transaction;
handlerType: HandlerTypes;
isTransactionOpen: boolean;
model: IModelService;
parentModel: IModelService | null;
relation: IRelation | null;
req: AxeRequest;
res: AxeResponse;
version: IVersion;
conditions?: IQuery;
formData?: any;
item?: any;
params?: any;
result?: any;
query?: Knex.QueryBuilder;
}
It is able to be accessed in hook, event, and some middleware functions.
import { IContext } from "axe-api";
export default async (context: IContext) => {
// add your custom logic in here
};
api
The general API data. It contains the following items.
interface IAPI {
// The root folder path
rootFolder: string;
// The app folder path
appFolder: string;
// The all version definitions
versions: IVersion[];
// The application configuration
config: IApplicationConfig;
}
database
The database connection object. It is basically a Knex connection object.
handlerType
The current handlerType
in the HTTP request. The possible values are;
enum HandlerTypes {
INSERT = "store",
PAGINATE = "paginate",
SHOW = "show",
UPDATE = "update",
DELETE = "destroy",
FORCE_DELETE = "force_delete",
PATCH = "patch",
ALL = "all",
}
isTransactionOpen
It shows if the database transaction is created for the current HTTP request.
model
The current model instance in the HTTP request.
export interface IModelService {
// The model name
name: string;
// The model instance
instance: Model;
// The model relations
relations: IRelation[];
// The all model columns
columns: IColumn[];
// The model column names
columnNames: string[];
// Hook definitions
hooks: ModelHooks;
// Event definitions
events: ModelHooks;
// Query limit configurations
queryLimits: IQueryLimitConfig[];
// Serialize function
serialize: SerializationFunction | null;
}
parentModel
If the current HTTP request is a child resource like api/v1/users/:id/posts
, parentModel
represents the parent model instance.
In this case, the user
model instance.
relation
The active relation definition if there is any.
The active relation definition if there is any. The interface and the enum definition are the following examples;
enum Relationships {
HAS_ONE = "HAS_ONE",
HAS_MANY = "HAS_MANY",
}
interface IRelation {
// The relation type
type: Relationships;
// The relation name such as 'posts', 'author', 'createdUser', etc.
name: string;
// The related model name
model: string;
// The primary key
primaryKey: string;
// The foreign key
foreignKey: string;
}
req
The AxeRequest instance.
res
The AxeResponse instance.
version
The active version information for the HTTP request.
interface IVersion {
// The version name
name: string;
// The version configurations
config: IVersionConfig;
// The version folder paths such as Models, Hooks, etc.
folders: IVersionFolder;
// All model list in the version
modelList: ModelListService;
// The model tree that is generated by the Axe API
modelTree: IModelService[];
}
conditions
The parsed query information that sent by the HTTP client.
interface IQuery {
// The nested query object
q: NestedWhere;
// The current page value
page: number;
// The per page value
per_page: number;
// The sorting definitions
sort: ISortField[];
// The field list to be fetched
fields: string[];
// The related resource query
with: IWith[];
// The soft-deleted item query
trashed: boolean;
}
formData
The parsed form data that sent by HTTP client. It is an object.
TIP
The fillable() function is used in the parsing process.
item
The fetched item by the query.
It is usable in the following handlers;
params
The user parameters in the HTTP request URL.
For example, for the following URL pattern, you can get the URL parameters;
/api/v1/users/:userId/posts/:id
{
"userId": 1,
"id": 2
}
result
The fetched item array by the query.
It is usable in the following handlers;
query
The active Knex.js query object.
During the SHOW
handler, Axe API creates a Knex.js query and adds the :id
parameters automatically to the query. If the query object is generated, you can use that object and add your custom logic.
For example, let's assume that you want to filter the transaction
resource by the logged user id automatically. So that every user can see only their transactions.
import { IContext } from "axe-api";
export default async (context: IContext) => {
const { query, req } = context;
query.where("user_id", req.auth.userId);
};