Skip to content

Commit

Permalink
render webrpcHeader on client and server if enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasJenicek committed Oct 21, 2024
1 parent 27fa214 commit 8d6b859
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 12 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ As you can see, the `-target` supports default `typescript`, any git URI, or a l
### Set custom template variables
Change any of the following values by passing `-option="Value"` CLI flag to `webrpc-gen`.

| webrpc-gen -option | Description | Default value |
|----------------------|----------------------------|----------------------------|
| `-client` | generate client code | unset (`false`) |
| `-server` | generate server code | unset (`false`) |
| webrpc-gen -option | Description | Default value | Version |
|--------------------|----------------------|---------------------------------------------------|---------|
| `-client` | generate client code | unset (`false`) | v0.0.1 |
| `-server` | generate server code | unset (`false`) | v0.0.1 |
| `-webrpcHeader` | `true` | enable client send webrpc version in http headers | v0.16.0 |

## LICENSE

Expand Down
59 changes: 56 additions & 3 deletions _examples/node-ts/server/server.gen.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,69 @@
/* eslint-disable */
// node-ts v1.0.0 bd572b349e330d81cc30b1ff3cf69d7ab59f1619
// node-ts v1.0.0 6713366104e62b8479d628a193e2a7ca03f37edc
// --
// Code generated by webrpc-gen@v0.19.3 with ../../ generator. DO NOT EDIT.
// Code generated by webrpc-gen@v0.20.3-1-gf6584bc with ../../ generator. DO NOT EDIT.
//
// webrpc-gen -schema=service.ridl -target=../../ -server -out=./server/server.gen.ts

export const WebrpcHeader = "Webrpc"

export const WebrpcHeaderValue = "[email protected];@unknown;[email protected]"

// WebRPC description and code-gen version
export const WebRPCVersion = "v1"

// Schema version of your RIDL schema
export const WebRPCSchemaVersion = "v1.0.0"

// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = "bd572b349e330d81cc30b1ff3cf69d7ab59f1619"
export const WebRPCSchemaHash = "6713366104e62b8479d628a193e2a7ca03f37edc"

type WebrpcGenVersions = {
webrpcGenVersion: string;
codeGenName: string;
codeGenVersion: string;
schemaName: string;
schemaVersion: string;
};

function versionFromHeader(headers: Headers): WebrpcGenVersions | null {
const headerValue = headers.get(WebrpcHeader);
if (!headerValue) {
throw new Error("header is empty or missing");
}

return parseWebrpcGenVersions(headerValue);
}

function parseWebrpcGenVersions(header: string): WebrpcGenVersions {
const versions = header.split(";");
if (versions.length < 3) {
throw new Error(`expected at least 3 parts while parsing webrpc header: ${header}`);
}

const [_, webrpcGenVersion] = versions[0].split("@");
if (!webrpcGenVersion) {
throw new Error(`webrpc gen version could not be parsed from: ${versions[0]}`);
}

const [tmplTarget, tmplVersion] = versions[1].split("@");
if (!tmplTarget || !tmplVersion) {
throw new Error(`tmplTarget and tmplVersion could not be parsed from: ${versions[1]}`);
}

const [schemaName, schemaVersion] = versions[2].split("@");
if (!schemaName || !schemaVersion) {
throw new Error(`schema name and schema version could not be parsed from: ${versions[2]}`);
}

return {
webrpcGenVersion,
codeGenName: tmplTarget,
codeGenVersion: tmplVersion,
schemaName,
schemaVersion,
};
}

