Create rest API quickly with Axe API framework: Part 1

Pavel Salauyou
6 min readOct 10, 2023

--

What is rest API?

To explain the API in simple terms, imagine you’re at a café and want to place an order. The cafe has a menu (API documentation) that lists all the available dishes and their details. You, as the client, can make a request to the waiter (API endpoint) specifying what you want to order (HTTP request). The waiter takes your request and communicates it to the kitchen (server), which prepares your order (performs the requested action) based on the information provided.

Once the kitchen is done, it sends your order back to the waiter (HTTP response) along with the relevant details, such as the dish you ordered and its status. The waiter then brings the prepared order to your table (response data), allowing you to interact with it.

In a similar way, a REST API enables different software applications to communicate and exchange data. The API acts as a waiter, accepting requests from clients, processing them, and returning the requested information or performing the requested actions. It follows a set of principles and standards to ensure a uniform and predictable way of interacting with the software system behind the API.

Axe API framework

Axe API is a powerful framework for Node.js written in TypeScript. It’s designed to make the lives of developers easier by handling repetitive tasks associated with models, allowing them to focus on creating unique and custom logic. With Axe API, you don’t have to worry about reinventing the wheel. It provides a solid foundation for building your APIs, incorporating a range of useful features and following best practices. By using Axe API, you can save valuable time and effort in the development process.

Key features of Axe API framework

  1. Faster API development
  2. Model-driven API
  3. Securing data input
  4. Automated validation
  5. Query your models
  6. Auto-created documentation

Let’s take a closer look at these points.

Faster API development

Axe API makes it easier to develop your API by taking care of basic tasks like routing, validation, querying, and handling HTTP requests. This allows developers to focus on their unique logic, which helps speed up the development process. Additionally, the framework provides dedicated areas where developers can add their own custom code.

Model-driven API

What is Model?

In TypeScript programming (or Javascript), a “Model” is like a way of organizing information in an application. Let’s imagine a café that allows customers to order food online.

In this case, the model represents the different parts of the online ordering system. It includes the café’s menu with all the available food options and the customer’s order details.

The menu model holds information about each food item offered by the café, like its name, description, price, and any special information like dietary restrictions or ingredients. It also has functions that help get the menu items from the café’s computer system.

The order model keeps track of what the customer has chosen to order. It stores information like the items they picked, how many of each item, the total price, and any special instructions the customer may have. The order model has functions to add items to the order, remove items, calculate the total price, and send the order to the café’s kitchen for preparation.

By using models, the café’s website can effectively handle the menu and the customer’s order. The model helps fetch the menu items from the café’s system and display them on the website so customers can choose what they want. As customers select items and add them to their order, the model keeps track of what they picked and how many. It also calculates the total price, including any taxes or discounts.

Models make it easier to organize the code, keep the information correct, and simplify the process of managing online food orders for the café. They separate the data and its handling from how it looks on the website, making it easier to manage customer orders and keep everything organized behind the scenes.

Axe API simplifies your project by removing the need for a Controller file. Instead, it focuses on the core part of your project, which is the model files.

Axe API automatically reads and understands your models, including their relationships, without requiring any manual intervention from you.

When you define your model in Axe API, the framework analyzes it and automatically generates all the necessary routes for Create, Read, Update, and Delete (CRUD) operations (known as CRUD operations).

Not only does Axe API create these routes for you, but it also handles all the CRUD operations behind the scenes, saving you from having to do any extra work.

Model files are essential for defining your API, as they allow you to specify various features that determine how your API functions.

There will be two menus in our cafe, one is regular and the other is for lunch. Here is an example model for the menu without properties:

import { Model } from 'axe-api';

class Menu extends Model {
// some properties that we will do next...
}

export default Menu;

Securing data input

In the api of our cafe there will be several models with many properties, but some properties do not need to have data written to them. In Axe API the list of allowed fields for writing can be specified by the fillable property:

import { Model } from 'axe-api';

class Menu extends Model {
get fillable() {
return ['name', 'description'];
}
}

export default Menu;

In the given example, the name and description fields are marked as fillable. This means that clients can create a new entry in your table by sending an HTTP POST request to your API, including these fields in the request. Even if there are other fields in the http query, only those fields defined in fillable will be written to the database.

Automated validation

Another key feature of this framework is the built-in field validation, usually in other forms or self-written APIs, validation occurs using the class validator using DTO objects. Class validator class is a pretty powerful thing, but it’s still a third-party library. It is not necessary to integrate third-party libraries into the Axe API, it is enough to describe validation rules in the model:

import { Model } from 'axe-api';

class Menu extends Model {
get fillable() {
return ['name', 'description'];
}

get validations() {
return {
name: "required|min:3|max:100",
description: "max:100",
};
}
}

export default Menu;

Pretty convenient, isn’t it? Axe API has many other features we will consider them on the example of creating our own api for ordering food online in our cafe.

Installing Axe API Cli

To start a new Axe API project, you can use the axe-magic command-line interface (CLI).

The axe-magic CLI is a helpful tool for setting up a new Axe API project. It works by retrieving a template from GitHub and configuring it accordingly.

To install this tool on your computer, simply run the command provided:

npm i -g axe-magic

Creating a new project

To create a new API project use this command:

axe-magic new food-order

After creating the project you need to set its dependencies, go to the food-order directory and run the command npm install:

cd food-order
npm install

Setup mysql database

Axe API supports many databases like PostgreSQL, CockroachDB, MSSQL, MySQL, MariaDB, SQLite3, Better-SQLite3, Oracle, and Amazon Redshift, but in our api we will use the most popular database — Mysql. To work with mysql database you need to install npm package mysql2 client:

npm install mysql

Let’s start by defining our database schema, go to mysql console and run command (or create a database in any other way convenient for you, for example, through a mysql GUI client):

CREATE DATABASE foodorder
CHARACTER SET utf8mb4
COLLATE utf8mb4_general_ci;

In order to connect to the database you need to modify an .env file in the root of the project with the following contents:

NODE_ENV=development
APP_PORT=3000
DB_CLIENT=mysql
DB_HOST=localhost
DB_USER=user
DB_PASSWORD=password
DB_DATABASE=foodorder

Change DB_USER and DB_PASSWORD to user and password of your mysql database .If the data in the .env file was correct, you can now run the Axe API application:

npm run start:dev

After running you will see the following output in the console:

[axe] All API versions have been resolved. 
[axe] [v1] All models have been resolved.
[axe] [v1] Database schema has been validated.
[axe] [v1] Model tree has been created.
[axe] [v1] Express routes have been created.
[axe] API listens requests on http://localhost:3000

If you are using mysql 8, you may have this error ER_NOT_SUPPORTED_AUTH_MODE to solve it, go to the mysql console and run the command:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Follow the http://localhost:3000 link and you should see the json object:

{
"name": "AXE API",
"description": "The next generation Rest API framework.",
"documentation": "https://axe-api.com/getting-started/crud/index.html"
}

You can see current routers at http://localhost:3000/routes, but nothing will be there now as we haven’t defined the models yet. In general, this is already an absolute working API, in the next article we will look at how to create models and make http requests.

--

--