Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep the same ctx object for all hooks #53

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion .github/workflows/twirp-ts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [14.x, 15.x]
node-version: [14.x, 16.x, 18.x]

steps:
- uses: actions/checkout@v2
Expand All @@ -28,3 +28,17 @@ jobs:
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.github_token }}
- name: Install Protoc
uses: arduino/setup-protoc@v1
with:
version: '3.19.4'
- run: |
npm ci
npm link ../
make
git diff --exit-code
working-directory: ./example/
- run: |
make
git diff --exit-code
working-directory: ./src/twirp/__mocks__
6 changes: 4 additions & 2 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
.PHONY: all clean ts-proto protobuf-ts

PROTOC_GEN_TWIRP_BIN="../protoc-gen-twirp_ts"
PROTOC_GEN_TWIRP_BIN="./node_modules/.bin/protoc-gen-twirp_ts"
PROTOC_GEN_PROTOBUF_TS_BIN="./node_modules/.bin/protoc-gen-ts"
PROTOC_GEN_TS_PROTO_BIN="./node_modules/.bin/protoc-gen-ts_proto"

OUT_DIR="./generated"

all: protobuf-ts

clean:
rm -rf $(OUT_DIR)
mkdir -p $(OUT_DIR)
Expand Down Expand Up @@ -39,4 +42,3 @@ protobuf-ts: clean
--twirp_ts_opt="openapi_gateway" \
--twirp_ts_out=$(OUT_DIR) \
./protos/*.proto

17 changes: 0 additions & 17 deletions example/generated/haberdasher.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,3 @@ components:
$ref: "#/components/schemas/Filter"
description: ""
description: ""
Filter:
properties:
order_by:
type: string
description: ""
pagination:
$ref: "#/components/schemas/Pagination"
description: ""
Pagination:
properties:
limit:
type: integer
description: ""
offset:
type: integer
description: ""
description: ""
17 changes: 0 additions & 17 deletions example/generated/haberdasher.twirp.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,3 @@ components:
$ref: "#/components/schemas/Filter"
description: ""
description: ""
Filter:
properties:
order_by:
type: string
description: ""
pagination:
$ref: "#/components/schemas/Pagination"
description: ""
Pagination:
properties:
limit:
type: integer
description: ""
offset:
type: integer
description: ""
description: ""
6 changes: 3 additions & 3 deletions example/generated/service.twirp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function matchHaberdasherRoute<T extends TwirpContext = TwirpContext>(
data: Buffer,
interceptors?: Interceptor<T, Size, Hat>[]
) => {
ctx = { ...ctx, methodName: "MakeHat" };
ctx.methodName = "MakeHat";
await events.onMatch(ctx);
return handleHaberdasherMakeHatRequest(
ctx,
Expand All @@ -190,7 +190,7 @@ function matchHaberdasherRoute<T extends TwirpContext = TwirpContext>(
data: Buffer,
interceptors?: Interceptor<T, FindHatRPC, FindHatRPC>[]
) => {
ctx = { ...ctx, methodName: "FindHat" };
ctx.methodName = "FindHat";
await events.onMatch(ctx);
return handleHaberdasherFindHatRequest(
ctx,
Expand All @@ -206,7 +206,7 @@ function matchHaberdasherRoute<T extends TwirpContext = TwirpContext>(
data: Buffer,
interceptors?: Interceptor<T, ListHatRPC, ListHatRPC>[]
) => {
ctx = { ...ctx, methodName: "ListHat" };
ctx.methodName = "ListHat";
await events.onMatch(ctx);
return handleHaberdasherListHatRequest(
ctx,
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/protoc-gen-twirp-ts/gen/twirp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ function genRouteHandler(ctx: any, file: FileDescriptorProto, service: ServiceDe
const cases = service.method.map(method => code`
case "${formatMethodName(ctx, method.name!)}":
return async (ctx: T, service: ${service.name}Twirp ,data: Buffer, interceptors?: ${Interceptor}<T, ${relativeMessageName(ctx, file, method.inputType)}, ${relativeMessageName(ctx, file, method.outputType)}>[]) => {
ctx = {...ctx, methodName: "${formatMethodName(ctx, method.name!)}" }
ctx.methodName = "${formatMethodName(ctx, method.name!)}"
await events.onMatch(ctx);
return handle${formatMethodName(ctx, method.name!, service.name)}Request(ctx, service, data, interceptors)
}
Expand Down Expand Up @@ -472,4 +472,4 @@ function formatMethodName(ctx: any, methodName: string, serviceName?: string) {

serviceName = serviceName || ""
return ctx.camelCase ? camelCase(serviceName) + camelCase(methodName) : serviceName + methodName;
}
}
29 changes: 29 additions & 0 deletions src/twirp/__mocks__/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.PHONY: all clean protobuf-ts
PROTOC_GEN_TWIRP_BIN="../../../protoc-gen-twirp_ts"
PROTOC_GEN_PROTOBUF_TS_BIN="../../../node_modules/.bin/protoc-gen-ts"

OUT_DIR="./"

ifeq ($(shell uname),Darwin)
SP=" "
endif

all: replace

clean:
find $(OUT_DIR) -name '*.ts' -delete

protobuf-ts: clean
protoc \
-I ./ \
-I ../../../example/protos \
--plugin=protoc-gen-ts=$(PROTOC_GEN_PROTOBUF_TS_BIN) \
--plugin=protoc-gen-twirp_ts=$(PROTOC_GEN_TWIRP_BIN) \
--ts_opt=client_none \
--ts_out=$(OUT_DIR) \
--twirp_ts_opt="gateway" \
--twirp_ts_out=$(OUT_DIR) \
./service.proto

replace: protobuf-ts
find $(OUT_DIR) -name '*.ts' -exec sed -i$(SP)'' 's/from "twirp-ts"/from "..\/index"/g' {} +
Loading