//
// Types
Expand Down Expand Up @@ -91,6 +142,8 @@ import express from 'express'
app.post('/*', async (req, res) => {
const requestPath = req.baseUrl + req.path

res.header(WebrpcHeader, WebrpcHeaderValue);

if (!req.body) {
res.status(400).send("webrpc error: missing body");

Expand Down
62 changes: 58 additions & 4 deletions _examples/node-ts/webapp/client.gen.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,69 @@
/* eslint-disable */
// node-ts v1.0.0 bd572b349e330d81cc30b1ff3cf69d7ab59f1619
// node-ts v1.0.0 6713366104e62b8479d628a193e2a7ca03f37edc
// --
// Code generated by webrpc-gen@v0.19.3 with ../../ generator. DO NOT EDIT.
// Code generated by webrpc-gen@v0.20.3-1-gf6584bc with ../../ generator. DO NOT EDIT.
//
// webrpc-gen -schema=service.ridl -target=../../ -client -out=./webapp/client.gen.ts

export const WebrpcHeader = "Webrpc"

export const WebrpcHeaderValue = "[email protected];@unknown;[email protected]"

// WebRPC description and code-gen version
export const WebRPCVersion = "v1"

// Schema version of your RIDL schema
export const WebRPCSchemaVersion = "v1.0.0"

// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = "bd572b349e330d81cc30b1ff3cf69d7ab59f1619"
export const WebRPCSchemaHash = "6713366104e62b8479d628a193e2a7ca03f37edc"

type WebrpcGenVersions = {
webrpcGenVersion: string;
codeGenName: string;
codeGenVersion: string;
schemaName: string;
schemaVersion: string;
};

function versionFromHeader(headers: Headers): WebrpcGenVersions | null {
const headerValue = headers.get(WebrpcHeader);
if (!headerValue) {
throw new Error("header is empty or missing");
}

return parseWebrpcGenVersions(headerValue);
}

function parseWebrpcGenVersions(header: string): WebrpcGenVersions {
const versions = header.split(";");
if (versions.length < 3) {
throw new Error(`expected at least 3 parts while parsing webrpc header: ${header}`);
}

const [_, webrpcGenVersion] = versions[0].split("@");
if (!webrpcGenVersion) {
throw new Error(`webrpc gen version could not be parsed from: ${versions[0]}`);
}

const [tmplTarget, tmplVersion] = versions[1].split("@");
if (!tmplTarget || !tmplVersion) {
throw new Error(`tmplTarget and tmplVersion could not be parsed from: ${versions[1]}`);
}

const [schemaName, schemaVersion] = versions[2].split("@");
if (!schemaName || !schemaVersion) {
throw new Error(`schema name and schema version could not be parsed from: ${versions[2]}`);
}

return {
webrpcGenVersion,
codeGenName: tmplTarget,
codeGenVersion: tmplVersion,
schemaName,
schemaVersion,
};
}

//
// Types
Expand Down Expand Up @@ -105,9 +156,12 @@ export class ExampleService implements ExampleService {
}

const createHTTPRequest = (body: object = {}, headers: object = {}, signal: AbortSignal | null = null): object => {
const headers = { ...headers, 'Content-Type': 'application/json' }
headers[WebrpcHeader] = WebrpcHeaderValue

return {
method: 'POST',
headers: { ...headers, 'Content-Type': 'application/json' },
headers: headers,
body: JSON.stringify(body || {}),
signal
}
Expand Down
7 changes: 6 additions & 1 deletion clientHelpers.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
{{- $schemaErrors := .SchemaErrors -}}
{{- $opts := .Opts -}}
const createHTTPRequest = (body: object = {}, headers: object = {}, signal: AbortSignal | null = null): object => {
const headers = { ...headers, 'Content-Type': 'application/json' }
{{- if eq $opts.webrpcHeader true }}
headers[WebrpcHeader] = WebrpcHeaderValue
{{- end }}

return {
method: 'POST',
headers: { ...headers, 'Content-Type': 'application/json' },
headers: headers,
body: JSON.stringify(body || {}),
signal
}
Expand Down
52 changes: 52 additions & 0 deletions main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{{- $opts := dict -}}
{{- set $opts "client" (ternary (in .Opts.client "" "true") true false) -}}
{{- set $opts "server" (ternary (in .Opts.server "" "true") true false) -}}
{{- set $opts "webrpcHeader" (ternary (eq (default .Opts.webrpcHeader "true") "false") false true) -}}

{{- /* Print help on -help. */ -}}
{{- if exists .Opts "help" -}}
Expand Down Expand Up @@ -67,6 +68,10 @@
//
// {{.WebrpcGenCommand}}

export const WebrpcHeader = "Webrpc"

export const WebrpcHeaderValue = "{{ .WebrpcHeader }}"

// WebRPC description and code-gen version
export const WebRPCVersion = "{{.WebrpcVersion}}"

Expand All @@ -76,6 +81,53 @@ export const WebRPCSchemaVersion = "{{.SchemaVersion}}"
// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = "{{.SchemaHash}}"

type WebrpcGenVersions = {
webrpcGenVersion: string;
codeGenName: string;
codeGenVersion: string;
schemaName: string;
schemaVersion: string;
};

function versionFromHeader(headers: Headers): WebrpcGenVersions | null {
const headerValue = headers.get(WebrpcHeader);
if (!headerValue) {
throw new Error("header is empty or missing");
}

return parseWebrpcGenVersions(headerValue);
}

function parseWebrpcGenVersions(header: string): WebrpcGenVersions {
const versions = header.split(";");
if (versions.length < 3) {
throw new Error(`expected at least 3 parts while parsing webrpc header: ${header}`);
}

const [_, webrpcGenVersion] = versions[0].split("@");
if (!webrpcGenVersion) {
throw new Error(`webrpc gen version could not be parsed from: ${versions[0]}`);
}

const [tmplTarget, tmplVersion] = versions[1].split("@");
if (!tmplTarget || !tmplVersion) {
throw new Error(`tmplTarget and tmplVersion could not be parsed from: ${versions[1]}`);
}

const [schemaName, schemaVersion] = versions[2].split("@");
if (!schemaName || !schemaVersion) {
throw new Error(`schema name and schema version could not be parsed from: ${versions[2]}`);
}

return {
webrpcGenVersion,
codeGenName: tmplTarget,
codeGenVersion: tmplVersion,
schemaName,
schemaVersion,
};
}

{{template "types" dict "Types" .Types "Services" .Services "TypeMap" $typeMap}}

{{- if $opts.client}}
Expand Down
2 changes: 2 additions & 0 deletions server.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import express from 'express'
app.post('/*', async (req, res) => {
const requestPath = req.baseUrl + req.path

res.header(WebrpcHeader, WebrpcHeaderValue);

if (!req.body) {
res.status(400).send("webrpc error: missing body");

Expand Down

0 comments on commit 8d6b859

Please sign in to comment.