File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- import { Effect } from 'effect' ;
1+ import { Effect , Option } from 'effect' ;
22import { onRequestEffect } from '@effect-firebase/admin' ;
3- import { PostId , PostRepository } from '@example/shared' ;
3+ import { OnExampleRequest , PostId , PostRepository } from '@example/shared' ;
44import { runtime } from './runtime.js' ;
5- import { SerializeError } from './error-handler.js' ;
65
6+ /**
7+ * Example using onRequestEffect with schemas.
8+ * Body is parsed and response is encoded automatically.
9+ */
710export const onExampleRequest = onRequestEffect (
811 {
912 region : 'europe-north1' ,
1013 cors : true ,
1114 runtime,
15+ bodySchema : OnExampleRequest . Input ,
16+ responseSchema : OnExampleRequest . Output ,
1217 } ,
13- ( _ , response ) =>
18+ ( body ) =>
1419 Effect . gen ( function * ( ) {
1520 const posts = yield * PostRepository ;
16- const id = PostId . make ( '123' ) ;
21+ const id = PostId . make ( body . id ) ;
1722 const post = yield * posts . getById ( id ) ;
18- response . status ( 200 ) . json ( post ) ;
19- } ) . pipe (
20- SerializeError ,
21- Effect . map ( ( result ) => {
22- if ( ! result ) return ;
23- response
24- . status ( result . error . tag === 'NoSuchElementException' ? 404 : 500 )
25- . json ( result . error ) ;
26- } )
27- )
23+ return Option . getOrThrow ( post ) ;
24+ } )
2825) ;
You can’t perform that action at this time.
0 commit comments