mirror of
https://github.com/Start9Labs/registry.git
synced 2026-04-01 20:44:15 +00:00
Feature/data url images (#107)
* changes serialization scheme for v1 api * removes dependent types
This commit is contained in:
committed by
GitHub
parent
dbd73fae7f
commit
5a590f0f4d
81
src/Handler/Package/Api.hs
Normal file
81
src/Handler/Package/Api.hs
Normal file
@@ -0,0 +1,81 @@
|
||||
{-# LANGUAGE DataKinds #-}
|
||||
{-# LANGUAGE GADTs #-}
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
{-# LANGUAGE RecordWildCards #-}
|
||||
{-# LANGUAGE TypeFamilies #-}
|
||||
{-# LANGUAGE UndecidableInstances #-}
|
||||
|
||||
module Handler.Package.Api where
|
||||
|
||||
import Data.ByteString.Base64 (encodeBase64)
|
||||
import Data.HashMap.Strict
|
||||
import Data.String.Interpolate.IsString (i)
|
||||
import Handler.Types.Api (ApiResponse (..), ApiVersion (..))
|
||||
import Lib.Types.Core
|
||||
import Lib.Types.Emver
|
||||
import Startlude (
|
||||
ByteString,
|
||||
Eq,
|
||||
Generic,
|
||||
NonEmpty,
|
||||
Show,
|
||||
Text,
|
||||
snd,
|
||||
($),
|
||||
(&),
|
||||
(.),
|
||||
(<$>),
|
||||
)
|
||||
import Yesod
|
||||
|
||||
|
||||
dataUrl :: (ContentType, ByteString) -> Text
|
||||
dataUrl (ct, payload) = [i|data:#{ct};base64,#{encodeBase64 payload}|]
|
||||
|
||||
|
||||
newtype PackageListRes = PackageListRes [PackageRes]
|
||||
deriving (Generic)
|
||||
instance ApiResponse PackageListRes where
|
||||
apiEncode V0 (PackageListRes pkgs) = toJSON $ apiEncode V0 <$> pkgs
|
||||
apiEncode V1 (PackageListRes pkgs) = toJSON $ apiEncode V1 <$> pkgs
|
||||
|
||||
|
||||
data PackageRes = PackageRes
|
||||
{ packageResIcon :: !(ContentType, ByteString)
|
||||
, packageResManifest :: !Value -- PackageManifest
|
||||
, packageResCategories :: ![Text]
|
||||
, packageResInstructions :: !Text
|
||||
, packageResLicense :: !Text
|
||||
, packageResVersions :: !(NonEmpty Version)
|
||||
, packageResDependencies :: !(HashMap PkgId DependencyRes)
|
||||
}
|
||||
deriving (Show, Generic)
|
||||
|
||||
|
||||
instance ApiResponse PackageRes where
|
||||
apiEncode v PackageRes{..} =
|
||||
object
|
||||
[ "icon"
|
||||
.= ( packageResIcon & case v of
|
||||
V0 -> encodeBase64 . snd
|
||||
V1 -> dataUrl
|
||||
)
|
||||
, "license" .= packageResLicense
|
||||
, "instructions" .= packageResInstructions
|
||||
, "manifest" .= packageResManifest
|
||||
, "categories" .= packageResCategories
|
||||
, "versions" .= packageResVersions
|
||||
, "dependency-metadata" .= (apiEncode v <$> packageResDependencies)
|
||||
]
|
||||
|
||||
|
||||
data DependencyRes = DependencyRes
|
||||
{ dependencyResTitle :: !Text
|
||||
, dependencyResIcon :: !(ContentType, ByteString)
|
||||
}
|
||||
deriving (Eq, Show)
|
||||
|
||||
|
||||
instance ApiResponse DependencyRes where
|
||||
apiEncode V0 DependencyRes{..} = object ["icon" .= encodeBase64 (snd dependencyResIcon), "title" .= dependencyResTitle]
|
||||
apiEncode V1 DependencyRes{..} = object ["icon" .= dataUrl dependencyResIcon, "title" .= dependencyResTitle]
|
||||
Reference in New Issue
Block a user