JSONLinesResponse Object
JSON Lines streaming with Response object
This example uses JSONLinesResponse object in order to get more control over the stream logic.
Result
Code
1404: Not Found
1404: Not Found
1404: Not Found
1404: Not Found
1import { prefix, get, operation, JSONLinesResponse } from 'vovk';
2import StreamService, { type Token } from './JSONLinesService.ts';
3
4@prefix('jsonlines-response-object')
5export default class JSONLinesResponseController {
6 @operation({
7 summary: 'Stream tokens using Response object',
8 description: 'Stream tokens to the client using Response object',
9 })
10 @get('tokens', { cors: true }) // CORS for OPTIONS
11 static async streamTokens(req: Request) {
12 const response = new JSONLinesResponse<Token>(req, {
13 headers: {
14 // CORS for GET
15 'Access-Control-Allow-Origin': '*',
16 'Access-Control-Allow-Methods': 'GET',
17 'Access-Control-Allow-Headers': 'Authorization',
18 },
19 });
20
21 void StreamService.streamTokens(response);
22
23 return response;
24 }
25}
1import { prefix, get, operation, JSONLinesResponse } from 'vovk';
2import StreamService, { type Token } from './JSONLinesService.ts';
3
4@prefix('jsonlines-response-object')
5export default class JSONLinesResponseController {
6 @operation({
7 summary: 'Stream tokens using Response object',
8 description: 'Stream tokens to the client using Response object',
9 })
10 @get('tokens', { cors: true }) // CORS for OPTIONS
11 static async streamTokens(req: Request) {
12 const response = new JSONLinesResponse<Token>(req, {
13 headers: {
14 // CORS for GET
15 'Access-Control-Allow-Origin': '*',
16 'Access-Control-Allow-Methods': 'GET',
17 'Access-Control-Allow-Headers': 'Authorization',
18 },
19 });
20
21 void StreamService.streamTokens(response);
22
23 return response;
24 }
25}
1import { prefix, get, operation, JSONLinesResponse } from 'vovk';
2import StreamService, { type Token } from './JSONLinesService.ts';
3
4@prefix('jsonlines-response-object')
5export default class JSONLinesResponseController {
6 @operation({
7 summary: 'Stream tokens using Response object',
8 description: 'Stream tokens to the client using Response object',
9 })
10 @get('tokens', { cors: true }) // CORS for OPTIONS
11 static async streamTokens(req: Request) {
12 const response = new JSONLinesResponse<Token>(req, {
13 headers: {
14 // CORS for GET
15 'Access-Control-Allow-Origin': '*',
16 'Access-Control-Allow-Methods': 'GET',
17 'Access-Control-Allow-Headers': 'Authorization',
18 },
19 });
20
21 void StreamService.streamTokens(response);
22
23 return response;
24 }
25}
1import { prefix, get, operation, JSONLinesResponse } from 'vovk';
2import StreamService, { type Token } from './JSONLinesService.ts';
3
4@prefix('jsonlines-response-object')
5export default class JSONLinesResponseController {
6 @operation({
7 summary: 'Stream tokens using Response object',
8 description: 'Stream tokens to the client using Response object',
9 })
10 @get('tokens', { cors: true }) // CORS for OPTIONS
11 static async streamTokens(req: Request) {
12 const response = new JSONLinesResponse<Token>(req, {
13 headers: {
14 // CORS for GET
15 'Access-Control-Allow-Origin': '*',
16 'Access-Control-Allow-Methods': 'GET',
17 'Access-Control-Allow-Headers': 'Authorization',
18 },
19 });
20
21 void StreamService.streamTokens(response);
22
23 return response;
24 }
25}
1'use client';
2import { useState } from 'react';
3import { JSONLinesResponseRPC } from 'vovk-client';
4import type { VovkYieldType } from 'vovk';
5
6export default function StreamExample() {
7 const [tokens, setTokens] = useState<VovkYieldType<typeof JSONLinesResponseRPC.streamTokens>[]>([]);
8
9 return (
10 <>
11 <button
12 onClick={async () => {
13 setTokens([]);
14 using stream = await JSONLinesResponseRPC.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 { JSONLinesResponseRPC } from 'vovk-client';
4import type { VovkYieldType } from 'vovk';
5
6export default function StreamExample() {
7 const [tokens, setTokens] = useState<VovkYieldType<typeof JSONLinesResponseRPC.streamTokens>[]>([]);
8
9 return (
10 <>
11 <button
12 onClick={async () => {
13 setTokens([]);
14 using stream = await JSONLinesResponseRPC.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 { JSONLinesResponseRPC } from 'vovk-client';
4import type { VovkYieldType } from 'vovk';
5
6export default function StreamExample() {
7 const [tokens, setTokens] = useState<VovkYieldType<typeof JSONLinesResponseRPC.streamTokens>[]>([]);
8
9 return (
10 <>
11 <button
12 onClick={async () => {
13 setTokens([]);
14 using stream = await JSONLinesResponseRPC.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 { JSONLinesResponseRPC } from 'vovk-client';
4import type { VovkYieldType } from 'vovk';
5
6export default function StreamExample() {
7 const [tokens, setTokens] = useState<VovkYieldType<typeof JSONLinesResponseRPC.streamTokens>[]>([]);
8
9 return (
10 <>
11 <button
12 onClick={async () => {
13 setTokens([]);
14 using stream = await JSONLinesResponseRPC.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