feat: added auto registering of api routes
This commit is contained in:
parent
b9b873a915
commit
e3a0a996fe
|
@ -0,0 +1,3 @@
|
||||||
|
import { ApiHandler } from '../../../types'
|
||||||
|
|
||||||
|
export default [] as ApiHandler[]
|
|
@ -0,0 +1,3 @@
|
||||||
|
import { ApiHandler } from '../../../types'
|
||||||
|
|
||||||
|
export default [] as ApiHandler[]
|
|
@ -0,0 +1,3 @@
|
||||||
|
import { ApiHandler } from '../../../types'
|
||||||
|
|
||||||
|
export default [] as ApiHandler[]
|
|
@ -1,10 +1,41 @@
|
||||||
import { Application } from 'express'
|
import { Application } from 'express'
|
||||||
|
import { ApiHandler } from '../../types'
|
||||||
import getEndpoints from './get'
|
import getEndpoints from './get'
|
||||||
|
import deleteEndpoints from './delete'
|
||||||
|
import postEndpoints from './post'
|
||||||
|
import patchEndpoints from './patch'
|
||||||
|
|
||||||
const prependApiPath = (path:string) => `/api${path}`
|
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) {
|
export function registerApis (app: Application) {
|
||||||
getEndpoints.forEach((getApi) => {
|
methods.forEach(({ method, endpoints }) => {
|
||||||
app.get(prependApiPath(getApi.path), getApi.handler)
|
endpoints.forEach((endpoint) => {
|
||||||
|
// @ts-ignore
|
||||||
|
app[method](prependApiPath(endpoint.path), endpoint.handler)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue