Skip to content

Commit

Permalink
JS HTTP fetch client (#2579) (#2630)
Browse files Browse the repository at this point in the history
* Initial scala.js setup

* JS HTTP fetch client (#2579)
  • Loading branch information
987Nabil authored Jan 19, 2024
1 parent db5d4db commit a9a2204
Show file tree
Hide file tree
Showing 349 changed files with 707 additions and 221 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
run: sbt '++ ${{ matrix.scala }}' zioHttpShadedTests/test

- name: Compress target directories
run: tar cf targets.tar zio-http-cli/target target zio-http/target zio-http-docs/target zio-http-gen/target zio-http-benchmarks/target zio-http-example/target zio-http-testkit/target zio-http-htmx/target project/target
run: tar cf targets.tar zio-http-cli/target target zio-http/jvm/target zio-http-docs/target zio-http-gen/target zio-http-benchmarks/target zio-http-example/target zio-http-testkit/target zio-http/js/target zio-http-htmx/target project/target

- name: Upload target directories
uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -203,7 +203,7 @@ jobs:
- name: Run Coverage
id: run_coverage
run: sbt 'coverage; project zioHttp; test; coverageReport'
run: sbt 'coverage; project zioHttpJVM; test; coverageReport'

- name: Push Codecov
id: push_codecov
Expand Down
79 changes: 53 additions & 26 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BuildHelper._
import Dependencies.{scalafmt, _}
import sbt.librarymanagement.ScalaArtifacts.isScala3
import scala.concurrent.duration._
import BuildHelper.*
import Dependencies.{scalafmt, *}

import scala.concurrent.duration.*

val releaseDrafterVersion = "5"

Expand Down Expand Up @@ -110,12 +110,14 @@ ThisBuild / githubWorkflowBuildTimeout := Some(60.minutes)
lazy val aggregatedProjects: Seq[ProjectReference] =
if (Shading.shadingEnabled) {
Seq(
zioHttp,
zioHttpJVM,
zioHttpJS,
zioHttpTestkit,
)
} else {
Seq(
zioHttp,
zioHttpJVM,
zioHttpJS,
zioHttpBenchmarks,
zioHttpCli,
zioHttpGen,
Expand All @@ -131,24 +133,34 @@ lazy val root = (project in file("."))
.settings(publishSetting(false))
.aggregate(aggregatedProjects: _*)

lazy val zioHttp = (project in file("zio-http"))
lazy val zioHttp = crossProject(JSPlatform, JVMPlatform)
.in(file("zio-http"))
.enablePlugins(Shading.plugins(): _*)
.settings(stdSettings("zio-http"))
.settings(publishSetting(true))
.settings(settingsWithHeaderLicense)
.settings(meta)
.settings(crossProjectSettings)
.settings(Shading.shadingSettings())
.settings(
libraryDependencies ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, _)) =>
Seq("org.scala-lang" % "scala-reflect" % scalaVersion.value)
case _ => Seq.empty
}
},
)
.jvmSettings(
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"),
libraryDependencies ++= netty ++ Seq(
libraryDependencies ++= Seq(
`zio`,
`zio-streams`,
`zio-schema`,
`zio-schema-json`,
`zio-schema-protobuf`,
`zio-test`,
`zio-test-sbt`,
`netty-incubator`,
),
libraryDependencies ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
Expand All @@ -157,14 +169,29 @@ lazy val zioHttp = (project in file("zio-http"))
case _ => Seq.empty
}
},
libraryDependencies ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, _)) =>
Seq("org.scala-lang" % "scala-reflect" % scalaVersion.value)
case _ => Seq.empty
}
},
libraryDependencies ++= netty ++ Seq(`netty-incubator`),
)
.jsSettings(
ThisProject / fork := false,
testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework")),
libraryDependencies ++= Seq(
"io.github.cquiroz" %%% "scala-java-time" % "2.5.0",
"io.github.cquiroz" %%% "scala-java-time-tzdb" % "2.5.0",
"org.scala-js" %%% "scalajs-dom" % "2.8.0",
"dev.zio" %%% "zio-test" % ZioVersion % "test",
"dev.zio" %%% "zio-test-sbt" % ZioVersion % "test",
"dev.zio" %%% "zio" % ZioVersion,
"dev.zio" %%% "zio-streams" % ZioVersion,
"dev.zio" %%% "zio-schema" % ZioSchemaVersion,
"dev.zio" %%% "zio-schema-json" % ZioSchemaVersion,
"dev.zio" %%% "zio-schema-protobuf" % ZioSchemaVersion,
),
)

lazy val zioHttpJS = zioHttp.js
.settings(scalaJSUseMainModuleInitializer := true)

lazy val zioHttpJVM = zioHttp.jvm

