Skip to content

Commit

Permalink
es/v2: Port /implementing-services (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
srikrsna-buf authored Oct 29, 2024
1 parent 22f1834 commit 284d8c5
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions docs/node/implementing-services.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ To register this service, call `router.service()`:

```ts
import { ConnectRouter, HandlerContext } from "@connectrpc/connect";
import { ElizaService } from "./gen/eliza_connect";
import { SayRequest, SayResponse } from "./gen/eliza_pb";
import { ElizaService, SayRequest } from "./gen/eliza_pb";
import { create } from "@bufbuild/protobuf";

export default (router: ConnectRouter) =>
router.service(ElizaService, {
async say(req: SayRequest, context: HandlerContext) {
return new SayResponse({
return {
sentence: `You said ${req.sentence}`,
});
};
}
});
```
Expand All @@ -59,7 +59,7 @@ message, or just an initializer for a response message:

```ts
function say(req: SayRequest) {
return new SayResponse({ sentence: `You said ${req.sentence}` });
return create(SayResponseSchema, { sentence: `You said ${req.sentence}` });
}
```

Expand All @@ -82,14 +82,15 @@ The context argument gives you access to headers and service metadata:

```ts
import { HandlerContext } from "@connectrpc/connect";
import { create } from "@bufbuild/protobuf";
import { SayRequest } from "./gen/eliza_pb";

function say(req: SayRequest, context: HandlerContext) {
ctx.service.typeName; // the protobuf type name "ElizaService"
ctx.method.name; // the protobuf rpc name "Say"
context.requestHeader.get("Foo");
context.responseHeader.set("Foo", "Bar");
return new SayResponse({ sentence: `You said ${req.sentence}` });
return { sentence: `You said ${req.sentence}` };
}
```

Expand Down Expand Up @@ -126,18 +127,24 @@ $ buf generate buf.build/googleapis/googleapis
```ts
import { Code, ConnectError } from "@connectrpc/connect";
import { ElizaService } from "./gen/eliza_connect";
import { LocalizedMessage } from "./gen/google/rpc/error_details_pb";
import { LocalizedMessageSchema } from "./gen/google/rpc/error_details_pb";

function say() {
const details = [
new LocalizedMessage({
locale: "fr-CH",
message: "Je n'ai plus de mots.",
}),
new LocalizedMessage({
locale: "ja-JP",
message: "もう言葉がありません。",
}),
{
desc: LocalizedMessageSchema,
value: {
locale: "fr-CH",
message: "Je n'ai plus de mots.",
}
},
{
desc: LocalizedMessageSchema,
value: {
locale: "ja-JP",
message: "もう言葉がありません。",
}
},
];
const metadata = new Headers({
"words-left": "none"
Expand Down Expand Up @@ -222,14 +229,14 @@ away:
```typescript
import type { MethodImpl, ServiceImpl } from "@connectrpc/connect";

export const say: MethodImpl<typeof ElizaService.methods.say> = ...
export const say: MethodImpl<typeof ElizaService.method.say> = ...

export const eliza: ServiceImpl<typeof ElizaService> = {
// ...
};

export class Eliza implements ServiceImpl<typeof ElizaService> {
async say(req: SayRequest) {
say(req: SayRequest) {
return {
sentence: `You said ${req.sentence}`,
};
Expand All @@ -250,8 +257,7 @@ export default (router: ConnectRouter) => {

// alternative for using const say
router.rpc(
ElizaService,
ElizaService.methods.say,
ElizaService.method.say,
say
);

Expand Down

0 comments on commit 284d8c5

Please sign in to comment.