Skip to content

Commit 5c1c4fe

Browse files
Copilothotlong
andcommitted
feat(json-rpc): add comprehensive tests and docs for Q2 features
- Add 18 E2E tests for object.count(), action.execute(), and batch requests - Enhance README with detailed examples for all three features - Document batch requests per JSON-RPC 2.0 spec §6 - Include examples for notifications, partial errors, and call chaining - All 135 tests passing Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 0edea55 commit 5c1c4fe

2 files changed

Lines changed: 1102 additions & 6 deletions

File tree

packages/protocols/json-rpc/README.md

Lines changed: 300 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,85 @@ Delete a record.
167167

168168
Count records matching filters.
169169

170-
**Request:**
170+
**Request (no filter - count all):**
171171
```json
172172
{
173173
"jsonrpc": "2.0",
174174
"method": "object.count",
175-
"params": ["users", {"active": true}],
175+
"params": ["users"],
176+
"id": 6
177+
}
178+
```
179+
180+
**Response:**
181+
```json
182+
{
183+
"jsonrpc": "2.0",
184+
"result": 42,
176185
"id": 6
177186
}
178187
```
179188

189+
**Request (with filter):**
190+
```json
191+
{
192+
"jsonrpc": "2.0",
193+
"method": "object.count",
194+
"params": ["users", {
195+
"type": "comparison",
196+
"field": "active",
197+
"operator": "=",
198+
"value": true
199+
}],
200+
"id": 7
201+
}
202+
```
203+
204+
**Response:**
205+
```json
206+
{
207+
"jsonrpc": "2.0",
208+
"result": 28,
209+
"id": 7
210+
}
211+
```
212+
213+
**Request (with complex filter):**
214+
```json
215+
{
216+
"jsonrpc": "2.0",
217+
"method": "object.count",
218+
"params": ["users", {
219+
"type": "logical",
220+
"operator": "and",
221+
"conditions": [
222+
{
223+
"type": "comparison",
224+
"field": "active",
225+
"operator": "=",
226+
"value": true
227+
},
228+
{
229+
"type": "comparison",
230+
"field": "role",
231+
"operator": "=",
232+
"value": "admin"
233+
}
234+
]
235+
}],
236+
"id": 8
237+
}
238+
```
239+
240+
**Response:**
241+
```json
242+
{
243+
"jsonrpc": "2.0",
244+
"result": 5,
245+
"id": 8
246+
}
247+
```
248+
180249
### Metadata Methods
181250

182251
#### metadata.list
@@ -245,6 +314,59 @@ Execute a custom action.
245314
}
246315
```
247316

317+
**Response:**
318+
```json
319+
{
320+
"jsonrpc": "2.0",
321+
"result": {
322+
"success": true,
323+
"messageId": "msg_1234567890",
324+
"to": "user@example.com",
325+
"subject": "Hello"
326+
},
327+
"id": 10
328+
}
329+
```
330+
331+
**Example: Calculate Discount**
332+
```json
333+
{
334+
"jsonrpc": "2.0",
335+
"method": "action.execute",
336+
"params": ["calculateDiscount", {
337+
"amount": 100,
338+
"percentage": 20
339+
}],
340+
"id": 11
341+
}
342+
```
343+
344+
**Response:**
345+
```json
346+
{
347+
"jsonrpc": "2.0",
348+
"result": {
349+
"originalAmount": 100,
350+
"discountPercentage": 20,
351+
"discountAmount": 20,
352+
"finalAmount": 80
353+
},
354+
"id": 11
355+
}
356+
```
357+
358+
**Error Response (action not found):**
359+
```json
360+
{
361+
"jsonrpc": "2.0",
362+
"error": {
363+
"code": -32603,
364+
"message": "Action not found: unknownAction"
365+
},
366+
"id": 12
367+
}
368+
```
369+
248370
#### action.list
249371

250372
List all available actions.
@@ -254,7 +376,21 @@ List all available actions.
254376
{
255377
"jsonrpc": "2.0",
256378
"method": "action.list",
257-
"id": 11
379+
"id": 13
380+
}
381+
```
382+
383+
**Response:**
384+
```json
385+
{
386+
"jsonrpc": "2.0",
387+
"result": [
388+
"sendEmail",
389+
"calculateDiscount",
390+
"processPayment",
391+
"generateReport"
392+
],
393+
"id": 13
258394
}
259395
```
260396

@@ -320,9 +456,11 @@ Get method signature and description.
320456

321457
## Advanced Features
322458

323-
### Batch Requests
459+
### Batch Requests (JSON-RPC 2.0 §6)
460+
461+
Execute multiple RPC calls in a single HTTP request. Per JSON-RPC 2.0 specification section 6, batch requests allow you to send an array of request objects and receive an array of response objects.
324462

