diff --git a/project/Module.scala b/project/Module.scala index ed4fd485d..bf4f96cc5 100644 --- a/project/Module.scala +++ b/project/Module.scala @@ -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.* @@ -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") ) } } diff --git a/project/searchapi.scala b/project/searchapi.scala index 5191b2582..38f458e86 100644 --- a/project/searchapi.scala +++ b/project/searchapi.scala @@ -43,7 +43,8 @@ object searchapi extends Module { "SubjectAggregationsDTO", "SubjectAggsInputDTO", "GrepSearchInputDTO", - "grep.GrepSearchResultsDTO" + "grep.GrepSearchResultsDTO", + "grep.GrepResultDTO" ) ) diff --git a/search-api/src/main/scala/no/ndla/searchapi/model/api/grep/GrepResultDTO.scala b/search-api/src/main/scala/no/ndla/searchapi/model/api/grep/GrepResultDTO.scala index c5eb38010..a0cc254d1 100644 --- a/search-api/src/main/scala/no/ndla/searchapi/model/api/grep/GrepResultDTO.scala +++ b/search-api/src/main/scala/no/ndla/searchapi/model/api/grep/GrepResultDTO.scala @@ -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 @@ -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") @@ -48,9 +55,12 @@ 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 s1: Schema["GrepLaererplanDTO"] = Schema.string + implicit val s2: Schema["GrepTverrfagligTemaDTO"] = Schema.string + implicit val decoder: Decoder[GrepResultDTO] = List[Decoder[GrepResultDTO]]( Decoder[GrepKjerneelementDTO].widen, Decoder[GrepKompetansemaalDTO].widen, @@ -169,9 +179,11 @@ case class GrepKompetansemaalSettDTO( ) extends GrepResultDTO case class GrepLaererplanDTO( code: String, - title: TitleDTO + title: TitleDTO, + typename: "GrepLaererplanDTO" = "GrepLaererplanDTO" ) extends GrepResultDTO case class GrepTverrfagligTemaDTO( code: String, - title: TitleDTO + title: TitleDTO, + typename: "GrepTverrfagligTemaDTO" = "GrepTverrfagligTemaDTO" ) extends GrepResultDTO diff --git a/typescript/types-backend/search-api.ts b/typescript/types-backend/search-api.ts index 783450b0d..9de900a90 100644 --- a/typescript/types-backend/search-api.ts +++ b/typescript/types-backend/search-api.ts @@ -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 | IGrepTverrfagligTemaDTO | IGrepKompetansemaalDTO | IGrepLaererplanDTO) export type GrepSort = ("-relevance" | "relevance" | "-title" | "title" | "-code" | "code") @@ -122,7 +122,7 @@ export interface IGrepKjerneelementDTO { title: ITitleDTO description: IDescriptionDTO laereplan: IGrepLaererplanDTO - type: "GrepKjerneelementDTO" + typename: "GrepKjerneelementDTO" } export interface IGrepKompetansemaalDTO { @@ -132,20 +132,20 @@ 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" + typename: "GrepLaererplanDTO" } export interface IGrepReferencedKjerneelementDTO { @@ -184,6 +184,7 @@ export interface IGrepSearchResultsDTO { export interface IGrepTverrfagligTemaDTO { code: string title: ITitleDTO + typename: "GrepTverrfagligTemaDTO" } export interface IGroupSearchResultDTO {