Skip to content

Latest commit

 

History

History
88 lines (66 loc) · 3.38 KB

README.md

File metadata and controls

88 lines (66 loc) · 3.38 KB

CI Coverage Status scala-index Scala.js

scommons-api

Common REST API Scala/Scala.js components

How to add it to your project

val scommonsApiVer = "1.0.0-SNAPSHOT"

libraryDependencies ++= Seq(
  // shared
  "org.scommons.api" %%% "scommons-api-core" % scommonsApiVer,
  "org.scommons.api" %%% "scommons-api-joda-time" % scommonsApiVer,

  // client/js only
  "org.scommons.api" %%% "scommons-api-xhr" % scommonsApiVer,
  
  // server/jvm only
  "org.scommons.api" %% "scommons-api-play-ws" % scommonsApiVer
)

Latest SNAPSHOT version is published to Sonatype Repo, just make sure you added the proper dependency resolver to your build.sbt settings:

resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"

How to use it

Joda Date/Time

You can use in your shared API data case classes date/time types from the joda library:

import org.joda.time._
import play.api.libs.json._

case class User(firstName: String,
                lastName: String,
                registeredAt: DateTime,
                birthDate: LocalDate,
                birthTime: LocalTime)

object User {
  // you only need to provide appropriate implicits:
  import scommons.api.jodatime.JodaTimeImplicits.{dateTimeReads => dtReads, dateTimeWrites => dtWrites}
  import scommons.api.jodatime.JodaTimeImplicits.{dateReads => dReads, dateWrites => dWrites}
  import scommons.api.jodatime.JodaTimeImplicits.{timeReads => tReads, timeWrites => tWrites}

  implicit val jsonFormat: Format[User] = Json.format[User]
}

For JS part the following types are defined as wrappers around the corresponding ISO time formatted string and do not contain any logic:

Once you receive from an API on JS side an object that uses them you can use toString method to get an ISO time formatted string and pass it to your favorite JS time library to parse it.

For example, you could use standard JS Date class to parse it:

import scala.scalajs.js

new js.Date(user.registeredAt.toString).getDate

How to Build

To build and run all the tests use the following command:

sbt test

Documentation

You can find more documentation here