/**
* Special subproject to sanity test the shaded version of zio-http. Run using
Expand Down Expand Up @@ -192,7 +219,7 @@ lazy val zioHttpShadedTests = if (Shading.shadingEnabled) {
),
)
.settings(publishSetting(false))
.settings(Test / test := (Test / test).dependsOn(zioHttp / publishLocal).value)
.settings(Test / test := (Test / test).dependsOn(zioHttpJVM / publishLocal).value)
} else {
(project in file(".")).settings(
Compile / sources := Nil,
Expand All @@ -217,7 +244,7 @@ lazy val zioHttpBenchmarks = (project in file("zio-http-benchmarks"))
"org.slf4j" % "slf4j-simple" % "2.0.11",
),
)
.dependsOn(zioHttp)
.dependsOn(zioHttpJVM)

lazy val zioHttpCli = (project in file("zio-http-cli"))
.settings(stdSettings("zio-http-cli"))
Expand All @@ -229,22 +256,22 @@ lazy val zioHttpCli = (project in file("zio-http-cli"))
`zio-test-sbt`,
),
)
.dependsOn(zioHttp)
.dependsOn(zioHttpJVM)
.dependsOn(zioHttpTestkit % Test)

lazy val zioHttpHtmx = (project in file("zio-http-htmx"))
.settings(
stdSettings("zio-http-htmx"),
publishSetting(true),
)
.dependsOn(zioHttp)
.dependsOn(zioHttpJVM)

lazy val zioHttpExample = (project in file("zio-http-example"))
.settings(stdSettings("zio-http-example"))
.settings(publishSetting(false))
.settings(runSettings(Debug.Main))
.settings(libraryDependencies ++= Seq(`jwt-core`))
.dependsOn(zioHttp, zioHttpCli)
.dependsOn(zioHttpJVM, zioHttpCli)

lazy val zioHttpGen = (project in file("zio-http-gen"))
.settings(stdSettings("zio-http-gen"))
Expand All @@ -257,7 +284,7 @@ lazy val zioHttpGen = (project in file("zio-http-gen"))
scalafmt.cross(CrossVersion.for3Use2_13),
),
)
.dependsOn(zioHttp)
.dependsOn(zioHttpJVM)

lazy val zioHttpTestkit = (project in file("zio-http-testkit"))
.enablePlugins(Shading.plugins(): _*)
Expand All @@ -272,7 +299,7 @@ lazy val zioHttpTestkit = (project in file("zio-http-testkit"))
`zio-test-sbt`,
),
)
.dependsOn(zioHttp)
.dependsOn(zioHttpJVM)

lazy val docs = project
.in(file("zio-http-docs"))
Expand All @@ -281,15 +308,15 @@ lazy val docs = project
scalacOptions -= "-Yno-imports",
scalacOptions -= "-Xfatal-warnings",
projectName := "ZIO Http",
mainModuleName := (zioHttp / moduleName).value,
mainModuleName := (zioHttpJVM / moduleName).value,
projectStage := ProjectStage.Development,
docsPublishBranch := "main",
ScalaUnidoc / unidoc / unidocProjectFilter := inProjects(zioHttp),
ScalaUnidoc / unidoc / unidocProjectFilter := inProjects(zioHttpJVM),
ciWorkflowName := "Continuous Integration",
libraryDependencies ++= Seq(
`jwt-core`,
"dev.zio" %% "zio-test" % ZioVersion,
),
)
.dependsOn(zioHttp)
.dependsOn(zioHttpJVM)
.enablePlugins(WebsitePlugin)
77 changes: 63 additions & 14 deletions project/BuildHelper.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sbt.Keys._
import sbt._
import scalafix.sbt.ScalafixPlugin.autoImport._
import xerial.sbt.Sonatype.autoImport._
import de.heikoseeberger.sbtheader.HeaderPlugin.autoImport.{headerLicense, HeaderLicense}
import de.heikoseeberger.sbtheader.HeaderPlugin.autoImport.{HeaderLicense, headerLicense}
import sbt.*
import sbt.Keys.*
import scalafix.sbt.ScalafixPlugin.autoImport.*
import xerial.sbt.Sonatype.autoImport.*
import sbtcrossproject.CrossPlugin.autoImport.crossProjectPlatform

object BuildHelper extends ScalaSettings {
val Scala212 = "2.12.18"
Expand Down Expand Up @@ -71,7 +72,7 @@ object BuildHelper extends ScalaSettings {
name := s"$prjName$shadedSuffix",
ThisBuild / crossScalaVersions := Seq(Scala212, Scala213, Scala3),
ThisBuild / scalaVersion := Scala213,
scalacOptions := stdOptions ++ extraOptions(scalaVersion.value),
scalacOptions ++= stdOptions ++ extraOptions(scalaVersion.value),
ThisBuild / scalafixDependencies ++=
List(
"com.github.vovapolu" %% "scaluzzi" % "0.1.23",
Expand Down Expand Up @@ -120,17 +121,65 @@ object BuildHelper extends ScalaSettings {
),
ThisBuild / developers := List(
Developer(
"tusharmath",
"Tushar Mathur",
"[email protected]",
new URL("https://github.com/tusharmath"),
"jdegoes",
"John De Goes",
"[email protected]",
url("http://degoes.net"),
),
Developer(
"amitksingh1490",
"Amit Kumar Singh",
"[email protected]",
new URL("https://github.com/amitksingh1490"),
"vigoo",
"Daniel Vigovszky",
"[email protected]",
url("https://vigoo.github.io/"),
),
Developer(
"987Nabil",
"Nabil Abdel-Hafeez",
"[email protected]",
url("https://github.com/987Nabil"),
),
),
)

def platformSpecificSources(platform: String, conf: String, baseDirectory: File)(versions: String*): Seq[File] =
for {
platform <- List("shared", platform)
version <- "scala" :: versions.toList.map("scala-" + _)
result = baseDirectory.getParentFile / platform.toLowerCase / "src" / conf / version
if result.exists
} yield result

def crossPlatformSources(scalaVer: String, platform: String, conf: String, baseDir: File): Seq[sbt.File] = {
val versions = CrossVersion.partialVersion(scalaVer) match {
case Some((2, 12)) =>
List("2.12", "2.12+", "2.12-2.13", "2.x")
case Some((2, 13)) =>
List("2.13", "2.12+", "2.13+", "2.12-2.13", "2.x")
case Some((3,_)) =>
List("3")
case _ =>
List()
}
platformSpecificSources(platform, conf, baseDir)(versions: _*)
}

lazy val crossProjectSettings = Seq(
Compile / unmanagedSourceDirectories ++= {
crossPlatformSources(
scalaVersion.value,
crossProjectPlatform.value.identifier,
"main",
baseDirectory.value
)
},
Test / unmanagedSourceDirectories ++= {
crossPlatformSources(
scalaVersion.value,
crossProjectPlatform.value.identifier,
"test",
baseDirectory.value
)
}
)

}
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object Dependencies {
val ScalaCompactCollectionVersion = "2.11.0"
val ZioVersion = "2.0.21"
val ZioCliVersion = "0.5.0"
val ZioSchemaVersion = "0.4.17"
val ZioSchemaVersion = "0.4.17+4-0acf5af7-SNAPSHOT"
val SttpVersion = "3.3.18"

val `jwt-core` = "com.github.jwt-scala" %% "jwt-core" % JwtCoreVersion
Expand Down
2 changes: 1 addition & 1 deletion project/ScoverageWorkFlow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object ScoverageWorkFlow {
name = Some("Update Build Definition"),
),
WorkflowStep.Sbt(
commands = List(s"coverage; project zioHttp; test; coverageReport"),
commands = List(s"coverage; project zioHttpJVM; test; coverageReport"),
id = Some("run_coverage"),
name = Some("Run Coverage"),
),
Expand Down
25 changes: 14 additions & 11 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.11.0")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0")
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.6")
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.6.3")
addSbtPlugin("io.spray" % "sbt-revolver" % "0.10.0")
addSbtPlugin("com.github.sbt" % "sbt-github-actions" % "0.22.0")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12")
addSbtPlugin("dev.zio" % "zio-sbt-website" % "0.3.10")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.10.0")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.9")
addSbtPlugin("io.get-coursier" % "sbt-shading" % "2.1.3")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.11.0")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0")
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.6")
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.6.3")
addSbtPlugin("io.spray" % "sbt-revolver" % "0.10.0")
addSbtPlugin("com.github.sbt" % "sbt-github-actions" % "0.22.0")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12")
addSbtPlugin("dev.zio" % "zio-sbt-website" % "0.3.10")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.10.0")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.9")
addSbtPlugin("io.get-coursier" % "sbt-shading" % "2.1.3")
addSbtPlugin("com.github.cb372" % "sbt-explicit-dependencies" % "0.3.1")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.14.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.2")
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ object CodeGen {
name

case scalaType =>
println(s"Unknown ScalaType: $scalaType")
throw new Exception(s"Unknown ScalaType: $scalaType")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package zio.http

import zio.ZLayer

trait DriverPlatformSpecific {
val default: ZLayer[Server.Config, Throwable, Driver] =
throw new UnsupportedOperationException("Not implemented for Scala.js")
}
10 changes: 10 additions & 0 deletions zio-http/js/src/main/scala/zio/http/HandlerPlatformSpecific.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package zio.http

import zio.Trace

trait HandlerPlatformSpecific {
self: Handler.type =>

def fromResource(path: String)(implicit trace: Trace): Handler[Any, Throwable, Any, Response] =
throw new UnsupportedOperationException("Not supported on Scala.js")
}
13 changes: 13 additions & 0 deletions zio-http/js/src/main/scala/zio/http/ServerPlatformSpecific.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package zio.http

import zio._

import zio.http.Server.Config

trait ServerPlatformSpecific {
val customized: ZLayer[Config, Throwable, Server] =
throw new UnsupportedOperationException("Not implemented for Scala.js")

val live: ZLayer[Config, Throwable, Server] =
throw new UnsupportedOperationException("Not implemented for Scala.js")
}
7 changes: 7 additions & 0 deletions zio-http/js/src/main/scala/zio/http/URLPlatformSpecific.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package zio.http

import scala.util.Try

trait URLPlatformSpecific {
self: URL =>
}
Loading

0 comments on commit a9a2204

Please sign in to comment.