Adding arbitrary express routes
This commit is contained in:
@@ -10,10 +10,11 @@ export class OrdService extends cds.ApplicationService {
|
|||||||
/**
|
/**
|
||||||
* Just an example to do something with id, if given.
|
* Just an example to do something with id, if given.
|
||||||
* Try it out with URLs like that:
|
* Try it out with URLs like that:
|
||||||
* http://localhost:4004/ord/v1/documents
|
*
|
||||||
* http://localhost:4004/ord/v1/documents/CatalogService
|
* - http://localhost:4004/ord/v1/documents
|
||||||
* http://localhost:4004/ord/v1/documents/CatalogService.Books
|
* - http://localhost:4004/ord/v1/documents/CatalogService
|
||||||
* http://localhost:4004/ord/v1/documents/CatalogService.Authors
|
* - http://localhost:4004/ord/v1/documents/CatalogService.Books
|
||||||
|
* - http://localhost:4004/ord/v1/documents/CatalogService.Authors
|
||||||
*/
|
*/
|
||||||
this.on('READ','csn', req => {
|
this.on('READ','csn', req => {
|
||||||
let csn = cds.context?.model || cds.model
|
let csn = cds.context?.model || cds.model
|
||||||
@@ -25,16 +26,17 @@ export class OrdService extends cds.ApplicationService {
|
|||||||
/**
|
/**
|
||||||
* Just an example to serve arbitrary content with a function.
|
* Just an example to serve arbitrary content with a function.
|
||||||
* Try it out with URLs like that:
|
* Try it out with URLs like that:
|
||||||
* http://localhost:4004/ord/v1/api?service=CatalogService
|
*
|
||||||
* http://localhost:4004/ord/v1/api?service=CatalogService&format=edmx
|
* - http://localhost:4004/ord/v1/api?service=CatalogService
|
||||||
* http://localhost:4004/ord/v1/api?service=CatalogService&format=edmx-v2
|
* - http://localhost:4004/ord/v1/api?service=CatalogService&format=edmx
|
||||||
* http://localhost:4004/ord/v1/api?service=CatalogService&format=openapi
|
* - http://localhost:4004/ord/v1/api?service=CatalogService&format=edmx-v2
|
||||||
|
* - http://localhost:4004/ord/v1/api?service=CatalogService&format=openapi
|
||||||
*/
|
*/
|
||||||
this.on('api', req => {
|
this.on('api', req => {
|
||||||
let csn = cds.context?.model || cds.model
|
let csn = cds.context?.model || cds.model
|
||||||
let { service, format } = req.data
|
let { service, format = 'csn' } = req.data
|
||||||
let { res } = req.http
|
let { res } = req.http
|
||||||
if (!format) {
|
if (format === 'csn') {
|
||||||
if (!service) return res.send(csn)
|
if (!service) return res.send(csn)
|
||||||
service = csn.services[service]
|
service = csn.services[service]
|
||||||
return res.send({ definitions: [ service, ...service.entities ] .reduce ((all,e) => {
|
return res.send({ definitions: [ service, ...service.entities ] .reduce ((all,e) => {
|
||||||
@@ -48,6 +50,24 @@ export class OrdService extends cds.ApplicationService {
|
|||||||
return res.send(api)
|
return res.send(api)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Example how to register arbitrary use the express app routes,
|
||||||
|
* and map them to our service's interface.
|
||||||
|
* Try it out with URLs like that:
|
||||||
|
*
|
||||||
|
* - http://localhost:4004/ord/v1/csn/CatalogService
|
||||||
|
* - http://localhost:4004/ord/v1/edmx/CatalogService
|
||||||
|
* - http://localhost:4004/ord/v1/openapi/CatalogService
|
||||||
|
* - http://localhost:4004/ord/v1/asyncapi/CatalogService
|
||||||
|
*
|
||||||
|
* NOTE: we add cds.middlewares.before to the route, which gives us all
|
||||||
|
* the context and auth handling, which is also available to CAP services.
|
||||||
|
*/
|
||||||
|
cds.app.get (`${this.path}/:api?/:service?`, cds.middlewares.before, req => {
|
||||||
|
const { api, service } = req.params
|
||||||
|
return this.api (service, api)
|
||||||
|
})
|
||||||
|
|
||||||
return super.init()
|
return super.init()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user