Skip to content

AnsgarLichter/nestjs-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NEST API

An API to create posts for an user. This project is used to test the compatibility of Nest with Bun. You will find the Node.js version in the main branch and the version for Bun in the branch migrate-to-bun.

This repository is based on the NestJS Starter Kit [v2].

Features

This API includes the following features:

Feature Info Progress
Authentication JWT Done
Authorization RBAC (Role based) Done
ORM Integration TypeORM Done
Logging winston Done
Request Validation class-validator Done
Validations Joi Done

Installation

Install the dependencies

npm install

Generate JWT public and private key pair for jwt authentication.

ssh-keygen -t rsa -b 2048 -m PEM -f jwtRS256.key
# Don't add passphrase
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub

You may save these key files in ./local directory as it is ignored in git.

Start an PostgreSQL server locally and create a new database: PostgreSQL Documentation

Create a .env file with the following properties:

JWT_PUBLIC_KEY_BASE64="<public key>"
APP_PORT=<port>
DB_HOST=<db host>
DB_PORT=<db port>
DB_NAME=<db name>
DB_USER=<db user>
DB_PASS=<db password>
JWT_ACCESS_TOKEN_EXP_IN_SEC=<seconds>
JWT_REFRESH_TOKEN_EXP_IN_SEC=<seconds>
DEFAULT_ADMIN_USER_PASSWORD=<password>

Encode keys to base64:

base64 -i local/jwtRS256.key

base64 -i local/jwtRS256.key.pub

Running the app

The Postgres server must berunning for the app to work

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Sending API requests

Registration & Login

Register

Property Value
Endpoint /api/v1/auth/register
Method POST
Body User
Response Registered User
{
    "name": "<full name>",
    "username": "<username>",
    "password": "<password>",
    "roles": "<roles: admin / user>",
    "email": "<email>",
    "isAccountDisabled": <boolean>
}

Login

Property Value
Endpoint /api/v1/auth/login
Method POST
Body Username & Password
Response Tokens
{
    "username": "<username>",
    "password": "<password>"
}

Refresh Token

Property Value
Endpoint /api/v1/auth/refresh-token
Method POST
Body Refresh Token
Response Tokens
{
    "refreshToken": "<token>"
}

Users

You have to supply a valid Bearer Token for the authorization check to work. If you don't have an user, please register first.

Read your own Profile

Property Value
Endpoint /api/v1/users/me
Method GET
Body -
Response User

Read all Users

Property Value
Endpoint /api/v1/users
Method GET
Body -
Response All Users

Read an User

Property Value
Endpoint /api/v1/users/{id}
Method GET
Body -
Response User

Update an User

Property Value
Endpoint /api/v1/articles/{id}
Method PATCH
Body Username & Password
Response User
{
    "name": "<name>",
    "password": "<password>"
}

Articles

You have to supply a valid Bearer Token for the authorization check to work. If you don't have an user, please register first.

Creating an Article

Property Value
Endpoint /api/v1/articles
Method POST
Body Article
Response Created Article
{
    "title": "<title>",
    "post": "<post>"
}

Read all Articles

Property Value
Endpoint /api/v1/articles
Method GET
Body -
Response All Articles

Read an Article

Property Value
Endpoint /api/v1/articles/{id}
Method GET
Body -
Response Article

Update an Article

Property Value
Endpoint /api/v1/articles/{id}
Method PATCH
Body Article
Response Updated Book
{
    "title": "<title>",
    "post": "<post>"
}

Delete an Article

Property Value
Endpoint /api/v1/articles/{id}
Method DELETE
Body -
Response -

About

NestJS Starter API to test the framework with Bun's runtime.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages