Skip to content

Commit

Permalink
export VersionFromHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasJenicek committed Oct 21, 2024
1 parent 8d6b859 commit 8750efa
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
18 changes: 7 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,17 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Install webrpc-gen
run: |
curl -o ./webrpc-gen -fLJO https://github.com/webrpc/webrpc/releases/download/v0.19.3/webrpc-gen.linux-amd64
chmod +x ./webrpc-gen
echo $PWD >> $GITHUB_PATH
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: "1.20"

# - name: Set up Go
# uses: actions/setup-go@v3
# with:
# go-version: "1.20"
# - name: Install webrpc-gen (development)
# run: git clone --single-branch https://github.com/webrpc/webrpc.git --branch master && cd webrpc && make install
- name: Install webrpc-gen (development)
run: git clone --single-branch https://github.com/webrpc/webrpc.git --branch master && cd webrpc && make install

- name: Regenerate examples
run: cd _examples && make generate

- name: Git diff of regenerated files
run: cd _examples && make diff

Expand Down
2 changes: 1 addition & 1 deletion _examples/node-ts/server/server.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type WebrpcGenVersions = {
schemaVersion: string;
};

function versionFromHeader(headers: Headers): WebrpcGenVersions | null {
export function VersionFromHeader(headers: Headers): WebrpcGenVersions | null {
const headerValue = headers.get(WebrpcHeader);
if (!headerValue) {
throw new Error("header is empty or missing");
Expand Down
4 changes: 2 additions & 2 deletions _examples/node-ts/webapp/client.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type WebrpcGenVersions = {
schemaVersion: string;
};

function versionFromHeader(headers: Headers): WebrpcGenVersions | null {
export function VersionFromHeader(headers: Headers): WebrpcGenVersions | null {
const headerValue = headers.get(WebrpcHeader);
if (!headerValue) {
throw new Error("header is empty or missing");
Expand Down Expand Up @@ -157,7 +157,7 @@ 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
headers[WebrpcHeader] = WebrpcHeaderValue

return {
method: 'POST',
Expand Down
62 changes: 58 additions & 4 deletions _examples/sse/webapp/client.gen.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,69 @@
/* eslint-disable */
// webrpc-sse-chat v1.0.0 e64bf9372e2faa45da9d449e8ea108dc0597a98d
// webrpc-sse-chat v1.0.0 a799dc63b082644f5d003c8881424546aee23a2c
// --
// 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 = "e64bf9372e2faa45da9d449e8ea108dc0597a98d"
export const WebRPCSchemaHash = "a799dc63b082644f5d003c8881424546aee23a2c"

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

export 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 @@ -233,9 +284,12 @@ const sseResponse = async (


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
2 changes: 1 addition & 1 deletion main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type WebrpcGenVersions = {
schemaVersion: string;
};

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

0 comments on commit 8750efa

Please sign in to comment.