Skip to content

Commit ca03e10

Browse files
authored
README.md for HttpApiEndpoint: clarify header schema usage and add note on header normalization (#5668)
1 parent 463345d commit ca03e10

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

packages/platform/README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -550,35 +550,52 @@ const csv = HttpApiEndpoint.get("csv")`/users/csv`
550550

551551
### Setting Request Headers
552552

553-
The `HttpApiEndpoint.setHeaders` method allows you to define the expected structure of request headers. You can specify the schema for each header and include additional metadata, such as descriptions.
553+
Use `HttpApiEndpoint.setHeaders` to declare a single, cumulative schema that describes all expected request headers.
554+
Provide one struct schema where each header name maps to its validator, and you can attach metadata such as descriptions.
554555

555-
**Example** (Defining Request Headers with Metadata)
556+
> [!IMPORTANT]
557+
> All headers are normalized to lowercase. Always use lowercase keys in the headers schema.
558+
559+
**Example** (Describe and validate custom headers)
556560

557561
```ts
558562
import { HttpApiEndpoint } from "@effect/platform"
559563
import { Schema } from "effect"
560564

565+
// Model for successful responses
561566
const User = Schema.Struct({
562567
id: Schema.Number,
563568
name: Schema.String,
564569
createdAt: Schema.DateTimeUtc
565570
})
566571

567572
const getUsers = HttpApiEndpoint.get("getUsers", "/users")
568-
// Specify the headers schema
573+
// Describe the headers the endpoint expects
569574
.setHeaders(
575+
// Declare a single struct schema for all headers
576+
// Header keys MUST be lowercase in the schema
570577
Schema.Struct({
571-
// Header must be a string
572-
"X-API-Key": Schema.String,
573-
// Header must be a string with an added description
574-
"X-Request-ID": Schema.String.annotations({
578+
// This header must be a string
579+
"x-api-key": Schema.String,
580+
581+
// A human-friendly description is useful for generated docs (e.g. OpenAPI)
582+
"x-request-id": Schema.String.annotations({
575583
description: "Unique identifier for the request"
576584
})
577585
})
578586
)
587+
// Successful response: an array of User
579588
.addSuccess(Schema.Array(User))
580589
```
581590

591+
You can test the endpoint by sending the headers:
592+
593+
```sh
594+
curl -H "X-API-Key: 1234567890" -H "X-Request-ID: 1234567890" http://localhost:3000/users
595+
```
596+
597+
The server validates these headers against the declared schema before handling the request.
598+
582599
## Defining a HttpApiGroup
583600

584601
You can group related endpoints under a single entity by using `HttpApiGroup.make`. This can help organize your code and provide a clearer structure for your API.

0 commit comments

Comments
 (0)