JSONLines
JSON Lines example
Text streaming with async generators .
Result
Code
1import { get, prefix, operation } from 'vovk';
2
3type Token = { message: string };
4
5@prefix('jsonlines')
6export default class JSONLinesController {
7 @operation({
8 summary: 'Stream tokens',
9 description: 'Stream tokens to the client',
10 })
11 @get('tokens', { cors: true })
12 static async *streamTokens() {
13 const tokens: Token[] = [
14 { message: 'Hello,' },
15 { message: ' World' },
16 { message: ' from' },
17 { message: ' Stream' },
18 { message: '!' },
19 ];
20
21 for (const token of tokens) {
22 yield token;
23 await new Promise((resolve) => setTimeout(resolve, 300));
24 }
25 }
26}
1import { get, prefix, operation } from 'vovk';
2
3type Token = { message: string };
4
5@prefix('jsonlines')
6export default class JSONLinesController {
7 @operation({
8 summary: 'Stream tokens',
9 description: 'Stream tokens to the client',
10 })
11 @get('tokens', { cors: true })
12 static async *streamTokens() {
13 const tokens: Token[] = [
14 { message: 'Hello,' },
15 { message: ' World' },
16 { message: ' from' },
17 { message: ' Stream' },
18 { message: '!' },
19 ];
20
21 for (const token of tokens) {
22 yield token;
23 await new Promise((resolve) => setTimeout(resolve, 300));
24 }
25 }
26}
1import { get, prefix, operation } from 'vovk';
2
3type Token = { message: string };
4
5@prefix('jsonlines')
6export default class JSONLinesController {
7 @operation({
8 summary: 'Stream tokens',
9 description: 'Stream tokens to the client',
10 })
11 @get('tokens', { cors: true })
12 static async *streamTokens() {
13 const tokens: Token[] = [
14 { message: 'Hello,' },
15 { message: ' World' },
16 { message: ' from' },
17 { message: ' Stream' },
18 { message: '!' },
19 ];
20
21 for (const token of tokens) {
22 yield token;
23 await new Promise((resolve) => setTimeout(resolve, 300));
24 }
25 }
26}
1import { get, prefix, operation } from 'vovk';
2
3type Token = { message: string };
4
5@prefix('jsonlines')
6export default class JSONLinesController {
7 @operation({
8 summary: 'Stream tokens',
9 description: 'Stream tokens to the client',
10 })
11 @get('tokens', { cors: true })
12 static async *streamTokens() {
13 const tokens: Token[] = [
14 { message: 'Hello,' },
15 { message: ' World' },
16 { message: ' from' },
17 { message: ' Stream' },
18 { message: '!' },
19 ];
20
21 for (const token of tokens) {
22 yield token;
23 await new Promise((resolve) => setTimeout(resolve, 300));
24 }
25 }
26}
1'use client';
2import { useState } from 'react';
3import { JSONLinesRPC } from 'vovk-client';
4import type { VovkYieldType } from 'vovk';
5
6export default function StreamExample() {
7 const [tokens, setTokens] = useState<VovkYieldType<typeof JSONLinesRPC.streamTokens>[]>([]);
8
9 return (
10 <>
11 <button
12 onClick={async () => {
13 setTokens([]);
14 using stream = await JSONLinesRPC.streamTokens();
15 for await (const token of stream) {
16 setTokens((tokens) => [...tokens, token]);
17 }
18 }}
19 >
20 Get JSON Lines response
21 </button>
22 <div>
23 {tokens.map(({ message }, i) => (
24 <span key={i}>{message}</span>
25 ))}
26 </div>
27 </>
28 );
29}
1'use client';
2import { useState } from 'react';
3import { JSONLinesRPC } from 'vovk-client';
4import type { VovkYieldType } from 'vovk';
5
6export default function StreamExample() {
7 const [tokens, setTokens] = useState<VovkYieldType<typeof JSONLinesRPC.streamTokens>[]>([]);
8
9 return (
10 <>
11 <button
12 onClick={async () => {
13 setTokens([]);
14 using stream = await JSONLinesRPC.streamTokens();
15 for await (const token of stream) {
16 setTokens((tokens) => [...tokens, token]);
17 }
18 }}
19 >
20 Get JSON Lines response
21 </button>
22 <div>
23 {tokens.map(({ message }, i) => (
24 <span key={i}>{message}</span>
25 ))}
26 </div>
27 </>
28 );
29}
1'use client';
2import { useState } from 'react';
3import { JSONLinesRPC } from 'vovk-client';
4import type { VovkYieldType } from 'vovk';
5
6export default function StreamExample() {
7 const [tokens, setTokens] = useState<VovkYieldType<typeof JSONLinesRPC.streamTokens>[]>([]);
8
9 return (
10 <>
11 <button
12 onClick={async () => {
13 setTokens([]);
14 using stream = await JSONLinesRPC.streamTokens();
15 for await (const token of stream) {
16 setTokens((tokens) => [...tokens, token]);
17 }
18 }}
19 >
20 Get JSON Lines response
21 </button>
22 <div>
23 {tokens.map(({ message }, i) => (
24 <span key={i}>{message}</span>
25 ))}
26 </div>
27 </>
28 );
29}
1'use client';
2import { useState } from 'react';
3import { JSONLinesRPC } from 'vovk-client';
4import type { VovkYieldType } from 'vovk';
5
6export default function StreamExample() {
7 const [tokens, setTokens] = useState<VovkYieldType<typeof JSONLinesRPC.streamTokens>[]>([]);
8
9 return (
10 <>
11 <button
12 onClick={async () => {
13 setTokens([]);
14 using stream = await JSONLinesRPC.streamTokens();
15 for await (const token of stream) {
16 setTokens((tokens) => [...tokens, token]);
17 }
18 }}
19 >
20 Get JSON Lines response
21 </button>
22 <div>
23 {tokens.map(({ message }, i) => (
24 <span key={i}>{message}</span>
25 ))}
26 </div>
27 </>
28 );
29}
Last updated on