All Projects → asad-mlbd → Deno Api Starter Oak

asad-mlbd / Deno Api Starter Oak

Deno RESTful API project starter for oak framework

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Deno Api Starter Oak

Streamcaster
Live streaming platform built with Node.js and React
Stars: ✭ 20 (-64.91%)
Mutual labels:  restful-api
Cfg Center
高性能配置读取服务器,为Yaml配置文件提供JSON RESTful读取接口
Stars: ✭ 39 (-31.58%)
Mutual labels:  restful-api
Ebook Building An Api Backend With Microprofile
Building an API Backend with MicroProfile
Stars: ✭ 53 (-7.02%)
Mutual labels:  restful-api
Djangorestframework Book
Django REST framework 3 中文文档, API参考, 最佳实践指南
Stars: ✭ 28 (-50.88%)
Mutual labels:  restful-api
Grocery Cms Php Restful Api
Grocery-CMS-PHP-Restful-API is an online grocery shop. The project is developed by using PHP/MySQL/Slim Restful API. The project has powerful backend cms to manage grocery shop online. it has features like add items, remove items, update price, manage orders etc. Restful API ready to embed in Application using JSON data.
Stars: ✭ 36 (-36.84%)
Mutual labels:  restful-api
Kbframe
一款基于Laravel框架开发的现代化二次开发框架,是高性能,高效率,高质量的企业级开发框架,具有驱动领域,敏捷开发,轻易上手,高内聚低耦合,开箱即用等特点。
Stars: ✭ 47 (-17.54%)
Mutual labels:  restful-api
Html Pdf Service
LGPL V3. Java Spring Boot microservice with RESTful webconsole and service endpoints that convert HTML to PDF, optionally styling with CSS and templating with JSON using Flying Saucer, PDF Box and Jackson libraries. Available on Docker Hub.
Stars: ✭ 12 (-78.95%)
Mutual labels:  restful-api
Keeper Core Api
Nunux Keeper core API
Stars: ✭ 55 (-3.51%)
Mutual labels:  restful-api
Restful Demo
A RESTful web service demo - building your own lightweight REST library in Swift
Stars: ✭ 37 (-35.09%)
Mutual labels:  restful-api
Calm
It is always Calm before a Tornado!
Stars: ✭ 50 (-12.28%)
Mutual labels:  restful-api
Tinyme
A tiny php framework based on flight and medoo with restful api service
Stars: ✭ 28 (-50.88%)
Mutual labels:  restful-api
Thinkphp5 Restfulapi
restful-api风格接口 APP接口 APP接口权限 oauth2.0 接口版本管理 接口鉴权
Stars: ✭ 949 (+1564.91%)
Mutual labels:  restful-api
Generator Http Fake Backend
Yeoman generator for building a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 49 (-14.04%)
Mutual labels:  restful-api
Restfulsense
A RESTFul operations client that serializes responses and throws meaningful exceptions for >= 400 status codes.
Stars: ✭ 28 (-50.88%)
Mutual labels:  restful-api
Rest Api Example
RESTful Blog API in Lumen 5.2
Stars: ✭ 53 (-7.02%)
Mutual labels:  restful-api
E Commerce 2 django
Guest register, user register, user login, user logout, account home page, product view history, change password, reset password, change name, send activation email when register, resend activation email, add shipping address, add billing address, add nickname to the addresses, edit shipping address, edit billing address, view list of your addresses, reuse shipping addresses when order products, reuse billing addresses when ordeer products, show sales analytics if staff or admin only using -chart.js-, get analytics data with Ajax, receive marketing email, change if user will receive marketing email or not by admin, send contact message with Ajax, products list, product detail, download product detail as a PDF file, download digital product files -if the user purchased that digital product only-, orders list, list of digital products files, order detail, download order detail as a PDF file, verify order ownership with Ajax -to secure order detail page-, show cart products, add or remove product from cart, checkout page, thanks page when order placed successfully, add or reuse payment method, add or reuse payment method with Ajax, search products by title, search products by description, search products by price, search products by tag title, write tags for products -by admin only-, auto fill contact email, full name if user logged in.
Stars: ✭ 20 (-64.91%)
Mutual labels:  restful-api
Stateful
Stateful RESTful Web Primitives
Stars: ✭ 40 (-29.82%)
Mutual labels:  restful-api
Api Strategy
Equinor API Strategy
Stars: ✭ 56 (-1.75%)
Mutual labels:  restful-api
Rest And Graphql
⚡️ Highly scalable REST API codebase with GraphQL layer on its ⚡️
Stars: ✭ 55 (-3.51%)
Mutual labels:  restful-api
Molten Boilerplate
A boilerplate for the molten framework by Bogdanp https://github.com/Bogdanp/molten
Stars: ✭ 50 (-12.28%)
Mutual labels:  restful-api

