You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The standard set of middlewares uses the following order:
218
+
218
219
```js
219
220
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
225
225
]
226
226
```
227
227
@@ -235,36 +235,40 @@ _ctx_auth_ requires that _authentication_ has run before.
235
235
236
236
This middleware initializes [cds.context](events#cds-context) and starts the continuation. It's required for every application.
237
237
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
+
238
245
### . auth() {.method}
239
246
240
247
[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).
241
248
249
+
242
250
### . ctx_model() {.method}
243
251
244
252
It adds the currently active model to the continuation. It's required for all applications using extensibility or feature toggles.
245
253
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
-
252
254
253
255
### .add(mw, pos?) {.method}
254
256
255
257
Registers additional middlewares at the specified position.
256
258
`mw` must be a function that returns an express middleware.
257
259
`pos` specified the index or a relative position within the middleware chain. If not specified, the middleware is added to the end.
258
260
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
+
266
269
<divid="beforecustomization" />
267
270
271
+
268
272
### Custom Middlewares
269
273
270
274
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
275
279
cds.middlewares= {
276
280
auth,
277
281
context,
278
-
ctx_auth,
279
282
ctx_model,
280
283
errors,
281
284
trace,
282
285
before = [
283
286
context(),
284
287
trace(),
285
288
auth(),
286
-
ctx_auth(),
287
289
ctx_model()
288
290
]
289
291
}
290
292
```
291
293
292
294
In order to plug in custom middlewares, you can override the complete list of middlewares or extend the list programmatically.
295
+
293
296
::: warning
294
297
Be aware that overriding requires constant updates as new middlewares by the framework are not automatically taken over.
295
298
:::
@@ -299,31 +302,34 @@ Be aware that overriding requires constant updates as new middlewares by the fra
299
302
#### Customization of `req.user`
300
303
301
304
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.
303
307
304
308
```js
305
309
cds.middlewares.before= [
306
310
cds.middlewares.context(),
307
311
cds.middlewares.trace(),
308
312
cds.middlewares.auth(),
309
-
functionreq_user (req,res,next) {
310
-
req.user.id='<my-idp>'+req.user.id
313
+
functionctx_user (_,__,next) {
314
+
constctx=cds.context
315
+
ctx.user.id='<my-idp>'+ctx.user.id
311
316
next()
312
317
},
313
-
cds.middlewares.ctx_auth()
318
+
cds.middlewares.ctx_model()
314
319
]
315
320
```
316
321
317
322
#### Enabling Feature Flags
318
323
324
+
You can register middlewares to customize `req.features`.
325
+
It must be done before `cds.context.model` is set for the current request.
319
326
320
327
```js
321
328
cds.middlewares.before= [
322
329
cds.middlewares.context(),
323
330
cds.middlewares.trace(),
324
331
cds.middlewares.auth(),
325
-
cds.middlewares.ctx_auth(),
326
-
functionreq_features (req,res,next) {
332
+
functionreq_features (req,_,next) {
327
333
req.features= ['<feature-1>', '<feature-2>']
328
334
next()
329
335
},
@@ -333,11 +339,13 @@ cds.middlewares.before = [
333
339
334
340
[Learn more about Feature Vector Providers.](../guides/extensibility/feature-toggles#feature-vector-providers){.learn-more}
335
341
342
+
336
343
### Current Limitations
337
344
338
345
- Configuration of middlewares must be done programmatically.
339
346
340
347
348
+
341
349
## cds. protocols
342
350
343
351
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