|
3 | 3 | "effect-http": minor |
4 | 4 | --- |
5 | 5 |
|
6 | | -New pipeable API. TODO: sum up all changes and provide migration guidelines. |
| 6 | +## New pipeable API |
| 7 | + |
| 8 | +Overall, the API based on unnamed objects containing `response` and `request` fields |
| 9 | +was replaced by more strict and explicit pipeable API which constructs new `Api`, `ApiGroup`, |
| 10 | +`ApiEndpoint`, `ApiResponse` and `ApiRequest` objects under the hood. |
| 11 | + |
| 12 | +Before |
| 13 | + |
| 14 | +```ts |
| 15 | +const api = pipe( |
| 16 | + Api.api({ title: "Users API" }), |
| 17 | + Api.get("getUser", "/user", { |
| 18 | + response: User, |
| 19 | + request: { query: GetUserQuery }, |
| 20 | + }), |
| 21 | +); |
| 22 | +``` |
| 23 | + |
| 24 | +After |
| 25 | + |
| 26 | +```ts |
| 27 | +const api = pipe( |
| 28 | + Api.make({ title: "Users API" }), |
| 29 | + Api.addEndpoint( |
| 30 | + pipe( |
| 31 | + Api.get("getUser", "/user"), |
| 32 | + Api.setResponseBody(UserResponse), |
| 33 | + Api.setRequestQuery(GetUserQuery) |
| 34 | + ) |
| 35 | + ) |
| 36 | +) |
| 37 | +``` |
| 38 | + |
| 39 | +## Multiple-response endpoints changes |
| 40 | + |
| 41 | +The `content` field was renamed to `body`. So on the client side, an endpoint with multiple |
| 42 | +responses now returns an object `{ status; body; headers }` instead of `{ status; content; headers }`. |
| 43 | +The same on the server side, the handling function shall return the object with a `body` field |
| 44 | +instead of `content`. |
| 45 | + |
| 46 | +Also, with the new API, the *full response* is applied only in case the there is a single response |
| 47 | +and it specifies headers or if there are multiple responses. So, in case the endpoint changes |
| 48 | +the response status, the client now returns the body only. |
| 49 | + |
| 50 | +```ts |
| 51 | +const api = pipe( |
| 52 | + Api.make({ title: "Users API" }), |
| 53 | + Api.addEndpoint( |
| 54 | + pipe( |
| 55 | + Api.post("createUser", "/user"), |
| 56 | + Api.setResponseStatus(201), |
| 57 | + Api.setResponseBody(UserResponse), |
| 58 | + ) |
| 59 | + ) |
| 60 | +) |
| 61 | + |
| 62 | +const client = Client.make(api) |
| 63 | +client.createUser({}) // now returns `UserResponse` instead of `{ status: 201; body: UserResponse; headers? }` |
| 64 | +``` |
| 65 | + |
| 66 | +Multiple responses can be now defined using `Api.addResponse` / `ApiGroup.addResponse` combinators. They |
| 67 | +accept either a `ApiResponse` object (constructed using `ApiResponse.make`) or an object of shape |
| 68 | +`{ status; body?; headers? }`. |
| 69 | + |
| 70 | +```ts |
| 71 | +const helloEndpoint = Api.post("hello", "/hello").pipe( |
| 72 | + // ApiResponse constructor |
| 73 | + Api.addResponse(ApiResponse.make(201, Schema.number)), |
| 74 | + // plain object |
| 75 | + Api.addResponse({ status: 204, headers: Schema.struct({ "x-another": Schema.NumberFromString }) }) |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +## Security |
| 80 | + |
| 81 | +The security was one of the reasons to introduce the new API. Previously, the way to declare |
| 82 | +an authorization for an endpoint was to specify it in the endpoint options. Now, it is part |
| 83 | +of the pipeable API. |
| 84 | + |
| 85 | +```ts |
| 86 | +const mySecuredEnpoint = Api.post("security", "/testSecurity").pipe( |
| 87 | + Api.setResponseBody(Schema.string), |
| 88 | + Api.addSecurity("myAwesomeBearerAuth", mySecuritySchema) |
| 89 | +) |
| 90 | + |
| 91 | +const api = Api.make().pipe( |
| 92 | + Api.addEndpoint(mySecuredEnpoint) |
| 93 | +) |
| 94 | +``` |
0 commit comments