Skip to content

Create STREAM endpoint

NOTE

Documentation in development. With the intention of being available soon. If you want to collaborate with the documentation you can do so here

Usage

js

import { Route }  from 'backan'
import { stream } from 'backan/stream'

const id    = 'loop'
const route = new Route( { path: id } )

route.add(
 {
  method    : 'post',
  path      : '/',
  request   : { body: { content: { 'application/json': { schema: route.validation.object( { loop: route.validation.number() } ) } } } },
  responses : {
   200 : route.response.responseStreamSuccess( route.validation.string() ),
   400 : route.response.responseJSONError400,
   500 : route.response.responseJSONError500,
  },
  tags : [ id ],
 },
 async c => {

  try {

   const json = c.req.valid( 'json' )

   return await stream(
    c,
    async stream => {

     const write = async ( data: string, success = true ) => {

      await stream.writeln(
       JSON.stringify( {
        success,
        data,
       } ),
      )

     }

     stream.onAbort( async () =>
      await write( 'aborted', false ),
     )

     for ( let i = 0; i < json.loop; i++ ) {

      await write( i.toString() )

     }

     await stream.close()

    },
    async ( e, stream ) => {

     await stream.writeln(
      JSON.stringify( route.response.add400ErrorObject( e ) ),
     )

    },
   )

  }
  catch ( e ) {

   return route.response.add400Error( c, e )

  }

 },
)

export default route