From 22bc7da297d56c97c09aaf5553acb295376b91ce Mon Sep 17 00:00:00 2001 From: Greg McKelvey Date: Thu, 5 Oct 2023 12:42:43 -0700 Subject: [PATCH 1/2] fix: fix missing implicit types When trying to bundle up the generated code with `tsc --declaration=true`, ts couldn't figure out the disableQuery type and the BaseInfiniteQueryOptions. This is able to be resolved if the exported querys are explicitly typed --- .../gen/eliza-BigIntService_connectquery.ts | 12 +++++++---- .../gen/eliza-ElizaService_connectquery.ts | 16 +++++++-------- .../src/gen/eliza-Haberdasher_connectquery.ts | 5 ++++- .../eliza-PaginatedService_connectquery.ts | 12 +++++++---- .../gen/eliza-SecondService_connectquery.ts | 9 +++++---- .../src/gen/eliza-Slouch_connectquery.ts | 5 ++++- examples/react/basic/tsconfig.json | 3 ++- .../v1/eliza-ElizaService_connectquery.ts | 9 +++++---- .../src/generateTs.ts | 20 ++++++++++++++----- 9 files changed, 59 insertions(+), 32 deletions(-) diff --git a/examples/react/basic/src/gen/eliza-BigIntService_connectquery.ts b/examples/react/basic/src/gen/eliza-BigIntService_connectquery.ts index d958f24f..5da87afa 100644 --- a/examples/react/basic/src/gen/eliza-BigIntService_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-BigIntService_connectquery.ts @@ -21,6 +21,8 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctions, + UnaryHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.BigIntService"; @@ -48,7 +50,9 @@ const $queryService = createQueryService({ service: BigIntService }); /** * @generated from rpc connectrpc.eliza.v1.BigIntService.Count */ -export const count = { - ...$queryService.count, - ...createUnaryHooks($queryService.count), -}; +export const count: UnaryFunctions & + UnaryHooks< + CountRequest, + CountResponse, + UnaryFunctions + > = { ...$queryService.count, ...createUnaryHooks($queryService.count) }; diff --git a/examples/react/basic/src/gen/eliza-ElizaService_connectquery.ts b/examples/react/basic/src/gen/eliza-ElizaService_connectquery.ts index 6fc441fd..1bde0671 100644 --- a/examples/react/basic/src/gen/eliza-ElizaService_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-ElizaService_connectquery.ts @@ -28,6 +28,8 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctions, + UnaryHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.ElizaService"; @@ -102,17 +104,15 @@ const $queryService = createQueryService({ service: ElizaService }); * * @generated from rpc connectrpc.eliza.v1.ElizaService.Say */ -export const say = { - ...$queryService.say, - ...createUnaryHooks($queryService.say), -}; +export const say: UnaryFunctions & + UnaryHooks> = + { ...$queryService.say, ...createUnaryHooks($queryService.say) }; /** * SayAgain is a unary RPC. Eliza responds to the prompt with a single sentence. * * @generated from rpc connectrpc.eliza.v1.ElizaService.SayAgain */ -export const sayAgain = { - ...$queryService.sayAgain, - ...createUnaryHooks($queryService.sayAgain), -}; +export const sayAgain: UnaryFunctions & + UnaryHooks> = + { ...$queryService.sayAgain, ...createUnaryHooks($queryService.sayAgain) }; diff --git a/examples/react/basic/src/gen/eliza-Haberdasher_connectquery.ts b/examples/react/basic/src/gen/eliza-Haberdasher_connectquery.ts index 1681b7fd..0aee386b 100644 --- a/examples/react/basic/src/gen/eliza-Haberdasher_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-Haberdasher_connectquery.ts @@ -21,6 +21,8 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctions, + UnaryHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.Haberdasher"; @@ -48,7 +50,8 @@ const $queryService = createQueryService({ service: Haberdasher }); /** * @generated from rpc connectrpc.eliza.v1.Haberdasher.Work */ -export const work = { +export const work: UnaryFunctions & + UnaryHooks> = { ...$queryService.work, ...createUnaryHooks($queryService.work), }; diff --git a/examples/react/basic/src/gen/eliza-PaginatedService_connectquery.ts b/examples/react/basic/src/gen/eliza-PaginatedService_connectquery.ts index 151bee5e..edd59960 100644 --- a/examples/react/basic/src/gen/eliza-PaginatedService_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-PaginatedService_connectquery.ts @@ -21,6 +21,8 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctions, + UnaryHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.PaginatedService"; @@ -48,7 +50,9 @@ const $queryService = createQueryService({ service: PaginatedService }); /** * @generated from rpc connectrpc.eliza.v1.PaginatedService.List */ -export const list = { - ...$queryService.list, - ...createUnaryHooks($queryService.list), -}; +export const list: UnaryFunctions & + UnaryHooks< + ListRequest, + ListResponse, + UnaryFunctions + > = { ...$queryService.list, ...createUnaryHooks($queryService.list) }; diff --git a/examples/react/basic/src/gen/eliza-SecondService_connectquery.ts b/examples/react/basic/src/gen/eliza-SecondService_connectquery.ts index 30640003..c547ac53 100644 --- a/examples/react/basic/src/gen/eliza-SecondService_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-SecondService_connectquery.ts @@ -28,6 +28,8 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctions, + UnaryHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.SecondService"; @@ -86,7 +88,6 @@ const $queryService = createQueryService({ service: SecondService }); * * @generated from rpc connectrpc.eliza.v1.SecondService.Say */ -export const say = { - ...$queryService.say, - ...createUnaryHooks($queryService.say), -}; +export const say: UnaryFunctions & + UnaryHooks> = + { ...$queryService.say, ...createUnaryHooks($queryService.say) }; diff --git a/examples/react/basic/src/gen/eliza-Slouch_connectquery.ts b/examples/react/basic/src/gen/eliza-Slouch_connectquery.ts index 9b61acf2..9a558456 100644 --- a/examples/react/basic/src/gen/eliza-Slouch_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-Slouch_connectquery.ts @@ -21,6 +21,8 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctions, + UnaryHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.Slouch"; @@ -48,7 +50,8 @@ const $queryService = createQueryService({ service: Slouch }); /** * @generated from rpc connectrpc.eliza.v1.Slouch.Work */ -export const work = { +export const work: UnaryFunctions & + UnaryHooks> = { ...$queryService.work, ...createUnaryHooks($queryService.work), }; diff --git a/examples/react/basic/tsconfig.json b/examples/react/basic/tsconfig.json index 1b139089..8cd11d89 100644 --- a/examples/react/basic/tsconfig.json +++ b/examples/react/basic/tsconfig.json @@ -16,7 +16,8 @@ "strict": true, "target": "ESNext", "useDefineForClassFields": true, - "types": ["jest", "node"] + "types": ["jest", "node"], + "declaration": true // necessary to check if generated code can be published }, "include": ["src", "./*.config.ts", "__mocks__"], "references": [{ "path": "./tsconfig.node.json" }] diff --git a/packages/protoc-gen-connect-query/snapshots/gen/connectrpc/eliza/v1/eliza-ElizaService_connectquery.ts b/packages/protoc-gen-connect-query/snapshots/gen/connectrpc/eliza/v1/eliza-ElizaService_connectquery.ts index bd771909..346ddc3f 100644 --- a/packages/protoc-gen-connect-query/snapshots/gen/connectrpc/eliza/v1/eliza-ElizaService_connectquery.ts +++ b/packages/protoc-gen-connect-query/snapshots/gen/connectrpc/eliza/v1/eliza-ElizaService_connectquery.ts @@ -29,6 +29,8 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctions, + UnaryHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.ElizaService"; @@ -92,7 +94,6 @@ const $queryService = createQueryService({ service: ElizaService }); * * @generated from rpc connectrpc.eliza.v1.ElizaService.Say */ -export const say = { - ...$queryService.say, - ...createUnaryHooks($queryService.say), -}; +export const say: UnaryFunctions & + UnaryHooks> = + { ...$queryService.say, ...createUnaryHooks($queryService.say) }; diff --git a/packages/protoc-gen-connect-query/src/generateTs.ts b/packages/protoc-gen-connect-query/src/generateTs.ts index cab19f21..2c97a57f 100644 --- a/packages/protoc-gen-connect-query/src/generateTs.ts +++ b/packages/protoc-gen-connect-query/src/generateTs.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import type { DescFile, DescService } from "@bufbuild/protobuf"; +import type { DescFile, DescMethod, DescService } from "@bufbuild/protobuf"; import { codegenInfo, MethodIdempotency, MethodKind } from "@bufbuild/protobuf"; import type { Schema } from "@bufbuild/protoplugin"; import { @@ -33,7 +33,8 @@ const { safeIdentifier } = codegenInfo; */ const generateServiceFile = (schema: Schema, protoFile: DescFile, extension: 'js' | 'ts') => - (service: DescService) => { + (service: DescService) => { + const isTs = extension === "ts"; const f = schema.generateFile( `${protoFile.name}-${localName(service)}_connectquery.${extension}`, ); @@ -63,7 +64,7 @@ const generateServiceFile = f.print(" },"); } f.print(" }"); - f.print("}", extension === "ts" ? " as const" : "", ";"); + f.print("}", isTs ? " as const" : "", ";"); f.print(); f.print(`const $queryService = `, @@ -74,13 +75,22 @@ const generateServiceFile = ); f.print(); - + const unaryFunctionType = (method: DescMethod) => [f.import('UnaryFunctions', '@connectrpc/connect-query'), `<${method.input.name}, ${method.output.name}>`] service.methods .filter((method) => method.methodKind === MethodKind.Unary) .forEach((method, index, filteredMethods) => { f.print(makeJsDoc(method)); + const methodTsType = [ + ": ", + ...unaryFunctionType(method), + ` & `, + f.import('UnaryHooks', '@connectrpc/connect-query'), + `<${method.input.name}, ${method.output.name}, `, + ...unaryFunctionType(method), `>` + ] + f.print( - `export const ${safeIdentifier(localName(method))} = { `, + `export const ${safeIdentifier(localName(method))}`, ...(isTs ? methodTsType : []), ` = { `, ` ...$queryService.${localName(method)},`, ` ...`, f.import('createUnaryHooks', '@connectrpc/connect-query'),`($queryService.${localName(method)})`, `};` From 3b4aa96c43ebd87af96b671471f9f77cdd551a11 Mon Sep 17 00:00:00 2001 From: Greg McKelvey Date: Fri, 6 Oct 2023 09:24:44 -0700 Subject: [PATCH 2/2] Use UnaryFunctionsWithHooks --- .../src/gen/eliza-BigIntService_connectquery.ts | 13 +++++-------- .../src/gen/eliza-ElizaService_connectquery.ts | 17 +++++++++-------- .../src/gen/eliza-Haberdasher_connectquery.ts | 6 ++---- .../gen/eliza-PaginatedService_connectquery.ts | 13 +++++-------- .../src/gen/eliza-SecondService_connectquery.ts | 10 +++++----- .../basic/src/gen/eliza-Slouch_connectquery.ts | 6 ++---- .../eliza/v1/eliza-ElizaService_connectquery.ts | 10 +++++----- .../protoc-gen-connect-query/src/generateTs.ts | 10 +++------- 8 files changed, 36 insertions(+), 49 deletions(-) diff --git a/examples/react/basic/src/gen/eliza-BigIntService_connectquery.ts b/examples/react/basic/src/gen/eliza-BigIntService_connectquery.ts index 5da87afa..f1a084c0 100644 --- a/examples/react/basic/src/gen/eliza-BigIntService_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-BigIntService_connectquery.ts @@ -21,8 +21,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, - UnaryFunctions, - UnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.BigIntService"; @@ -50,9 +49,7 @@ const $queryService = createQueryService({ service: BigIntService }); /** * @generated from rpc connectrpc.eliza.v1.BigIntService.Count */ -export const count: UnaryFunctions & - UnaryHooks< - CountRequest, - CountResponse, - UnaryFunctions - > = { ...$queryService.count, ...createUnaryHooks($queryService.count) }; +export const count: UnaryFunctionsWithHooks = { + ...$queryService.count, + ...createUnaryHooks($queryService.count), +}; diff --git a/examples/react/basic/src/gen/eliza-ElizaService_connectquery.ts b/examples/react/basic/src/gen/eliza-ElizaService_connectquery.ts index 1bde0671..1b46e4f9 100644 --- a/examples/react/basic/src/gen/eliza-ElizaService_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-ElizaService_connectquery.ts @@ -28,8 +28,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, - UnaryFunctions, - UnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.ElizaService"; @@ -104,15 +103,17 @@ const $queryService = createQueryService({ service: ElizaService }); * * @generated from rpc connectrpc.eliza.v1.ElizaService.Say */ -export const say: UnaryFunctions & - UnaryHooks> = - { ...$queryService.say, ...createUnaryHooks($queryService.say) }; +export const say: UnaryFunctionsWithHooks = { + ...$queryService.say, + ...createUnaryHooks($queryService.say), +}; /** * SayAgain is a unary RPC. Eliza responds to the prompt with a single sentence. * * @generated from rpc connectrpc.eliza.v1.ElizaService.SayAgain */ -export const sayAgain: UnaryFunctions & - UnaryHooks> = - { ...$queryService.sayAgain, ...createUnaryHooks($queryService.sayAgain) }; +export const sayAgain: UnaryFunctionsWithHooks = { + ...$queryService.sayAgain, + ...createUnaryHooks($queryService.sayAgain), +}; diff --git a/examples/react/basic/src/gen/eliza-Haberdasher_connectquery.ts b/examples/react/basic/src/gen/eliza-Haberdasher_connectquery.ts index 0aee386b..7f0b63df 100644 --- a/examples/react/basic/src/gen/eliza-Haberdasher_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-Haberdasher_connectquery.ts @@ -21,8 +21,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, - UnaryFunctions, - UnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.Haberdasher"; @@ -50,8 +49,7 @@ const $queryService = createQueryService({ service: Haberdasher }); /** * @generated from rpc connectrpc.eliza.v1.Haberdasher.Work */ -export const work: UnaryFunctions & - UnaryHooks> = { +export const work: UnaryFunctionsWithHooks = { ...$queryService.work, ...createUnaryHooks($queryService.work), }; diff --git a/examples/react/basic/src/gen/eliza-PaginatedService_connectquery.ts b/examples/react/basic/src/gen/eliza-PaginatedService_connectquery.ts index edd59960..76162fb3 100644 --- a/examples/react/basic/src/gen/eliza-PaginatedService_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-PaginatedService_connectquery.ts @@ -21,8 +21,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, - UnaryFunctions, - UnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.PaginatedService"; @@ -50,9 +49,7 @@ const $queryService = createQueryService({ service: PaginatedService }); /** * @generated from rpc connectrpc.eliza.v1.PaginatedService.List */ -export const list: UnaryFunctions & - UnaryHooks< - ListRequest, - ListResponse, - UnaryFunctions - > = { ...$queryService.list, ...createUnaryHooks($queryService.list) }; +export const list: UnaryFunctionsWithHooks = { + ...$queryService.list, + ...createUnaryHooks($queryService.list), +}; diff --git a/examples/react/basic/src/gen/eliza-SecondService_connectquery.ts b/examples/react/basic/src/gen/eliza-SecondService_connectquery.ts index c547ac53..1f0b0a63 100644 --- a/examples/react/basic/src/gen/eliza-SecondService_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-SecondService_connectquery.ts @@ -28,8 +28,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, - UnaryFunctions, - UnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.SecondService"; @@ -88,6 +87,7 @@ const $queryService = createQueryService({ service: SecondService }); * * @generated from rpc connectrpc.eliza.v1.SecondService.Say */ -export const say: UnaryFunctions & - UnaryHooks> = - { ...$queryService.say, ...createUnaryHooks($queryService.say) }; +export const say: UnaryFunctionsWithHooks = { + ...$queryService.say, + ...createUnaryHooks($queryService.say), +}; diff --git a/examples/react/basic/src/gen/eliza-Slouch_connectquery.ts b/examples/react/basic/src/gen/eliza-Slouch_connectquery.ts index 9a558456..efb93877 100644 --- a/examples/react/basic/src/gen/eliza-Slouch_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-Slouch_connectquery.ts @@ -21,8 +21,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, - UnaryFunctions, - UnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.Slouch"; @@ -50,8 +49,7 @@ const $queryService = createQueryService({ service: Slouch }); /** * @generated from rpc connectrpc.eliza.v1.Slouch.Work */ -export const work: UnaryFunctions & - UnaryHooks> = { +export const work: UnaryFunctionsWithHooks = { ...$queryService.work, ...createUnaryHooks($queryService.work), }; diff --git a/packages/protoc-gen-connect-query/snapshots/gen/connectrpc/eliza/v1/eliza-ElizaService_connectquery.ts b/packages/protoc-gen-connect-query/snapshots/gen/connectrpc/eliza/v1/eliza-ElizaService_connectquery.ts index 346ddc3f..b47caae9 100644 --- a/packages/protoc-gen-connect-query/snapshots/gen/connectrpc/eliza/v1/eliza-ElizaService_connectquery.ts +++ b/packages/protoc-gen-connect-query/snapshots/gen/connectrpc/eliza/v1/eliza-ElizaService_connectquery.ts @@ -29,8 +29,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, - UnaryFunctions, - UnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.ElizaService"; @@ -94,6 +93,7 @@ const $queryService = createQueryService({ service: ElizaService }); * * @generated from rpc connectrpc.eliza.v1.ElizaService.Say */ -export const say: UnaryFunctions & - UnaryHooks> = - { ...$queryService.say, ...createUnaryHooks($queryService.say) }; +export const say: UnaryFunctionsWithHooks = { + ...$queryService.say, + ...createUnaryHooks($queryService.say), +}; diff --git a/packages/protoc-gen-connect-query/src/generateTs.ts b/packages/protoc-gen-connect-query/src/generateTs.ts index 2c97a57f..cee3d445 100644 --- a/packages/protoc-gen-connect-query/src/generateTs.ts +++ b/packages/protoc-gen-connect-query/src/generateTs.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import type { DescFile, DescMethod, DescService } from "@bufbuild/protobuf"; +import type { DescFile, DescService } from "@bufbuild/protobuf"; import { codegenInfo, MethodIdempotency, MethodKind } from "@bufbuild/protobuf"; import type { Schema } from "@bufbuild/protoplugin"; import { @@ -75,18 +75,14 @@ const generateServiceFile = ); f.print(); - const unaryFunctionType = (method: DescMethod) => [f.import('UnaryFunctions', '@connectrpc/connect-query'), `<${method.input.name}, ${method.output.name}>`] service.methods .filter((method) => method.methodKind === MethodKind.Unary) .forEach((method, index, filteredMethods) => { f.print(makeJsDoc(method)); const methodTsType = [ ": ", - ...unaryFunctionType(method), - ` & `, - f.import('UnaryHooks', '@connectrpc/connect-query'), - `<${method.input.name}, ${method.output.name}, `, - ...unaryFunctionType(method), `>` + f.import('UnaryFunctionsWithHooks', '@connectrpc/connect-query'), + `<${method.input.name}, ${method.output.name}>` ] f.print(