Skip to content

Commit 154ae7e

Browse files
tatethurstonTate
and
Tate
authored
remove naming suffixes (#125)
Removes 'Service' suffix from generated service types. Removes 'Handler' suffix from 'create<Service>' helper. This is a breaking change. This enables better out of the box integration with `buf` which expects service names to end with the Serice suffix. When following that recommendation, TwirpScript would generate 'FooServiceService'. Co-authored-by: Tate <[email protected]>
1 parent 75c2605 commit 154ae7e

File tree

30 files changed

+101
-133
lines changed

30 files changed

+101
-133
lines changed

README.md

+15-21
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,9 @@ If you have an existing Twirp server you're connecting to and only need a client
182182
`src/server/haberdasher/index.ts`
183183

184184
```ts
185-
import {
186-
HaberdasherService,
187-
createHaberdasherHandler,
188-
} from "../../protos/haberdasher.pb";
185+
import { Haberdasher, createHaberdasher } from "../../protos/haberdasher.pb";
189186

190-
const Haberdasher: HaberdasherService = {
187+
const haberdasher: Haberdasher = {
191188
MakeHat: (size) => {
192189
return {
193190
inches: size.inches,
@@ -197,7 +194,7 @@ const Haberdasher: HaberdasherService = {
197194
},
198195
};
199196

200-
export const HaberdasherHandler = createHaberdasherHandler(Haberdasher);
197+
export const haberdasherHandler = createHaberdasher(haberdasher);
201198
```
202199

203200
#### 5. Connect your service to your application server
@@ -207,11 +204,11 @@ export const HaberdasherHandler = createHaberdasherHandler(Haberdasher);
207204
```ts
208205
import { createServer } from "http";
209206
import { createTwirpServer } from "twirpscript";
210-
import { HaberdasherHandler } from "./haberdasher";
207+
import { haberdasherHandler } from "./haberdasher";
211208

212209
const PORT = 8080;
213210

214-
const app = createTwirpServer([HaberdasherHandler]);
211+
const app = createTwirpServer([haberdasherHandler]);
215212

216213
createServer(app).listen(PORT, () =>
217214
console.log(`Server listening on port ${PORT}`)
@@ -309,12 +306,12 @@ Servers can be configured by passing a configuration object to `createTwirpServe
309306
```ts
310307
import { createServer } from "http";
311308
import { createTwirpServer } from "twirpscript";
312-
import { HaberdasherHandler } from "./haberdasher";
309+
import { haberdasherHandler } from "./haberdasher";
313310

314311
const PORT = 8080;
315312

316313
// This removes the "/twirp" prefix in the RPC path
317-
const app = createTwirpServer([HaberdasherHandler], { prefix: "" });
314+
const app = createTwirpServer([haberdasherHandler], { prefix: "" });
318315

319316
createServer(app).listen(PORT, () =>
320317
console.log(`Server listening on port ${PORT}`)
@@ -347,13 +344,10 @@ Custom fields can be added to the context object via [middleware](#middleware--i
347344
If you setup middleware similiar to the [authentication middleware example](https://github.com/tatethurston/TwirpScript#example-3), you could read the `currentUser` `username` property in your service handler. See the [authentication example](https://github.com/tatethurston/twirpscript/tree/main/examples/authentication) for a full application.
348345

349346
```ts
350-
import {
351-
HaberdasherService,
352-
createHaberdasherHandler,
353-
} from "../../protos/haberdasher.pb";
347+
import { Haberdasher, createHaberdasher } from "../../protos/haberdasher.pb";
354348
import { Context } from "../some-path-to-your-definition";
355349

356-
const Haberdasher: HaberdasherService<Context> = {
350+
const haberdasher: Haberdasher<Context> = {
357351
MakeHat: (size, ctx) => {
358352
return {
359353
inches: size.inches,
@@ -363,7 +357,7 @@ const Haberdasher: HaberdasherService<Context> = {
363357
},
364358
};
365359

366-
export const HaberdasherHandler = createHaberdasherHandler(HaberdasherService);
360+
export const haberdasherHandler = createHaberdasher(haberdasher);
367361
```
368362

369363
### Middleware / Interceptors
@@ -425,18 +419,18 @@ The middleware handler will receive `req`, `context` and `next` parameters. `req
425419
```ts
426420
import { createServer } from "http";
427421
import { createTwirpServer, TwirpError } from "twirpscript";
428-
import { AuthenticationHandler } from "./authentication";
422+
import { authenticationHandler } from "./authentication";
429423

430424
export interface Context {
431425
currentUser: { username: string };
432426
}
433427

434-
const services = [AuthenticationHandler]
428+
const services = [authenticationHandler]
435429
const app = createTwirpServer<Context, typeof services>(services);
436430

437431
app.use(async (req, ctx, next) => {
438432
// exception so unauthenticated users can authenticate
439-
if (ctx.service.name === AuthenticationHandler.name) {
433+
if (ctx.service.name === authenticationHandler.name) {
440434
return next();
441435
}
442436

@@ -537,11 +531,11 @@ response) have been written. Called with the current `context` and the response.
537531
```ts
538532
import { createServer } from "http";
539533
import { createTwirpServer } from "twirpscript";
540-
import { HaberdasherHandler } from "./haberdasher";
534+
import { habderdasherHandler } from "./haberdasher";
541535

542536
const PORT = 8080;
543537

544-
const app = createTwirpServer([HaberdasherHandler]);
538+
const app = createTwirpServer([habderdasherHandler]);
545539

546540
app.on("responseSent", (ctx) => {
547541
// log or report

clientcompat/src/clientcompat.pb.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,15 @@ export async function NoopMethodJSON(
7070
}
7171

7272
//========================================//
73-
// CompatService Service //
73+
// CompatService //
7474
//========================================//
7575

76-
export interface CompatServiceService<Context = unknown> {
76+
export interface CompatService<Context = unknown> {
7777
Method: (req: Req, context: Context) => Promise<Resp> | Resp;
7878
NoopMethod: (empty: Empty, context: Context) => Promise<Empty> | Empty;
7979
}
8080

81-
export function createCompatServiceHandler<Context>(
82-
service: CompatServiceService<Context>
83-
) {
81+
export function createCompatService<Context>(service: CompatService<Context>) {
8482
return {
8583
name: "twirp.clientcompat.CompatService",
8684
methods: {

examples/authentication/src/client/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const App: FC = () => {
6565

6666
return (
6767
<div>
68-
<h1>Haberdasher Service</h1>
68+
<h1>Haberdasher </h1>
6969
<h3>Current User: </h3>
7070
{user ? (
7171
<div>

examples/authentication/src/protos/authentication.pb.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ export async function LoginJSON(
5252
}
5353

5454
//========================================//
55-
// Authentication Service //
55+
// Authentication //
5656
//========================================//
5757

58-
export interface AuthenticationService<Context = unknown> {
58+
export interface Authentication<Context = unknown> {
5959
/**
6060
* Login in a user
6161
*/
@@ -65,8 +65,8 @@ export interface AuthenticationService<Context = unknown> {
6565
) => Promise<CurrentUser> | CurrentUser;
6666
}
6767

68-
export function createAuthenticationHandler<Context>(
69-
service: AuthenticationService<Context>
68+
export function createAuthentication<Context>(
69+
service: Authentication<Context>
7070
) {
7171
return {
7272
name: "Authentication",

examples/authentication/src/protos/haberdasher.pb.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,20 @@ export async function MakeHatJSON(
4848
}
4949

5050
//========================================//
51-
// Haberdasher Service //
51+
// Haberdasher //
5252
//========================================//
5353

5454
/**
5555
* Haberdasher service makes hats for clients.
5656
*/
57-
export interface HaberdasherService<Context = unknown> {
57+
export interface Haberdasher<Context = unknown> {
5858
/**
5959
* MakeHat produces a hat of mysterious, randomly-selected color!
6060
*/
6161
MakeHat: (size: Size, context: Context) => Promise<Hat> | Hat;
6262
}
6363

64-
export function createHaberdasherHandler<Context>(
65-
service: HaberdasherService<Context>
66-
) {
64+
export function createHaberdasher<Context>(service: Haberdasher<Context>) {
6765
return {
6866
name: "Haberdasher",
6967
methods: {

examples/authentication/src/server/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { createServer } from "http";
22
import { createTwirpServer } from "twirpscript";
3-
import { AuthenticationHandler, HaberdasherHandler } from "./services";
3+
import { authenticationHandler, habderdasherHandler } from "./services";
44
import { Context } from "./context";
55
import { cors, requireAuthentication } from "./middleware";
66

77
const PORT = 8080;
8-
const services = [AuthenticationHandler, HaberdasherHandler];
8+
const services = [authenticationHandler, habderdasherHandler];
99

1010
const app = createTwirpServer<Context, typeof services>(services)
1111
.use(cors)
12-
.use(requireAuthentication({ exceptions: [AuthenticationHandler.name] }));
12+
.use(requireAuthentication({ exceptions: [authenticationHandler.name] }));
1313

1414
createServer(app).listen(PORT, () =>
1515
console.log(`Server listening on port ${PORT}`)

examples/authentication/src/server/middleware/requireAuthentication.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IncomingMessage } from "http";
22
import { Middleware, TwirpErrorResponse } from "twirpscript";
33
import { Context } from "../context";
4-
import { getCurrentUser, UnauthenticatedUser } from "../services";
4+
import { getCurrentUser, unauthenticatedUser } from "../services";
55

66
interface RequireAuthenticationOpts {
77
exceptions: string[];
@@ -13,7 +13,7 @@ export function requireAuthentication({
1313
return async (req, ctx, next) => {
1414
for (let exception of exceptions) {
1515
if (ctx.service?.name === exception) {
16-
ctx.currentUser = UnauthenticatedUser;
16+
ctx.currentUser = unauthenticatedUser;
1717
return next();
1818
}
1919
}

examples/authentication/src/server/services/authentication/index.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
AuthenticationService,
3-
createAuthenticationHandler,
2+
Authentication,
3+
createAuthentication,
44
CurrentUser,
55
Credentials,
66
} from "../../../protos/authentication.pb";
@@ -27,7 +27,7 @@ function login(credentials: Credentials): CurrentUser | undefined {
2727
/**
2828
* Sentinal value for unauthenticated routes.
2929
*/
30-
export const UnauthenticatedUser: CurrentUser = {
30+
export const unauthenticatedUser: CurrentUser = {
3131
username: "",
3232
token: "",
3333
};
@@ -41,7 +41,7 @@ export function getCurrentUser(
4141
return sessions.find((s) => s.token === token);
4242
}
4343

44-
export const Authentication: AuthenticationService = {
44+
export const authentication: Authentication = {
4545
Login: (credentials) => {
4646
const user = login(credentials);
4747
if (!user) {
@@ -54,5 +54,4 @@ export const Authentication: AuthenticationService = {
5454
},
5555
};
5656

57-
export const AuthenticationHandler =
58-
createAuthenticationHandler(Authentication);
57+
export const authenticationHandler = createAuthentication(authentication);
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { Context } from "../../context";
2-
import {
3-
HaberdasherService,
4-
createHaberdasherHandler,
5-
} from "../../../protos/haberdasher.pb";
2+
import { Haberdasher, createHaberdasher } from "../../../protos/haberdasher.pb";
63

74
function choose<T>(list: T[]): T {
85
return list[Math.floor(Math.random() * list.length)];
96
}
107

11-
export const Haberdasher: HaberdasherService<Context> = {
8+
export const haberdasher: Haberdasher<Context> = {
129
MakeHat: (size, ctx) => {
1310
const username = ctx.currentUser.username;
1411
const hat = choose(["beanie", "fedora", "top hat", "cowboy", "beret"]);
@@ -22,4 +19,4 @@ export const Haberdasher: HaberdasherService<Context> = {
2219
},
2320
};
2421

25-
export const HaberdasherHandler = createHaberdasherHandler(Haberdasher);
22+
export const habderdasherHandler = createHaberdasher(haberdasher);

examples/authentication/src/server/services/haberdasher/test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { describe, it, expect } from "@jest/globals";
2-
import { Haberdasher } from ".";
2+
import { haberdasher } from ".";
33

44
describe("Haberdasher", () => {
55
describe("Haberdasher.MakeHat", () => {
66
it("makes hats", () => {
77
const size = { inches: 12 };
88
const ctx = { currentUser: { username: "tate" } };
99

10-
expect(Haberdasher.MakeHat(size, ctx)).toEqual(
10+
expect(haberdasher.MakeHat(size, ctx)).toEqual(
1111
expect.objectContaining({
1212
inches: size.inches,
1313
name: expect.stringMatching("tate"),
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export {
2-
AuthenticationHandler,
2+
authenticationHandler,
33
getCurrentUser,
4-
UnauthenticatedUser,
4+
unauthenticatedUser,
55
} from "./authentication";
6-
export { HaberdasherHandler } from "./haberdasher";
6+
export { habderdasherHandler } from "./haberdasher";
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { HaberdasherService, createHaberdasherHandler } from "./haberdasher.pb";
1+
import { Haberdasher, createHaberdasher } from "./haberdasher.pb";
22

33
function choose<T>(list: T[]): T {
44
return list[Math.floor(Math.random() * list.length)];
55
}
66

7-
export const Haberdasher: HaberdasherService = {
7+
export const haberdasher: Haberdasher = {
88
MakeHat: (size) => {
99
return {
1010
inches: size.inches,
@@ -14,4 +14,4 @@ export const Haberdasher: HaberdasherService = {
1414
},
1515
};
1616

17-
export const HaberdasherHandler = createHaberdasherHandler(Haberdasher);
17+
export const habderdasherHandler = createHaberdasher(haberdasher);

examples/aws-lambda/src/haberdasher.pb.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,20 @@ export async function MakeHatJSON(
4848
}
4949

5050
//========================================//
51-
// Haberdasher Service //
51+
// Haberdasher //
5252
//========================================//
5353

5454
/**
5555
* Haberdasher service makes hats for clients.
5656
*/
57-
export interface HaberdasherService<Context = unknown> {
57+
export interface Haberdasher<Context = unknown> {
5858
/**
5959
* MakeHat produces a hat of mysterious, randomly-selected color!
6060
*/
6161
MakeHat: (size: Size, context: Context) => Promise<Hat> | Hat;
6262
}
6363

64-
export function createHaberdasherHandler<Context>(
65-
service: HaberdasherService<Context>
66-
) {
64+
export function createHaberdasher<Context>(service: Haberdasher<Context>) {
6765
return {
6866
name: "Haberdasher",
6967
methods: {

examples/aws-lambda/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { createTwirpServerless } from "twirpscript";
2-
import { HaberdasherHandler } from "./habderdasher";
2+
import { habderdasherHandler } from "./habderdasher";
33
import {
44
Handler,
55
APIGatewayProxyEvent,
66
APIGatewayProxyResult,
77
} from "aws-lambda";
88

9-
const app = createTwirpServerless([HaberdasherHandler]);
9+
const app = createTwirpServerless([habderdasherHandler]);
1010

1111
const handler: Handler<APIGatewayProxyEvent, APIGatewayProxyResult> = async (
1212
event

examples/javascript-fullstack/src/client/index.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const App = () => {
2222

2323
return (
2424
<div>
25-
<h1>Haberdasher Service</h1>
25+
<h1>Haberdasher </h1>
2626
<h3>Hats: </h3>
2727
<ul>
2828
{hats.map((hat) => (

examples/javascript-fullstack/src/protos/haberdasher.pb.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export async function MakeHatJSON(size, config) {
4040
return response;
4141
}
4242

43-
export function createHaberdasherHandler(service) {
43+
export function createHaberdasher(service) {
4444
return {
4545
name: "Haberdasher",
4646
methods: {

0 commit comments

Comments
 (0)