Skip to content

Commit e7698f9

Browse files
[cds^8] cds.middlewares.before (#1094)
* [cds^8] cds.middlewares.before * rework cds.middlewares.before * Update node.js/cds-serve.md * Update node.js/cds-serve.md * Apply suggestions from code review --------- Co-authored-by: René Jeglinsky <[email protected]>
1 parent 8a548d6 commit e7698f9

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

node.js/cds-serve.md

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,13 @@ app.use (cds.middlewares.before, protocol_adapter)
215215
```
216216

217217
The standard set of middlewares uses the following order:
218+
218219
```js
219220
cds.middlewares.before = [
220-
context(), // provides cds.context
221-
trace(), // provides detailed trace logs when DEBUG=trace
222-
auth(), // provides req.user & tenant
223-
ctx_auth(), // propagates auth results to cds.context
224-
ctx_model(), // fills in cds.context.model
221+
context(), // provides cds.context
222+
trace(), // provides detailed trace logs when DEBUG=trace
223+
auth(), // provides cds.context.user & .tenant
224+
ctx_model(), // fills in cds.context.model, in case of extensibility
225225
]
226226
```
227227

@@ -235,36 +235,40 @@ _ctx_auth_ requires that _authentication_ has run before.
235235

236236
This middleware initializes [cds.context](events#cds-context) and starts the continuation. It's required for every application.
237237

238+
239+
### . trace() {.method}
240+
241+
The tracing middleware allows you to do a first-level performance analysis. It logs how much time is spent on which layer of the framework when serving a request.
242+
To enable this middleware, you can set for example the [environment variable](cds-log#debug-env-variable) `DEBUG=trace`.
243+
244+
238245
### . auth() {.method}
239246

240247
[By configuring an authentication strategy](./authentication#strategies), a middleware is mounted that fulfills the configured strategy and subsequently adds the user and tenant identified by that strategy to [cds.context](events#cds-context).
241248

249+
242250
### . ctx_model() {.method}
243251

244252
It adds the currently active model to the continuation. It's required for all applications using extensibility or feature toggles.
245253

246-
### . trace() {.method}
247-
248-
The tracing middleware allows you to do a first-level performance analysis. It logs how much time is spent on which layer of the framework when serving a request.
249-
To enable this middleware, you can set for example the [environment variable](cds-log#debug-env-variable) `DEBUG=trace`.
250-
251-
252254

253255
### .add(mw, pos?) {.method}
254256

255257
Registers additional middlewares at the specified position.
256258
`mw` must be a function that returns an express middleware.
257259
`pos` specified the index or a relative position within the middleware chain. If not specified, the middleware is added to the end.
258260

259-
```js
260-
cds.middlewares.add (mw, {at:0}) // to the front
261-
cds.middlewares.add (mw, {at:2})
262-
cds.middlewares.add (mw, {before:'auth'})
263-
cds.middlewares.add (mw, {after:'auth'})
264-
cds.middlewares.add (mw) // to the end
265-
```
261+
```js
262+
cds.middlewares.add (mw, {at:0}) // to the front
263+
cds.middlewares.add (mw, {at:2})
264+
cds.middlewares.add (mw, {before:'auth'})
265+
cds.middlewares.add (mw, {after:'auth'})
266+
cds.middlewares.add (mw) // to the end
267+
```
268+
266269
<div id="beforecustomization" />
267270

271+
268272
### Custom Middlewares
269273

270274
The configuration of middlewares must be done programmatically before bootstrapping the CDS services, for example, in a [custom server.js](cds-serve#custom-server-js).
@@ -275,21 +279,20 @@ The framework exports the default middlewares itself and the list of middlewares
275279
cds.middlewares = {
276280
auth,
277281
context,
278-
ctx_auth,
279282
ctx_model,
280283
errors,
281284
trace,
282285
before = [
283286
context(),
284287
trace(),
285288
auth(),
286-
ctx_auth(),
287289
ctx_model()
288290
]
289291
}
290292
```
291293

292294
In order to plug in custom middlewares, you can override the complete list of middlewares or extend the list programmatically.
295+
293296
::: warning
294297
Be aware that overriding requires constant updates as new middlewares by the framework are not automatically taken over.
295298
:::
@@ -299,31 +302,34 @@ Be aware that overriding requires constant updates as new middlewares by the fra
299302
#### Customization of `req.user`
300303

301304
You can register middlewares to customize `req.user`.
302-
It must be set after authentication but before `cds.context` is initialized.
305+
It must be done after authentication.
306+
If `cds.context.tenant` is manipulated as well, it must also be done before `cds.context.model` is set for the current request.
303307

304308
```js
305309
cds.middlewares.before = [
306310
cds.middlewares.context(),
307311
cds.middlewares.trace(),
308312
cds.middlewares.auth(),
309-
function req_user (req,res,next) {
310-
req.user.id = '<my-idp>' + req.user.id
313+
function ctx_user (_,__,next) {
314+
const ctx = cds.context
315+
ctx.user.id = '<my-idp>' + ctx.user.id
311316
next()
312317
},
313-
cds.middlewares.ctx_auth()
318+
cds.middlewares.ctx_model()
314319
]
315320
```
316321

317322
#### Enabling Feature Flags
318323

324+
You can register middlewares to customize `req.features`.
325+
It must be done before `cds.context.model` is set for the current request.
319326

320327
```js
321328
cds.middlewares.before = [
322329
cds.middlewares.context(),
323330
cds.middlewares.trace(),
324331
cds.middlewares.auth(),
325-
cds.middlewares.ctx_auth(),
326-
function req_features (req,res,next) {
332+
function req_features (req,_,next) {
327333
req.features = ['<feature-1>', '<feature-2>']
328334
next()
329335
},
@@ -333,11 +339,13 @@ cds.middlewares.before = [
333339

334340
[Learn more about Feature Vector Providers.](../guides/extensibility/feature-toggles#feature-vector-providers){.learn-more}
335341

342+
336343
### Current Limitations
337344

338345
- Configuration of middlewares must be done programmatically.
339346

340347

348+
341349
## cds. protocols
342350

343351
The framework provides adapters for OData V4 and REST out of the box. In addition, GraphQL can be served by using our open source package [`@cap-js/graphql`](https://github.com/cap-js/graphql).

0 commit comments

Comments
 (0)