bxo 0.0.10-dev.6 โ 0.0.10-dev.7
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/example/smart-auto-array-example.ts +99 -0
- package/package.json +1 -1
- package/src/index.ts +15 -5
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import BXO, { z } from "../src";
|
|
2
|
+
|
|
3
|
+
async function main() {
|
|
4
|
+
console.log("๐ Testing BXO smart autoArrayParse feature\n");
|
|
5
|
+
|
|
6
|
+
// Example 1: Smart autoArrayParse (enabled by default)
|
|
7
|
+
console.log("๐ Example 1: Smart autoArrayParse - only converts when schema expects arrays");
|
|
8
|
+
const app1 = new BXO({ serve: { port: 3001 } }); // Default: autoArrayParse: true
|
|
9
|
+
|
|
10
|
+
// Route with mixed schema - some fields expect arrays, others don't
|
|
11
|
+
app1.get("/api/search", (ctx) => {
|
|
12
|
+
return ctx.json({
|
|
13
|
+
message: "Smart autoArrayParse - only arrays where expected",
|
|
14
|
+
tags: ctx.query.tags, // Should be array (schema expects array)
|
|
15
|
+
categories: ctx.query.categories, // Should be array (schema expects array)
|
|
16
|
+
query: ctx.query.query, // Should be string (schema expects string)
|
|
17
|
+
limit: ctx.query.limit, // Should be string (schema expects string)
|
|
18
|
+
isArray: {
|
|
19
|
+
tags: Array.isArray(ctx.query.tags),
|
|
20
|
+
categories: Array.isArray(ctx.query.categories),
|
|
21
|
+
query: Array.isArray(ctx.query.query),
|
|
22
|
+
limit: Array.isArray(ctx.query.limit)
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}, {
|
|
26
|
+
query: z.object({
|
|
27
|
+
tags: z.array(z.string()).optional(), // Expects array
|
|
28
|
+
categories: z.array(z.string()).optional(), // Expects array
|
|
29
|
+
query: z.string().optional(), // Expects string
|
|
30
|
+
limit: z.string().optional() // Expects string
|
|
31
|
+
})
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Example 2: Disabled autoArrayParse
|
|
35
|
+
console.log("๐ Example 2: Disabled autoArrayParse - original behavior");
|
|
36
|
+
const app2 = new BXO({
|
|
37
|
+
serve: { port: 3002 },
|
|
38
|
+
autoArrayParse: false
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
app2.get("/api/search", (ctx) => {
|
|
42
|
+
return ctx.json({
|
|
43
|
+
message: "Disabled autoArrayParse - original behavior",
|
|
44
|
+
tags: ctx.query.tags,
|
|
45
|
+
categories: ctx.query.categories,
|
|
46
|
+
query: ctx.query.query,
|
|
47
|
+
limit: ctx.query.limit,
|
|
48
|
+
isArray: {
|
|
49
|
+
tags: Array.isArray(ctx.query.tags),
|
|
50
|
+
categories: Array.isArray(ctx.query.categories),
|
|
51
|
+
query: Array.isArray(ctx.query.query),
|
|
52
|
+
limit: Array.isArray(ctx.query.limit)
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}, {
|
|
56
|
+
query: z.object({
|
|
57
|
+
tags: z.array(z.string()).optional(),
|
|
58
|
+
categories: z.array(z.string()).optional(),
|
|
59
|
+
query: z.string().optional(),
|
|
60
|
+
limit: z.string().optional()
|
|
61
|
+
})
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Example 3: Route without schema (no autoArrayParse effect)
|
|
65
|
+
console.log("๐ Example 3: No schema - autoArrayParse has no effect");
|
|
66
|
+
const app3 = new BXO({
|
|
67
|
+
serve: { port: 3003 },
|
|
68
|
+
autoArrayParse: true
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
app3.get("/api/no-schema", (ctx) => {
|
|
72
|
+
return ctx.json({
|
|
73
|
+
message: "No schema - autoArrayParse has no effect",
|
|
74
|
+
tags: ctx.query.tags,
|
|
75
|
+
query: ctx.query.query,
|
|
76
|
+
isArray: {
|
|
77
|
+
tags: Array.isArray(ctx.query.tags),
|
|
78
|
+
query: Array.isArray(ctx.query.query)
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Start all servers
|
|
84
|
+
app1.start();
|
|
85
|
+
app2.start();
|
|
86
|
+
app3.start();
|
|
87
|
+
|
|
88
|
+
console.log("โ
All servers started!");
|
|
89
|
+
console.log("\n๐งช Test URLs:");
|
|
90
|
+
console.log("1. Smart (enabled): http://localhost:3001/api/search?tags=javascript&categories=web&query=search&limit=10");
|
|
91
|
+
console.log("2. Disabled: http://localhost:3002/api/search?tags=javascript&categories=web&query=search&limit=10");
|
|
92
|
+
console.log("3. No schema: http://localhost:3003/api/no-schema?tags=javascript&query=search");
|
|
93
|
+
console.log("\n๐ Multiple values:");
|
|
94
|
+
console.log("1. Smart: http://localhost:3001/api/search?tags=javascript&tags=typescript&categories=web&query=search");
|
|
95
|
+
console.log("2. Disabled: http://localhost:3002/api/search?tags=javascript&tags=typescript&categories=web&query=search");
|
|
96
|
+
console.log("3. No schema: http://localhost:3003/api/no-schema?tags=javascript&tags=typescript&query=search");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
main();
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -220,11 +220,21 @@ function parseQuery(searchParams: URLSearchParams, schemaOrAutoArrayParse?: z.Zo
|
|
|
220
220
|
}
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
-
// Apply autoArrayParse transformation if enabled
|
|
224
|
-
if (shouldAutoArrayParse) {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
223
|
+
// Apply smart autoArrayParse transformation if enabled and schema is provided
|
|
224
|
+
if (shouldAutoArrayParse && schema) {
|
|
225
|
+
// Check if schema expects arrays for specific fields
|
|
226
|
+
const schemaShape = (schema as any)._def?.shape;
|
|
227
|
+
if (schemaShape) {
|
|
228
|
+
for (const [key, value] of Object.entries(out)) {
|
|
229
|
+
const fieldSchema = schemaShape[key];
|
|
230
|
+
if (fieldSchema && typeof value === 'string') {
|
|
231
|
+
// Check if the field schema expects an array
|
|
232
|
+
const isArraySchema = fieldSchema._def?.typeName === 'ZodArray' ||
|
|
233
|
+
(fieldSchema._def?.innerType && fieldSchema._def?.typeName === 'ZodOptional' && fieldSchema._def?.innerType._def?.typeName === 'ZodArray');
|
|
234
|
+
if (isArraySchema) {
|
|
235
|
+
out[key] = [value];
|
|
236
|
+
}
|
|
237
|
+
}
|
|
228
238
|
}
|
|
229
239
|
}
|
|
230
240
|
}
|