Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type for srv.on('error') and fix srv.send overload #9

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Copy link
Contributor

@daogrady daogrady Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

headers appears to be a recurring parameter type throughout the services definition. If I gather this from CAPire correctly, we are referring to general HTTP headers here, is that correct? If so, we could probably use import type { IncomingHttpHeaders } from 'node:http' type across all signatures. That would give us a nice, strict typing, consistency (currently we have a mix of object and {}), and code completion:

Screenshot 2024-01-08 at 07 45 01

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For most use cases, I think that would be an options. However, the messages of headers are of a different format.
Here, we'd need to have additional type, I suppose.

Copy link
Contributor

@daogrady daogrady Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I think for this PR where you are adding a missing signature it is absolutely fine.
For the long run I am strongly in favour of adding a dedicated type for these headers. Maybe we can use the existing type from node:http to derive ours. But let's postpone this as well.

}

/**
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfectly in line with all our other handler signatures, so this is absolutely fine. I am just wondering: can we stricten the return type for handlers in any way in general? Can it maybe be derived from the parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type does not matter for before/after/on('error').
Promises are awaited.

For regular on-handlers the return type might be undefined or an instance or array of "Books" or a promise resolving to one of them.

I don't know if that's somehow expressable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should be able to express this using TypedRequest<T>. It would then look more or less like this:

interface FooHandler<T> {
  (..., req: TypedRequest<T>): T | Promise<T> 
}

which would basically mean "T in, T out". But lets postpone this into a separate issue, as it seems to be beyond the scope of your actual fix.

}

// `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