Skip to content

Commit

Permalink
maybe?
Browse files Browse the repository at this point in the history
  • Loading branch information
jnatten committed Jan 7, 2025
1 parent ac31bae commit c2dd38a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
10 changes: 6 additions & 4 deletions project/Module.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import GithubWorkflowPlugin.autoImport.*
import com.scalatsi.plugin.ScalaTsiPlugin.autoImport.{
typescriptExports,
typescriptGenerationImports,
typescriptOutputFile
typescriptOutputFile,
typescriptTaggedUnionDiscriminator
}
import org.scalafmt.sbt.ScalafmtPlugin.autoImport.*
import org.typelevel.sbt.tpolecat.TpolecatPlugin.autoImport.*
Expand Down Expand Up @@ -192,9 +193,10 @@ trait Module {

protected def typescriptSettings(imports: Seq[String], exports: Seq[String]) = {
Seq(
typescriptGenerationImports := imports,
typescriptExports := exports,
typescriptOutputFile := file("./typescript/types-backend") / s"${this.moduleName}.ts"
typescriptGenerationImports := imports,
typescriptExports := exports,
typescriptOutputFile := file("./typescript/types-backend") / s"${this.moduleName}.ts",
typescriptTaggedUnionDiscriminator := Some("typename")
)
}
}
3 changes: 2 additions & 1 deletion project/searchapi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ object searchapi extends Module {
"SubjectAggregationsDTO",
"SubjectAggsInputDTO",
"GrepSearchInputDTO",
"grep.GrepSearchResultsDTO"
"grep.GrepSearchResultsDTO",
"grep.GrepResultDTO"
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
package no.ndla.searchapi.model.api.grep

import cats.implicits.*
import com.scalatsi.TSType.fromCaseClass
import com.scalatsi.TypescriptType.{TSLiteralString, TSString, TSUnion}
import com.scalatsi.{TSIType, TSNamedType, TSType}
import com.scalatsi.dsl.*
import io.circe.generic.auto.*
import sttp.tapir.generic.auto.*

import scala.reflect.runtime.universe.*
import io.circe.syntax.*
import io.circe.{Decoder, Encoder, Json}
import no.ndla.language.Language
Expand All @@ -29,6 +35,7 @@ import no.ndla.searchapi.model.search.SearchableGrepElement
import sttp.tapir.Schema
import sttp.tapir.Schema.annotations.description

import scala.reflect.ClassTag
import scala.util.{Success, Try}

@description("Information about a single grep search result entry")
Expand All @@ -48,9 +55,11 @@ object GrepResultDTO {
}
// NOTE: Adding the discriminator field that scala-tsi generates in the typescript type.
// Useful for guarding the type of the object in the frontend.
json.mapObject(_.add("type", Json.fromString(result.getClass.getSimpleName)))
json.mapObject(_.add("typename", Json.fromString(result.getClass.getSimpleName)))
}

implicit val s2: Schema["GrepTverrfagligTemaDTO"] = Schema.string

implicit val decoder: Decoder[GrepResultDTO] = List[Decoder[GrepResultDTO]](
Decoder[GrepKjerneelementDTO].widen,
Decoder[GrepKompetansemaalDTO].widen,
Expand Down Expand Up @@ -173,5 +182,8 @@ case class GrepLaererplanDTO(
) extends GrepResultDTO
case class GrepTverrfagligTemaDTO(
code: String,
title: TitleDTO
title: TitleDTO,
// NOTE: Hack to add the discriminator field because scala-tsi ignores this class for some reason.
// Seems like a scala-tsi bug?
typename: "GrepTverrfagligTemaDTO" = "GrepTverrfagligTemaDTO"
) extends GrepResultDTO
10 changes: 5 additions & 5 deletions typescript/types-backend/search-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// DO NOT EDIT: generated file by scala-tsi

export type GrepResultDTO = (IGrepKompetansemaalDTO | IGrepKjerneelementDTO | IGrepLaererplanDTO | IGrepTverrfagligTemaDTO | IGrepKompetansemaalSettDTO)
export type GrepResultDTO = (IGrepKjerneelementDTO | IGrepKompetansemaalSettDTO | IGrepLaererplanDTO | IGrepKompetansemaalDTO | IGrepTverrfagligTemaDTO)

export type GrepSort = ("-relevance" | "relevance" | "-title" | "title" | "-code" | "code")

Expand Down Expand Up @@ -122,7 +122,7 @@ export interface IGrepKjerneelementDTO {
title: ITitleDTO
description: IDescriptionDTO
laereplan: IGrepLaererplanDTO
type: "GrepKjerneelementDTO"
typename: "GrepKjerneelementDTO"
}

export interface IGrepKompetansemaalDTO {
Expand All @@ -132,20 +132,19 @@ export interface IGrepKompetansemaalDTO {
kompetansemaalSett: IGrepReferencedKompetansemaalSettDTO
tverrfagligeTemaer: IGrepTverrfagligTemaDTO[]
kjerneelementer: IGrepReferencedKjerneelementDTO[]
type: "GrepKompetansemaalDTO"
typename: "GrepKompetansemaalDTO"
}

export interface IGrepKompetansemaalSettDTO {
code: string
title: ITitleDTO
kompetansemaal: IGrepReferencedKompetansemaalDTO[]
type: "GrepKompetansemaalSettDTO"
typename: "GrepKompetansemaalSettDTO"
}

export interface IGrepLaererplanDTO {
code: string
title: ITitleDTO
type: "GrepLaererplanDTO"
}

export interface IGrepReferencedKjerneelementDTO {
Expand Down Expand Up @@ -184,6 +183,7 @@ export interface IGrepSearchResultsDTO {
export interface IGrepTverrfagligTemaDTO {
code: string
title: ITitleDTO
typename: "GrepTverrfagligTemaDTO"
}

export interface IGroupSearchResultDTO {
Expand Down

0 comments on commit c2dd38a

Please sign in to comment.