feat: refactored server to use ts and improved folder structure

This commit is contained in:
Roberto Tonino
2021-04-03 19:46:54 +02:00
parent f98abb384c
commit 8e4e2ff5eb
43 changed files with 4125 additions and 452 deletions

View File

@@ -0,0 +1,3 @@
import { ApiHandler } from '../../../types'
export default [] as ApiHandler[]

View File

@@ -0,0 +1,3 @@
import sample from './sample'
export default [sample]

View File

@@ -0,0 +1,11 @@
import { ApiHandler } from '../../../types'
const path: ApiHandler['path'] = '/sample'
const handler: ApiHandler['handler'] = (_, res) => {
res.send('Mandi')
}
const apiHandler: ApiHandler = { path, handler }
export default apiHandler

View File

@@ -0,0 +1,3 @@
import { ApiHandler } from '../../../types'
export default [] as ApiHandler[]

View File

@@ -0,0 +1,3 @@
import { ApiHandler } from '../../../types'
export default [] as ApiHandler[]

View File

@@ -0,0 +1,42 @@
import type { Application } from 'express'
import type { ApiHandler } from '../../types'
import getEndpoints from './get'
import deleteEndpoints from './delete'
import postEndpoints from './post'
import patchEndpoints from './patch'
const prependApiPath = (path: string) => `/api${path}`
interface Method {
method: string
endpoints: ApiHandler[]
}
const methods: Method[] = [
{
method: 'get',
endpoints: getEndpoints
},
{
method: 'delete',
endpoints: deleteEndpoints
},
{
method: 'post',
endpoints: postEndpoints
},
{
method: 'patch',
endpoints: patchEndpoints
}
]
export function registerApis(app: Application) {
methods.forEach(({ method, endpoints }) => {
endpoints.forEach(endpoint => {
// @ts-ignore
app[method](prependApiPath(endpoint.path), endpoint.handler)
})
})
}

View File

@@ -0,0 +1,14 @@
import express from 'express'
const router = express.Router()
/**
* GET home page
*
* @since 0.0.0
*/
router.get('/', (_, res) => {
res.render('index', { title: 'Express' })
})
export default router

View File

@@ -0,0 +1,14 @@
import express from 'express'
const router = express.Router()
/**
* GET users listing.
*
* @since 0.0.0
*/
router.get('/', (_, res) => {
res.send('respond with a resource')
})
export default router