Skip to content

Commit 5fcd1df

Browse files
vkozyurasmahatirenejeglinsky
authored
Improve description of srv.send (#1930)
Co-authored-by: Mahati Shankar <[email protected]> Co-authored-by: René Jeglinsky <[email protected]>
1 parent c927898 commit 5fcd1df

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

guides/providing-services.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,18 +1123,45 @@ POST .../sue/Foo(2)/Sue.order {"x":3} // bound action
11231123
<br>
11241124

11251125

1126-
**Programmatic** usage via **generic APIs** would look like this for Node.js:
1126+
**Programmatic** usage via **generic APIs** for Node.js:
1127+
1128+
For unbound actions and functions:
1129+
1130+
```ts
1131+
async function srv.send (
1132+
event : string | { event, data?, headers?: object },
1133+
data? : object | any
1134+
)
1135+
return : result of this.dispatch(req)
1136+
```
1137+
1138+
For bound actions and functions:
1139+
1140+
```ts
1141+
async function srv.send (
1142+
event : string | { event, entity, data?, params?: array of object, headers?: object },
1143+
entity : string,
1144+
data? : object | any
1145+
)
1146+
return : result of this.dispatch(req)
1147+
```
1148+
1149+
- `event` is a name of a custom action or function
1150+
- `entity` is a name of an entity
1151+
- `params` are keys of the entity instance
1152+
1153+
Programmatic usage would look like this for Node.js:
11271154

11281155
```js
11291156
const srv = await cds.connect.to('Sue')
11301157
// unbound actions/functions
11311158
await srv.send('sum',{x:1,y:2})
11321159
await srv.send('stock',{id:2})
11331160
await srv.send('add',{x:11,to:2})
1134-
// bound actions/functions
1161+
// actions/functions bound to collection
11351162
await srv.send('getStock','Foo',{id:2})
1136-
//for passing the params property, use this syntax
1137-
await srv.send({ event: 'order', entity: 'Foo', data: {x:3}, params: [2]})
1163+
// for actions/functions bound to entity instance, use this syntax
1164+
await srv.send({ event: 'order', entity: 'Foo', data: {x:3}, params: [{id:2}]})
11381165
```
11391166

11401167
> Note: Always pass the target entity name as second argument for bound actions/functions.

node.js/core-services.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,9 +796,11 @@ return : result of this.dispatch(req)
796796

797797
Use this method to send synchronous requests to a service for execution.
798798

799-
- `method` can be an HTTP method, or a name of a custom action or function
799+
- `method` is an HTTP method
800800
- `path` can be an arbitrary URL, starting with a leading `'/'`, it is passed to a service without any modification as a string
801801

802+
To call bound / unbound actions and functions from the service, further variants of `srv.send` are additionally supported, as described in the section [Calling Actions / Functions](../guides/providing-services#calling-actions-functions). Basically, use the action or function name instead of the HTTP method.
803+
802804
Examples:
803805

804806
```js

0 commit comments

Comments
 (0)