Skip to content

Instantly share code, notes, and snippets.

@Grubba27
Created January 3, 2024 19:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grubba27/74f739b7d54d426e9537473c7beec71a to your computer and use it in GitHub Desktop.
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
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