middleware$

API for the middleware$ function

Functions that will run before the actual function. Useful for authentication, logging, etc. Can be used for both mutation$ and query$.

Usage

import { middleware$, query$ } from '@solid-mediakit/prpc'
import { z } from 'zod'

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

export const cleanSyntaxQuery = query$({
  queryFn: async ({ payload, event$, ctx$ }) => {
    ctx$.test
    console.log('called', event$.request.headers.get('user-agent'))
    return { result: payload.a + payload.b }
  },
  key: 'cleanSyntaxQuery',
  schema: z.object({
    a: z.number().max(5),
    b: z.number().max(10),
  }),
  // middleware3 was returned from `middleware$`
  middleware: [middleware3],
})

If you want to pipe a middleware, check out the pipe function.