Skip to content

Commit 97c6674

Browse files
Add query keys (#41)
* Add query keys * Simplify example * Regenrate sse example * Update readme
1 parent 09ddd42 commit 97c6674

File tree

7 files changed

+66
-24
lines changed

7 files changed

+66
-24
lines changed

README.md

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
1-
webrpc-gen Typescript templates
2-
===============================
1+
# webrpc-gen Typescript templates
32

43
This repo contains the templates used by the `webrpc-gen` cli to code-generate
54
webrpc Typescript server and client code.
65

76
This generator, from a webrpc schema/design file will code-generate:
87

98
1. Client -- an isomorphic/universal Typescript client to speak to a webrpc server using the
10-
provided schema. This client is compatible with any webrpc server language (ie. Go, nodejs, etc.).
11-
As the client is isomorphic, means you can use this within a Web browser or use the client in a
12-
server like nodejs -- both without needing any dependencies. I suggest to read the generated TS
13-
output of the generated code, and you shall see, its nothing fancy, just the sort of thing you'd
14-
write by hand.
9+
provided schema. This client is compatible with any webrpc server language (ie. Go, nodejs, etc.).
10+
As the client is isomorphic, means you can use this within a Web browser or use the client in a
11+
server like nodejs -- both without needing any dependencies. I suggest to read the generated TS
12+
output of the generated code, and you shall see, its nothing fancy, just the sort of thing you'd
13+
write by hand.
1514

1615
2. Server -- a nodejs Typescript server handler. See examples.
1716

17+
## Features
18+
19+
### Query Keys for React Query / SWR
20+
21+
The generated client includes a `queryKey` property with type-safe query key generators for each endpoint. This makes it easy to use with popular data-fetching libraries:
22+
23+
```typescript
24+
import { useQuery } from '@tanstack/react-query'
25+
import { Example } from './client.gen'
26+
27+
const client = new Example('http://localhost:3000', fetch)
28+
29+
function UserProfile({ userId }) {
30+
const { data } = useQuery({
31+
queryKey: client.queryKey.getUser({ userId }),
32+
queryFn: ({ signal }) => client.getUser({ userId }, undefined, signal)
33+
})
34+
35+
return <div>{data?.user.name}</div>
36+
}
37+
```
38+
39+
The query keys follow the pattern `[ServiceName, methodName, request?]` and are fully type-safe with `as const` assertions.
40+
1841
## Usage
1942

2043
```
2144
webrpc-gen -schema=example.ridl -target=typescript -server -client -out=./example.gen.ts
2245
```
2346

24-
or
47+
or
2548

2649
```
2750
webrpc-gen -schema=example.ridl -target=github.com/webrpc/[email protected] -server -client -out=./example.gen.ts
@@ -36,12 +59,13 @@ webrpc-gen -schema=example.ridl -target=./local-templates-on-disk -server -clien
3659
As you can see, the `-target` supports default `typescript`, any git URI, or a local folder :)
3760

3861
### Set custom template variables
62+
3963
Change any of the following values by passing `-option="Value"` CLI flag to `webrpc-gen`.
4064

4165
| webrpc-gen -option | Description | Default value | Version |
42-
|--------------------|----------------------|---------------------------------------------------|---------|
66+
| ------------------ | -------------------- | ------------------------------------------------- | ------- |
4367
| `-client` | generate client code | unset (`false`) | v0.0.1 |
44-
| `-server` | generate server code | unset (`false`) | v0.0.1 |
68+
| `-server` | generate server code | unset (`false`) | v0.0.1 |
4569
| `-webrpcHeader` | `true` | enable client send webrpc version in http headers | v0.15.0 |
4670

4771
## LICENSE

_examples/node-ts/server-fastify/server.gen.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable */
22
// node-ts v1.0.0 21701cae51b73d035bf2180831cdb38220bbbccc
33
// --
4-
// Code generated by Webrpc-gen@v0.28.2-4-gcd30baf with ../../../gen-typescript generator. DO NOT EDIT.
4+
// Code generated by Webrpc-gen@v0.29.0 with ../../../gen-typescript generator. DO NOT EDIT.
55
//
66
// webrpc-gen -schema=service.ridl -target=../../../gen-typescript -server -out=./server-fastify/server.gen.ts
77

@@ -535,7 +535,7 @@ export const webrpcErrorByCode: { [code: number]: any } = {
535535

536536
export const WebrpcHeader = "Webrpc"
537537

538-
export const WebrpcHeaderValue = "webrpc@v0.28.2-4-gcd30baf;gen-typescript@unknown;[email protected]"
538+
export const WebrpcHeaderValue = "webrpc@v0.29.0;gen-typescript@unknown;[email protected]"
539539

540540
type WebrpcGenVersions = {
541541
WebrpcGenVersion: string;

_examples/node-ts/server-hono/server.gen.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable */
22
// node-ts v1.0.0 21701cae51b73d035bf2180831cdb38220bbbccc
33
// --
4-
// Code generated by Webrpc-gen@v0.28.2-4-gcd30baf with ../../../gen-typescript generator. DO NOT EDIT.
4+
// Code generated by Webrpc-gen@v0.29.0 with ../../../gen-typescript generator. DO NOT EDIT.
55
//
66
// webrpc-gen -schema=service.ridl -target=../../../gen-typescript -server -out=./server-hono/server.gen.ts
77

@@ -535,7 +535,7 @@ export const webrpcErrorByCode: { [code: number]: any } = {
535535

536536
export const WebrpcHeader = "Webrpc"
537537

538-
export const WebrpcHeaderValue = "webrpc@v0.28.2-4-gcd30baf;gen-typescript@unknown;[email protected]"
538+
export const WebrpcHeaderValue = "webrpc@v0.29.0;gen-typescript@unknown;[email protected]"
539539

540540
type WebrpcGenVersions = {
541541
WebrpcGenVersion: string;

_examples/node-ts/server/server.gen.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable */
22
// node-ts v1.0.0 21701cae51b73d035bf2180831cdb38220bbbccc
33
// --
4-
// Code generated by Webrpc-gen@v0.28.2-4-gcd30baf with ../../../gen-typescript generator. DO NOT EDIT.
4+
// Code generated by Webrpc-gen@v0.29.0 with ../../../gen-typescript generator. DO NOT EDIT.
55
//
66
// webrpc-gen -schema=service.ridl -target=../../../gen-typescript -server -out=./server/server.gen.ts
77

@@ -535,7 +535,7 @@ export const webrpcErrorByCode: { [code: number]: any } = {
535535

536536
export const WebrpcHeader = "Webrpc"
537537

538-
export const WebrpcHeaderValue = "webrpc@v0.28.2-4-gcd30baf;gen-typescript@unknown;[email protected]"
538+
export const WebrpcHeaderValue = "webrpc@v0.29.0;gen-typescript@unknown;[email protected]"
539539

540540
type WebrpcGenVersions = {
541541
WebrpcGenVersion: string;

_examples/node-ts/webapp/client.gen.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable */
22
// node-ts v1.0.0 21701cae51b73d035bf2180831cdb38220bbbccc
33
// --
4-
// Code generated by Webrpc-gen@v0.28.2-4-gcd30baf with ../../../gen-typescript generator. DO NOT EDIT.
4+
// Code generated by Webrpc-gen@v0.29.0 with ../../../gen-typescript generator. DO NOT EDIT.
55
//
66
// webrpc-gen -schema=service.ridl -target=../../../gen-typescript -client -out=./webapp/client.gen.ts
77

@@ -102,6 +102,12 @@ export class Example implements ExampleClient {
102102
return this.hostname + this.path + name
103103
}
104104

105+
queryKey = {
106+
ping: () => ['Example', 'ping'] as const,
107+
getUser: (req: GetUserRequest) => ['Example', 'getUser', req] as const,
108+
getArticle: (req: GetArticleRequest) => ['Example', 'getArticle', req] as const,
109+
}
110+
105111
ping = (headers?: object, signal?: AbortSignal): Promise<PingResponse> => {
106112
return this.fetch(
107113
this.url('Ping'),
@@ -484,7 +490,7 @@ export const webrpcErrorByCode: { [code: number]: any } = {
484490

485491
export const WebrpcHeader = "Webrpc"
486492

487-
export const WebrpcHeaderValue = "webrpc@v0.28.2-4-gcd30baf;gen-typescript@unknown;[email protected]"
493+
export const WebrpcHeaderValue = "webrpc@v0.29.0;gen-typescript@unknown;[email protected]"
488494

489495
type WebrpcGenVersions = {
490496
WebrpcGenVersion: string;

_examples/sse/webapp/client.gen.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable */
22
// webrpc-sse-chat v1.0.0 a799dc63b082644f5d003c8881424546aee23a2c
33
// --
4-
// Code generated by Webrpc-gen@v0.28.2-4-gcd30baf with ../../ generator. DO NOT EDIT.
4+
// Code generated by Webrpc-gen@v0.29.0 with ../../ generator. DO NOT EDIT.
55
//
66
// webrpc-gen -schema=service.ridl -target=../../ -client -out=./webapp/client.gen.ts
77

@@ -81,6 +81,12 @@ export class Chat implements ChatClient {
8181
return this.hostname + this.path + name
8282
}
8383

84+
queryKey = {
85+
sendMessage: (req: SendMessageRequest) => ['Chat', 'sendMessage', req] as const,
86+
subscribeMessages: (req: SubscribeMessagesRequest) => ['Chat', 'subscribeMessages', req] as const,
87+
subscribeUsers: () => ['Chat', 'subscribeUsers'] as const,
88+
}
89+
8490
sendMessage = (req: SendMessageRequest, headers?: object, signal?: AbortSignal): Promise<SendMessageResponse> => {
8591
return this.fetch(
8692
this.url('SendMessage'),
@@ -550,7 +556,7 @@ export const webrpcErrorByCode: { [code: number]: any } = {
550556

551557
export const WebrpcHeader = "Webrpc"
552558

553-
export const WebrpcHeaderValue = "webrpc@v0.28.2-4-gcd30baf;@unknown;[email protected]"
559+
export const WebrpcHeaderValue = "webrpc@v0.29.0;@unknown;[email protected]"
554560

555561
type WebrpcGenVersions = {
556562
WebrpcGenVersion: string;

client.go.tmpl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
// Client
99
//
1010

11-
{{- "\n" -}}{{- range .Services}}
12-
export class {{.Name}} implements {{.Name}}Client {
11+
{{- "\n" -}}{{- range $serviceIndex, $service := .Services}}
12+
export class {{$service.Name}} implements {{$service.Name}}Client {
1313
protected hostname: string
1414
protected fetch: Fetch
15-
protected path = '/rpc/{{.Name}}/'
15+
protected path = '/rpc/{{$service.Name}}/'
1616

1717
constructor(hostname: string, fetch: Fetch) {
1818
this.hostname = hostname.replace(/\/*$/, '')
@@ -22,7 +22,13 @@ export class {{.Name}} implements {{.Name}}Client {
2222
private url(name: string): string {
2323
return this.hostname + this.path + name
2424
}
25-
{{range $_, $method := .Methods}}
25+
26+
queryKey = {
27+
{{- range $i, $method := $service.Methods}}
28+
{{firstLetterToLower .Name}}: ({{if .Inputs | len}}req: {{.Name}}{{if $opts.compat}}Args{{else}}Request{{end}}{{end}}) => ['{{$service.Name}}', '{{firstLetterToLower .Name}}'{{if .Inputs | len}}, req{{end}}] as const,
29+
{{- end}}
30+
}
31+
{{range $_, $method := $service.Methods}}
2632
{{firstLetterToLower .Name}} = ({{template "methodInputs" dict "Method" . "Opts" $opts "TypeMap" $typeMap}}): {{if $method.StreamOutput}}WebrpcStreamController{{else}}{{if $method.Succinct}}Promise<{{(index $method.Outputs 0).Type}}>{{else}}Promise<{{$method.Name}}{{if $opts.compat}}Return{{else}}Response{{end}}>{{end}}{{end}} => {
2733
{{- if $method.StreamOutput }}
2834
const abortController = new AbortController()

0 commit comments

Comments
 (0)