cancel
Showing results for 
Search instead for 
Did you mean: 

TypeScript and cds-typer

akuller_q
Participant

Hello all,

now that UI5 also gets released TypeScript support, I would like to finally tackle this.

At the moment I'm stuck with the cds-typer not showing me the types according to the doc: https://cap.cloud.sap/docs/node.js/typescript#generating-model-types-automatically

This is the current code that works, what bothers me about this one?

  • The entity must be specified with .name
  • Changing the type in the handler
  • Only one entity can be specified
import { A_SalesOrder } from '#cds-models/SalesOrderService'
import cds from '@sap/cds'
module.exports = class SalesOrder extends cds.ApplicationService {
async init() {
const apiSalesOrder = await cds.connect.to('ZAPI_SALES_ORDER_SRV')
this.on('*', A_SalesOrder.name, req => {
const data = req.data as A_SalesOrder
console.log(data.SoldToParty)
return apiSalesOrder.run(req.query)
})
}
}

Development happens in BAS.

user: salesorder $ npm ls
salesorder@0.0.2 /home/user/projects/salesorder
├── @cap-js/cds-typer@0.6.1
├── @sap-cloud-sdk/http-client@3.4.0
├── @sap/cds@7.1.1
├── @tsconfig/node18@18.2.0
├── @types/node@20.5.0
├── express@4.18.2
└── passport@0.6.0

user: salesorder $ node -v
v18.14.2<br>

Has anyone got this working?

Is there perhaps a demo repository? The SAP samples are all a bit dusty.

Many thanks

daniel_ogrady
Explorer
0 Kudos

Hi Andre,

you have probably hit a limitation (as of today) of how the types of the CDS API can consume the types generated by cds-typer. The cases of passing '*' as request type or passing in multiple entities had not been considered at this point.

I will file this as gap in the types so it can get fixed at some point. For now you'll probably have to explicitly define a combination of one request type and one entity, I'm afraid.

If I am understanding correctly, you are looking for something like this?

const handler = (req: TypedRequest<A_SalesOrder | B_SalesOrder>) => {
// also handle B_SalesOrder in here?
const data = req.data as A_SalesOrder
console.log(data.SoldToParty)
return apiSalesOrder.run(req.query)
} this.on('READ', A_SalesOrder, handler)
this.on('READ', B_SalesOrder, handler)
// etc...

Best,
Daniel

Accepted Solutions (0)

Answers (0)