Skip to content

Commit 0111641

Browse files
authored
fix: signature of cds.tx (#544)
1 parent af994b4 commit 0111641

3 files changed

Lines changed: 11 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
1111
- `ResultHandler` now returns `unknown` instead of `void`, to accommodate asynchronous functions when having `@typescript-eslint/strict-void-return` activated
1212
- Documentation for `cds.test.axios` mentioning that `@cap-js/cds-test@1` now returns an `axios` facade in absence of `axios`.
1313
- made `cds.context.locale` optional
14+
- `cds.tx(ƒ)` now returns the return type of `ƒ`
1415

1516
### Deprecated
1617
- `cds.test.chai`, `cds.test.assert` pointing to either `cds.test.expect` or a custom import of `chai`.

apis/services.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ export class QueryAPI {
9292
transaction: QueryAPI['tx']
9393

9494
tx: {
95-
(fn: (tx: Transaction) => object): Promise<unknown>,
95+
<T>(fn: (tx: Transaction) => T): Promise<Awaited<T>>,
9696
(context?: object): Transaction,
97-
(context: object, fn: (tx: Transaction) => object): Promise<unknown>,
97+
<T>(context: object, fn: (tx: Transaction) => T): Promise<Awaited<T>>,
9898
}
9999

100100
}
@@ -519,9 +519,9 @@ export function spawn (options: SpawnOptions, fn: (tx: Transaction) => object):
519519
* @see [docs](https://cap.cloud.sap/docs/node.js/cds-tx)
520520
*/
521521
export const tx: {
522-
(fn: (tx: Transaction) => object): Promise<any>,
522+
<T>(fn: (tx: Transaction) => T): Promise<T>,
523523
(context?: object): Transaction,
524-
(context: object, fn: (tx: Transaction) => object): Promise<any>,
524+
<T>(context: object, fn: (tx: Transaction) => T): Promise<T>,
525525
}
526526
export const run: Service['run']
527527
export const foreach: Service['foreach']

test/typescript/apis/project/cds-services.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,9 @@ asrv.on('', (req) => {
513513
})
514514
asrv.dispatch('foo')
515515
asrv.dispatch(['foo', 'bar'])
516+
517+
testType<Promise<number>>(cds.tx((tx) => 42))
518+
testType<Promise<number>>(cds.tx({}, (tx) => 42))
519+
testType<Promise<number>>(asrv.tx((tx) => 42))
520+
testType<Promise<number>>(asrv.tx({}, (tx) => 42))
521+
testType<number>(await srv.tx(async (tx) => 42))

0 commit comments

Comments
 (0)