Skip to content

Is it possible to return a response from a procedure? #954

@marbemac

Description

@marbemac

Basically for "proxy" style use cases. We're trying out https://electric-sql.com and would love to leverage all the existing middleware, logging setup, tracing, etc we've got with oRPC, in proxy scenarios.

Here's an (imperfect but demonstrative) example for electric:

const sync = base.
  // re-use our existing orpc middlewares, logging, etc
  .use(requireAuth)
  // expose a plain http endpoint for electric
  .route({
    method: 'GET',
    path: '/api/sync/threads',
  })
  // electric's standard search params
  .input(z.object({
    live: z.string().optional(),
    handle: z.string().optional(),
    offset: z.string().optional(),
    cursor: z.string().optional(),
  }))
  // proxy the electric sync requests to electric backend
  .handler(async ({ context, input, errors }) => {
    // .. set input params on the url of course.. skipping in this example
    const electricUrl = new URL('http://localhost:4028/v1/shape');

    electricUrl.searchParams.set('table', 'threads');

    // access orpc context as needed...
    // const xyz = context.db.get(foo)

    // proxy the request
    const response = await fetch(electricUrl, {
      cf: { cacheEverything: true },
    });

    if (!response.ok) {
      throw errors.INTERNAL_SERVER_ERROR({ message: `Sync error: ${response.statusText}` });
    }

    return response;
  });

And then use it like so on the client:

const stream = new ShapeStream({
  url: `http://{server w orpc}/api/threads/sync`,
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions