Skip to content

Commit

Permalink
Add type for srv.on('error') and fix srv.send overload (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-vogel committed Jan 8, 2024
1 parent 4595992 commit 5ca9955
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Version 0.2.0 - tbd

### Changed
### Added

- Type for special error listener `srv.on('error')`

### Fixed

- `srv.send` overload to also allow optional headers


## Version 0.1.0 - 2023-12-13
Expand Down
7 changes: 6 additions & 1 deletion apis/services.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class Service extends QueryAPI {
<T = any>(details: { event: types.event; data?: object; headers?: object }): Promise<T>
<T = any>(details: { query: ConstructedQuery; data?: object; headers?: object }): Promise<T>
<T = any>(details: { method: types.eventName; path: string; data?: object; headers?: object }): Promise<T>
<T = any>(details: { event: types.eventName; entity: LinkedDefinition | string; data?: object; params?: object }): Promise<T>
<T = any>(details: { event: types.eventName; entity: LinkedDefinition | string; data?: object; params?: object; headers?: object }): Promise<T>
}

/**
Expand Down Expand Up @@ -216,6 +216,7 @@ export class Service extends QueryAPI {
on<F extends CdsFunction>(unboundAction: F, handler: ActionEventHandler<F['__parameters'], void | Error | F['__returns']>): this
on(eve: types.event, entity: types.target, handler: OnEventHandler): this
on(eve: types.event, handler: OnEventHandler): this
on(eve: 'error', handler: OnErrorHandler): this


// onSucceeded (eve: types.Events, entity: types.Target, handler: types.EventHandler): this
Expand Down Expand Up @@ -290,6 +291,10 @@ interface OnEventHandler {
(req: Request, next: Function): Promise<any> | any | void
}

interface OnErrorHandler {
(err: Error, req: Request): any | void
}

// `Partial` wraps any type and allows all properties to be undefined
// in addition to their actual type.
// This is important in the context of CRUD events, where
Expand Down
6 changes: 6 additions & 0 deletions test/typescript/apis/project/cds-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ await srv.emit({ event: 'UPDATE', data: {} })
await srv.send({ event: 'AuthorCreated', data: {}, headers: {} })
await srv.send({ event: 'feeEstimation', entity: networkGroups, data: {name:'Volta'}})
await srv.send({ event: 'feeEstimation', entity: networkGroups, data: {name:'Volta'}, params: {my: 7,new: 8}})
await srv.send({ event: 'feeEstimation', entity: networkGroups, data: {name:'Volta'}, params: {my: 7,new: 8}, headers: {accept: 'application/json'}})

// single args
await srv.send('CREATE', 'Books', {}, {})
Expand Down Expand Up @@ -180,6 +181,11 @@ srv.on('CREATE', Books, (req, next) => {
next()
})

// special error handler
srv.on('error', (err, req) => {
err.message
req.event
})

// Typed bound/ unbound actions
// The handler must return a number to be in line with action's signature (or void)
Expand Down

0 comments on commit 5ca9955

Please sign in to comment.