Skip to content

Commit a5886dd

Browse files
committed
Add documentation for handler/route separation
1 parent 8a4542d commit a5886dd

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

packages/app/fastify-api-contracts/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This module requires `fastify-type-provider-zod` type provider to work and is ES
66

77
# Usage
88

9+
Basic usage pattern:
10+
911
```ts
1012
import { buildFastifyNoPayloadRoute, buildFastifyPayloadRoute, injectPost, injectGet } from '@lokalise/fastify-api-contracts'
1113

@@ -68,3 +70,30 @@ const getResponse = await injectGet(app, contract, {
6870
headers: async () => { authorization: 'some-value'} // headers producing function (sync or async) can be passed as well
6971
})
7072
```
73+
74+
In case you prefer to separate handler definition from route definition, you can use functions `buildFastifyNoPayloadRouteHandler` (for GET and DELETE handlers) and `buildFastifyPayloadRouteHandler` (for PUT, POST and PATCH handlers):
75+
76+
```ts
77+
import {
78+
buildFastifyPayloadRoute,
79+
buildFastifyPayloadRouteHandler
80+
} from '@lokalise/fastify-api-contracts'
81+
82+
const contract = buildPayloadRoute({
83+
method: 'post',
84+
requestBodySchema: REQUEST_BODY_SCHEMA,
85+
successResponseBodySchema: BODY_SCHEMA,
86+
requestPathParamsSchema: PATH_PARAMS_SCHEMA,
87+
pathResolver: (pathParams) => `/users/${pathParams.userId}`,
88+
})
89+
90+
const handler = buildFastifyPayloadRouteHandler(contract,
91+
async (req, reply) => {
92+
// handler definition here, req and reply will be correctly typed based on the contract
93+
}
94+
)
95+
96+
const routes = [
97+
buildFastifyPayloadRoute(contract, handler),
98+
]
99+
```

0 commit comments

Comments
 (0)