Created
January 3, 2024 19:56
-
-
Save Grubba27/74f739b7d54d426e9537473c7beec71a to your computer and use it in GitHub Desktop.
create your own tRPC-like api with JavaScript proxy and a little bit of ts magic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const setProxySettings = ( | |
{ | |
call, | |
filter, | |
}: { | |
call: ({ path, args }: { path: unknown; args: unknown[] }) => any; | |
filter: (path: string[]) => unknown; | |
} = { | |
call: ({ path, args }) => { | |
return { | |
path, | |
args, | |
}; | |
}, | |
filter: (path: string[]) => path, | |
} | |
) => { | |
const createProxyClient = <T,>(path: string[] = []) => { | |
const proxy = new Proxy(() => {}, { | |
get(_, key: string) { | |
if (typeof key !== "string" || key === "then" || key === "toJSON") { | |
// special case for if the proxy is accidentally treated | |
// like a PromiseLike (like in `Promise.resolve(proxy)`) | |
return undefined; | |
} | |
return createProxyClient([...path, key]); | |
}, | |
apply(_1, _2, args) { | |
const filteredPath = filter(path); | |
return call({ | |
path: filteredPath, | |
args, | |
}); | |
}, | |
}) as unknown as T; | |
return proxy; | |
}; | |
return createProxyClient; | |
}; | |
const createProxyClient = setProxySettings({ | |
call: ({ path, args }) => { | |
console.log("call", { path, args }); | |
return { | |
path, | |
args, | |
}; | |
}, | |
filter: (path) => path, | |
}); | |
// @ts-ignore | |
const createClient = <T,>() => createProxyClient<T>() as T; | |
const obj = { | |
foo: (param: string) => "str", | |
nested: { | |
some: (k: string) => "foo", | |
}, | |
}; | |
const p = createClient<typeof obj>(); | |
p.nested.some("fsd"); | |
// logs: | |
// "call", { | |
// "path": [ | |
// "nested", | |
// "some" | |
// ], | |
// "args": [ | |
// "fsd" | |
// ] | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment