|
1 | 1 | import { mockPromise } from '#mocks/Promise.js';
|
2 | 2 | import { Buffer } from 'node:buffer';
|
3 | 3 | import { readFile } from 'node:fs/promises';
|
4 |
| -import { lastValueFrom, map, of } from 'rxjs'; |
| 4 | +import { concatMap, firstValueFrom, lastValueFrom, map, of, share, tap } from 'rxjs'; |
5 | 5 | import { TestScheduler } from 'rxjs/testing';
|
6 | 6 | import { afterAll, beforeAll, beforeEach, afterEach, describe, expect, test, vi } from 'vitest';
|
7 | 7 |
|
| 8 | +import { requestJSON } from './request'; |
| 9 | + |
8 | 10 | describe('json', () => {
|
9 | 11 | let testScheduler;
|
10 | 12 |
|
@@ -484,5 +486,37 @@ describe('json', () => {
|
484 | 486 |
|
485 | 487 | console.log((await data).globalSymbol === deserialized.globalSymbol);
|
486 | 488 | });
|
| 489 | + |
| 490 | + test('demo', async () => { |
| 491 | + const { serialize, deserialize } = await import('./json'); |
| 492 | + |
| 493 | + const baseURL = 'https://jsonplaceholder.typicode.com/'; |
| 494 | + const eMail = '[email protected]'; |
| 495 | + const user = of(new URL(`users?email=${eMail}`, baseURL)).pipe( |
| 496 | + requestJSON(), |
| 497 | + tap(() => console.log('REQUEST')), |
| 498 | + map(([item]) => item), |
| 499 | + share() |
| 500 | + ); |
| 501 | + |
| 502 | + const resolvePosts = () => source => |
| 503 | + source.pipe( |
| 504 | + concatMap(user => of(new URL(`users/${user.id}/posts`, baseURL)).pipe(requestJSON())) |
| 505 | + ); |
| 506 | + |
| 507 | + const resolveTodos = () => source => |
| 508 | + source.pipe( |
| 509 | + concatMap(user => of(new URL(`users/${user.id}/todos`, baseURL)).pipe(requestJSON())) |
| 510 | + ); |
| 511 | + |
| 512 | + const p = of({ |
| 513 | + user, |
| 514 | + posts: user.pipe(resolvePosts()), |
| 515 | + todos: user.pipe(resolveTodos()) |
| 516 | + }).pipe(serialize()); |
| 517 | + |
| 518 | + const res = await firstValueFrom(p); |
| 519 | + console.log(JSON.parse(res)); |
| 520 | + }); |
487 | 521 | /* v8 ignore stop */
|
488 | 522 | });
|
0 commit comments