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

Replace TypedRequest<T> with Request<T> #358

Merged
merged 4 commits into from
Dec 5, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
- Added missing properties for `log` in `cds.env`
- Added overload for `service.read` to be called with a `ref`

### Removed
- [breaking] Removed type `TypedRequest<T>` and replaced it with just `Request<T>`

### Fixed

- Use `Required` instead of `DeepRequired` in projection function to avoid complexity errors from TypeScript
Expand Down
6 changes: 3 additions & 3 deletions apis/events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export class EventContext {
/**
* @see [capire docs](https://cap.cloud.sap/docs/node.js/events)
*/
export class Event extends EventContext {
export class Event<T = unknown> extends EventContext {

event: string

data: any
data: T

headers: any

Expand All @@ -49,7 +49,7 @@ export class Event extends EventContext {
/**
* @see [capire docs](https://cap.cloud.sap/docs/node.js/events)
*/
export class Request extends Event {
export class Request<T = any> extends Event<T> {

params: (string | object)[]

Expand Down
8 changes: 3 additions & 5 deletions apis/services.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,11 @@ type CdsFunction = {
__returns: any,
}

type TypedRequest<T> = Omit<Request, 'data'> & { data: T }

// https://cap.cloud.sap/docs/node.js/core-services#srv-on-before-after
declare namespace CRUDEventHandler {
type Before<P, R = P | void | Error> = (req: TypedRequest<P>) => Promise<R> | R
type On<P, R = P | void | Error> = (req: TypedRequest<P>, next: (...args: any[]) => Promise<R> | R) => Promise<R> | R
type After<P, R = P | void | Error> = (data: undefined | P, req: TypedRequest<P>) => Promise<R> | R
type Before<P, R = P | void | Error> = (req: Request<P>) => Promise<R> | R
type On<P, R = P | void | Error> = (req: Request<P>, next: (...args: any[]) => Promise<R> | R) => Promise<R> | R
type After<P, R = P | void | Error> = (data: undefined | P, req: Request<P>) => Promise<R> | R
}

// Handlers for actions try to infer the passed .data property
Expand Down
10 changes: 5 additions & 5 deletions test/typescript/apis/project/cds-services.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cds, { Service, TypedRequest } from '@sap/cds'
import cds, { Service, Request } from '@sap/cds'
import { Bars, Bar, Foo, Foos, action } from './dummy'
const model = cds.reflect({})
const { Book: Books } = model.entities
Expand Down Expand Up @@ -216,10 +216,10 @@ srv.on('error', (err, req) => {
})


function isOne(p: TypedRequest<Foo> | Foo | undefined ) { if(!p) return; p instanceof Foo ? p.x.toFixed : p.data.x.toFixed}
function isMany(p: TypedRequest<Foos> | Foos | undefined) { if(!p) return; p instanceof Foos ? p[0].x.toFixed : p.data[0].x.toFixed}
function isOne(p: Request<Foo> | Foo | undefined ) { if(!p) return; p instanceof Foo ? p.x.toFixed : p.data.x.toFixed}
function isMany(p: Request<Foos> | Foos | undefined) { if(!p) return; p instanceof Foos ? p[0].x.toFixed : p.data[0].x.toFixed}

function isOneOfMany(p: TypedRequest<Foo | Bar> | Foo | Bar | undefined ) {
function isOneOfMany(p: Request<Foo | Bar> | Foo | Bar | undefined ) {
if(!p) return;
if ("data" in p) {
if (p.data instanceof Foo) p.data.x.toFixed;
Expand All @@ -229,7 +229,7 @@ function isOneOfMany(p: TypedRequest<Foo | Bar> | Foo | Bar | undefined ) {
else p.name.split;
}
}
function isManyOfMany(p: TypedRequest<Foos | Bars> | Foos | Bars | undefined) {
function isManyOfMany(p: Request<Foos | Bars> | Foos | Bars | undefined) {
if(!p) return;
if ("data" in p) {
if (p.data instanceof Foos) p.data[0].x.toFixed;
Expand Down
Loading