MCPcopy Index your code
hub / github.com/upstash/jstack / createProxy

Function createProxy

packages/jstack/src/server/client.ts:218–279  ·  view source on GitHub ↗
(
  baseClient: any,
  baseUrl: string,
  path: string[] = [],
)

Source from the content-addressed store, hash-verified

216}
217
218function createProxy(
219 baseClient: any,
220 baseUrl: string,
221 path: string[] = [],
222): any {
223 return new Proxy(baseClient, {
224 get(target, prop, receiver) {
225 if (typeof prop === "string") {
226 const routePath = [...path, prop]
227
228 if (prop === "$get") {
229 return async (...args: any[]) => {
230 const [data, options] = args
231 const serializedQuery = serializeWithSuperJSON(data)
232 return target.$get({ query: serializedQuery }, options)
233 }
234 }
235
236 if (prop === "$post") {
237 return async (...args: any[]) => {
238 const [data, options] = args
239 const serializedJson = serializeWithSuperJSON(data)
240 return target.$post({ json: serializedJson }, options)
241 }
242 }
243
244 if (prop === "$url") {
245 return (args?: any) => {
246 const endpointPath = `/${routePath.slice(0, -1).join("/")}`
247 const normalizedPath = endpointPath.replace(baseUrl, "")
248 const url = new URL(baseUrl + normalizedPath)
249
250 if (args?.query) {
251 Object.entries(args.query).forEach(([key, value]) => {
252 url.searchParams.append(key, String(value))
253 })
254 }
255
256 return url
257 }
258 }
259
260 if (prop === "$ws") {
261 return () => {
262 const endpointPath = `/${routePath.slice(0, -1).join("/")}`
263 const normalizedPath = endpointPath.replace(baseUrl, "")
264 const url = new URL(baseUrl + normalizedPath)
265
266 // Change protocol to ws:// or wss:// depending on if https or http
267 url.protocol = url.protocol === "https:" ? "wss:" : "ws:"
268
269 return new ClientSocket(url)
270 }
271 }
272
273 return createProxy(target[prop], baseUrl, routePath)
274 }
275

Callers 2

createClientFunction · 0.85
getFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected