Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recurse #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Promisify<T> = T extends (...args: any[]) => Promise<any>
? T // already a promise
: T extends (...args: infer A) => infer R
? (...args: A) => Promise<R>
: T extends object
? PromisifyMethods<T>
: T; // not a function;

type PromisifyMethods<T extends object> = {
Expand Down Expand Up @@ -60,21 +62,39 @@ export function rpcClient<T extends object>(url: string, options?: RpcOptions) {
return result;
};


function get(prop: string): any {
return new Proxy(
(...args: any) => request(prop.toString(), args),
{
get(_, childProp) {
if (isValidProp(childProp))
return get(`${prop}.${childProp}`);
}
}
)
}

return new Proxy(
{},
{
/* istanbul ignore next */
get(target, prop, receiver) {
if (typeof prop === "symbol") return;
if (prop.startsWith("$")) return;
if (prop in Object.prototype) return;
if (prop === "toJSON") return;
return (...args: any) => request(prop.toString(), args);
},
get(_, prop) {
if (isValidProp(prop))
return get(prop);
}
}
) as PromisifyMethods<T>;
}

/* istanbul ignore next */
function isValidProp(prop: string | symbol): prop is string {
if (typeof prop === "symbol") return false;
if (prop.startsWith("$")) return false;
if (prop in Object.prototype) return false;
if (prop === "toJSON") return false;
return true;
}

function removeTrailingUndefs(values: any[]) {
const a = [...values];
while (a.length && a[a.length - 1] === undefined) a.length--;
Expand Down
7 changes: 6 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ export async function handleRpc(
error: { code: -32600, message: "Invalid Request" },
};
}
const { jsonrpc, method, params } = request;
const { jsonrpc, params } = request;
const path = request.method.split('.');
const method = path.pop()!;
for (const property of path)
if (hasProperty(service, property))
service = service[property] as object;
if (!hasMethod(service, method)) {
console.log("Method %s not found", method, service);
return {
Expand Down
8 changes: 8 additions & 0 deletions src/test/RequestAwareService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ export class RequestAwareService implements Service {
echoHeader(name: string) {
return this.headers?.[name.toLowerCase()];
}

get recurse() {
return {
method() {
return 'recurse.method';
}
}
}
}
6 changes: 6 additions & 0 deletions src/test/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ tap.test("should throw on errors", async (t) => {
const promise = client.sorry("Dave");
t.rejects(promise, new RpcError("Sorry Dave.", -32000));
});

tap.test("should support recursion on property access", async (t) => {
const client = rpcClient<Service>(apiUrl);
const result = await client.recurse.method();
t.equal(result, "recurse.method");
});
6 changes: 6 additions & 0 deletions src/test/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export const service = {
echoHeader(name: string): string | string[] | undefined {
throw new Error("This service can't access request headers");
},

recurse: {
method() {
return 'recurse.method';
}
}
};

export type Service = typeof service;