https://github.com/helloakn/typescript-for-restapi
This repository is just for passion and you can refer for your project.
https://github.com/helloakn/typescript-for-restapi
express jest jest-mocking restful-api typescript
Last synced: about 2 months ago
JSON representation
This repository is just for passion and you can refer for your project.
- Host: GitHub
- URL: https://github.com/helloakn/typescript-for-restapi
- Owner: helloakn
- License: mit
- Created: 2024-08-14T14:48:27.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-25T06:11:50.000Z (almost 2 years ago)
- Last Synced: 2025-02-05T22:37:31.143Z (over 1 year ago)
- Topics: express, jest, jest-mocking, restful-api, typescript
- Language: TypeScript
- Homepage:
- Size: 103 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Typescript For RestAPI
[](https://github.com/helloakn/typescript-for-restapi) [](https://github.com/helloakn/typescript-for-restapi) [](https://github.com/helloakn/typescript-for-restapi)
[](https://github.com/helloakn/typescript-for-restapi)
[](https://github.com/helloakn/typescript-for-restapi)
## Table of content
- [1] File Structure
- [2] Git Conventional Commits Message
- [3] Routing Eample
----
### [1] File Structure
```
-typescript-for-restapi
├── __tests__ # for testing
│ ├── e2e-tests # for all the integration tests
│ └── unit-tests # for all the integration tests
├── src # for typescripts
│ ├── app # AS AS Open/Close Principle
│ │ ├── controllers
│ │ │ └─ ...
│ │ ├── middlewares
│ │ ├── models # for database table
│ │ └── routes # for admin / user / swigger api routes etc
│ ├── config # for main functions and components
│ │ └─ ...
│ ├── core # for main functions and components
│ │ └─ ...
├...
```
---
### [2] Git Conventional Commits Message
[Type] : Message
Type => [Create], [Modify], [Fix], [Delete]
Message => Describe about the commit
Example
[Create] message event for bla bla bla
[Modify] message event for bla bla bla
[Fix] message event for bla bla bla
---
### [3] Routing Eample
* Route : without controller
* Route : with controller
* Route : prefix
* Route : Authorization {with guard}
* Route : Multiple request method
#### Route Example
* Route : without controller
```javascript
import type { Request, Response, NextFunction } from 'express';
import Routers from '@/core/route'
import { REQUEST_METHODS } from '@/config';
const { GET } = REQUEST_METHODS;
const routers = new Routers();
const demoFun = (req: Request, res: Response, next?: NextFunction) => {
res.status(200).json({ 'msg': 'tes testFunt' })
}
routers.addRoute([GET], '/demo', [demoFun])
```
* Route : controller
```javascript
import ServiceCheckController from '@/app/controllers/serviceCheck.controller';
import Routers from '@/core/route'
import { REQUEST_METHODS } from '@/config';
const { GET } = REQUEST_METHODS;
const routers = new Routers();
export default routers
.addRoute([GET], '/health-check', ServiceCheckController.healthCheck)
```
* Route : Prefix
```javascript
/*
route prefix
*/
import type { Request, Response, NextFunction } from 'express';
import Routers from '@/core/route'
import { REQUEST_METHODS } from '@/config';
const { GET } = REQUEST_METHODS;
const routers = new Routers();
const testFun = (req: Request, res: Response, next?: NextFunction) => {
res.status(200).json({ 'msg': 'tes testFunt' })
}
export default routers
.addPrefix('/api/v1', (routers) => {
routers
.addRoute([GET], '/register', [testFun])
.addRoute([GET], '/login', [testFun])
return routers;
});
```
* Route : Authorization {with guard}
```javascript
/*
route authorization
*/
import type { Request, Response, NextFunction } from 'express';
import { isAuthorizeAccess } from '@/app/middlewares';
import Routers from '@/core/route'
import { REQUEST_METHODS } from '@/config';
const { GET } = REQUEST_METHODS;
const routers = new Routers();
const testFun = (req: Request, res: Response, next?: NextFunction) => {
res.status(200).json({ 'msg': 'tes testFunt' })
}
export default routers
.addPrefix('/api/v1/user', (routers) => {
routers
.addRoute([GET], '/profile', [isAuthorizeAccess, testFun])
return routers;
});
```
* Route : Multiple request method
```javascript
import type { Request, Response, NextFunction } from 'express';
import Routers from '@/core/route'
import { REQUEST_METHODS } from '@/config';
const { GET,POST,PUT } = REQUEST_METHODS;
const routers = new Routers();
const testFun = (req: Request, res: Response, next?: NextFunction) => {
res.status(200).json({ 'msg': 'tes testFunt' })
}
export default routers
.addRoute([GET,POST,PUT], '/profile', [ testFun])
```
- - -