Skip to content

Commit acdc31f

Browse files
authored
Fix/multiple methods with the same name (#49)
* Fix edge case when defining multiple methods with the same name on different services * Updated deps
1 parent 5f2723c commit acdc31f

File tree

5 files changed

+246
-107
lines changed

5 files changed

+246
-107
lines changed

example/generated/service.twirp.ts

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ function matchHaberdasherRoute<T extends TwirpContext = TwirpContext>(
176176
) => {
177177
ctx = { ...ctx, methodName: "MakeHat" };
178178
await events.onMatch(ctx);
179-
return handleMakeHatRequest(ctx, service, data, interceptors);
179+
return handleHaberdasherMakeHatRequest(
180+
ctx,
181+
service,
182+
data,
183+
interceptors
184+
);
180185
};
181186
case "FindHat":
182187
return async (
@@ -187,7 +192,12 @@ function matchHaberdasherRoute<T extends TwirpContext = TwirpContext>(
187192
) => {
188193
ctx = { ...ctx, methodName: "FindHat" };
189194
await events.onMatch(ctx);
190-
return handleFindHatRequest(ctx, service, data, interceptors);
195+
return handleHaberdasherFindHatRequest(
196+
ctx,
197+
service,
198+
data,
199+
interceptors
200+
);
191201
};
192202
case "ListHat":
193203
return async (
@@ -198,7 +208,12 @@ function matchHaberdasherRoute<T extends TwirpContext = TwirpContext>(
198208
) => {
199209
ctx = { ...ctx, methodName: "ListHat" };
200210
await events.onMatch(ctx);
201-
return handleListHatRequest(ctx, service, data, interceptors);
211+
return handleHaberdasherListHatRequest(
212+
ctx,
213+
service,
214+
data,
215+
interceptors
216+
);
202217
};
203218
default:
204219
events.onNotFound();
@@ -207,57 +222,74 @@ function matchHaberdasherRoute<T extends TwirpContext = TwirpContext>(
207222
}
208223
}
209224

210-
function handleMakeHatRequest<T extends TwirpContext = TwirpContext>(
225+
function handleHaberdasherMakeHatRequest<T extends TwirpContext = TwirpContext>(
211226
ctx: T,
212227
service: HaberdasherTwirp,
213228
data: Buffer,
214229
interceptors?: Interceptor<T, Size, Hat>[]
215230
): Promise<string | Uint8Array> {
216231
switch (ctx.contentType) {
217232
case TwirpContentType.JSON:
218-
return handleMakeHatJSON<T>(ctx, service, data, interceptors);
233+
return handleHaberdasherMakeHatJSON<T>(ctx, service, data, interceptors);
219234
case TwirpContentType.Protobuf:
220-
return handleMakeHatProtobuf<T>(ctx, service, data, interceptors);
235+
return handleHaberdasherMakeHatProtobuf<T>(
236+
ctx,
237+
service,
238+
data,
239+
interceptors
240+
);
221241
default:
222242
const msg = "unexpected Content-Type";
223243
throw new TwirpError(TwirpErrorCode.BadRoute, msg);
224244
}
225245
}
226246

227-
function handleFindHatRequest<T extends TwirpContext = TwirpContext>(
247+
function handleHaberdasherFindHatRequest<T extends TwirpContext = TwirpContext>(
228248
ctx: T,
229249
service: HaberdasherTwirp,
230250
data: Buffer,
231251
interceptors?: Interceptor<T, FindHatRPC, FindHatRPC>[]
232252
): Promise<string | Uint8Array> {
233253
switch (ctx.contentType) {
234254
case TwirpContentType.JSON:
235-
return handleFindHatJSON<T>(ctx, service, data, interceptors);
255+
return handleHaberdasherFindHatJSON<T>(ctx, service, data, interceptors);
236256
case TwirpContentType.Protobuf:
237-
return handleFindHatProtobuf<T>(ctx, service, data, interceptors);
257+
return handleHaberdasherFindHatProtobuf<T>(
258+
ctx,
259+
service,
260+
data,
261+
interceptors
262+
);
238263
default:
239264
const msg = "unexpected Content-Type";
240265
throw new TwirpError(TwirpErrorCode.BadRoute, msg);
241266
}
242267
}
243268

244-
function handleListHatRequest<T extends TwirpContext = TwirpContext>(
269+
function handleHaberdasherListHatRequest<T extends TwirpContext = TwirpContext>(
245270
ctx: T,
246271
service: HaberdasherTwirp,
247272
data: Buffer,
248273
interceptors?: Interceptor<T, ListHatRPC, ListHatRPC>[]
249274
): Promise<string | Uint8Array> {
250275
switch (ctx.contentType) {
251276
case TwirpContentType.JSON:
252-
return handleListHatJSON<T>(ctx, service, data, interceptors);
277+
return handleHaberdasherListHatJSON<T>(ctx, service, data, interceptors);
253278
case TwirpContentType.Protobuf:
254-
return handleListHatProtobuf<T>(ctx, service, data, interceptors);
279+
return handleHaberdasherListHatProtobuf<T>(
280+
ctx,
281+
service,
282+
data,
283+
interceptors
284+
);
255285
default:
256286
const msg = "unexpected Content-Type";
257287
throw new TwirpError(TwirpErrorCode.BadRoute, msg);
258288
}
259289
}
260-
async function handleMakeHatJSON<T extends TwirpContext = TwirpContext>(
290+
async function handleHaberdasherMakeHatJSON<
291+
T extends TwirpContext = TwirpContext
292+
>(
261293
ctx: T,
262294
service: HaberdasherTwirp,
263295
data: Buffer,
@@ -297,7 +329,9 @@ async function handleMakeHatJSON<T extends TwirpContext = TwirpContext>(
297329
);
298330
}
299331

300-
async function handleFindHatJSON<T extends TwirpContext = TwirpContext>(
332+
async function handleHaberdasherFindHatJSON<
333+
T extends TwirpContext = TwirpContext
334+
>(
301335
ctx: T,
302336
service: HaberdasherTwirp,
303337
data: Buffer,
@@ -337,7 +371,9 @@ async function handleFindHatJSON<T extends TwirpContext = TwirpContext>(
337371
);
338372
}
339373

340-
async function handleListHatJSON<T extends TwirpContext = TwirpContext>(
374+
async function handleHaberdasherListHatJSON<
375+
T extends TwirpContext = TwirpContext
376+
>(
341377
ctx: T,
342378
service: HaberdasherTwirp,
343379
data: Buffer,
@@ -376,7 +412,9 @@ async function handleListHatJSON<T extends TwirpContext = TwirpContext>(
376412
}) as string
377413
);
378414
}
379-
async function handleMakeHatProtobuf<T extends TwirpContext = TwirpContext>(
415+
async function handleHaberdasherMakeHatProtobuf<
416+
T extends TwirpContext = TwirpContext
417+
>(
380418
ctx: T,
381419
service: HaberdasherTwirp,
382420
data: Buffer,
@@ -410,7 +448,9 @@ async function handleMakeHatProtobuf<T extends TwirpContext = TwirpContext>(
410448
return Buffer.from(Hat.toBinary(response));
411449
}
412450

413-
async function handleFindHatProtobuf<T extends TwirpContext = TwirpContext>(
451+
async function handleHaberdasherFindHatProtobuf<
452+
T extends TwirpContext = TwirpContext
453+
>(
414454
ctx: T,
415455
service: HaberdasherTwirp,
416456
data: Buffer,
@@ -444,7 +484,9 @@ async function handleFindHatProtobuf<T extends TwirpContext = TwirpContext>(
444484
return Buffer.from(FindHatRPC.toBinary(response));
445485
}
446486

447-
async function handleListHatProtobuf<T extends TwirpContext = TwirpContext>(
487+
async function handleHaberdasherListHatProtobuf<
488+
T extends TwirpContext = TwirpContext
489+
>(
448490
ctx: T,
449491
service: HaberdasherTwirp,
450492
data: Buffer,

package-lock.json

Lines changed: 38 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "twirp-ts",
3-
"version": "2.4.1",
3+
"version": "2.5.0",
44
"description": "Typescript implementation of the Twirp protocol",
55
"main": "build/twirp/index.js",
66
"bin": {
@@ -31,7 +31,7 @@
3131
"yaml": "^1.10.2"
3232
},
3333
"devDependencies": {
34-
"@protobuf-ts/plugin": "^2.0.7",
34+
"@protobuf-ts/plugin": "^2.5.0",
3535
"@types/dot-object": "^2.1.2",
3636
"@types/jest": "^26.0.23",
3737
"@types/node": "^15.12.2",
@@ -46,7 +46,7 @@
4646
"typescript": "^4.3.2"
4747
},
4848
"peerDependencies": {
49-
"@protobuf-ts/plugin": "^2.0.0-alpha.27",
49+
"@protobuf-ts/plugin": "^2.5.0",
5050
"ts-proto": "^1.81.3"
5151
},
5252
"peerDependenciesMeta": {

0 commit comments

Comments
 (0)