Deno RESTful API using OAK

logo

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

  • Swagger Open API doc
  • Docker container environment
  • JWT authentication
  • User authorization
  • Request validation
  • .env config management
  • Coding architecture with Router, Service & Repository layers
  • Application Error Handling
  • Request timing logging
  • Generic request logging

Important links

  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

Setup

We can run the project with/ without Docker.

  • Pre-Requisite

    • For dockerized environment we need
      • docker,
      • docker-compose installed.
    • To run API server without Docker we need
      • MySQL server running &
      • Deno run time installed
  • Configuration

    • In application root, rename example env file env.example to .env.
    • An example env file contains MySQL credentials for the dockerized environment. For non-docker setup, update MySQL credentials here.
  • 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

Migrations

We use nessie to manage database migration.

  • In the application root, we have nessie.config.ts. Make sure to update this with the DB credentials.
  • Run the following command to run the migration. Migration should create necessary tables and dump the data.
$ deno run --allow-net --allow-read --allow-write https://deno.land/x/[email protected]/cli.ts migrate

With this, the user table would be created and the table would be seeded with fake data

  • Further, to add new migration, for example, to create new product table run
deno run --allow-net --allow-read --allow-write https://deno.land/x/[email protected]/cli.ts make create_product

Modules

Package Purpose
[email protected] Deno middleware framework
[email protected] Read env variables
[email protected] MySQL driver for Deno
[email protected] DB migration tool for Deno
[email protected] validation library
[email protected] JWT token encoding
[email protected] bcrypt encription lib

Project Layout

.
├── .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)

How to add a new route

  • Router hanlders are defined in 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.
  • All routes are bind with router handlers in routes.ts file.
  • To create CRUD for cat
    • Create file cat.routes.ts
    • Write router handler methods,
    //cat.routes.ts
    import * as catService from "./../services/cat.service.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);
    

How to validate request body

//auth.routes.ts
import {
  required,
  isEmail,
} from "https://deno.land/x/[email protected]/src/rules.ts";

import { requestValidator } from "./../middlewares/request-validator.middleware.ts";

/** 
 * request body schema 
 * for cat create/update 
 * */
const catSchema = {
  name: [required],
  email: [required, isEmail],
};

/**
 * create cat
 */
const createCat = [
  /** request validation middleware */
  requestValidator({ bodyRules: catSchema }),
  /** router handler */
  async (ctx: Context) => {
    // ... router handler code to create cat
  },
];

How to use JWT authorization

  • Here, We used JWT based authentication
  • Necessary JWT constants should be configured in .env (copy from .env.example).
# Access token validity in ms
JWT_ACCESS_TOKEN_EXP=600000

# Refresh token validity in ms
JWT_REFRESH_TOKEN_EXP=3600000

# Secret secuirity string
JWT_TOKEN_SECRET=HEGbulKGDblAFYskBLml
  • Request header should contain JWT bearer token as Authorization key.
  • Middleware JWTAuthMiddleware 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 middleware.
  • To define different levels of authentication guard in different route handlers, middleware userGuard 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

/**
 * get list of users 
 * user with ADMIN role only can access
 */
const getUsers = [
  userGuard(UserRole.ADMIN),
  async (ctx: Context) => {
    // ... route handlers code
  },
];


/**
 * get signed user detail 
 * any authenticated user can access
 */
const getMe = [
  userGuard(),
  async (ctx: Context) => {
    // ... route handlers code
  },
];

Error handling

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/asad-mlbd/deno-api-starter-oak.

Contributors

Roadmap

  • [x] Open API integration
  • [x] API Doc
  • [x] Validation
  • [x] JWT Auth
  • [ ] Unit Testing
  • [ ] Logger
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].