Defining RPC methods nicely

Since the methods are pretty big it's nicer to define them in a separate file, but then to add them as RPC methods I do:
export default class Worker extends WorkerEntrypoint {
fetch(request: Request) {
return app.fetch(request);
}

hello() {
return hello();
}
}
export default class Worker extends WorkerEntrypoint {
fetch(request: Request) {
return app.fetch(request);
}

hello() {
return hello();
}
}
Will I really have to define them all by using that "dumb" "self"-call? Also, since I'll likely want the HTTP api to just call the same method instead of having two places that refer to the same function it'd be nice to have it so I have a single source of truth, making the code:
export default class Worker extends WorkerEntrypoint {
fetch(request: Request) {
return app.fetch(request);
}

static hello() {
return hello()
}

hello() {
return Worker.hello();
}
}
export default class Worker extends WorkerEntrypoint {
fetch(request: Request) {
return app.fetch(request);
}

static hello() {
return hello()
}

hello() {
return Worker.hello();
}
}
Seems pretty big considering one could (if this didn't have to be a class):
const myMethods = {
hello,
}
const myMethods = {
hello,
}
1 Reply
veeque
veequeOP13h ago
Plus, it'd be nice to use the myMethods example because I can just export its type for use wherever I service-bind

Did you find this page helpful?