diff --git a/example/generated/service.twirp.ts b/example/generated/service.twirp.ts index 6dd683f..458b936 100644 --- a/example/generated/service.twirp.ts +++ b/example/generated/service.twirp.ts @@ -176,7 +176,12 @@ function matchHaberdasherRoute( ) => { ctx = { ...ctx, methodName: "MakeHat" }; await events.onMatch(ctx); - return handleMakeHatRequest(ctx, service, data, interceptors); + return handleHaberdasherMakeHatRequest( + ctx, + service, + data, + interceptors + ); }; case "FindHat": return async ( @@ -187,7 +192,12 @@ function matchHaberdasherRoute( ) => { ctx = { ...ctx, methodName: "FindHat" }; await events.onMatch(ctx); - return handleFindHatRequest(ctx, service, data, interceptors); + return handleHaberdasherFindHatRequest( + ctx, + service, + data, + interceptors + ); }; case "ListHat": return async ( @@ -198,7 +208,12 @@ function matchHaberdasherRoute( ) => { ctx = { ...ctx, methodName: "ListHat" }; await events.onMatch(ctx); - return handleListHatRequest(ctx, service, data, interceptors); + return handleHaberdasherListHatRequest( + ctx, + service, + data, + interceptors + ); }; default: events.onNotFound(); @@ -207,7 +222,7 @@ function matchHaberdasherRoute( } } -function handleMakeHatRequest( +function handleHaberdasherMakeHatRequest( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -215,16 +230,21 @@ function handleMakeHatRequest( ): Promise { switch (ctx.contentType) { case TwirpContentType.JSON: - return handleMakeHatJSON(ctx, service, data, interceptors); + return handleHaberdasherMakeHatJSON(ctx, service, data, interceptors); case TwirpContentType.Protobuf: - return handleMakeHatProtobuf(ctx, service, data, interceptors); + return handleHaberdasherMakeHatProtobuf( + ctx, + service, + data, + interceptors + ); default: const msg = "unexpected Content-Type"; throw new TwirpError(TwirpErrorCode.BadRoute, msg); } } -function handleFindHatRequest( +function handleHaberdasherFindHatRequest( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -232,16 +252,21 @@ function handleFindHatRequest( ): Promise { switch (ctx.contentType) { case TwirpContentType.JSON: - return handleFindHatJSON(ctx, service, data, interceptors); + return handleHaberdasherFindHatJSON(ctx, service, data, interceptors); case TwirpContentType.Protobuf: - return handleFindHatProtobuf(ctx, service, data, interceptors); + return handleHaberdasherFindHatProtobuf( + ctx, + service, + data, + interceptors + ); default: const msg = "unexpected Content-Type"; throw new TwirpError(TwirpErrorCode.BadRoute, msg); } } -function handleListHatRequest( +function handleHaberdasherListHatRequest( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -249,15 +274,22 @@ function handleListHatRequest( ): Promise { switch (ctx.contentType) { case TwirpContentType.JSON: - return handleListHatJSON(ctx, service, data, interceptors); + return handleHaberdasherListHatJSON(ctx, service, data, interceptors); case TwirpContentType.Protobuf: - return handleListHatProtobuf(ctx, service, data, interceptors); + return handleHaberdasherListHatProtobuf( + ctx, + service, + data, + interceptors + ); default: const msg = "unexpected Content-Type"; throw new TwirpError(TwirpErrorCode.BadRoute, msg); } } -async function handleMakeHatJSON( +async function handleHaberdasherMakeHatJSON< + T extends TwirpContext = TwirpContext +>( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -297,7 +329,9 @@ async function handleMakeHatJSON( ); } -async function handleFindHatJSON( +async function handleHaberdasherFindHatJSON< + T extends TwirpContext = TwirpContext +>( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -337,7 +371,9 @@ async function handleFindHatJSON( ); } -async function handleListHatJSON( +async function handleHaberdasherListHatJSON< + T extends TwirpContext = TwirpContext +>( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -376,7 +412,9 @@ async function handleListHatJSON( }) as string ); } -async function handleMakeHatProtobuf( +async function handleHaberdasherMakeHatProtobuf< + T extends TwirpContext = TwirpContext +>( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -410,7 +448,9 @@ async function handleMakeHatProtobuf( return Buffer.from(Hat.toBinary(response)); } -async function handleFindHatProtobuf( +async function handleHaberdasherFindHatProtobuf< + T extends TwirpContext = TwirpContext +>( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -444,7 +484,9 @@ async function handleFindHatProtobuf( return Buffer.from(FindHatRPC.toBinary(response)); } -async function handleListHatProtobuf( +async function handleHaberdasherListHatProtobuf< + T extends TwirpContext = TwirpContext +>( ctx: T, service: HaberdasherTwirp, data: Buffer, diff --git a/package-lock.json b/package-lock.json index 5f50e48..c1e267c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -845,18 +845,34 @@ } }, "@protobuf-ts/plugin": { - "version": "2.0.7", - "resolved": "https://registry.npmjs.org/@protobuf-ts/plugin/-/plugin-2.0.7.tgz", - "integrity": "sha512-gQQryonFATSDB9GL0+qxFKljEq9db4gVHP+XP7rNLbSMXRZ4/NOqqCcZqDsQYfhjEehkjP51//oD7Yk3YcLzlA==", + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/@protobuf-ts/plugin/-/plugin-2.5.0.tgz", + "integrity": "sha512-+lTtBPZad4gGE9eh9lMn+Fq79u4JB8YY/7uEmJSl8pk+4vMp6W02TxWYBMW4gBh5hzcZHGdEBx90OQxt1MDeJQ==", "dev": true, "requires": { - "@protobuf-ts/plugin-framework": "^2.0.7", - "@protobuf-ts/protoc": "^2.0.7", - "@protobuf-ts/runtime": "^2.0.7", - "@protobuf-ts/runtime-rpc": "^2.0.7", + "@protobuf-ts/plugin-framework": "^2.5.0", + "@protobuf-ts/protoc": "^2.5.0", + "@protobuf-ts/runtime": "^2.5.0", + "@protobuf-ts/runtime-rpc": "^2.5.0", "typescript": "^3.9" }, "dependencies": { + "@protobuf-ts/plugin-framework": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/@protobuf-ts/plugin-framework/-/plugin-framework-2.5.0.tgz", + "integrity": "sha512-bOf6oP4P0rjNp7bS8NSF/4O9a2WqHb3SSl4aNYzsNvtEddmC5laMfaUGr+6IePzgCNNl19w4wG4m981NSFZDqw==", + "dev": true, + "requires": { + "@protobuf-ts/runtime": "^2.5.0", + "typescript": "^3.9" + } + }, + "@protobuf-ts/runtime": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/@protobuf-ts/runtime/-/runtime-2.5.0.tgz", + "integrity": "sha512-ClfuZQ3iPVABJ0CXLGI044eox1PtmrIbbXZaHYczdFJjaxxn8U7vnwb1dPBM9JZ2I6H1VLZu8A9Z5nRv7zydsg==", + "dev": true + }, "typescript": { "version": "3.9.10", "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.10.tgz", @@ -882,9 +898,9 @@ } }, "@protobuf-ts/protoc": { - "version": "2.0.7", - "resolved": "https://registry.npmjs.org/@protobuf-ts/protoc/-/protoc-2.0.7.tgz", - "integrity": "sha512-bZSAAPv4GaPRomKXgcy/rkJzquzltPt9FAziwPvi1cl5Q0bO1kKoLIM/yIt0pcYtQhDLvz+JZAXt9g+AVEPxbQ==", + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/@protobuf-ts/protoc/-/protoc-2.5.0.tgz", + "integrity": "sha512-zrvKEOMKRFNb51Rrhghq4zWxoVCDEYmJJSUmbeTWoYl67u2DyWQMbFQy3HkwjF4kmhJUsG6HQdQHdcPBk4Yo6A==", "dev": true }, "@protobuf-ts/runtime": { @@ -893,12 +909,20 @@ "integrity": "sha512-jT8FYEX7NkAzxZXVjshIhtCV/ReuZm/3sCH0GWnaa8woy9VG+He0N+dpj2svaJbkdUThSxJE3zwmJcH0/3vEsw==" }, "@protobuf-ts/runtime-rpc": { - "version": "2.0.7", - "resolved": "https://registry.npmjs.org/@protobuf-ts/runtime-rpc/-/runtime-rpc-2.0.7.tgz", - "integrity": "sha512-AHBk8kKeZI5QV8EYxxpozNbu4goH3Nh3F43Nlw9ttl4KPilCJlNlGZBnxEG/Je7+/T703DfGNbXaVCb7QZ6TXw==", + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/@protobuf-ts/runtime-rpc/-/runtime-rpc-2.5.0.tgz", + "integrity": "sha512-zO5W1/66Ok68muvmjG0+ucTWTfzhCUurnqyspqcWGF9DDgGKSmrtR8V1MRV051UoTonOxm7NZZvM6VTbBv9DcA==", "dev": true, "requires": { - "@protobuf-ts/runtime": "^2.0.7" + "@protobuf-ts/runtime": "^2.5.0" + }, + "dependencies": { + "@protobuf-ts/runtime": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/@protobuf-ts/runtime/-/runtime-2.5.0.tgz", + "integrity": "sha512-ClfuZQ3iPVABJ0CXLGI044eox1PtmrIbbXZaHYczdFJjaxxn8U7vnwb1dPBM9JZ2I6H1VLZu8A9Z5nRv7zydsg==", + "dev": true + } } }, "@protobufjs/aspromise": { diff --git a/package.json b/package.json index 186bccb..7ca37d4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "twirp-ts", - "version": "2.4.1", + "version": "2.5.0", "description": "Typescript implementation of the Twirp protocol", "main": "build/twirp/index.js", "bin": { @@ -31,7 +31,7 @@ "yaml": "^1.10.2" }, "devDependencies": { - "@protobuf-ts/plugin": "^2.0.7", + "@protobuf-ts/plugin": "^2.5.0", "@types/dot-object": "^2.1.2", "@types/jest": "^26.0.23", "@types/node": "^15.12.2", @@ -46,7 +46,7 @@ "typescript": "^4.3.2" }, "peerDependencies": { - "@protobuf-ts/plugin": "^2.0.0-alpha.27", + "@protobuf-ts/plugin": "^2.5.0", "ts-proto": "^1.81.3" }, "peerDependenciesMeta": { diff --git a/src/protoc-gen-twirp-ts/gen/twirp.ts b/src/protoc-gen-twirp-ts/gen/twirp.ts index 7127f1b..5d6d650 100644 --- a/src/protoc-gen-twirp-ts/gen/twirp.ts +++ b/src/protoc-gen-twirp-ts/gen/twirp.ts @@ -81,7 +81,7 @@ function genClient(ctx: any, file: FileDescriptorProto, service: ServiceDescript function genTwirpClientInterface(ctx: any, file: FileDescriptorProto, service: ServiceDescriptorProto) { const methods = service.method.map((method) => { return code` - ${formatMethodName(ctx, method.name)}(request: ${relativeMessageName(ctx, file, method.inputType)}): Promise<${relativeMessageName(ctx, file, method.outputType)}> + ${formatMethodName(ctx, method.name!)}(request: ${relativeMessageName(ctx, file, method.inputType)}): Promise<${relativeMessageName(ctx, file, method.outputType)}> ` }); @@ -101,11 +101,11 @@ function genTwirpClientInterface(ctx: any, file: FileDescriptorProto, service: S function genTwripClientJSONImpl(ctx: any, file: FileDescriptorProto, service: ServiceDescriptorProto) { const methods = service.method.map((method) => { return code` - ${formatMethodName(ctx, method.name)}(request: ${relativeMessageName(ctx, file, method.inputType)}): Promise<${relativeMessageName(ctx, file, method.outputType)}> { + ${formatMethodName(ctx, method.name!)}(request: ${relativeMessageName(ctx, file, method.inputType)}): Promise<${relativeMessageName(ctx, file, method.outputType)}> { const data = ${relativeMessageName(ctx, file, method.inputType)}.${encodeJSON(ctx,"request")}; const promise = this.rpc.request( "${file.package ? file.package + "." : ""}${service.name}", - "${formatMethodName(ctx, method.name)}", + "${formatMethodName(ctx, method.name!)}", "application/json", data as object, ); @@ -116,7 +116,7 @@ function genTwripClientJSONImpl(ctx: any, file: FileDescriptorProto, service: Se const bindings = service.method.map((method) => { return code` - this.${formatMethodName(ctx, method.name)}.bind(this); + this.${formatMethodName(ctx, method.name!)}.bind(this); ` }) @@ -141,11 +141,11 @@ function genTwripClientJSONImpl(ctx: any, file: FileDescriptorProto, service: Se function genTwripClientProtobufImpl(ctx: any, file: FileDescriptorProto, service: ServiceDescriptorProto) { const methods = service.method.map((method) => { return code` - ${formatMethodName(ctx, method.name)}(request: ${relativeMessageName(ctx, file, method.inputType)}): Promise<${relativeMessageName(ctx, file, method.outputType)}> { + ${formatMethodName(ctx, method.name!)}(request: ${relativeMessageName(ctx, file, method.inputType)}): Promise<${relativeMessageName(ctx, file, method.outputType)}> { const data = ${relativeMessageName(ctx, file, method.inputType)}.${encodeProtobuf(ctx, "request")}; const promise = this.rpc.request( "${file.package ? file.package + "." : ""}${service.name}", - "${formatMethodName(ctx, method.name)}", + "${formatMethodName(ctx, method.name!)}", "application/protobuf", data, ); @@ -156,7 +156,7 @@ function genTwripClientProtobufImpl(ctx: any, file: FileDescriptorProto, service const bindings = service.method.map((method) => { return code` - this.${formatMethodName(ctx, method.name)}.bind(this); + this.${formatMethodName(ctx, method.name!)}.bind(this); ` }) @@ -183,16 +183,16 @@ function genTwirpService(ctx: any, file: FileDescriptorProto, service: ServiceDe const serverMethods = service.method.map((method) => { return code` - ${formatMethodName(ctx, method.name)}(ctx: T, request: ${relativeMessageName(ctx, file, method.inputType)}): Promise<${relativeMessageName(ctx, file, method.outputType)}> + ${formatMethodName(ctx, method.name!)}(ctx: T, request: ${relativeMessageName(ctx, file, method.inputType)}): Promise<${relativeMessageName(ctx, file, method.outputType)}> ` }) const methodEnum = service.method.map((method) => { - return code`${formatMethodName(ctx, method.name)} = "${formatMethodName(ctx, method.name)}",` + return code`${formatMethodName(ctx, method.name!)} = "${formatMethodName(ctx, method.name!)}",` }) const methodList = service.method.map((method) => { - return code`${importService}Method.${formatMethodName(ctx, method.name)}` + return code`${importService}Method.${formatMethodName(ctx, method.name!)}` }) return code` @@ -249,11 +249,11 @@ function genServer(ctx: any, file: FileDescriptorProto, service: ServiceDescript */ function genRouteHandler(ctx: any, file: FileDescriptorProto, service: ServiceDescriptorProto) { const cases = service.method.map(method => code` - case "${formatMethodName(ctx, method.name)}": + case "${formatMethodName(ctx, method.name!)}": return async (ctx: T, service: ${service.name}Twirp ,data: Buffer, interceptors?: ${Interceptor}[]) => { - ctx = {...ctx, methodName: "${formatMethodName(ctx, method.name)}" } + ctx = {...ctx, methodName: "${formatMethodName(ctx, method.name!)}" } await events.onMatch(ctx); - return handle${formatMethodName(ctx, method.name)}Request(ctx, service, data, interceptors) + return handle${formatMethodName(ctx, method.name!, service.name)}Request(ctx, service, data, interceptors) } `) @@ -279,12 +279,12 @@ function genRouteHandler(ctx: any, file: FileDescriptorProto, service: ServiceDe function genHandleRequestMethod(ctx: any, file: FileDescriptorProto, service: ServiceDescriptorProto) { return service.method.map(method => { return code` - function handle${formatMethodName(ctx, method.name)}Request(ctx: T, service: ${service.name}Twirp ,data: Buffer, interceptors?: ${Interceptor}[]): Promise { + function handle${formatMethodName(ctx, method.name!, service.name)}Request(ctx: T, service: ${service.name}Twirp ,data: Buffer, interceptors?: ${Interceptor}[]): Promise { switch (ctx.contentType) { case ${TwirpContentType}.JSON: - return handle${formatMethodName(ctx, method.name)}JSON(ctx, service, data, interceptors); + return handle${formatMethodName(ctx, method.name!, service.name)}JSON(ctx, service, data, interceptors); case ${TwirpContentType}.Protobuf: - return handle${formatMethodName(ctx, method.name)}Protobuf(ctx, service, data, interceptors); + return handle${formatMethodName(ctx, method.name!, service.name)}Protobuf(ctx, service, data, interceptors); default: const msg = "unexpected Content-Type"; throw new ${TwirpError}(${TwirpErrorCode}.BadRoute, msg); @@ -304,7 +304,7 @@ function genHandleJSONRequest(ctx: any, file: FileDescriptorProto, service: Serv return service.method.map(method => { return code` - async function handle${formatMethodName(ctx, method.name)}JSON(ctx: T, service: ${service.name}Twirp, data: Buffer, interceptors?: ${Interceptor}[]) { + async function handle${formatMethodName(ctx, method.name!, service.name)}JSON(ctx: T, service: ${service.name}Twirp, data: Buffer, interceptors?: ${Interceptor}[]) { let request: ${relativeMessageName(ctx, file, method.inputType)} let response: ${relativeMessageName(ctx, file, method.outputType)} @@ -321,10 +321,10 @@ function genHandleJSONRequest(ctx: any, file: FileDescriptorProto, service: Serv if (interceptors && interceptors.length > 0) { const interceptor = ${chainInterceptors}(...interceptors) as Interceptor response = await interceptor(ctx, request!, (ctx, inputReq) => { - return service.${formatMethodName(ctx, method.name)}(ctx, inputReq); + return service.${formatMethodName(ctx, method.name!)}(ctx, inputReq); }); } else { - response = await service.${formatMethodName(ctx, method.name)}(ctx, request!) + response = await service.${formatMethodName(ctx, method.name!)}(ctx, request!) } return JSON.stringify(${relativeMessageName(ctx, file, method.outputType)}.${encodeJSON(ctx,"response")} as string); @@ -343,7 +343,7 @@ function genHandleProtobufRequest(ctx: any, file: FileDescriptorProto, service: return service.method.map(method => { return code` - async function handle${formatMethodName(ctx, method.name)}Protobuf(ctx: T, service: ${service.name}Twirp, data: Buffer, interceptors?: ${Interceptor}[]) { + async function handle${formatMethodName(ctx, method.name!, service.name)}Protobuf(ctx: T, service: ${service.name}Twirp, data: Buffer, interceptors?: ${Interceptor}[]) { let request: ${relativeMessageName(ctx, file, method.inputType)} let response: ${relativeMessageName(ctx, file, method.outputType)} @@ -359,10 +359,10 @@ function genHandleProtobufRequest(ctx: any, file: FileDescriptorProto, service: if (interceptors && interceptors.length > 0) { const interceptor = ${chainInterceptors}(...interceptors) as Interceptor response = await interceptor(ctx, request!, (ctx, inputReq) => { - return service.${formatMethodName(ctx, method.name)}(ctx, inputReq); + return service.${formatMethodName(ctx, method.name!)}(ctx, inputReq); }); } else { - response = await service.${formatMethodName(ctx, method.name)}(ctx, request!) + response = await service.${formatMethodName(ctx, method.name!)}(ctx, request!) } return Buffer.from(${relativeMessageName(ctx, file, method.outputType)}.${encodeProtobuf(ctx, "response")}); @@ -467,8 +467,9 @@ function createRelativeImportPath(currentPath: string, pathToImportFrom: string) return fromPath; } -function formatMethodName(ctx: any, methodName?: string) { +function formatMethodName(ctx: any, methodName: string, serviceName?: string) { if(methodName === undefined) return undefined; - return ctx.camelCase ? camelCase(methodName) : methodName; + serviceName = serviceName || "" + return ctx.camelCase ? camelCase(serviceName) + camelCase(methodName) : serviceName + methodName; } \ No newline at end of file diff --git a/src/twirp/__mocks__/service.twirp.ts b/src/twirp/__mocks__/service.twirp.ts index 7bb4412..2587b4a 100644 --- a/src/twirp/__mocks__/service.twirp.ts +++ b/src/twirp/__mocks__/service.twirp.ts @@ -38,7 +38,10 @@ export class HaberdasherClientJSON implements HaberdasherClient { this.ListHat.bind(this); } MakeHat(request: Size): Promise { - const data = Size.toJson(request, { useProtoFieldName: true }); + const data = Size.toJson(request, { + useProtoFieldName: true, + emitDefaultValues: false, + }); const promise = this.rpc.request( "twirp.example.haberdasher.Haberdasher", "MakeHat", @@ -51,7 +54,10 @@ export class HaberdasherClientJSON implements HaberdasherClient { } FindHat(request: FindHatRPC): Promise { - const data = FindHatRPC.toJson(request, { useProtoFieldName: true }); + const data = FindHatRPC.toJson(request, { + useProtoFieldName: true, + emitDefaultValues: false, + }); const promise = this.rpc.request( "twirp.example.haberdasher.Haberdasher", "FindHat", @@ -64,7 +70,10 @@ export class HaberdasherClientJSON implements HaberdasherClient { } ListHat(request: ListHatRPC): Promise { - const data = ListHatRPC.toJson(request, { useProtoFieldName: true }); + const data = ListHatRPC.toJson(request, { + useProtoFieldName: true, + emitDefaultValues: false, + }); const promise = this.rpc.request( "twirp.example.haberdasher.Haberdasher", "ListHat", @@ -167,7 +176,12 @@ function matchHaberdasherRoute( ) => { ctx = { ...ctx, methodName: "MakeHat" }; await events.onMatch(ctx); - return handleMakeHatRequest(ctx, service, data, interceptors); + return handleHaberdasherMakeHatRequest( + ctx, + service, + data, + interceptors + ); }; case "FindHat": return async ( @@ -178,7 +192,12 @@ function matchHaberdasherRoute( ) => { ctx = { ...ctx, methodName: "FindHat" }; await events.onMatch(ctx); - return handleFindHatRequest(ctx, service, data, interceptors); + return handleHaberdasherFindHatRequest( + ctx, + service, + data, + interceptors + ); }; case "ListHat": return async ( @@ -189,7 +208,12 @@ function matchHaberdasherRoute( ) => { ctx = { ...ctx, methodName: "ListHat" }; await events.onMatch(ctx); - return handleListHatRequest(ctx, service, data, interceptors); + return handleHaberdasherListHatRequest( + ctx, + service, + data, + interceptors + ); }; default: events.onNotFound(); @@ -198,7 +222,7 @@ function matchHaberdasherRoute( } } -function handleMakeHatRequest( +function handleHaberdasherMakeHatRequest( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -206,16 +230,21 @@ function handleMakeHatRequest( ): Promise { switch (ctx.contentType) { case TwirpContentType.JSON: - return handleMakeHatJSON(ctx, service, data, interceptors); + return handleHaberdasherMakeHatJSON(ctx, service, data, interceptors); case TwirpContentType.Protobuf: - return handleMakeHatProtobuf(ctx, service, data, interceptors); + return handleHaberdasherMakeHatProtobuf( + ctx, + service, + data, + interceptors + ); default: const msg = "unexpected Content-Type"; throw new TwirpError(TwirpErrorCode.BadRoute, msg); } } -function handleFindHatRequest( +function handleHaberdasherFindHatRequest( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -223,16 +252,21 @@ function handleFindHatRequest( ): Promise { switch (ctx.contentType) { case TwirpContentType.JSON: - return handleFindHatJSON(ctx, service, data, interceptors); + return handleHaberdasherFindHatJSON(ctx, service, data, interceptors); case TwirpContentType.Protobuf: - return handleFindHatProtobuf(ctx, service, data, interceptors); + return handleHaberdasherFindHatProtobuf( + ctx, + service, + data, + interceptors + ); default: const msg = "unexpected Content-Type"; throw new TwirpError(TwirpErrorCode.BadRoute, msg); } } -function handleListHatRequest( +function handleHaberdasherListHatRequest( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -240,15 +274,22 @@ function handleListHatRequest( ): Promise { switch (ctx.contentType) { case TwirpContentType.JSON: - return handleListHatJSON(ctx, service, data, interceptors); + return handleHaberdasherListHatJSON(ctx, service, data, interceptors); case TwirpContentType.Protobuf: - return handleListHatProtobuf(ctx, service, data, interceptors); + return handleHaberdasherListHatProtobuf( + ctx, + service, + data, + interceptors + ); default: const msg = "unexpected Content-Type"; throw new TwirpError(TwirpErrorCode.BadRoute, msg); } } -async function handleMakeHatJSON( +async function handleHaberdasherMakeHatJSON< + T extends TwirpContext = TwirpContext +>( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -261,8 +302,10 @@ async function handleMakeHatJSON( const body = JSON.parse(data.toString() || "{}"); request = Size.fromJson(body, { ignoreUnknownFields: true }); } catch (e) { - const msg = "the json request could not be decoded"; - throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true); + if (e instanceof Error) { + const msg = "the json request could not be decoded"; + throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true); + } } if (interceptors && interceptors.length > 0) { @@ -271,19 +314,24 @@ async function handleMakeHatJSON( Size, Hat >; - response = await interceptor(ctx, request, (ctx, inputReq) => { + response = await interceptor(ctx, request!, (ctx, inputReq) => { return service.MakeHat(ctx, inputReq); }); } else { - response = await service.MakeHat(ctx, request); + response = await service.MakeHat(ctx, request!); } return JSON.stringify( - Hat.toJson(response, { useProtoFieldName: true }) as string + Hat.toJson(response, { + useProtoFieldName: true, + emitDefaultValues: false, + }) as string ); } -async function handleFindHatJSON( +async function handleHaberdasherFindHatJSON< + T extends TwirpContext = TwirpContext +>( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -296,8 +344,10 @@ async function handleFindHatJSON( const body = JSON.parse(data.toString() || "{}"); request = FindHatRPC.fromJson(body, { ignoreUnknownFields: true }); } catch (e) { - const msg = "the json request could not be decoded"; - throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true); + if (e instanceof Error) { + const msg = "the json request could not be decoded"; + throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true); + } } if (interceptors && interceptors.length > 0) { @@ -306,19 +356,24 @@ async function handleFindHatJSON( FindHatRPC, FindHatRPC >; - response = await interceptor(ctx, request, (ctx, inputReq) => { + response = await interceptor(ctx, request!, (ctx, inputReq) => { return service.FindHat(ctx, inputReq); }); } else { - response = await service.FindHat(ctx, request); + response = await service.FindHat(ctx, request!); } return JSON.stringify( - FindHatRPC.toJson(response, { useProtoFieldName: true }) as string + FindHatRPC.toJson(response, { + useProtoFieldName: true, + emitDefaultValues: false, + }) as string ); } -async function handleListHatJSON( +async function handleHaberdasherListHatJSON< + T extends TwirpContext = TwirpContext +>( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -331,8 +386,10 @@ async function handleListHatJSON( const body = JSON.parse(data.toString() || "{}"); request = ListHatRPC.fromJson(body, { ignoreUnknownFields: true }); } catch (e) { - const msg = "the json request could not be decoded"; - throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true); + if (e instanceof Error) { + const msg = "the json request could not be decoded"; + throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true); + } } if (interceptors && interceptors.length > 0) { @@ -341,18 +398,23 @@ async function handleListHatJSON( ListHatRPC, ListHatRPC >; - response = await interceptor(ctx, request, (ctx, inputReq) => { + response = await interceptor(ctx, request!, (ctx, inputReq) => { return service.ListHat(ctx, inputReq); }); } else { - response = await service.ListHat(ctx, request); + response = await service.ListHat(ctx, request!); } return JSON.stringify( - ListHatRPC.toJson(response, { useProtoFieldName: true }) as string + ListHatRPC.toJson(response, { + useProtoFieldName: true, + emitDefaultValues: false, + }) as string ); } -async function handleMakeHatProtobuf( +async function handleHaberdasherMakeHatProtobuf< + T extends TwirpContext = TwirpContext +>( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -364,8 +426,10 @@ async function handleMakeHatProtobuf( try { request = Size.fromBinary(data); } catch (e) { - const msg = "the protobuf request could not be decoded"; - throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true); + if (e instanceof Error) { + const msg = "the protobuf request could not be decoded"; + throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true); + } } if (interceptors && interceptors.length > 0) { @@ -374,17 +438,19 @@ async function handleMakeHatProtobuf( Size, Hat >; - response = await interceptor(ctx, request, (ctx, inputReq) => { + response = await interceptor(ctx, request!, (ctx, inputReq) => { return service.MakeHat(ctx, inputReq); }); } else { - response = await service.MakeHat(ctx, request); + response = await service.MakeHat(ctx, request!); } return Buffer.from(Hat.toBinary(response)); } -async function handleFindHatProtobuf( +async function handleHaberdasherFindHatProtobuf< + T extends TwirpContext = TwirpContext +>( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -396,8 +462,10 @@ async function handleFindHatProtobuf( try { request = FindHatRPC.fromBinary(data); } catch (e) { - const msg = "the protobuf request could not be decoded"; - throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true); + if (e instanceof Error) { + const msg = "the protobuf request could not be decoded"; + throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true); + } } if (interceptors && interceptors.length > 0) { @@ -406,17 +474,19 @@ async function handleFindHatProtobuf( FindHatRPC, FindHatRPC >; - response = await interceptor(ctx, request, (ctx, inputReq) => { + response = await interceptor(ctx, request!, (ctx, inputReq) => { return service.FindHat(ctx, inputReq); }); } else { - response = await service.FindHat(ctx, request); + response = await service.FindHat(ctx, request!); } return Buffer.from(FindHatRPC.toBinary(response)); } -async function handleListHatProtobuf( +async function handleHaberdasherListHatProtobuf< + T extends TwirpContext = TwirpContext +>( ctx: T, service: HaberdasherTwirp, data: Buffer, @@ -428,8 +498,10 @@ async function handleListHatProtobuf( try { request = ListHatRPC.fromBinary(data); } catch (e) { - const msg = "the protobuf request could not be decoded"; - throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true); + if (e instanceof Error) { + const msg = "the protobuf request could not be decoded"; + throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true); + } } if (interceptors && interceptors.length > 0) { @@ -438,11 +510,11 @@ async function handleListHatProtobuf( ListHatRPC, ListHatRPC >; - response = await interceptor(ctx, request, (ctx, inputReq) => { + response = await interceptor(ctx, request!, (ctx, inputReq) => { return service.ListHat(ctx, inputReq); }); } else { - response = await service.ListHat(ctx, request); + response = await service.ListHat(ctx, request!); } return Buffer.from(ListHatRPC.toBinary(response));