Skip to content

Commit 1c46058

Browse files
committed
Fix build and add docs
1 parent 58f1f92 commit 1c46058

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

packages/app/backend-http-client/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,34 @@ It's up to the caller of the function to handle the received error or throw an e
7676
Read [this article](https://antman-does-software.com/stop-catching-errors-in-typescript-use-the-either-type-to-make-your-code-predictable) for more information on how `Either` works and its benefits.
7777
7878
Additionally, `DefiniteEither` is also provided. It is a variation of the aforementioned `Either`, which may or may not have `error` set, but always has `result`.
79+
80+
### API contract-based requests
81+
82+
`backend-http-client` supports using API contracts, created with `@lokalise/universal-ts-utils/api-contracts/apiContracts` in order to make fully type-safe HTTP requests.
83+
84+
Usage example:
85+
86+
```ts
87+
import { somePostRouteDefinition, someGetRouteDefinition } from 'some-service-api-contracts'
88+
import { sendByPayloadRoute, buildClient } from '@lokalise/backend-http-client'
89+
90+
const MY_BASE_URL = 'http://localhost:8080'
91+
const client = buildClient(MY_BASE_URL)
92+
93+
const responseBody1 = await sendByPayloadRoute(client, somePostRouteDefinition,
94+
// pass contract-defined request params, such as body, query and headers here
95+
{
96+
pathParams: {
97+
userId: 1,
98+
},
99+
body: {
100+
isActive: true,
101+
},
102+
},
103+
// pass backend-http-client options here
104+
{
105+
validateResponse: false,
106+
requestLabel: 'Test request',
107+
}
108+
)
109+
```

packages/app/backend-http-client/src/client/httpClient.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,10 @@ describe('httpClient', () => {
602602
id: z.string(),
603603
})
604604
const apiContract = buildPayloadRoute({
605+
successResponseBodySchema: schema,
606+
requestPathParamsSchema: z.undefined(),
605607
method: 'post',
606-
requestBodySchema: schema,
608+
requestBodySchema: z.undefined(),
607609
pathResolver: () => '/products/1',
608610
})
609611

@@ -618,10 +620,8 @@ describe('httpClient', () => {
618620
sendByPayloadRoute(
619621
client,
620622
apiContract,
621-
// @ts-expect-error We intentionally send missing body
622623
{},
623624
{
624-
responseSchema: schema,
625625
validateResponse: true,
626626
requestLabel: 'Test request',
627627
},

packages/app/backend-http-client/src/client/httpClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ export function sendByPayloadRoute<
378378
>,
379379
options: Omit<
380380
RequestOptions<InferSchemaOutput<ResponseBodySchema>, IsEmptyResponseExpected, boolean>,
381-
'headers' | 'query' | 'isEmptyResponseExpected' | 'responseSchema'
381+
'body' | 'headers' | 'query' | 'isEmptyResponseExpected' | 'responseSchema'
382382
>,
383383
) {
384384
return sendResourceChange(

0 commit comments

Comments
 (0)