Overview

Typically, a project is broken down into several sub-projects:

graph BT
endpoints
server -.-> endpoints
client -.-> endpoints

The endpoints sub-project contains the description of the communication
protocol. The server sub-project implements this communication protocol.
The client sub-project uses the protocol to communicate with the server.

Description of the HTTP endpoints

Let’s define a first artifact, cross-compiled for Scala.js, and containing a description of the
endpoints of a web service.

import endpoints.algebra.{Endpoints, CirceEntities}
import io.circe.generic.JsonCodec/**
* Defines the HTTP endpoints description of a web service implementing a counter.
* This web service has two endpoints: one for getting the current value of the counter,
* and one for incrementing it.
* It uses circe.io for JSON marshalling.
*/traitCounterEndpointsextendsEndpointswithCirceEntities{
/**
* Get the counter current value.
* Uses the HTTP verb “GET” and URL path “/current-value”.
* The response entity is a JSON document representing the counter value.
*/val currentValue = endpoint(get(path / "current-value"), jsonResponse[Counter])
/**
* Increments the counter value.
* Uses the HTTP verb “POST” and URL path “/increment”.
* The request entity is a JSON document representing the increment to apply to the counter.
* The response entity is empty.
*/val increment = endpoint(post(path / "increment", jsonRequest[Increment]), emptyResponse)
}
@JsonCodeccaseclassCounter(value: Int)@JsonCodeccaseclassIncrement(step: Int)