@@ -182,12 +182,9 @@ If you have an existing Twirp server you're connecting to and only need a client
182
182
` src/server/haberdasher/index.ts `
183
183
184
184
``` ts
185
- import {
186
- HaberdasherService ,
187
- createHaberdasherHandler ,
188
- } from " ../../protos/haberdasher.pb" ;
185
+ import { Haberdasher , createHaberdasher } from " ../../protos/haberdasher.pb" ;
189
186
190
- const Haberdasher : HaberdasherService = {
187
+ const haberdasher : Haberdasher = {
191
188
MakeHat : (size ) => {
192
189
return {
193
190
inches: size .inches ,
@@ -197,7 +194,7 @@ const Haberdasher: HaberdasherService = {
197
194
},
198
195
};
199
196
200
- export const HaberdasherHandler = createHaberdasherHandler ( Haberdasher );
197
+ export const haberdasherHandler = createHaberdasher ( haberdasher );
201
198
```
202
199
203
200
#### 5. Connect your service to your application server
@@ -207,11 +204,11 @@ export const HaberdasherHandler = createHaberdasherHandler(Haberdasher);
207
204
``` ts
208
205
import { createServer } from " http" ;
209
206
import { createTwirpServer } from " twirpscript" ;
210
- import { HaberdasherHandler } from " ./haberdasher" ;
207
+ import { haberdasherHandler } from " ./haberdasher" ;
211
208
212
209
const PORT = 8080 ;
213
210
214
- const app = createTwirpServer ([HaberdasherHandler ]);
211
+ const app = createTwirpServer ([haberdasherHandler ]);
215
212
216
213
createServer (app ).listen (PORT , () =>
217
214
console .log (` Server listening on port ${PORT } ` )
@@ -309,12 +306,12 @@ Servers can be configured by passing a configuration object to `createTwirpServe
309
306
``` ts
310
307
import { createServer } from " http" ;
311
308
import { createTwirpServer } from " twirpscript" ;
312
- import { HaberdasherHandler } from " ./haberdasher" ;
309
+ import { haberdasherHandler } from " ./haberdasher" ;
313
310
314
311
const PORT = 8080 ;
315
312
316
313
// This removes the "/twirp" prefix in the RPC path
317
- const app = createTwirpServer ([HaberdasherHandler ], { prefix: " " });
314
+ const app = createTwirpServer ([haberdasherHandler ], { prefix: " " });
318
315
319
316
createServer (app ).listen (PORT , () =>
320
317
console .log (` Server listening on port ${PORT } ` )
@@ -347,13 +344,10 @@ Custom fields can be added to the context object via [middleware](#middleware--i
347
344
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.
348
345
349
346
``` ts
350
- import {
351
- HaberdasherService ,
352
- createHaberdasherHandler ,
353
- } from " ../../protos/haberdasher.pb" ;
347
+ import { Haberdasher , createHaberdasher } from " ../../protos/haberdasher.pb" ;
354
348
import { Context } from " ../some-path-to-your-definition" ;
355
349
356
- const Haberdasher : HaberdasherService <Context > = {
350
+ const haberdasher : Haberdasher <Context > = {
357
351
MakeHat : (size , ctx ) => {
358
352
return {
359
353
inches: size .inches ,
@@ -363,7 +357,7 @@ const Haberdasher: HaberdasherService<Context> = {
363
357
},
364
358
};
365
359
366
- export const HaberdasherHandler = createHaberdasherHandler ( HaberdasherService );
360
+ export const haberdasherHandler = createHaberdasher ( haberdasher );
367
361
```
368
362
369
363
### Middleware / Interceptors
@@ -425,18 +419,18 @@ The middleware handler will receive `req`, `context` and `next` parameters. `req
425
419
``` ts
426
420
import { createServer } from " http" ;
427
421
import { createTwirpServer , TwirpError } from " twirpscript" ;
428
- import { AuthenticationHandler } from " ./authentication" ;
422
+ import { authenticationHandler } from " ./authentication" ;
429
423
430
424
export interface Context {
431
425
currentUser: { username: string };
432
426
}
433
427
434
- const services = [AuthenticationHandler ]
428
+ const services = [authenticationHandler ]
435
429
const app = createTwirpServer <Context , typeof services >(services );
436
430
437
431
app .use (async (req , ctx , next ) => {
438
432
// exception so unauthenticated users can authenticate
439
- if (ctx .service .name === AuthenticationHandler .name ) {
433
+ if (ctx .service .name === authenticationHandler .name ) {
440
434
return next ();
441
435
}
442
436
@@ -537,11 +531,11 @@ response) have been written. Called with the current `context` and the response.
537
531
` ` ` ts
538
532
import { createServer } from " http" ;
539
533
import { createTwirpServer } from " twirpscript" ;
540
- import { HaberdasherHandler } from " ./haberdasher" ;
534
+ import { habderdasherHandler } from " ./haberdasher" ;
541
535
542
536
const PORT = 8080 ;
543
537
544
- const app = createTwirpServer ([HaberdasherHandler ]);
538
+ const app = createTwirpServer ([habderdasherHandler ]);
545
539
546
540
app .on (" responseSent" , (ctx ) => {
547
541
// log or report
0 commit comments