Skip to content

Commit a491f7e

Browse files
committed
chore: Update example
1 parent f9e8340 commit a491f7e

1 file changed

Lines changed: 12 additions & 15 deletions

File tree

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
import { Effect } from 'effect';
1+
import { Effect, Option } from 'effect';
22
import { onRequestEffect } from '@effect-firebase/admin';
3-
import { PostId, PostRepository } from '@example/shared';
3+
import { OnExampleRequest, PostId, PostRepository } from '@example/shared';
44
import { 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+
*/
710
export 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
);

0 commit comments

Comments
 (0)