Build a Typescript RESTful API with Agatee

Agatee is a lightweight framework for building Typescript RESTful API with ExpressJs.

PREREQUISITES

You should be familiar with the following: Typescript, Express framework.

Getting Started

Assuming that you already have nodejs and npm installed.

npm install -g typescript
npm install -g ts-node
npm install -g @agatee/cli

Check the agatee CLI installation:

gat -v

It will prompt the version of Agatee CLI that you’ve installed:

Create a new project

Let’s create our first agatee application by using the following command:

gat create sample-api

We can run our application by typing the below command:

cd sample-api
gat serve

Now our App is running on port 3000 and if we try to visit http://localhost:3000, we’ll get an error :

That because we didn’t set a route for the path ‘/’, so let’s add it.
Open the sample-api folder with your favorite IDE and add a method route in the app/app.ts

Then just call the route in bootstrap method before the call of server.start :

Now let’s save change and go back to http://localhost:3000 to see the result

Generate an MVC Component

Before generating our first component , let’s add Mongoose feature to our app by taping:

gat add mongoose -db sampledb

We can config the connection to our database in .env file

Create a new component named user with CRUD endpoint by using the command below:

gat generate component user --with -CRUD model --no-root

This will generate 3 files in app/components/user : user.model.ts, user.controllers.ts and user.router.ts

To make our component reachable just add UserRouter to the AppModule routes array in app/app.ts:

The User Component is now reachable via http://localhost:3000/user

Let’s edit our user Schema in user.model.ts and add fields ‘username’, ‘age’ ,‘createdAt’, and‘role’ before testing CRUD routes endpoint:

The CRUD path is set in user.router.ts

Don’t forget that our user component is reachable via http://localhost:3000/user , then the path declared in @GET, @POST, @PUT, @DELETE decorators params are reachable from there.
So,

For creating a new user: POST to http://localhost:3000/user
For reading : GET to http://localhost:3000/user/${idToRead}
For updating: PUT to http://localhost:3000/user
For deleting: DELETE to http://localhost:3000/user/${idToDelete}

Create a new route endpoint inner Component

For creating a new route inner a component just add a new method in user.router.ts.
For example, let’s create a new route for getting all admin user.

In user.router.ts :

The new route is reachable from http://localhost:3000/user/get-admin with method GET
Before we test the new endpoint, let’s add the getAdmin method to our UserContoller in user.controllers.ts

We’ve done ! Congratulations !

For more information about Agatee, you can visit the official documentation https://niainagitlab.gitlab.io/agatee-doc

Thank you for reading !

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store