|
1 | 1 | /* eslint-disable @typescript-eslint/no-unused-vars */ |
2 | 2 | import { expectTypeOf } from 'vitest' |
| 3 | +import { hc } from './client' |
3 | 4 | import { Context } from './context' |
4 | 5 | import { createMiddleware } from './helper/factory' |
5 | 6 | import { Hono } from './hono' |
@@ -2399,3 +2400,302 @@ describe('status code', () => { |
2399 | 2400 | expectTypeOf<Actual>().toEqualTypeOf<204 | 201 | 200>() |
2400 | 2401 | }) |
2401 | 2402 | }) |
| 2403 | + |
| 2404 | +describe('RPC supports Middleware responses', () => { |
| 2405 | + it('Should handle the responses from 1 handler', async () => { |
| 2406 | + const routes = new Hono().get('/single', (c) => c.json({ '200': true }, 200)) |
| 2407 | + const client = hc<typeof routes>('http://localhost') |
| 2408 | + const res = await client.single.$get() |
| 2409 | + if (res.status === 200) { |
| 2410 | + ;(await res.json())['200'] |
| 2411 | + } |
| 2412 | + }) |
| 2413 | + |
| 2414 | + it('Should handle the responses from 2 handlers', async () => { |
| 2415 | + const routes = new Hono().get( |
| 2416 | + '/double', |
| 2417 | + async (c) => c.json({ '400': true }, 400), |
| 2418 | + (c) => c.json({ '200': true }, 200) |
| 2419 | + ) |
| 2420 | + const client = hc<typeof routes>('http://localhost') |
| 2421 | + const res = await client.double.$get() |
| 2422 | + if (res.status === 400) { |
| 2423 | + ;(await res.json())['400'] |
| 2424 | + } |
| 2425 | + if (res.status === 200) { |
| 2426 | + ;(await res.json())['200'] |
| 2427 | + } |
| 2428 | + }) |
| 2429 | + |
| 2430 | + it('Should handle the responses from 3 handlers', async () => { |
| 2431 | + const routes = new Hono().get( |
| 2432 | + '/foo', |
| 2433 | + async (c) => c.json({ '500': true }, 500), |
| 2434 | + async (c) => c.json({ '400': true }, 400), |
| 2435 | + (c) => c.json({ '200': true }, 200) |
| 2436 | + ) |
| 2437 | + const client = hc<typeof routes>('http://localhost') |
| 2438 | + const res = await client.foo.$get() |
| 2439 | + if (res.status === 500) { |
| 2440 | + ;(await res.json())['500'] |
| 2441 | + } |
| 2442 | + if (res.status === 400) { |
| 2443 | + ;(await res.json())['400'] |
| 2444 | + } |
| 2445 | + if (res.status === 200) { |
| 2446 | + ;(await res.json())['200'] |
| 2447 | + } |
| 2448 | + }) |
| 2449 | + |
| 2450 | + it('Should handle the responses from 4 handlers', async () => { |
| 2451 | + const routes = new Hono().get( |
| 2452 | + '/bar', |
| 2453 | + async (c) => c.json({ '500': true }, 500), |
| 2454 | + async (c) => c.json({ '400': true }, 400), |
| 2455 | + async (c) => c.json({ '300': true }, 300), |
| 2456 | + (c) => c.json({ '200': true }, 200) |
| 2457 | + ) |
| 2458 | + const client = hc<typeof routes>('http://localhost') |
| 2459 | + const res = await client.bar.$get() |
| 2460 | + if (res.status === 500) { |
| 2461 | + ;(await res.json())['500'] |
| 2462 | + } |
| 2463 | + if (res.status === 400) { |
| 2464 | + ;(await res.json())['400'] |
| 2465 | + } |
| 2466 | + if (res.status === 300) { |
| 2467 | + ;(await res.json())['300'] |
| 2468 | + } |
| 2469 | + if (res.status === 200) { |
| 2470 | + ;(await res.json())['200'] |
| 2471 | + } |
| 2472 | + }) |
| 2473 | + |
| 2474 | + it('Should handle the responses from 5 handlers', async () => { |
| 2475 | + const routes = new Hono().get( |
| 2476 | + '/baz', |
| 2477 | + async (c) => c.json({ '500': true }, 500), |
| 2478 | + async (c) => c.json({ '400': true }, 400), |
| 2479 | + async (c) => c.json({ '300': true }, 300), |
| 2480 | + async (c) => c.json({ '201': true }, 201), |
| 2481 | + (c) => c.json({ '200': true }, 200) |
| 2482 | + ) |
| 2483 | + const client = hc<typeof routes>('http://localhost') |
| 2484 | + const res = await client.baz.$get() |
| 2485 | + if (res.status === 500) { |
| 2486 | + ;(await res.json())['500'] |
| 2487 | + } |
| 2488 | + if (res.status === 400) { |
| 2489 | + ;(await res.json())['400'] |
| 2490 | + } |
| 2491 | + if (res.status === 300) { |
| 2492 | + ;(await res.json())['300'] |
| 2493 | + } |
| 2494 | + if (res.status === 201) { |
| 2495 | + ;(await res.json())['201'] |
| 2496 | + } |
| 2497 | + if (res.status === 200) { |
| 2498 | + ;(await res.json())['200'] |
| 2499 | + } |
| 2500 | + }) |
| 2501 | + |
| 2502 | + it('Should handle the responses from 6 handlers', async () => { |
| 2503 | + const routes = new Hono().get( |
| 2504 | + '/qux', |
| 2505 | + async (c) => c.json({ '500': true }, 500), |
| 2506 | + async (c) => c.json({ '400': true }, 400), |
| 2507 | + async (c) => c.json({ '300': true }, 300), |
| 2508 | + async (c) => c.json({ '201': true }, 201), |
| 2509 | + async (c) => c.json({ '202': true }, 202), |
| 2510 | + (c) => c.json({ '200': true }, 200) |
| 2511 | + ) |
| 2512 | + const client = hc<typeof routes>('http://localhost') |
| 2513 | + const res = await client.qux.$get() |
| 2514 | + if (res.status === 500) { |
| 2515 | + ;(await res.json())['500'] |
| 2516 | + } |
| 2517 | + if (res.status === 400) { |
| 2518 | + ;(await res.json())['400'] |
| 2519 | + } |
| 2520 | + if (res.status === 300) { |
| 2521 | + ;(await res.json())['300'] |
| 2522 | + } |
| 2523 | + if (res.status === 201) { |
| 2524 | + ;(await res.json())['201'] |
| 2525 | + } |
| 2526 | + if (res.status === 202) { |
| 2527 | + ;(await res.json())['202'] |
| 2528 | + } |
| 2529 | + if (res.status === 200) { |
| 2530 | + ;(await res.json())['200'] |
| 2531 | + } |
| 2532 | + }) |
| 2533 | + |
| 2534 | + it('Should handle the responses from 7 handlers', async () => { |
| 2535 | + const routes = new Hono().get( |
| 2536 | + '/seven', |
| 2537 | + async (c) => c.json({ '500': true }, 500), |
| 2538 | + async (c) => c.json({ '400': true }, 400), |
| 2539 | + async (c) => c.json({ '300': true }, 300), |
| 2540 | + async (c) => c.json({ '201': true }, 201), |
| 2541 | + async (c) => c.json({ '202': true }, 202), |
| 2542 | + async (c) => c.json({ '203': true }, 203), |
| 2543 | + (c) => c.json({ '200': true }, 200) |
| 2544 | + ) |
| 2545 | + const client = hc<typeof routes>('http://localhost') |
| 2546 | + const res = await client.seven.$get() |
| 2547 | + if (res.status === 500) { |
| 2548 | + ;(await res.json())['500'] |
| 2549 | + } |
| 2550 | + if (res.status === 400) { |
| 2551 | + ;(await res.json())['400'] |
| 2552 | + } |
| 2553 | + if (res.status === 300) { |
| 2554 | + ;(await res.json())['300'] |
| 2555 | + } |
| 2556 | + if (res.status === 201) { |
| 2557 | + ;(await res.json())['201'] |
| 2558 | + } |
| 2559 | + if (res.status === 202) { |
| 2560 | + ;(await res.json())['202'] |
| 2561 | + } |
| 2562 | + if (res.status === 203) { |
| 2563 | + ;(await res.json())['203'] |
| 2564 | + } |
| 2565 | + if (res.status === 200) { |
| 2566 | + ;(await res.json())['200'] |
| 2567 | + } |
| 2568 | + }) |
| 2569 | + |
| 2570 | + it('Should handle the responses from 8 handlers', async () => { |
| 2571 | + const routes = new Hono().get( |
| 2572 | + '/eight', |
| 2573 | + async (c) => c.json({ '500': true }, 500), |
| 2574 | + async (c) => c.json({ '400': true }, 400), |
| 2575 | + async (c) => c.json({ '401': true }, 401), |
| 2576 | + async (c) => c.json({ '300': true }, 300), |
| 2577 | + async (c) => c.json({ '201': true }, 201), |
| 2578 | + async (c) => c.json({ '202': true }, 202), |
| 2579 | + async (c) => c.json({ '203': true }, 203), |
| 2580 | + (c) => c.json({ '200': true }, 200) |
| 2581 | + ) |
| 2582 | + const client = hc<typeof routes>('http://localhost') |
| 2583 | + const res = await client.eight.$get() |
| 2584 | + if (res.status === 500) { |
| 2585 | + ;(await res.json())['500'] |
| 2586 | + } |
| 2587 | + if (res.status === 400) { |
| 2588 | + ;(await res.json())['400'] |
| 2589 | + } |
| 2590 | + if (res.status === 401) { |
| 2591 | + ;(await res.json())['401'] |
| 2592 | + } |
| 2593 | + if (res.status === 300) { |
| 2594 | + ;(await res.json())['300'] |
| 2595 | + } |
| 2596 | + if (res.status === 201) { |
| 2597 | + ;(await res.json())['201'] |
| 2598 | + } |
| 2599 | + if (res.status === 202) { |
| 2600 | + ;(await res.json())['202'] |
| 2601 | + } |
| 2602 | + if (res.status === 203) { |
| 2603 | + ;(await res.json())['203'] |
| 2604 | + } |
| 2605 | + if (res.status === 200) { |
| 2606 | + ;(await res.json())['200'] |
| 2607 | + } |
| 2608 | + }) |
| 2609 | + |
| 2610 | + it('Should handle the responses from 9 handlers', async () => { |
| 2611 | + const routes = new Hono().get( |
| 2612 | + '/nine', |
| 2613 | + async (c) => c.json({ '500': true }, 500), |
| 2614 | + async (c) => c.json({ '400': true }, 400), |
| 2615 | + async (c) => c.json({ '401': true }, 401), |
| 2616 | + async (c) => c.json({ '403': true }, 403), |
| 2617 | + async (c) => c.json({ '300': true }, 300), |
| 2618 | + async (c) => c.json({ '201': true }, 201), |
| 2619 | + async (c) => c.json({ '202': true }, 202), |
| 2620 | + async (c) => c.json({ '203': true }, 203), |
| 2621 | + (c) => c.json({ '200': true }, 200) |
| 2622 | + ) |
| 2623 | + const client = hc<typeof routes>('http://localhost') |
| 2624 | + const res = await client.nine.$get() |
| 2625 | + if (res.status === 500) { |
| 2626 | + ;(await res.json())['500'] |
| 2627 | + } |
| 2628 | + if (res.status === 400) { |
| 2629 | + ;(await res.json())['400'] |
| 2630 | + } |
| 2631 | + if (res.status === 401) { |
| 2632 | + ;(await res.json())['401'] |
| 2633 | + } |
| 2634 | + if (res.status === 403) { |
| 2635 | + ;(await res.json())['403'] |
| 2636 | + } |
| 2637 | + if (res.status === 300) { |
| 2638 | + ;(await res.json())['300'] |
| 2639 | + } |
| 2640 | + if (res.status === 201) { |
| 2641 | + ;(await res.json())['201'] |
| 2642 | + } |
| 2643 | + if (res.status === 202) { |
| 2644 | + ;(await res.json())['202'] |
| 2645 | + } |
| 2646 | + if (res.status === 203) { |
| 2647 | + ;(await res.json())['203'] |
| 2648 | + } |
| 2649 | + if (res.status === 200) { |
| 2650 | + ;(await res.json())['200'] |
| 2651 | + } |
| 2652 | + }) |
| 2653 | + |
| 2654 | + it('Should handle the responses from 10 handlers', async () => { |
| 2655 | + const routes = new Hono().get( |
| 2656 | + '/ten', |
| 2657 | + async (c) => c.json({ '500': true }, 500), |
| 2658 | + async (c) => c.json({ '400': true }, 400), |
| 2659 | + async (c) => c.json({ '401': true }, 401), |
| 2660 | + async (c) => c.json({ '403': true }, 403), |
| 2661 | + async (c) => c.json({ '404': true }, 404), |
| 2662 | + async (c) => c.json({ '300': true }, 300), |
| 2663 | + async (c) => c.json({ '201': true }, 201), |
| 2664 | + async (c) => c.json({ '202': true }, 202), |
| 2665 | + async (c) => c.json({ '203': true }, 203), |
| 2666 | + (c) => c.json({ '200': true }, 200) |
| 2667 | + ) |
| 2668 | + const client = hc<typeof routes>('http://localhost') |
| 2669 | + const res = await client.ten.$get() |
| 2670 | + if (res.status === 500) { |
| 2671 | + ;(await res.json())['500'] |
| 2672 | + } |
| 2673 | + if (res.status === 400) { |
| 2674 | + ;(await res.json())['400'] |
| 2675 | + } |
| 2676 | + if (res.status === 401) { |
| 2677 | + ;(await res.json())['401'] |
| 2678 | + } |
| 2679 | + if (res.status === 403) { |
| 2680 | + ;(await res.json())['403'] |
| 2681 | + } |
| 2682 | + if (res.status === 404) { |
| 2683 | + ;(await res.json())['404'] |
| 2684 | + } |
| 2685 | + if (res.status === 300) { |
| 2686 | + ;(await res.json())['300'] |
| 2687 | + } |
| 2688 | + if (res.status === 201) { |
| 2689 | + ;(await res.json())['201'] |
| 2690 | + } |
| 2691 | + if (res.status === 202) { |
| 2692 | + ;(await res.json())['202'] |
| 2693 | + } |
| 2694 | + if (res.status === 203) { |
| 2695 | + ;(await res.json())['203'] |
| 2696 | + } |
| 2697 | + if (res.status === 200) { |
| 2698 | + ;(await res.json())['200'] |
| 2699 | + } |
| 2700 | + }) |
| 2701 | +}) |
0 commit comments