diff --git a/examples/react/basic/src/gen/eliza-BigIntService_connectquery.ts b/examples/react/basic/src/gen/eliza-BigIntService_connectquery.ts index d958f24f..f1a084c0 100644 --- a/examples/react/basic/src/gen/eliza-BigIntService_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-BigIntService_connectquery.ts @@ -21,6 +21,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.BigIntService"; @@ -48,7 +49,7 @@ const $queryService = createQueryService({ service: BigIntService }); /** * @generated from rpc connectrpc.eliza.v1.BigIntService.Count */ -export const 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 6fc441fd..1b46e4f9 100644 --- a/examples/react/basic/src/gen/eliza-ElizaService_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-ElizaService_connectquery.ts @@ -28,6 +28,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.ElizaService"; @@ -102,7 +103,7 @@ const $queryService = createQueryService({ service: ElizaService }); * * @generated from rpc connectrpc.eliza.v1.ElizaService.Say */ -export const say = { +export const say: UnaryFunctionsWithHooks = { ...$queryService.say, ...createUnaryHooks($queryService.say), }; @@ -112,7 +113,7 @@ export const say = { * * @generated from rpc connectrpc.eliza.v1.ElizaService.SayAgain */ -export const 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 1681b7fd..7f0b63df 100644 --- a/examples/react/basic/src/gen/eliza-Haberdasher_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-Haberdasher_connectquery.ts @@ -21,6 +21,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.Haberdasher"; @@ -48,7 +49,7 @@ const $queryService = createQueryService({ service: Haberdasher }); /** * @generated from rpc connectrpc.eliza.v1.Haberdasher.Work */ -export const work = { +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 151bee5e..76162fb3 100644 --- a/examples/react/basic/src/gen/eliza-PaginatedService_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-PaginatedService_connectquery.ts @@ -21,6 +21,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.PaginatedService"; @@ -48,7 +49,7 @@ const $queryService = createQueryService({ service: PaginatedService }); /** * @generated from rpc connectrpc.eliza.v1.PaginatedService.List */ -export const 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 30640003..1f0b0a63 100644 --- a/examples/react/basic/src/gen/eliza-SecondService_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-SecondService_connectquery.ts @@ -28,6 +28,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.SecondService"; @@ -86,7 +87,7 @@ const $queryService = createQueryService({ service: SecondService }); * * @generated from rpc connectrpc.eliza.v1.SecondService.Say */ -export const 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 9b61acf2..efb93877 100644 --- a/examples/react/basic/src/gen/eliza-Slouch_connectquery.ts +++ b/examples/react/basic/src/gen/eliza-Slouch_connectquery.ts @@ -21,6 +21,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.Slouch"; @@ -48,7 +49,7 @@ const $queryService = createQueryService({ service: Slouch }); /** * @generated from rpc connectrpc.eliza.v1.Slouch.Work */ -export const work = { +export const work: UnaryFunctionsWithHooks = { ...$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..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,6 +29,7 @@ import { MethodKind } from "@bufbuild/protobuf"; import { createQueryService, createUnaryHooks, + UnaryFunctionsWithHooks, } from "@connectrpc/connect-query"; export const typeName = "connectrpc.eliza.v1.ElizaService"; @@ -92,7 +93,7 @@ const $queryService = createQueryService({ service: ElizaService }); * * @generated from rpc connectrpc.eliza.v1.ElizaService.Say */ -export const 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 cab19f21..cee3d445 100644 --- a/packages/protoc-gen-connect-query/src/generateTs.ts +++ b/packages/protoc-gen-connect-query/src/generateTs.ts @@ -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,18 @@ const generateServiceFile = ); f.print(); - service.methods .filter((method) => method.methodKind === MethodKind.Unary) .forEach((method, index, filteredMethods) => { f.print(makeJsDoc(method)); + const methodTsType = [ + ": ", + f.import('UnaryFunctionsWithHooks', '@connectrpc/connect-query'), + `<${method.input.name}, ${method.output.name}>` + ] + 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)})`, `};`