Skip to content

Commit

Permalink
Fix/multiple methods with the same name (#49)
Browse files Browse the repository at this point in the history
* Fix edge case when defining multiple methods with the same name on different services

* Updated deps
  • Loading branch information
fenos authored Apr 29, 2022
1 parent 5f2723c commit acdc31f
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 107 deletions.
78 changes: 60 additions & 18 deletions example/generated/service.twirp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ function matchHaberdasherRoute<T extends TwirpContext = TwirpContext>(
) => {
ctx = { ...ctx, methodName: "MakeHat" };
await events.onMatch(ctx);
return handleMakeHatRequest(ctx, service, data, interceptors);
return handleHaberdasherMakeHatRequest(
ctx,
service,
data,
interceptors
);
};
case "FindHat":
return async (
Expand All @@ -187,7 +192,12 @@ function matchHaberdasherRoute<T extends TwirpContext = TwirpContext>(
) => {
ctx = { ...ctx, methodName: "FindHat" };
await events.onMatch(ctx);
return handleFindHatRequest(ctx, service, data, interceptors);
return handleHaberdasherFindHatRequest(
ctx,
service,
data,
interceptors
);
};
case "ListHat":
return async (
Expand All @@ -198,7 +208,12 @@ function matchHaberdasherRoute<T extends TwirpContext = TwirpContext>(
) => {
ctx = { ...ctx, methodName: "ListHat" };
await events.onMatch(ctx);
return handleListHatRequest(ctx, service, data, interceptors);
return handleHaberdasherListHatRequest(
ctx,
service,
data,
interceptors
);
};
default:
events.onNotFound();
Expand All @@ -207,57 +222,74 @@ function matchHaberdasherRoute<T extends TwirpContext = TwirpContext>(
}
}

function handleMakeHatRequest<T extends TwirpContext = TwirpContext>(
function handleHaberdasherMakeHatRequest<T extends TwirpContext = TwirpContext>(
ctx: T,
service: HaberdasherTwirp,
data: Buffer,
interceptors?: Interceptor<T, Size, Hat>[]
): Promise<string | Uint8Array> {
switch (ctx.contentType) {
case TwirpContentType.JSON:
return handleMakeHatJSON<T>(ctx, service, data, interceptors);
return handleHaberdasherMakeHatJSON<T>(ctx, service, data, interceptors);
case TwirpContentType.Protobuf:
return handleMakeHatProtobuf<T>(ctx, service, data, interceptors);
return handleHaberdasherMakeHatProtobuf<T>(
ctx,
service,
data,
interceptors
);
default:
const msg = "unexpected Content-Type";
throw new TwirpError(TwirpErrorCode.BadRoute, msg);
}
}

function handleFindHatRequest<T extends TwirpContext = TwirpContext>(
function handleHaberdasherFindHatRequest<T extends TwirpContext = TwirpContext>(
ctx: T,
service: HaberdasherTwirp,
data: Buffer,
interceptors?: Interceptor<T, FindHatRPC, FindHatRPC>[]
): Promise<string | Uint8Array> {
switch (ctx.contentType) {
case TwirpContentType.JSON:
return handleFindHatJSON<T>(ctx, service, data, interceptors);
return handleHaberdasherFindHatJSON<T>(ctx, service, data, interceptors);
case TwirpContentType.Protobuf:
return handleFindHatProtobuf<T>(ctx, service, data, interceptors);
return handleHaberdasherFindHatProtobuf<T>(
ctx,
service,
data,
interceptors
);
default:
const msg = "unexpected Content-Type";
throw new TwirpError(TwirpErrorCode.BadRoute, msg);
}
}

function handleListHatRequest<T extends TwirpContext = TwirpContext>(
function handleHaberdasherListHatRequest<T extends TwirpContext = TwirpContext>(
ctx: T,
service: HaberdasherTwirp,
data: Buffer,
interceptors?: Interceptor<T, ListHatRPC, ListHatRPC>[]
): Promise<string | Uint8Array> {
switch (ctx.contentType) {
case TwirpContentType.JSON:
return handleListHatJSON<T>(ctx, service, data, interceptors);
return handleHaberdasherListHatJSON<T>(ctx, service, data, interceptors);
case TwirpContentType.Protobuf:
return handleListHatProtobuf<T>(ctx, service, data, interceptors);
return handleHaberdasherListHatProtobuf<T>(
ctx,
service,
data,
interceptors
);
default:
const msg = "unexpected Content-Type";
throw new TwirpError(TwirpErrorCode.BadRoute, msg);
}
}
async function handleMakeHatJSON<T extends TwirpContext = TwirpContext>(
async function handleHaberdasherMakeHatJSON<
T extends TwirpContext = TwirpContext
>(
ctx: T,
service: HaberdasherTwirp,
data: Buffer,
Expand Down Expand Up @@ -297,7 +329,9 @@ async function handleMakeHatJSON<T extends TwirpContext = TwirpContext>(
);
}

async function handleFindHatJSON<T extends TwirpContext = TwirpContext>(
async function handleHaberdasherFindHatJSON<
T extends TwirpContext = TwirpContext
>(
ctx: T,
service: HaberdasherTwirp,
data: Buffer,
Expand Down Expand Up @@ -337,7 +371,9 @@ async function handleFindHatJSON<T extends TwirpContext = TwirpContext>(
);
}

async function handleListHatJSON<T extends TwirpContext = TwirpContext>(
async function handleHaberdasherListHatJSON<
T extends TwirpContext = TwirpContext
>(
ctx: T,
service: HaberdasherTwirp,
data: Buffer,
Expand Down Expand Up @@ -376,7 +412,9 @@ async function handleListHatJSON<T extends TwirpContext = TwirpContext>(
}) as string
);
}
async function handleMakeHatProtobuf<T extends TwirpContext = TwirpContext>(
async function handleHaberdasherMakeHatProtobuf<
T extends TwirpContext = TwirpContext
>(
ctx: T,
service: HaberdasherTwirp,
data: Buffer,
Expand Down Expand Up @@ -410,7 +448,9 @@ async function handleMakeHatProtobuf<T extends TwirpContext = TwirpContext>(
return Buffer.from(Hat.toBinary(response));
}

async function handleFindHatProtobuf<T extends TwirpContext = TwirpContext>(
async function handleHaberdasherFindHatProtobuf<
T extends TwirpContext = TwirpContext
>(
ctx: T,
service: HaberdasherTwirp,
data: Buffer,
Expand Down Expand Up @@ -444,7 +484,9 @@ async function handleFindHatProtobuf<T extends TwirpContext = TwirpContext>(
return Buffer.from(FindHatRPC.toBinary(response));
}

async function handleListHatProtobuf<T extends TwirpContext = TwirpContext>(
async function handleHaberdasherListHatProtobuf<
T extends TwirpContext = TwirpContext
>(
ctx: T,
service: HaberdasherTwirp,
data: Buffer,
Expand Down
52 changes: 38 additions & 14 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
Loading

0 comments on commit acdc31f

Please sign in to comment.