@xano/developer-mcp 1.0.74 → 2.0.0-beta.0
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/README.md +104 -37
- package/dist/cli_docs/index.d.ts +13 -37
- package/dist/cli_docs/index.js +28 -39
- package/dist/cli_docs/topics/integration.js +1 -1
- package/dist/cli_docs/topics/workspace.js +1 -1
- package/dist/index.js +58 -55
- package/dist/lib.d.ts +3 -1
- package/dist/lib.js +1 -1
- package/dist/meta_api_docs/format.js +1 -1
- package/dist/meta_api_docs/index.d.ts +14 -42
- package/dist/meta_api_docs/index.js +34 -46
- package/dist/tools/cli_docs.d.ts +2 -3
- package/dist/tools/cli_docs.js +3 -5
- package/dist/tools/define_tool.d.ts +47 -0
- package/dist/tools/define_tool.js +36 -0
- package/dist/tools/index.d.ts +88 -221
- package/dist/tools/index.js +80 -109
- package/dist/tools/mcp_version.d.ts +4 -25
- package/dist/tools/mcp_version.js +12 -19
- package/dist/tools/meta_api_docs.d.ts +2 -3
- package/dist/tools/meta_api_docs.js +3 -5
- package/dist/tools/validate_xanoscript.d.ts +18 -65
- package/dist/tools/validate_xanoscript.js +56 -54
- package/dist/tools/xanoscript_docs.d.ts +25 -74
- package/dist/tools/xanoscript_docs.js +86 -83
- package/dist/xanoscript_docs/README.md +5 -5
- package/dist/xanoscript_docs/addons.md +1 -3
- package/dist/xanoscript_docs/apis.md +2 -2
- package/dist/xanoscript_docs/database.md +1 -1
- package/dist/xanoscript_docs/essentials.md +6 -6
- package/dist/xanoscript_docs/functions.md +3 -3
- package/dist/xanoscript_docs/integrations.md +5 -5
- package/dist/xanoscript_docs/middleware.md +1 -1
- package/dist/xanoscript_docs/performance.md +4 -4
- package/dist/xanoscript_docs/realtime.md +2 -2
- package/dist/xanoscript_docs/security.md +3 -6
- package/dist/xanoscript_docs/survival.md +1 -1
- package/dist/xanoscript_docs/syntax/string-filters.md +1 -1
- package/dist/xanoscript_docs/syntax.md +10 -12
- package/dist/xanoscript_docs/tasks.md +1 -2
- package/dist/xanoscript_docs/tools.md +3 -5
- package/dist/xanoscript_docs/types.md +2 -2
- package/dist/xanoscript_docs/unit-testing.md +1 -1
- package/dist/xanoscript_docs/workflow-tests.md +1 -1
- package/dist/xanoscript_docs/working.md +4 -4
- package/package.json +5 -4
- package/dist/api_docs/format.d.ts +0 -5
- package/dist/api_docs/format.js +0 -171
- package/dist/api_docs/index.d.ts +0 -52
- package/dist/api_docs/index.js +0 -111
- package/dist/api_docs/topics/agent.d.ts +0 -2
- package/dist/api_docs/topics/agent.js +0 -142
- package/dist/api_docs/topics/api.d.ts +0 -2
- package/dist/api_docs/topics/api.js +0 -176
- package/dist/api_docs/topics/apigroup.d.ts +0 -2
- package/dist/api_docs/topics/apigroup.js +0 -124
- package/dist/api_docs/topics/authentication.d.ts +0 -2
- package/dist/api_docs/topics/authentication.js +0 -61
- package/dist/api_docs/topics/branch.d.ts +0 -2
- package/dist/api_docs/topics/branch.js +0 -73
- package/dist/api_docs/topics/file.d.ts +0 -2
- package/dist/api_docs/topics/file.js +0 -70
- package/dist/api_docs/topics/function.d.ts +0 -2
- package/dist/api_docs/topics/function.js +0 -164
- package/dist/api_docs/topics/history.d.ts +0 -2
- package/dist/api_docs/topics/history.js +0 -149
- package/dist/api_docs/topics/mcp_server.d.ts +0 -2
- package/dist/api_docs/topics/mcp_server.js +0 -139
- package/dist/api_docs/topics/middleware.d.ts +0 -2
- package/dist/api_docs/topics/middleware.js +0 -156
- package/dist/api_docs/topics/realtime.d.ts +0 -2
- package/dist/api_docs/topics/realtime.js +0 -112
- package/dist/api_docs/topics/start.d.ts +0 -2
- package/dist/api_docs/topics/start.js +0 -107
- package/dist/api_docs/topics/table.d.ts +0 -2
- package/dist/api_docs/topics/table.js +0 -195
- package/dist/api_docs/topics/task.d.ts +0 -2
- package/dist/api_docs/topics/task.js +0 -165
- package/dist/api_docs/topics/tool.d.ts +0 -2
- package/dist/api_docs/topics/tool.js +0 -150
- package/dist/api_docs/topics/workflows.d.ts +0 -2
- package/dist/api_docs/topics/workflows.js +0 -131
- package/dist/api_docs/topics/workspace.d.ts +0 -2
- package/dist/api_docs/topics/workspace.js +0 -153
- package/dist/api_docs/types.d.ts +0 -79
- package/dist/api_docs/types.js +0 -4
- package/dist/cli_docs/topics/run.d.ts +0 -2
- package/dist/cli_docs/topics/run.js +0 -248
- package/dist/meta_api_docs/format.test.d.ts +0 -1
- package/dist/meta_api_docs/format.test.js +0 -272
- package/dist/meta_api_docs/index.test.d.ts +0 -1
- package/dist/meta_api_docs/index.test.js +0 -128
- package/dist/meta_api_docs/types.test.d.ts +0 -1
- package/dist/meta_api_docs/types.test.js +0 -132
- package/dist/templates/init-workspace.d.ts +0 -10
- package/dist/templates/init-workspace.js +0 -278
- package/dist/templates/xanoscript-index.d.ts +0 -11
- package/dist/templates/xanoscript-index.js +0 -72
- package/dist/tools/index.test.d.ts +0 -1
- package/dist/tools/index.test.js +0 -179
- package/dist/tools/xanoscript_docs.test.d.ts +0 -1
- package/dist/tools/xanoscript_docs.test.js +0 -197
- package/dist/xanoscript.test.d.ts +0 -1
- package/dist/xanoscript.test.js +0 -421
- package/dist/xanoscript_docs/cheatsheet.md +0 -287
- package/dist/xanoscript_docs/ephemeral.md +0 -330
- package/dist/xanoscript_docs/quickstart.md +0 -692
- package/dist/xanoscript_docs/run.md +0 -196
- package/dist/xanoscript_docs/schema.md +0 -300
- package/dist/xanoscript_docs_auto/README.md +0 -119
- package/dist/xanoscript_docs_auto/agents.md +0 -446
- package/dist/xanoscript_docs_auto/apis.md +0 -517
- package/dist/xanoscript_docs_auto/control-flow.md +0 -543
- package/dist/xanoscript_docs_auto/database.md +0 -551
- package/dist/xanoscript_docs_auto/debugging.md +0 -527
- package/dist/xanoscript_docs_auto/filters.md +0 -464
- package/dist/xanoscript_docs_auto/functions.md +0 -431
- package/dist/xanoscript_docs_auto/integrations.md +0 -657
- package/dist/xanoscript_docs_auto/mcp-servers.md +0 -408
- package/dist/xanoscript_docs_auto/operators.md +0 -368
- package/dist/xanoscript_docs_auto/syntax.md +0 -287
- package/dist/xanoscript_docs_auto/tables.md +0 -447
- package/dist/xanoscript_docs_auto/tasks.md +0 -479
- package/dist/xanoscript_docs_auto/testing.md +0 -574
- package/dist/xanoscript_docs_auto/tools.md +0 -485
- package/dist/xanoscript_docs_auto/triggers.md +0 -595
- package/dist/xanoscript_docs_auto/types.md +0 -323
- package/dist/xanoscript_docs_auto/variables.md +0 -462
- package/dist/xanoscript_docs_auto/version.json +0 -5
|
@@ -1,692 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
applyTo: "**/*.xs"
|
|
3
|
-
---
|
|
4
|
-
|
|
5
|
-
# Quickstart & Common Patterns
|
|
6
|
-
|
|
7
|
-
Essential patterns for XanoScript development. Use this as a quick reference for frequently-used code patterns.
|
|
8
|
-
|
|
9
|
-
> **TL;DR:** Quick reference for common patterns. Key rules: use `text` not `string`, `elseif` not `else if`, `params` not `body` for api.request, parentheses around filters in expressions.
|
|
10
|
-
|
|
11
|
-
## Quick Reference
|
|
12
|
-
|
|
13
|
-
### Variable Access Rules
|
|
14
|
-
|
|
15
|
-
**Inputs must use `$input.fieldname` — no shorthand exists.**
|
|
16
|
-
|
|
17
|
-
Input fields declared in the `input {}` block are only accessible via the `$input` prefix. You cannot reference them as bare variables.
|
|
18
|
-
|
|
19
|
-
```xs
|
|
20
|
-
input {
|
|
21
|
-
text name
|
|
22
|
-
int age
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// ❌ Wrong — $name is not defined; inputs live on $input
|
|
26
|
-
var $greeting { value = "Hello, " ~ $name }
|
|
27
|
-
|
|
28
|
-
// ✅ Correct
|
|
29
|
-
var $greeting { value = "Hello, " ~ $input.name }
|
|
30
|
-
var $is_adult { value = $input.age >= 18 }
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
**Stack variables have an optional `$var.` prefix — both forms are identical.**
|
|
34
|
-
|
|
35
|
-
```xs
|
|
36
|
-
var $total { value = 100 }
|
|
37
|
-
|
|
38
|
-
// ✅ Both are the same thing
|
|
39
|
-
$var.total // explicit prefix form
|
|
40
|
-
$total // shorthand form (no prefix)
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
### Reserved Variable Names
|
|
44
|
-
|
|
45
|
-
These variable names are reserved and cannot be used:
|
|
46
|
-
|
|
47
|
-
| Variable | Description |
|
|
48
|
-
|----------|-------------|
|
|
49
|
-
| `$response` | API/function response (auto-populated) |
|
|
50
|
-
| `$output` | Output value |
|
|
51
|
-
| `$input` | Input parameters from request/function call |
|
|
52
|
-
| `$auth` | Authenticated user context |
|
|
53
|
-
| `$env` | Environment variables and request context |
|
|
54
|
-
| `$db` | Database table reference for queries |
|
|
55
|
-
| `$this` | Current context reference |
|
|
56
|
-
| `$result` | Used in reduce operations |
|
|
57
|
-
| `$index` | Reserved for some array operations |
|
|
58
|
-
|
|
59
|
-
```xs
|
|
60
|
-
// ❌ Wrong - using reserved variable name
|
|
61
|
-
var $response { value = "test" } // Error: $response is reserved
|
|
62
|
-
|
|
63
|
-
// ✅ Correct - use a different name
|
|
64
|
-
var $api_response { value = "test" }
|
|
65
|
-
var $my_result { value = "test" }
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
### Type Names (Common Aliases)
|
|
69
|
-
|
|
70
|
-
XanoScript uses specific type names. Common aliases from other languages won't work.
|
|
71
|
-
|
|
72
|
-
> **Full reference:** For complete type details and validation, see `xanoscript_docs({ topic: "types" })`.
|
|
73
|
-
|
|
74
|
-
| ❌ Wrong | ✅ Correct | Description |
|
|
75
|
-
|----------|------------|-------------|
|
|
76
|
-
| `boolean` | `bool` | Boolean true/false |
|
|
77
|
-
| `integer` | `int` | 32-bit integer |
|
|
78
|
-
| `string` | `text` | UTF-8 string |
|
|
79
|
-
| `number` | `decimal` | Floating-point number |
|
|
80
|
-
| `float` | `decimal` | Floating-point number |
|
|
81
|
-
| `array` | `type[]` | Array (e.g., `text[]`, `int[]`) |
|
|
82
|
-
| `list` | `type[]` | Array (e.g., `text[]`, `int[]`) |
|
|
83
|
-
|
|
84
|
-
```xs
|
|
85
|
-
// ❌ Wrong - invalid type names
|
|
86
|
-
input {
|
|
87
|
-
boolean is_active // Error: use "bool"
|
|
88
|
-
integer count // Error: use "int"
|
|
89
|
-
string name // Error: use "text"
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// ✅ Correct - proper XanoScript types
|
|
93
|
-
input {
|
|
94
|
-
bool is_active
|
|
95
|
-
int count
|
|
96
|
-
text name
|
|
97
|
-
}
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
### Variable Declaration
|
|
101
|
-
```xs
|
|
102
|
-
var $name { value = "initial value" }
|
|
103
|
-
var $count { value = 0 }
|
|
104
|
-
var $data { value = { key: "value" } }
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
### Conditional Logic
|
|
108
|
-
```xs
|
|
109
|
-
conditional {
|
|
110
|
-
if ($status == "active") {
|
|
111
|
-
var $result { value = "Active user" }
|
|
112
|
-
}
|
|
113
|
-
elseif ($status == "pending") {
|
|
114
|
-
var $result { value = "Pending approval" }
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
var $result { value = "Unknown status" }
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
### Early Return
|
|
123
|
-
|
|
124
|
-
Stop execution and return a value immediately. Useful for guard clauses and skipping unnecessary work.
|
|
125
|
-
|
|
126
|
-
```xs
|
|
127
|
-
// Guard clause - return early if condition is met
|
|
128
|
-
conditional {
|
|
129
|
-
if ($input.skip) {
|
|
130
|
-
return { value = null }
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Return early with a value
|
|
135
|
-
conditional {
|
|
136
|
-
if ($input.use_cache && $cached_result != null) {
|
|
137
|
-
return { value = $cached_result }
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Short-circuit with a successful response
|
|
142
|
-
db.get "order" {
|
|
143
|
-
field_name = "id"
|
|
144
|
-
field_value = $input.order_id
|
|
145
|
-
} as $order
|
|
146
|
-
|
|
147
|
-
conditional {
|
|
148
|
-
if ($order.status == "completed") {
|
|
149
|
-
return { value = $order }
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Continue with expensive processing for non-completed orders...
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
### API Request with Error Handling
|
|
157
|
-
```xs
|
|
158
|
-
api.request {
|
|
159
|
-
url = "https://api.example.com/data"
|
|
160
|
-
method = "POST"
|
|
161
|
-
params = $payload // Note: "params" is used for request body, NOT "body"
|
|
162
|
-
headers = ["Content-Type: application/json", "Authorization: Bearer " ~ $env.API_KEY]
|
|
163
|
-
} as $api_result
|
|
164
|
-
|
|
165
|
-
precondition ($api_result.response.status == 200) {
|
|
166
|
-
error_type = "standard"
|
|
167
|
-
error = "API request failed"
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
var $data { value = $api_result.response.result }
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
### api.request Response Structure
|
|
174
|
-
The response object contains:
|
|
175
|
-
```xs
|
|
176
|
-
$result.response.status // HTTP status code (200, 404, 500, etc.)
|
|
177
|
-
$result.response.result // Parsed response body (JSON decoded)
|
|
178
|
-
$result.response.headers // Response headers object
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
### While Loop
|
|
182
|
-
```xs
|
|
183
|
-
stack {
|
|
184
|
-
var $counter { value = 0 }
|
|
185
|
-
while ($counter < 10) {
|
|
186
|
-
each {
|
|
187
|
-
var.update $counter { value = $counter + 1 }
|
|
188
|
-
debug.log { value = "Iteration: " ~ ($counter|to_text) }
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
### String Concatenation
|
|
195
|
-
```xs
|
|
196
|
-
var $greeting { value = "Hello, " ~ $input.name ~ "!" }
|
|
197
|
-
|
|
198
|
-
// With filters (use parentheses)
|
|
199
|
-
var $message { value = "Status: " ~ ($status|to_text) ~ " - " ~ ($data|json_encode) }
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
### Input Block Syntax
|
|
203
|
-
|
|
204
|
-
> **Full reference:** For complete input types and validation options, see `xanoscript_docs({ topic: "types" })`.
|
|
205
|
-
|
|
206
|
-
The `?` has different meanings depending on placement:
|
|
207
|
-
- **After the type** (`text?`) → **nullable** (value can be `null`)
|
|
208
|
-
- **After the variable name** (`name?`) → **not required** (optional)
|
|
209
|
-
|
|
210
|
-
```xs
|
|
211
|
-
input {
|
|
212
|
-
// Required, not nullable
|
|
213
|
-
text name
|
|
214
|
-
|
|
215
|
-
// Required, nullable (caller must provide, but can send null)
|
|
216
|
-
text? name
|
|
217
|
-
|
|
218
|
-
// Optional, not nullable (can be omitted, but if sent must have value)
|
|
219
|
-
text name?
|
|
220
|
-
|
|
221
|
-
// Optional AND nullable (can be omitted or sent as null)
|
|
222
|
-
text? name?
|
|
223
|
-
|
|
224
|
-
// Optional with default value
|
|
225
|
-
text role?="user"
|
|
226
|
-
|
|
227
|
-
// With filters applied
|
|
228
|
-
email contact filters=trim|lower
|
|
229
|
-
|
|
230
|
-
// Array type
|
|
231
|
-
text[] tags filters=trim
|
|
232
|
-
|
|
233
|
-
// Nested object with schema
|
|
234
|
-
object address {
|
|
235
|
-
schema {
|
|
236
|
-
text street
|
|
237
|
-
text city
|
|
238
|
-
text country?="US"
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
### Error Types Reference
|
|
245
|
-
|
|
246
|
-
> **Full reference:** For try-catch, throw, and preconditions, see `xanoscript_docs({ topic: "syntax" })`.
|
|
247
|
-
|
|
248
|
-
| Type | HTTP Status | Use Case |
|
|
249
|
-
|------|-------------|----------|
|
|
250
|
-
| `inputerror` | 400 Bad Request | Invalid input data |
|
|
251
|
-
| `accessdenied` | 403 Forbidden | Authorization failure |
|
|
252
|
-
| `notfound` | 404 Not Found | Resource doesn't exist |
|
|
253
|
-
| `standard` | 500 Internal Server Error | General errors |
|
|
254
|
-
|
|
255
|
-
### Quick Filter Reference
|
|
256
|
-
|
|
257
|
-
The most common filters at a glance:
|
|
258
|
-
|
|
259
|
-
| Filter | Example | Result |
|
|
260
|
-
|--------|---------|--------|
|
|
261
|
-
| `trim` | `" hello "|trim` | `"hello"` |
|
|
262
|
-
| `to_lower` | `"HELLO"|to_lower` | `"hello"` |
|
|
263
|
-
| `first` | `[1,2,3]|first` | `1` |
|
|
264
|
-
| `count` | `[1,2,3]|count` | `3` |
|
|
265
|
-
| `to_int` | `"42"|to_int` | `42` |
|
|
266
|
-
| `json_encode` | `{a:1}|json_encode` | `"{\"a\":1}"` |
|
|
267
|
-
| `get` | `$obj|get:"key":"default"` | value or default |
|
|
268
|
-
|
|
269
|
-
**Null handling:** `$val ?? "default"` or `$val|first_notnull:"default"`
|
|
270
|
-
|
|
271
|
-
> **Full reference:** See `xanoscript_docs({ topic: "syntax" })` for all 100+ filters organized by category (string, array, object, type, date, encoding).
|
|
272
|
-
|
|
273
|
-
---
|
|
274
|
-
|
|
275
|
-
## Common Patterns
|
|
276
|
-
|
|
277
|
-
### 1. Input Validation
|
|
278
|
-
|
|
279
|
-
```xs
|
|
280
|
-
// Required field check
|
|
281
|
-
precondition ($input.email != null && $input.email != "") {
|
|
282
|
-
error_type = "inputerror"
|
|
283
|
-
error = "Email is required"
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// Format validation
|
|
287
|
-
precondition ($input.email|contains:"@") {
|
|
288
|
-
error_type = "inputerror"
|
|
289
|
-
error = "Invalid email format"
|
|
290
|
-
}
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
### 2. Database CRUD
|
|
294
|
-
|
|
295
|
-
| Operation | Use Case | Example |
|
|
296
|
-
|-----------|----------|---------|
|
|
297
|
-
| `db.get` | Single record by ID | `db.get "users" { field_name = "id" field_value = 1 } as $user` |
|
|
298
|
-
| `db.query` | Filtered list | `db.query "users" { where = $db.users.active == true } as $users` |
|
|
299
|
-
| `db.add` | Insert | `db.add "users" { data = { name: "John" } } as $new` |
|
|
300
|
-
| `db.edit` | Update | `db.edit "users" { field_name = "id" field_value = 1 data = { name: "Jane" } }` |
|
|
301
|
-
| `db.del` | Delete | `db.del "users" { field_name = "id" field_value = 1 }` |
|
|
302
|
-
|
|
303
|
-
> **Full reference:** See `xanoscript_docs({ topic: "database" })` for joins, bulk operations, transactions, and more.
|
|
304
|
-
|
|
305
|
-
### 3. Optional Field Handling
|
|
306
|
-
|
|
307
|
-
```xs
|
|
308
|
-
// Build object with optional fields
|
|
309
|
-
var $data { value = { required_field: $input.required } }
|
|
310
|
-
|
|
311
|
-
conditional {
|
|
312
|
-
if ($input.optional_field != null) {
|
|
313
|
-
var.update $data { value = $data|set:"optional_field":$input.optional_field }
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
// Or use set_ifnotnull
|
|
318
|
-
var $data {
|
|
319
|
-
value = { required: $input.required }|set_ifnotnull:"optional":$input.optional
|
|
320
|
-
}
|
|
321
|
-
```
|
|
322
|
-
|
|
323
|
-
### 4. Loop Through Array
|
|
324
|
-
|
|
325
|
-
```xs
|
|
326
|
-
// Using foreach
|
|
327
|
-
foreach ($items) {
|
|
328
|
-
each as $item {
|
|
329
|
-
debug.log { value = $item.name }
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
// Using map filter
|
|
334
|
-
var $names { value = $items|map:$$.name }
|
|
335
|
-
|
|
336
|
-
// Using filter
|
|
337
|
-
var $active_items { value = $items|filter:$$.is_active == true }
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
### 5. Error Handling with Try-Catch
|
|
341
|
-
|
|
342
|
-
> **Full reference:** For all error handling patterns, see `xanoscript_docs({ topic: "syntax" })`.
|
|
343
|
-
|
|
344
|
-
```xs
|
|
345
|
-
try_catch {
|
|
346
|
-
try {
|
|
347
|
-
api.request {
|
|
348
|
-
url = "https://api.example.com/risky"
|
|
349
|
-
method = "GET"
|
|
350
|
-
} as $result
|
|
351
|
-
}
|
|
352
|
-
catch {
|
|
353
|
-
debug.log { value = "Request failed, using fallback" }
|
|
354
|
-
var $result { value = { response: { result: null } } }
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
```
|
|
358
|
-
|
|
359
|
-
### 6. Authentication Check
|
|
360
|
-
|
|
361
|
-
> **Full reference:** For security best practices, see `xanoscript_docs({ topic: "security" })`.
|
|
362
|
-
|
|
363
|
-
```xs
|
|
364
|
-
// Require authenticated user
|
|
365
|
-
precondition ($auth.id != null) {
|
|
366
|
-
error_type = "accessdenied"
|
|
367
|
-
error = "Authentication required"
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
// Check user owns resource
|
|
371
|
-
db.get "post" {
|
|
372
|
-
where = $db.post.id == $input.post_id
|
|
373
|
-
} as $post
|
|
374
|
-
|
|
375
|
-
precondition ($post.user_id == $auth.id) {
|
|
376
|
-
error_type = "accessdenied"
|
|
377
|
-
error = "You can only edit your own posts"
|
|
378
|
-
}
|
|
379
|
-
```
|
|
380
|
-
|
|
381
|
-
### 7. Pagination
|
|
382
|
-
|
|
383
|
-
```xs
|
|
384
|
-
var $page { value = $input.page ?? 1 }
|
|
385
|
-
var $limit { value = $input.limit ?? 20 }
|
|
386
|
-
var $offset { value = ($page - 1) * $limit }
|
|
387
|
-
|
|
388
|
-
db.query "item" {
|
|
389
|
-
where = $db.item.is_active == true
|
|
390
|
-
order = [{ field: created_at, direction: desc }]
|
|
391
|
-
paging = { limit: $limit, offset: $offset }
|
|
392
|
-
count = true
|
|
393
|
-
} as $result
|
|
394
|
-
|
|
395
|
-
response = {
|
|
396
|
-
items: $result.items,
|
|
397
|
-
total: $result.count,
|
|
398
|
-
page: $page,
|
|
399
|
-
pages: ($result.count / $limit)|ceil
|
|
400
|
-
}
|
|
401
|
-
```
|
|
402
|
-
|
|
403
|
-
### 8. Building Complex Objects
|
|
404
|
-
|
|
405
|
-
```xs
|
|
406
|
-
// Step by step
|
|
407
|
-
var $response { value = {} }
|
|
408
|
-
var.update $response { value = $response|set:"user":$user }
|
|
409
|
-
var.update $response { value = $response|set:"posts":$posts }
|
|
410
|
-
var.update $response { value = $response|set:"stats":{ count: $posts|count } }
|
|
411
|
-
|
|
412
|
-
// Or all at once
|
|
413
|
-
var $response {
|
|
414
|
-
value = {
|
|
415
|
-
user: $user,
|
|
416
|
-
posts: $posts,
|
|
417
|
-
stats: { count: $posts|count }
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
```
|
|
421
|
-
|
|
422
|
-
### 9. Date/Time Operations
|
|
423
|
-
|
|
424
|
-
> **Full reference:** For all date/time filters, see `xanoscript_docs({ topic: "syntax" })`.
|
|
425
|
-
|
|
426
|
-
```xs
|
|
427
|
-
// Current timestamp
|
|
428
|
-
var $now { value = now }
|
|
429
|
-
|
|
430
|
-
// Format for display
|
|
431
|
-
var $formatted { value = now|format_timestamp:"Y-m-d H:i:s":"UTC" }
|
|
432
|
-
|
|
433
|
-
// Relative time
|
|
434
|
-
var $yesterday { value = now|transform_timestamp:"-1 day" }
|
|
435
|
-
var $next_week { value = now|transform_timestamp:"+7 days" }
|
|
436
|
-
|
|
437
|
-
// Compare dates
|
|
438
|
-
db.query "event" {
|
|
439
|
-
where = $db.event.start_date >= now
|
|
440
|
-
} as $upcoming_events
|
|
441
|
-
```
|
|
442
|
-
|
|
443
|
-
### 10. JSON API Response
|
|
444
|
-
|
|
445
|
-
> **Full reference:** For external API patterns, see `xanoscript_docs({ topic: "integrations" })`.
|
|
446
|
-
|
|
447
|
-
```xs
|
|
448
|
-
api.request {
|
|
449
|
-
url = "https://api.openai.com/v1/chat/completions"
|
|
450
|
-
method = "POST"
|
|
451
|
-
params = {
|
|
452
|
-
model: "gpt-4",
|
|
453
|
-
messages: [{ role: "user", content: $input.prompt }]
|
|
454
|
-
}
|
|
455
|
-
headers = [
|
|
456
|
-
"Content-Type: application/json",
|
|
457
|
-
"Authorization: Bearer " ~ $env.OPENAI_API_KEY
|
|
458
|
-
]
|
|
459
|
-
} as $api_result
|
|
460
|
-
|
|
461
|
-
conditional {
|
|
462
|
-
if ($api_result.response.status == 200) {
|
|
463
|
-
var $answer { value = $api_result.response.result.choices|first|get:"message"|get:"content" }
|
|
464
|
-
}
|
|
465
|
-
else {
|
|
466
|
-
throw {
|
|
467
|
-
name = "APIError"
|
|
468
|
-
value = "OpenAI API error: " ~ ($api_result.response.status|to_text)
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
```
|
|
473
|
-
|
|
474
|
-
---
|
|
475
|
-
|
|
476
|
-
## Common Mistakes
|
|
477
|
-
|
|
478
|
-
### 1. Using `else if` instead of `elseif`
|
|
479
|
-
```xs
|
|
480
|
-
// ❌ Wrong
|
|
481
|
-
conditional {
|
|
482
|
-
if (...) { }
|
|
483
|
-
else if (...) { } // Parse error!
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
// ✅ Correct
|
|
487
|
-
conditional {
|
|
488
|
-
if (...) { }
|
|
489
|
-
elseif (...) { }
|
|
490
|
-
}
|
|
491
|
-
```
|
|
492
|
-
|
|
493
|
-
### 2. Missing parentheses in filter concatenation
|
|
494
|
-
```xs
|
|
495
|
-
// ❌ Wrong - parse error
|
|
496
|
-
var $msg { value = $status|to_text ~ " - " ~ $data|json_encode }
|
|
497
|
-
|
|
498
|
-
// ✅ Correct - wrap filtered expressions in parentheses
|
|
499
|
-
var $msg { value = ($status|to_text) ~ " - " ~ ($data|json_encode) }
|
|
500
|
-
```
|
|
501
|
-
|
|
502
|
-
### 3. Missing parentheses in filter comparisons
|
|
503
|
-
```xs
|
|
504
|
-
// ❌ Wrong - parse error
|
|
505
|
-
if ($array|count > 0) { }
|
|
506
|
-
|
|
507
|
-
// ✅ Correct - wrap filter expression in parentheses
|
|
508
|
-
if (($array|count) > 0) { }
|
|
509
|
-
```
|
|
510
|
-
|
|
511
|
-
### 4. Using `body` instead of `params` for api.request
|
|
512
|
-
```xs
|
|
513
|
-
// ❌ Wrong - "body" is not valid
|
|
514
|
-
api.request {
|
|
515
|
-
url = "..."
|
|
516
|
-
method = "POST"
|
|
517
|
-
body = $payload
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
// ✅ Correct - use "params" for request body
|
|
521
|
-
api.request {
|
|
522
|
-
url = "..."
|
|
523
|
-
method = "POST"
|
|
524
|
-
params = $payload
|
|
525
|
-
}
|
|
526
|
-
```
|
|
527
|
-
|
|
528
|
-
### 5. Using `default` filter (doesn't exist)
|
|
529
|
-
```xs
|
|
530
|
-
// ❌ Wrong - no "default" filter exists
|
|
531
|
-
var $value { value = $input.optional|default:"fallback" }
|
|
532
|
-
|
|
533
|
-
// ✅ Correct - use first_notnull or ?? operator
|
|
534
|
-
var $value { value = $input.optional|first_notnull:"fallback" }
|
|
535
|
-
// or
|
|
536
|
-
var $value { value = $input.optional ?? "fallback" }
|
|
537
|
-
|
|
538
|
-
// For object key access with default, use get with 3rd parameter
|
|
539
|
-
var $val { value = $obj|get:"key":"default_value" }
|
|
540
|
-
```
|
|
541
|
-
|
|
542
|
-
### 6. Using reserved variable names
|
|
543
|
-
```xs
|
|
544
|
-
// ❌ Wrong - $response is reserved
|
|
545
|
-
var $response { value = "test" }
|
|
546
|
-
|
|
547
|
-
// ✅ Correct - use a different name
|
|
548
|
-
var $api_response { value = "test" }
|
|
549
|
-
```
|
|
550
|
-
|
|
551
|
-
### 7. Wrong type names
|
|
552
|
-
```xs
|
|
553
|
-
// ❌ Wrong - invalid type names
|
|
554
|
-
input {
|
|
555
|
-
boolean active // Use "bool"
|
|
556
|
-
integer count // Use "int"
|
|
557
|
-
string name // Use "text"
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
// ✅ Correct
|
|
561
|
-
input {
|
|
562
|
-
bool active
|
|
563
|
-
int count
|
|
564
|
-
text name
|
|
565
|
-
}
|
|
566
|
-
```
|
|
567
|
-
|
|
568
|
-
### 8. Object literal syntax (using = instead of :)
|
|
569
|
-
```xs
|
|
570
|
-
// ❌ Wrong - object literals use : not =
|
|
571
|
-
var $data { value = { customer = $id } }
|
|
572
|
-
|
|
573
|
-
// ✅ Correct - use : for object properties
|
|
574
|
-
var $data { value = { customer: $id } }
|
|
575
|
-
```
|
|
576
|
-
|
|
577
|
-
### 9. Commas in block properties (`=`) vs object literals (`:`)
|
|
578
|
-
|
|
579
|
-
Block properties use `=` and go on **separate lines with no commas**. Object literals use `:` and **require commas** between entries.
|
|
580
|
-
|
|
581
|
-
```xs
|
|
582
|
-
// ❌ Wrong - block properties don't use commas
|
|
583
|
-
throw {
|
|
584
|
-
name = "Error",
|
|
585
|
-
value = "message"
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
// ✅ Correct - no commas between block properties (=)
|
|
589
|
-
throw {
|
|
590
|
-
name = "Error"
|
|
591
|
-
value = "message"
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
// ✅ Correct - object literals (:) DO use commas
|
|
595
|
-
db.query "dad_jokes" {
|
|
596
|
-
sort = {dad_jokes.id: "rand", dad_jokes.joke: "asc"}
|
|
597
|
-
return = {type: "single"}
|
|
598
|
-
} as $joke
|
|
599
|
-
```
|
|
600
|
-
|
|
601
|
-
### 10. Using $env in run.job input blocks
|
|
602
|
-
```xs
|
|
603
|
-
// ❌ Wrong - $env not allowed in input blocks
|
|
604
|
-
run.job "my_job" {
|
|
605
|
-
input {
|
|
606
|
-
text api_key = $env.API_KEY
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
// ✅ Correct - access $env in the stack instead
|
|
611
|
-
run.job "my_job" {
|
|
612
|
-
stack {
|
|
613
|
-
var $api_key { value = $env.API_KEY }
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
```
|
|
617
|
-
|
|
618
|
-
### 11. Using `object` type without schema
|
|
619
|
-
```xs
|
|
620
|
-
// ❌ Wrong - object requires a schema
|
|
621
|
-
input {
|
|
622
|
-
object data // Error: needs schema
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
// ✅ Correct - use json for arbitrary data
|
|
626
|
-
input {
|
|
627
|
-
json data // Accepts any JSON
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
// ✅ Or define a schema for object
|
|
631
|
-
input {
|
|
632
|
-
object data {
|
|
633
|
-
schema {
|
|
634
|
-
text name
|
|
635
|
-
int id
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
```
|
|
640
|
-
|
|
641
|
-
### 12. While loop outside of stack block
|
|
642
|
-
```xs
|
|
643
|
-
// ❌ Wrong - while must be inside stack
|
|
644
|
-
while (true) {
|
|
645
|
-
each { ... }
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
// ✅ Correct - wrap in stack block
|
|
649
|
-
stack {
|
|
650
|
-
while (true) {
|
|
651
|
-
each { ... }
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
```
|
|
655
|
-
|
|
656
|
-
### 13. Multiple constructs in one file
|
|
657
|
-
Each `.xs` file must contain exactly **one** construct. Placing two constructs in the same file causes a parse error.
|
|
658
|
-
|
|
659
|
-
```xs
|
|
660
|
-
// ❌ Wrong - two constructs in one file
|
|
661
|
-
function "helper_a" {
|
|
662
|
-
stack { ... }
|
|
663
|
-
response = $result
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
function "helper_b" {
|
|
667
|
-
stack { ... }
|
|
668
|
-
response = $result
|
|
669
|
-
}
|
|
670
|
-
```
|
|
671
|
-
|
|
672
|
-
```
|
|
673
|
-
// ✅ Correct - one construct per file
|
|
674
|
-
// function/helper_a.xs → contains only "helper_a"
|
|
675
|
-
// function/helper_b.xs → contains only "helper_b"
|
|
676
|
-
```
|
|
677
|
-
|
|
678
|
-
---
|
|
679
|
-
|
|
680
|
-
## Related Topics
|
|
681
|
-
|
|
682
|
-
Explore more with `xanoscript_docs({ topic: "<topic>" })`:
|
|
683
|
-
|
|
684
|
-
| Topic | Description |
|
|
685
|
-
|-------|-------------|
|
|
686
|
-
| `syntax` | Complete filter reference, operators, system variables |
|
|
687
|
-
| `types` | Data types, input validation, schema definitions |
|
|
688
|
-
| `database` | All db.* operations: query, get, add, edit, delete |
|
|
689
|
-
| `functions` | Reusable function stacks, async patterns, loops |
|
|
690
|
-
| `apis` | HTTP endpoints, authentication, CRUD patterns |
|
|
691
|
-
| `security` | Security best practices and authentication |
|
|
692
|
-
| `integrations` | External API patterns (OpenAI, Stripe, etc.) |
|