pipe$

API for the pipe$ function

This will modify the return type of an existing middleware, in a type safe way.

Usage

import { middleware$, pipe$, query$, response$, error$ } from '@prpc/solid'
import { z } from 'zod'

const myMiddleware1 = middleware$(({ request$ }) => {
  console.log('req', request$)
  return Math.random() > 0.5 ? { test: true } : { test: null }
})

const middleWare2 = pipe$(myMiddleware1, (ctx) => {
  if (ctx.test === null) {
    return error$('test is null')
  }
  return {
    test: ctx.test,
    o: 1,
  }
})

export const add = query$(
  ({ payload, ctx$ }) => {
    console.log({ ctx$ }) // { b: number; test: boolean; o: number}
    const result = payload.a + payload.b
    return response$(
      { result },
      {
        headers: {
          'set-cookie': 'solid-testing=1',
        },
      }
    )
  },
  'add',
  z.object({
    a: z.number(),
    b: z.number(),
  }),
  middleware3
)