325-
Execute multiple RPC calls in a single HTTP request.
463+
#### Basic Batch Request
326464

327465
**Request:**
328466
```json
@@ -337,6 +475,12 @@ Execute multiple RPC calls in a single HTTP request.
337475
"method": "object.find",
338476
"params": ["users", {}],
339477
"id": 2
478+
},
479+
{
480+
"jsonrpc": "2.0",
481+
"method": "object.count",
482+
"params": ["users"],
483+
"id": 3
340484
}
341485
]
342486
```
@@ -351,8 +495,158 @@ Execute multiple RPC calls in a single HTTP request.
351495
},
352496
{
353497
"jsonrpc": "2.0",
354-
"result": {"value": [...], "count": 10},
498+
"result": [
499+
{"id": "1", "name": "Alice"},
500+
{"id": "2", "name": "Bob"}
501+
],
355502
"id": 2
503+
},
504+
{
505+
"jsonrpc": "2.0",
506+
"result": 2,
507+
"id": 3
508+
}
509+
]
510+
```
511+
512+
#### Batch with Mixed Operations
513+
514+
Execute CRUD operations, counts, and actions in a single batch:
515+
516+
**Request:**
517+
```json
518+
[
519+
{
520+
"jsonrpc": "2.0",
521+
"method": "object.create",
522+
"params": ["products", {"name": "Laptop", "price": 999}],
523+
"id": 1
524+
},
525+
{
526+
"jsonrpc": "2.0",
527+
"method": "object.count",
528+
"params": ["products"],
529+
"id": 2
530+
},
531+
{
532+
"jsonrpc": "2.0",
533+
"method": "action.execute",
534+
"params": ["sendEmail", {"to": "admin@example.com", "subject": "New Product"}],
535+
"id": 3
536+
}
537+
]
538+
```
539+
540+
**Response:**
541+
```json
542+
[
543+
{
544+
"jsonrpc": "2.0",
545+
"result": {"id": "prod-123", "name": "Laptop", "price": 999},
546+
"id": 1
547+
},
548+
{
549+
"jsonrpc": "2.0",
550+
"result": 42,
551+
"id": 2
552+
},
553+
{
554+
"jsonrpc": "2.0",
555+
"result": {"success": true, "messageId": "msg_123"},
556+
"id": 3
557+
}
558+
]
559+
```
560+
561+
#### Batch with Notifications
562+
563+
Requests without an `id` are notifications and don't return responses:
564+
565+
**Request:**
566+
```json
567+
[
568+
{
569+
"jsonrpc": "2.0",
570+
"method": "object.count",
571+
"params": ["users"],
572+
"id": 1
573+
},
574+
{
575+
"jsonrpc": "2.0",
576+
"method": "action.execute",
577+
"params": ["logEvent", {"event": "user_login"}]
578+
// No id - this is a notification
579+
},
580+
{
581+
"jsonrpc": "2.0",
582+
"method": "metadata.list",
583+
"id": 2
584+
}
585+
]
586+
```
587+
588+
**Response:**
589+
```json
590+
[
591+
{
592+
"jsonrpc": "2.0",
593+
"result": 100,
594+
"id": 1
595+
},
596+
{
597+
"jsonrpc": "2.0",
598+
"result": ["users", "products"],
599+
"id": 2
600+
}
601+
]
602+
```
603+
604+
Note: Only 2 responses because the notification (no `id`) doesn't return a response.
605+
606+
#### Batch with Partial Errors
607+
608+
Individual requests can fail without affecting other requests in the batch:
609+
610+
**Request:**
611+
```json
612+
[
613+
{
614+
"jsonrpc": "2.0",
615+
"method": "object.count",
616+
"params": ["users"],
617+
"id": 1
618+
},
619+
{
620+
"jsonrpc": "2.0",
621+
"method": "object.get",
622+
"params": ["users", "non-existent-id"],
623+
"id": 2
624+
},
625+
{
626+
"jsonrpc": "2.0",
627+
"method": "metadata.list",
628+
"id": 3
629+
}
630+
]
631+
```
632+
633+
**Response:**
634+
```json
635+
[
636+
{
637+
"jsonrpc": "2.0",
638+
"result": 100,
639+
"id": 1
640+
},
641+
{
642+
"jsonrpc": "2.0",
643+
"result": null,
644+
"id": 2
645+
},
646+
{
647+
"jsonrpc": "2.0",
648+
"result": ["users", "products"],
649+
"id": 3
356650
}
357651
]
358652
```

0 commit comments

Comments
 (0)