@@ -31,7 +31,7 @@ Let's define a simple CRUD API for managing users. First, we need to make an
3131` HttpApiGroup ` that contains our endpoints.
3232
3333``` ts
34- import { HttpApiEndpoint , HttpApiGroup } from " @effect/platform"
34+ import { HttpApiEndpoint , HttpApiGroup , HttpApiSchema } from " @effect/platform"
3535import { Schema } from " effect"
3636
3737// Our domain "User" Schema
@@ -44,17 +44,16 @@ class User extends Schema.Class<User>("User")({
4444const usersApi = HttpApiGroup .make (" users" )
4545 .add (
4646 // each endpoint has a name and a path
47- HttpApiEndpoint .get (" findById" , " /users/:id" )
47+ // You can use a template string to define path parameter schemas
48+ HttpApiEndpoint .get (
49+ " findById"
50+ )` /users/${HttpApiSchema .param (" id" , Schema .NumberFromString )} `
4851 // the endpoint can have a Schema for a successful response
4952 .addSuccess (User )
50- // and here is a Schema for the path parameters
51- .setPath (
52- Schema .Struct ({
53- id: Schema .NumberFromString
54- })
55- )
5653 )
5754 .add (
55+ // you can also pass the path as a string and use `.setPath` to define the
56+ // path parameter schema
5857 HttpApiEndpoint .post (" create" , " /users" )
5958 .addSuccess (User )
6059 // and here is a Schema for the request payload / body
@@ -68,9 +67,15 @@ const usersApi = HttpApiGroup.make("users")
6867 )
6968 )
7069 // by default, the endpoint will respond with a 204 No Content
71- .add (HttpApiEndpoint .del (" delete" , " /users/:id" ))
7270 .add (
73- HttpApiEndpoint .patch (" update" , " /users/:id" )
71+ HttpApiEndpoint .del (
72+ " delete"
73+ )` /users/${HttpApiSchema .param (" id" , Schema .NumberFromString )} `
74+ )
75+ .add (
76+ HttpApiEndpoint .patch (
77+ " update"
78+ )` /users/${HttpApiSchema .param (" id" , Schema .NumberFromString )} `
7479 .addSuccess (User )
7580 .setPayload (
7681 Schema .Struct ({
@@ -85,7 +90,9 @@ We will use this API style in the following examples:
8590
8691``` ts
8792class UsersApi extends HttpApiGroup .make (" users" ).add (
88- HttpApiEndpoint .get (" findById" , " /users/:id" )
93+ HttpApiEndpoint .get (
94+ " findById"
95+ )` /users/${HttpApiSchema .param (" id" , Schema .NumberFromString )} `
8996 // ... same as above
9097) {}
9198```
@@ -117,7 +124,9 @@ import { OpenApi } from "@effect/platform"
117124
118125class UsersApi extends HttpApiGroup .make (" users" )
119126 .add (
120- HttpApiEndpoint .get (" findById" , " /users/:id" )
127+ HttpApiEndpoint .get (
128+ " findById"
129+ )` /users/${HttpApiSchema .param (" id" , Schema .NumberFromString )} `
121130 // ... same as above
122131 )
123132 // add an OpenApi title & description
@@ -170,11 +179,12 @@ class Unauthorized extends Schema.TaggedError<Unauthorized>()(
170179
171180class UsersApi extends HttpApiGroup .make (" users" )
172181 .add (
173- HttpApiEndpoint .get (" findById" , " /users/:id" )
182+ HttpApiEndpoint .get (
183+ " findById"
184+ )` /users/${HttpApiSchema .param (" id" , Schema .NumberFromString )} `
174185 // here we are adding our error response
175186 .addError (UserNotFound , { status: 404 })
176187 .addSuccess (User )
177- .setPath (Schema .Struct ({ id: Schema .NumberFromString }))
178188 )
179189 // or we could add an error to the group
180190 .addError (Unauthorized , { status: 401 }) {}
@@ -195,7 +205,7 @@ shape of the multipart request.
195205import { HttpApiSchema , Multipart } from " @effect/platform"
196206
197207class UsersApi extends HttpApiGroup .make (" users" ).add (
198- HttpApiEndpoint .post (" upload" , " /users/upload" ) .setPayload (
208+ HttpApiEndpoint .post (" upload" ) ` /users/upload ` .setPayload (
199209 HttpApiSchema .Multipart (
200210 Schema .Struct ({
201211 // add a "files" field to the schema
@@ -215,7 +225,7 @@ Here is an example of changing the encoding to text/csv:
215225
216226``` ts
217227class UsersApi extends HttpApiGroup .make (" users" ).add (
218- HttpApiEndpoint .get (" csv" , " /users/csv" ) .addSuccess (
228+ HttpApiEndpoint .get (" csv" ) ` /users/csv ` .addSuccess (
219229 Schema .String .pipe (
220230 HttpApiSchema .withEncoding ({
221231 kind: " Text" ,
@@ -248,7 +258,8 @@ import {
248258 HttpApi ,
249259 HttpApiBuilder ,
250260 HttpApiEndpoint ,
251- HttpApiGroup
261+ HttpApiGroup ,
262+ HttpApiSchema
252263} from " @effect/platform"
253264import { DateTime , Effect , Layer , Schema } from " effect"
254265
@@ -260,13 +271,11 @@ class User extends Schema.Class<User>("User")({
260271}) {}
261272
262273class UsersApi extends HttpApiGroup .make (" users" ).add (
263- HttpApiEndpoint .get (" findById" , " /users/:id" )
264- .addSuccess (User )
265- .setPath (
266- Schema .Struct ({
267- id: Schema .NumberFromString
268- })
269- )
274+ HttpApiEndpoint .get (
275+ " findById"
276+ )` /users/${HttpApiSchema .param (" id" , Schema .NumberFromString )} ` .addSuccess (
277+ User
278+ )
270279) {}
271280
272281class MyApi extends HttpApi .empty .add (UsersApi ) {}
@@ -428,7 +437,7 @@ class Logger extends HttpApiMiddleware.Tag<Logger>()("Http/Logger", {
428437// apply the middleware to an `HttpApiGroup`
429438class UsersApi extends HttpApiGroup .make (" users" )
430439 .add (
431- HttpApiEndpoint .get (" findById" , " /:id " )
440+ HttpApiEndpoint .get (" findById" ) ` /${ Schema . NumberFromString } `
432441 // apply the middleware to a single endpoint
433442 .middleware (Logger )
434443 )
@@ -492,7 +501,7 @@ class Authorization extends HttpApiMiddleware.Tag<Authorization>()(
492501// apply the middleware to an `HttpApiGroup`
493502class UsersApi extends HttpApiGroup .make (" users" )
494503 .add (
495- HttpApiEndpoint .get (" findById" , " /:id " )
504+ HttpApiEndpoint .get (" findById" ) ` /${ Schema . NumberFromString } `
496505 // apply the middleware to a single endpoint
497506 .middleware (Authorization )
498507 )
0 commit comments