Introduction π
Hey there, fellow developers! π
For the past couple of days, Iβve been working on building an API using TypeScript along with some exciting technologies:
Bun.js ποΈ β A fast JavaScript runtime, an alternative to Node.js
Hono.js β‘ β A lightweight and efficient web framework, replacing Express
Drizzle ORM π β A modern and type-safe ORM for database interactions
Turso βοΈ β A high-performance SQL database hosting solution
This has been a rewarding experience, and Iβd love to share my progress so far.
Why This Stack? π€
I chose these technologies because:
β
Bun.js is extremely fast compared to Node.js. It significantly improves execution speed and reduces startup time.
β
Hono.js is lightweight and perfect for building APIs without the overhead of Express.
β
Drizzle ORM provides type safety and better database schema management.
β
Turso offers a powerful SQL database with serverless hosting capabilities.
My Progress So Far π―
Iβve made great progress in key areas:
β
JWT Authentication β Implemented token-based authentication
β
Password Hashing β Ensured security with password encryption
β
Project Structuring β Organized the project following best practices
πΉ Project Structure
To keep the code clean and maintainable, I followed this folder structure:
/src
βββ database
β βββ queries
β β βββ insert.query.ts
β β βββ select.query.ts
β β βββ index.ts
β β βββ schema.ts
βββ routes
β βββ auth.route.ts
β βββ post.route.ts
βββ schemasValidator
β βββ schema.validator.ts
βββ utils
β βββ config.utils.ts
β βββ hashing.utils.ts
β βββ jwt.util.ts
β βββ index.ts
.env
.gitignore
bun.lock
LICENSE
README.md
tsconfig.json
package.json
drizzle.config.ts
Code Snippets π οΈ
π JWT Authentication with Hono.js
import { sign } from "hono/jwt";
import { JWT_SECRET } from "../utils/config.utils";
export async function jwtGenerate(id:number, username:string, email:string, password:string){
const payload = {
sub: username,
id:id,
email: email,
password: password,
exp: Math.floor(Date.now()/1000) + 60 * 5, // if you want expire in one day use Math.floor(Date.now()/1000) + 60 * 60 * 24
}
if (JWT_SECRET === 'YOUR JWT_SECRET') {
throw new Error('You must set the JWT_SECRET env variable')
}
const token = await sign(payload, JWT_SECRET)
return token
}
π Hashing Passwords
export async function hashingPassword(pass: string){
const hashedPassword = await Bun.password.hash(pass);
return hashedPassword
}
export async function matchHashedPassword(pass: string, hashPass: string){
const isMatch = await Bun.password.verify(pass, hashPass);
return isMatch
}
Open Source Contribution π
Iβll be sharing the GitHub repository, so if youβre interested in:
Checking out the code
Suggesting improvements
Contributing to the project
Youβre more than welcome! π
Next Steps π
In the next updates, I plan to:
Implement role-based authorization (RBAC)
Add better error handling and logging
Optimize database queries using indexed searches in Turso
Deploy the API for public use
Letβs Connect! π¬
π‘ Got any suggestions or ideas? Drop them in the comments! Letβs build something great together.
Also, follow this blog for updates as I continue refining the API and adding new features! π
Social Media π
Letβs stay connected! You can find me on:
βοΈ Api-Rest-TypeScript
Feel free to reach out or follow me for more updates and content!