solidjs-community / mediakit

A set of utilities to use with your Solid apps.
https://mediakit-taupe.vercel.app
138 stars 14 forks source link

pRPC builder$ middleware leaks #43

Closed OrJDev closed 7 months ago

OrJDev commented 7 months ago

I'm aware it leaks i only realized after publishing and I didn't want to revert, i already thought of a fix but its late here so i couldn't implement it, what we are going to do is instead of using the callMw from the builder we are going to he transforming the builder file asWell. So basically

File 1

export const myBuilder = builder$().middleware(()=> ({f: 1}))

File 2

import { myBuilder } from "./prpc"
myBuilder.query$(...)

Turn into

File 1

const myBuilder_mw1 = ()=> ({f: 1})

export const myBuilder_mws = [myBuilder_mw1];

export const myBuilder = builder$()

File 2

import { myBuilder, myBuilder_mws} from "./prpc"

// more transformation logic

Basically this allows us to import the function directly and only run it on the server as expected

OrJDev commented 7 months ago

Regarding this solution, the hard part is finally done, i was able to make the plugin plugin transform this:

import { builder$ } from '@solid-mediakit/prpc'

export const helloBuilder = builder$()
  .middleware$(() => {
    console.log('middleware 1')
    return {
      hello: 1,
    }
  })
  .middleware$((ctx) => {
    console.log('middleware 2')
    return {
      ...ctx,
      world: 2,
    }
  })
  .middleware$((ctx) => {
    console.log('middleware 3')
    return {
      ...ctx,
      sdsdgsdg: 'sdg',
    }
  })

export const byeBuilder = builder$()
  .middleware$(() => {
    console.log('bye middleware das')
    return {
      bye: 1,
    }
  })
  .middleware$(() => {
    console.log('bye middleware das 2')
    return {
      hehe: 1,
    }
  })

into this:

import { callMiddleware$ } from "@solid-mediakit/prpc";
import { getRequestEvent } from "solid-js/web";
import { cache } from "@solidjs/router";
import { validateZod } from "@solid-mediakit/prpc";
import { builder$ } from '@solid-mediakit/prpc';
export const helloBuilder = builder$();
export const byeBuilder = builder$();
export const _$$helloBuilder_mws = [() => {
  console.log('middleware 1');
  return {
    hello: 1
  };
}, ctx => {
  console.log('middleware 2');
  return {
    ...ctx,
    world: 2
  };
}, ctx => {
  console.log('middleware 3');
  return {
    ...ctx,
    sdsdgsdg: 'sdg'
  };
}];
export const _$$byeBuilder_mws = [() => {
  console.log('bye middleware das');
  return {
    bye: 1
  };
}, () => {
  console.log('bye middleware das 2');
  return {
    hehe: 1
  };
}];

now i just need to implement it on the consumption

changes were pushed here: https://github.com/solidjs-community/mediakit/commit/19867296b662380d2aa032f8a9ddc272187fe368

OrJDev commented 7 months ago

should be fixed in https://github.com/solidjs-community/mediakit/commit/25dac1ab45dcd194c818e489a010733e66380d1f