Deno RESTful API project starter for oak framework
This is a starter project to create Deno RESTful API using oak. oak is a middleware framework and router middleware for Deno, inspired by popular Node.js framework Koa and @koa/router.
This project covers
Router
, Service
& Repository
layers 1) Setup
2) Migrations
3) Modules
4) Project Layout
5) How to add a new route
6) How to validate request body
7) How to use JWT authorization
8) How to add auth guards
9) Error handling
10) Contributing
11) Contributors
12) Roadmap
We can run the project with/ without Docker.
env.example
to .env
.Run API
For Docker: Up docker-compose, this will create a docker container with the database with the given name in env.
$ docker-compose up --build
For non-docker run API server with Deno run time
$ deno run --allow-read --allow-net app.ts
API
at http://localhost:8000Adminer
at http://localhost:8080Swagger Open API
Doc at http://localhost:8105We use nessie to manage database migration.
nessie.config.ts
. Make sure to update this with the DB credentials.
$ deno run --allow-net --allow-read --allow-write https://deno.land/x/nessie@v1.0.0-rc3/cli.ts migrate
With this, the user table would be created and the table would be seeded with fake data
deno run --allow-net --allow-read --allow-write https://deno.land/x/nessie@v1.0.0-rc3/cli.ts make create_product
Package | Purpose |
---|---|
oak@v5.0.0"">oak@v5.0.0 | Deno middleware framework |
dotenv@v0.4.2"">dotenv@v0.4.2 | Read env variables |
mysql@2.2.0"">mysql@2.2.0 | MySQL driver for Deno |
nessie@v1.0.0-rc3"">nessie@v1.0.0-rc3 | DB migration tool for Deno |
validasaur@v0.7.0"">validasaur@v0.7.0 | validation library |
djwt@v0.9.0"">djwt@v0.9.0 | JWT token encoding |
bcrypt@v0.2.1"">bcrypt@v0.2.1 | bcrypt encription lib |
.
├── .env (Make sure to create this file from given .env.example)
├── config/
| |── config.ts (configuration object)
├── db/
| |── migrations/
| |── seeds/
| ├── db.ts (DB connection object)
├── middlewares/
├── migrations/
├── services/
├── repositories/
├── helpers/
├── routes/
|── types/
|── types.ts (all types exported here)
├── app.ts (application server)
├── openapi.yml (Swagger open api definition)
└── nessie.config.ts (DB configuration for nessie migration)
routes
folder. For each entity there should be separate routes file. For example user related CRUD router handlers are defined in user.routes.ts
file.routes.ts
file. To create CRUD for cat
cat.routes.ts
get list of cats
*/
const getCats = [
async (ctx: Context) => {
const cats = await catService.getCats();
ctx.response.body = cats;
}
];
//export route handler methods
exports { getCats };
```
Then bind getCats
route handler with router in routes.ts
file -
//routes.ts
import * as catRoutes from "./cat.routes.ts";
// ... router initialization codes
router
.get("/cats", ...catRoutes.getCats);
import { requestValidator } from “./../middlewares/request-validator.middleware.ts”;
/**
/**
.env
(copy from .env.example
).JWT_REFRESH_TOKEN_EXP=3600000
JWT_TOKEN_SECRET=HEGbulKGDblAFYskBLml
- Request header should contain JWT bearer token as `Authorization` key.
- Middleware [JWTAuthMiddleware](./middlewares/jwt-auth.middleware.ts) used to parse the `Authorization` header and decode the payload as `ctx.user`.
## How to add auth guards
- Auth guards are dependent on the `ctx.user` provided by [JWTAuthMiddleware](./middlewares/jwt-auth.middleware.ts) middleware.
- To define different levels of authentication guard in different route handlers, middleware [userGuard](./middlewares/user-guard.middleware.ts) defined.
- `userGuard` middleware optionally takes allowed user's roles as parameter. Otherwise, it will check only for the signed user.
- Here is the example usage:-
//user.routes.ts
/**
/**
Bug reports and pull requests are welcome on GitHub at https://github.com/asad-mlbd/deno-api-starter-oak.