@xano/developer-mcp 2.2.2-beta.2 → 2.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli_docs/index.js +2 -0
- package/dist/cli_docs/topics/function.js +49 -2
- package/dist/cli_docs/topics/knowledge.d.ts +2 -0
- package/dist/cli_docs/topics/knowledge.js +127 -0
- package/dist/cli_docs/topics/sandbox.js +10 -10
- package/dist/cli_docs/topics/start.js +3 -2
- package/dist/cli_docs/topics/static_host.js +5 -1
- package/dist/cli_docs/topics/tenant.js +60 -0
- package/dist/cli_docs/topics/unit_test.js +3 -1
- package/dist/cli_docs/topics/workspace.js +29 -8
- package/dist/tools/index.d.ts +33 -2
- package/dist/tools/index.js +19 -2
- package/dist/tools/xano_knowledge.d.ts +65 -0
- package/dist/tools/xano_knowledge.js +162 -0
- package/dist/xanoscript_docs/docs_index.json +0 -6
- package/dist/xanoscript_docs/middleware.md +122 -379
- package/dist/xanoscript_docs/version.json +2 -2
- package/package.json +1 -1
- package/dist/xanoscript_docs/knowledge.md +0 -146
|
@@ -4,91 +4,52 @@ applyTo: "middleware/*.xs"
|
|
|
4
4
|
|
|
5
5
|
# Middleware
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
> with a `middleware = { pre: [...], post: [...] }` block on a **query/API**, a
|
|
9
|
-
> **function**, or an **API group**. One middleware object serves both phases;
|
|
10
|
-
> branch on `$input.type` (`"pre"` | `"post"`). Its `input` block is fixed to
|
|
11
|
-
> `vars` + `type` — custom parameters are stripped.
|
|
12
|
-
|
|
13
|
-
Middleware intercepts requests before (`pre`) and after (`post`) your queries,
|
|
14
|
-
functions, tasks, and tools run. You define the middleware in one place and attach
|
|
15
|
-
it to the constructs that should use it.
|
|
7
|
+
Middleware intercepts and processes requests before and after your functions, queries, tasks, and tools execute.
|
|
16
8
|
|
|
17
9
|
## Quick Reference
|
|
18
10
|
|
|
19
11
|
```xs
|
|
20
|
-
|
|
21
|
-
middleware
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
stack {
|
|
30
|
-
conditional {
|
|
31
|
-
if ($input.type == "post") {
|
|
32
|
-
// post-phase work
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
// pre-phase work
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
response = { server_time: now }
|
|
41
|
-
response_strategy = "merge"
|
|
42
|
-
exception_policy = "silent"
|
|
43
|
-
}
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
```xs
|
|
47
|
-
// 2. Attach it — reference the middleware by name
|
|
48
|
-
query list_items verb=GET {
|
|
49
|
-
api_group = "catalog"
|
|
50
|
-
|
|
51
|
-
stack {
|
|
52
|
-
db.query "items" { } as $items
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
response = $items
|
|
56
|
-
middleware = {
|
|
57
|
-
pre : [{name: "rate_limit"}]
|
|
58
|
-
post: [{name: "add_metadata"}]
|
|
59
|
-
}
|
|
12
|
+
middleware "<name>" {
|
|
13
|
+
description = "What this middleware does"
|
|
14
|
+
exception_policy = "silent" | "rethrow" | "critical"
|
|
15
|
+
response_strategy = "merge" | "replace"
|
|
16
|
+
input { ... }
|
|
17
|
+
stack { ... }
|
|
18
|
+
response = $result
|
|
60
19
|
}
|
|
61
20
|
```
|
|
62
21
|
|
|
63
|
-
|
|
22
|
+
### Required Blocks
|
|
23
|
+
| Block | Purpose |
|
|
64
24
|
|-------|---------|
|
|
65
|
-
| `
|
|
66
|
-
| `
|
|
67
|
-
| `
|
|
68
|
-
|
|
69
|
-
|
|
25
|
+
| `input` | Define parameters passed to middleware |
|
|
26
|
+
| `stack` | Processing logic |
|
|
27
|
+
| `response` | Output returned to caller |
|
|
28
|
+
|
|
29
|
+
### Optional Attributes
|
|
30
|
+
| Attribute | Values | Default | Description |
|
|
31
|
+
|-----------|--------|---------|-------------|
|
|
32
|
+
| `description` | text | - | Documents middleware purpose |
|
|
33
|
+
| `exception_policy` | `silent`, `rethrow`, `critical` | `rethrow` | How to handle errors |
|
|
34
|
+
| `response_strategy` | `merge`, `replace` | `merge` | How response combines with original |
|
|
70
35
|
|
|
71
36
|
---
|
|
72
37
|
|
|
73
|
-
##
|
|
38
|
+
## Basic Structure
|
|
74
39
|
|
|
75
40
|
```xs
|
|
76
|
-
middleware log_request {
|
|
41
|
+
middleware "log_request" {
|
|
77
42
|
description = "Logs all incoming requests"
|
|
78
43
|
|
|
79
44
|
input {
|
|
80
|
-
json
|
|
81
|
-
enum type {
|
|
82
|
-
values = ["pre", "post"]
|
|
83
|
-
}
|
|
45
|
+
json request_data
|
|
84
46
|
}
|
|
85
47
|
|
|
86
48
|
stack {
|
|
87
|
-
var $timestamp { value = now }
|
|
88
49
|
debug.log {
|
|
89
50
|
value = {
|
|
90
|
-
timestamp:
|
|
91
|
-
data: $input.
|
|
51
|
+
timestamp: now,
|
|
52
|
+
data: $input.request_data
|
|
92
53
|
}
|
|
93
54
|
}
|
|
94
55
|
}
|
|
@@ -97,237 +58,70 @@ middleware log_request {
|
|
|
97
58
|
}
|
|
98
59
|
```
|
|
99
60
|
|
|
100
|
-
### Required Blocks
|
|
101
|
-
| Block | Required | Purpose |
|
|
102
|
-
|-------|----------|---------|
|
|
103
|
-
| `input` | Yes | Fixed system inputs (`vars`, `type`) — see Input Contract |
|
|
104
|
-
| `stack` | Yes | Processing logic |
|
|
105
|
-
| `response` | No | Output returned to the caller (omit it freely) |
|
|
106
|
-
|
|
107
|
-
Omitting `input` or `stack` is a syntax error. Omitting `response` is fine.
|
|
108
|
-
|
|
109
|
-
### Optional Attributes
|
|
110
|
-
| Attribute | Values | Default | Description |
|
|
111
|
-
|-----------|--------|---------|-------------|
|
|
112
|
-
| `description` | text | - | Documents the middleware (stored as a leading comment) |
|
|
113
|
-
| `exception_policy` | `silent`, `rethrow`, `critical` | `silent` | How errors are handled |
|
|
114
|
-
| `response_strategy` | `merge`, `replace` | `merge` | How the middleware response combines with the original |
|
|
115
|
-
|
|
116
|
-
Values outside the listed enums are rejected (`Input "…" is not one of the allowable values.`).
|
|
117
|
-
|
|
118
|
-
> The quoted name (`middleware "<name>"`) parses, but the canonical form is
|
|
119
|
-
> unquoted: `middleware <name> { … }`. There is **no** `type` attribute on the
|
|
120
|
-
> declaration — `type = "post"` fails with `Invalid block: type`. Which phase a
|
|
121
|
-
> middleware runs in is decided where you attach it, not here.
|
|
122
|
-
|
|
123
|
-
---
|
|
124
|
-
|
|
125
|
-
## Input Contract (footgun)
|
|
126
|
-
|
|
127
|
-
A middleware's `input` block is **fixed**. It is always normalised to exactly two
|
|
128
|
-
system inputs, and **any custom parameters you add are silently discarded on push**:
|
|
129
|
-
|
|
130
|
-
```xs
|
|
131
|
-
// ❌ These custom params are stripped — $input.token / $input.user_id become null
|
|
132
|
-
input {
|
|
133
|
-
text token
|
|
134
|
-
int user_id
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// ✅ The only inputs that survive — this is what every middleware receives
|
|
138
|
-
input {
|
|
139
|
-
json vars // request (pre) or response (post) payload context
|
|
140
|
-
enum type { // which phase this invocation is running in
|
|
141
|
-
values = ["pre", "post"]
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
Read middleware data through `$input.vars` and branch on `$input.type` — never
|
|
147
|
-
through named custom parameters.
|
|
148
|
-
|
|
149
|
-
### Pre vs Post phases
|
|
150
|
-
|
|
151
|
-
There is **one middleware object per name, and it serves both phases**. The same
|
|
152
|
-
`stack` runs for the pre and the post invocation; `$input.type` (`"pre"` |
|
|
153
|
-
`"post"`) tells you which one you are in. Branch on it to do pre-work vs post-work
|
|
154
|
-
in a single middleware:
|
|
155
|
-
|
|
156
|
-
```xs
|
|
157
|
-
stack {
|
|
158
|
-
conditional {
|
|
159
|
-
if ($input.type == "pre") {
|
|
160
|
-
// runs BEFORE the endpoint — inspect $input.vars (the request);
|
|
161
|
-
// short-circuit with a precondition to block the request.
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
// runs AFTER the endpoint — $input.vars carries the response;
|
|
165
|
-
// transform or augment it (combined per response_strategy).
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
---
|
|
172
|
-
|
|
173
|
-
## Attaching Middleware
|
|
174
|
-
|
|
175
|
-
A middleware does nothing until it is **attached**. Attach it with a `middleware`
|
|
176
|
-
block that lists middleware by name for the `pre` and/or `post` phase:
|
|
177
|
-
|
|
178
|
-
```xs
|
|
179
|
-
middleware = {
|
|
180
|
-
pre : [{name: "rate_limit"}]
|
|
181
|
-
post: [{name: "add_metadata"}]
|
|
182
|
-
}
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
Both lists are optional and independent — supply only the phase you need. A list
|
|
186
|
-
can hold more than one middleware, and they run in the order written:
|
|
187
|
-
|
|
188
|
-
```xs
|
|
189
|
-
middleware = {
|
|
190
|
-
pre: [{name: "rate_limit"}, {name: "auth_check"}]
|
|
191
|
-
}
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
You can attach middleware at three scopes:
|
|
195
|
-
|
|
196
|
-
### On a query / API endpoint (per-endpoint)
|
|
197
|
-
|
|
198
|
-
```xs
|
|
199
|
-
query create_order verb=POST {
|
|
200
|
-
api_group = "orders"
|
|
201
|
-
|
|
202
|
-
input {
|
|
203
|
-
json order
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
stack {
|
|
207
|
-
db.add "orders" { data = $input.order } as $new
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
response = $new
|
|
211
|
-
middleware = {
|
|
212
|
-
pre : [{name: "rate_limit"}]
|
|
213
|
-
post: [{name: "add_metadata"}]
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
### On a function (function-stack)
|
|
219
|
-
|
|
220
|
-
```xs
|
|
221
|
-
function process_payment {
|
|
222
|
-
input {
|
|
223
|
-
decimal amount
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
stack {
|
|
227
|
-
var $result { value = { charged: $input.amount } }
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
response = $result
|
|
231
|
-
middleware = {
|
|
232
|
-
post: [{name: "audit_log"}]
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
### On an API group (applies to the whole group)
|
|
238
|
-
|
|
239
|
-
Attach a `middleware` block to the `api_group` declaration to run it for every
|
|
240
|
-
endpoint in that group:
|
|
241
|
-
|
|
242
|
-
```xs
|
|
243
|
-
api_group orders {
|
|
244
|
-
canonical = "orders_grp"
|
|
245
|
-
middleware = {
|
|
246
|
-
pre : [{name: "rate_limit"}]
|
|
247
|
-
post: [{name: "add_metadata"}]
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
```
|
|
251
|
-
|
|
252
|
-
> **Workspace-wide (global) middleware is configured in the Xano dashboard.** The
|
|
253
|
-
> `middleware` block is only valid on queries, functions, and API groups — placing
|
|
254
|
-
> it on the `workspace` declaration fails with `Invalid block: middleware`. To run
|
|
255
|
-
> a middleware across the whole workspace, attach it from the dashboard's
|
|
256
|
-
> middleware settings.
|
|
257
|
-
|
|
258
|
-
> ⚠️ **Middleware names must match exactly.** A name that doesn't resolve to an
|
|
259
|
-
> existing middleware is silently blanked (`{name: ""}`) on push, and the endpoint
|
|
260
|
-
> then runs with no middleware — no error at push or request time. Double-check the
|
|
261
|
-
> spelling against the middleware's declared name.
|
|
262
|
-
|
|
263
61
|
---
|
|
264
62
|
|
|
265
63
|
## Exception Policies
|
|
266
64
|
|
|
267
|
-
Control how
|
|
65
|
+
Control how middleware handles errors:
|
|
268
66
|
|
|
269
|
-
### silent
|
|
270
|
-
Errors are caught and ignored
|
|
67
|
+
### silent
|
|
68
|
+
Errors are caught and ignored. Execution continues normally.
|
|
271
69
|
|
|
272
70
|
```xs
|
|
273
|
-
middleware optional_enrichment {
|
|
71
|
+
middleware "optional_enrichment" {
|
|
274
72
|
exception_policy = "silent"
|
|
275
73
|
|
|
276
74
|
input {
|
|
277
|
-
|
|
278
|
-
enum type { values = ["pre", "post"] }
|
|
75
|
+
int user_id
|
|
279
76
|
}
|
|
280
77
|
|
|
281
78
|
stack {
|
|
282
|
-
// If this fails,
|
|
283
|
-
api.
|
|
284
|
-
url = "https://api.example.com/enrich"
|
|
285
|
-
method = "GET"
|
|
79
|
+
// If this fails, request continues without enrichment
|
|
80
|
+
api.call "external_service" {
|
|
81
|
+
url = "https://api.example.com/enrich/" ~ $input.user_id
|
|
286
82
|
} as $enriched
|
|
287
83
|
}
|
|
288
84
|
|
|
289
|
-
response =
|
|
85
|
+
response = $enriched
|
|
290
86
|
}
|
|
291
87
|
```
|
|
292
88
|
|
|
293
|
-
### rethrow
|
|
294
|
-
Errors are passed through to the caller
|
|
89
|
+
### rethrow (default)
|
|
90
|
+
Errors are passed through to the caller. Standard error handling applies.
|
|
295
91
|
|
|
296
92
|
```xs
|
|
297
|
-
middleware validate_token {
|
|
93
|
+
middleware "validate_token" {
|
|
298
94
|
exception_policy = "rethrow"
|
|
299
95
|
|
|
300
96
|
input {
|
|
301
|
-
|
|
302
|
-
enum type { values = ["pre", "post"] }
|
|
97
|
+
text token
|
|
303
98
|
}
|
|
304
99
|
|
|
305
100
|
stack {
|
|
306
|
-
precondition (
|
|
101
|
+
precondition ($input.token|strlen > 0) {
|
|
307
102
|
error_type = "accessdenied"
|
|
308
103
|
error = "Token required"
|
|
309
104
|
}
|
|
310
105
|
}
|
|
311
106
|
|
|
312
|
-
response =
|
|
107
|
+
response = { valid: true }
|
|
313
108
|
}
|
|
314
109
|
```
|
|
315
110
|
|
|
316
111
|
### critical
|
|
317
|
-
Errors are treated as critical failures
|
|
112
|
+
Errors are treated as critical failures. Request is immediately terminated.
|
|
318
113
|
|
|
319
114
|
```xs
|
|
320
|
-
middleware security_check {
|
|
115
|
+
middleware "security_check" {
|
|
321
116
|
exception_policy = "critical"
|
|
322
117
|
|
|
323
118
|
input {
|
|
324
|
-
|
|
325
|
-
enum type { values = ["pre", "post"] }
|
|
119
|
+
text ip_address
|
|
326
120
|
}
|
|
327
121
|
|
|
328
122
|
stack {
|
|
329
123
|
db.query "blocked_ips" {
|
|
330
|
-
where = $db.blocked_ips.ip == $
|
|
124
|
+
where = $db.blocked_ips.ip == $input.ip_address
|
|
331
125
|
return = { type: "exists" }
|
|
332
126
|
} as $is_blocked
|
|
333
127
|
|
|
@@ -337,7 +131,7 @@ middleware security_check {
|
|
|
337
131
|
}
|
|
338
132
|
}
|
|
339
133
|
|
|
340
|
-
response =
|
|
134
|
+
response = { allowed: true }
|
|
341
135
|
}
|
|
342
136
|
```
|
|
343
137
|
|
|
@@ -345,68 +139,54 @@ middleware security_check {
|
|
|
345
139
|
|
|
346
140
|
## Response Strategies
|
|
347
141
|
|
|
348
|
-
Control how
|
|
349
|
-
This applies to the **post** phase, where `$input.vars` carries the response.
|
|
142
|
+
Control how middleware response combines with the original response:
|
|
350
143
|
|
|
351
144
|
### merge (default)
|
|
352
|
-
|
|
145
|
+
Middleware response is merged with original response.
|
|
353
146
|
|
|
354
147
|
```xs
|
|
355
|
-
middleware add_metadata {
|
|
148
|
+
middleware "add_metadata" {
|
|
356
149
|
response_strategy = "merge"
|
|
357
150
|
|
|
358
151
|
input {
|
|
359
|
-
json vars
|
|
360
|
-
enum type { values = ["pre", "post"] }
|
|
361
152
|
}
|
|
362
153
|
|
|
363
154
|
stack {
|
|
364
|
-
var $
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
server_time: $server_time,
|
|
370
|
-
version: "1.0.0"
|
|
155
|
+
var $meta {
|
|
156
|
+
value = {
|
|
157
|
+
server_time: now,
|
|
158
|
+
version: "1.0.0"
|
|
159
|
+
}
|
|
371
160
|
}
|
|
372
161
|
}
|
|
162
|
+
|
|
163
|
+
response = { _meta: $meta }
|
|
373
164
|
}
|
|
374
165
|
// Original: { data: [...] }
|
|
375
|
-
// Result:
|
|
166
|
+
// Result: { data: [...], _meta: { server_time: ..., version: "1.0.0" } }
|
|
376
167
|
```
|
|
377
168
|
|
|
378
|
-
> ⚠️ **`now` only evaluates as a direct value.** Bare `now` returns the current
|
|
379
|
-
> timestamp when it is the whole value of a `var` or a top-level entry of the
|
|
380
|
-
> `response` object. Nested **inside** a `response` object literal (e.g. under
|
|
381
|
-
> `_meta`) it is emitted as the literal string `"now"`. Compute it into a `var`
|
|
382
|
-
> first and reference that var:
|
|
383
|
-
> ```xs
|
|
384
|
-
> stack { var $server_time { value = now } }
|
|
385
|
-
> response = { _meta: { server_time: $server_time } } // ✅ real timestamp
|
|
386
|
-
> ```
|
|
387
|
-
|
|
388
169
|
### replace
|
|
389
|
-
|
|
170
|
+
Middleware response completely replaces original response.
|
|
390
171
|
|
|
391
172
|
```xs
|
|
392
|
-
middleware
|
|
173
|
+
middleware "transform_response" {
|
|
393
174
|
response_strategy = "replace"
|
|
394
175
|
|
|
395
176
|
input {
|
|
396
|
-
json
|
|
397
|
-
enum type { values = ["pre", "post"] }
|
|
177
|
+
json original_response
|
|
398
178
|
}
|
|
399
179
|
|
|
400
180
|
stack {
|
|
401
|
-
var $
|
|
181
|
+
var $transformed {
|
|
402
182
|
value = {
|
|
403
183
|
success: true,
|
|
404
|
-
payload: $input.
|
|
184
|
+
payload: $input.original_response
|
|
405
185
|
}
|
|
406
186
|
}
|
|
407
187
|
}
|
|
408
188
|
|
|
409
|
-
response = $
|
|
189
|
+
response = $transformed
|
|
410
190
|
}
|
|
411
191
|
```
|
|
412
192
|
|
|
@@ -414,153 +194,116 @@ middleware wrap_response {
|
|
|
414
194
|
|
|
415
195
|
## Common Patterns
|
|
416
196
|
|
|
417
|
-
|
|
418
|
-
response data from `$input.vars` and use `$input.type` to act only in the phase you
|
|
419
|
-
want.
|
|
420
|
-
|
|
421
|
-
### Audit logging (post)
|
|
197
|
+
### Request Logging
|
|
422
198
|
|
|
423
199
|
```xs
|
|
424
|
-
middleware audit_log {
|
|
425
|
-
description = "Logs requests for
|
|
200
|
+
middleware "audit_log" {
|
|
201
|
+
description = "Logs all API requests for audit trail"
|
|
426
202
|
exception_policy = "silent"
|
|
427
203
|
|
|
428
204
|
input {
|
|
429
|
-
|
|
430
|
-
|
|
205
|
+
text endpoint
|
|
206
|
+
text method
|
|
207
|
+
json request_body?
|
|
208
|
+
int user_id?
|
|
431
209
|
}
|
|
432
210
|
|
|
433
211
|
stack {
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
}
|
|
212
|
+
db.add "audit_log" {
|
|
213
|
+
data = {
|
|
214
|
+
endpoint: $input.endpoint,
|
|
215
|
+
method: $input.method,
|
|
216
|
+
request_body: $input.request_body,
|
|
217
|
+
user_id: $input.user_id,
|
|
218
|
+
ip_address: $env.$remote_ip,
|
|
219
|
+
created_at: now
|
|
443
220
|
}
|
|
444
221
|
}
|
|
445
222
|
}
|
|
223
|
+
|
|
224
|
+
response = null
|
|
446
225
|
}
|
|
447
226
|
```
|
|
448
227
|
|
|
449
|
-
### Rate
|
|
228
|
+
### Rate Limiting
|
|
450
229
|
|
|
451
230
|
```xs
|
|
452
|
-
middleware rate_limit {
|
|
453
|
-
description = "
|
|
231
|
+
middleware "rate_limit" {
|
|
232
|
+
description = "Enforces rate limits per user"
|
|
233
|
+
exception_policy = "rethrow"
|
|
454
234
|
|
|
455
235
|
input {
|
|
456
|
-
|
|
457
|
-
|
|
236
|
+
int user_id
|
|
237
|
+
int max_requests?=100
|
|
238
|
+
int window_seconds?=60
|
|
458
239
|
}
|
|
459
240
|
|
|
460
241
|
stack {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
ttl = 60
|
|
467
|
-
error = "Rate limit exceeded"
|
|
468
|
-
}
|
|
469
|
-
}
|
|
242
|
+
redis.ratelimit {
|
|
243
|
+
key = "ratelimit:" ~ $input.user_id
|
|
244
|
+
max = $input.max_requests
|
|
245
|
+
ttl = $input.window_seconds
|
|
246
|
+
error = "Rate limit exceeded"
|
|
470
247
|
}
|
|
471
248
|
}
|
|
249
|
+
|
|
250
|
+
response = null
|
|
472
251
|
}
|
|
473
252
|
```
|
|
474
253
|
|
|
475
|
-
### Response
|
|
254
|
+
### Response Caching
|
|
476
255
|
|
|
477
256
|
```xs
|
|
478
|
-
middleware cache_response {
|
|
257
|
+
middleware "cache_response" {
|
|
479
258
|
description = "Caches responses in Redis"
|
|
480
259
|
exception_policy = "silent"
|
|
481
260
|
|
|
482
261
|
input {
|
|
483
|
-
|
|
484
|
-
|
|
262
|
+
text cache_key
|
|
263
|
+
int ttl_seconds?=300
|
|
264
|
+
json response_data
|
|
485
265
|
}
|
|
486
266
|
|
|
487
267
|
stack {
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
data = $input.vars
|
|
493
|
-
ttl = 300
|
|
494
|
-
}
|
|
495
|
-
}
|
|
268
|
+
redis.set {
|
|
269
|
+
key = $input.cache_key
|
|
270
|
+
value = $input.response_data|json_encode
|
|
271
|
+
expire = $input.ttl_seconds
|
|
496
272
|
}
|
|
497
273
|
}
|
|
274
|
+
|
|
275
|
+
response = null
|
|
498
276
|
}
|
|
499
277
|
```
|
|
500
278
|
|
|
501
279
|
---
|
|
502
280
|
|
|
503
|
-
##
|
|
281
|
+
## Applying Middleware
|
|
504
282
|
|
|
505
|
-
|
|
506
|
-
```xs
|
|
507
|
-
input {
|
|
508
|
-
text token // stripped on push — $input.token is null at runtime
|
|
509
|
-
}
|
|
510
|
-
```
|
|
511
|
-
✅ **Read from the fixed `vars` input:**
|
|
512
|
-
```xs
|
|
513
|
-
input {
|
|
514
|
-
json vars
|
|
515
|
-
enum type { values = ["pre", "post"] }
|
|
516
|
-
}
|
|
517
|
-
// ... reference $input.vars, e.g. ($input.vars|get:"token")
|
|
518
|
-
```
|
|
283
|
+
Middleware is configured at the branch level. See `xano_xanoscript_docs({ topic: "branch" })` for configuration details.
|
|
519
284
|
|
|
520
|
-
❌ **Expecting a `type` attribute to pick the phase:**
|
|
521
|
-
```xs
|
|
522
|
-
middleware log_request {
|
|
523
|
-
type = "post" // Invalid block: type
|
|
524
|
-
...
|
|
525
|
-
}
|
|
526
|
-
```
|
|
527
|
-
✅ **One object serves both phases — branch on `$input.type`:**
|
|
528
285
|
```xs
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
286
|
+
branch "production" {
|
|
287
|
+
middleware = {
|
|
288
|
+
query: {
|
|
289
|
+
pre: ["validate_token", "rate_limit"],
|
|
290
|
+
post: ["audit_log", "cache_response"]
|
|
291
|
+
},
|
|
292
|
+
function: {
|
|
293
|
+
pre: ["validate_token"],
|
|
294
|
+
post: ["audit_log"]
|
|
295
|
+
}
|
|
532
296
|
}
|
|
533
297
|
}
|
|
534
298
|
```
|
|
535
299
|
|
|
536
|
-
❌ **Misspelling the attached name:**
|
|
537
|
-
```xs
|
|
538
|
-
middleware = { pre: [{name: "rate_limitr"}] } // silently becomes {name: ""} — no middleware runs
|
|
539
|
-
```
|
|
540
|
-
✅ **Match the declared middleware name exactly:**
|
|
541
|
-
```xs
|
|
542
|
-
middleware = { pre: [{name: "rate_limit"}] }
|
|
543
|
-
```
|
|
544
|
-
|
|
545
|
-
❌ **Putting a middleware block on the workspace declaration:**
|
|
546
|
-
```xs
|
|
547
|
-
workspace "my-app" {
|
|
548
|
-
middleware = { pre: [...] } // Invalid block: middleware
|
|
549
|
-
}
|
|
550
|
-
```
|
|
551
|
-
✅ **Attach on a query, function, or API group — or configure workspace-wide
|
|
552
|
-
middleware in the dashboard.**
|
|
553
|
-
|
|
554
300
|
---
|
|
555
301
|
|
|
556
302
|
## Best Practices
|
|
557
303
|
|
|
558
|
-
1. **Keep middleware focused**
|
|
559
|
-
2. **
|
|
560
|
-
|
|
561
|
-
3. **Attach at the right scope** — per-endpoint for targeted logic, on the API
|
|
562
|
-
group for cross-cutting concerns shared by every endpoint in the group.
|
|
563
|
-
4. **Mind performance** — middleware runs on every matching request.
|
|
304
|
+
1. **Keep middleware focused** - Each middleware should do one thing well
|
|
305
|
+
2. **Use appropriate exception policies** - Critical for security, silent for optional enrichment
|
|
306
|
+
3. **Consider performance** - Middleware runs on every request; log even silent failures
|
|
564
307
|
|
|
565
308
|
---
|
|
566
309
|
|
|
@@ -568,7 +311,7 @@ middleware in the dashboard.**
|
|
|
568
311
|
|
|
569
312
|
| Topic | Description |
|
|
570
313
|
|-------|-------------|
|
|
571
|
-
| `
|
|
572
|
-
| `functions` | Reusable function stacks |
|
|
314
|
+
| `branch` | Branch-level middleware configuration |
|
|
573
315
|
| `security` | Authentication and authorization patterns |
|
|
574
|
-
| `
|
|
316
|
+
| `apis` | API endpoint request/response lifecycle |
|
|
317
|
+
| `performance` | Performance optimization strategies |
|