feat: added auto registering of api routes

This commit is contained in:
Roberto Tonino 2021-04-03 19:38:10 +02:00
parent b9b873a915
commit e3a0a996fe
4 changed files with 42 additions and 2 deletions

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,3 @@
import { ApiHandler } from '../../../types'
export default [] as ApiHandler[]

View File

@ -1,10 +1,41 @@
import { Application } from 'express'
import { 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) {
getEndpoints.forEach((getApi) => {
app.get(prependApiPath(getApi.path), getApi.handler)
methods.forEach(({ method, endpoints }) => {
endpoints.forEach((endpoint) => {
// @ts-ignore
app[method](prependApiPath(endpoint.path), endpoint.handler)
})
})
}