Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Commit

Permalink
Add separate Share and Local Api modules
Browse files Browse the repository at this point in the history
  • Loading branch information
hojberg committed Apr 6, 2022
1 parent 8f7fc11 commit e7ec23d
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 262 deletions.
219 changes: 0 additions & 219 deletions src/Api.elm

This file was deleted.

12 changes: 2 additions & 10 deletions src/Env.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module Env exposing (..)

import Api exposing (ApiBasePath(..))
import Browser.Navigation as Nav
import Code.CodebaseApi as CodebaseApi
import Code.Config
import Code.Perspective exposing (Perspective)
import Lib.Api
import Lib.Api exposing (ApiBasePath(..))
import Lib.OperatingSystem as OS exposing (OperatingSystem)


Expand Down Expand Up @@ -37,15 +36,8 @@ init flags navKey perspective =

toCodeConfig : CodebaseApi.ToApiEndpointUrl -> Env -> Code.Config.Config
toCodeConfig toApiEndpointUrl env =
let
(ApiBasePath path) =
env.apiBasePath

apiBasePath =
Lib.Api.ApiBasePath path
in
{ operatingSystem = env.operatingSystem
, perspective = env.perspective
, toApiEndpointUrl = toApiEndpointUrl
, apiBasePath = apiBasePath
, apiBasePath = env.apiBasePath
}
101 changes: 101 additions & 0 deletions src/UnisonLocal/Api.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
module UnisonLocal.Api exposing
( codebaseApiEndpointToEndpointUrl
, codebaseHash
, namespace
)

import Code.CodebaseApi as CodebaseApi
import Code.EntityId as EntityId
import Code.FullyQualifiedName as FQN exposing (FQN)
import Code.Hash as Hash exposing (Hash)
import Code.Perspective as Perspective exposing (Perspective(..))
import Code.Syntax as Syntax
import Lib.Api as Api exposing (EndpointUrl(..))
import Regex
import Url.Builder exposing (QueryParameter, int, string)


codebaseHash : EndpointUrl
codebaseHash =
EndpointUrl [ "list" ] [ string "namespace" "." ]


namespace : Perspective -> FQN -> EndpointUrl
namespace perspective fqn =
let
queryParams =
[ rootBranch (Perspective.codebaseHash perspective) ]
in
EndpointUrl [ "namespaces", FQN.toString fqn ] queryParams


codebaseApiEndpointToEndpointUrl : CodebaseApi.CodebaseEndpoint -> EndpointUrl
codebaseApiEndpointToEndpointUrl cbEndpoint =
case cbEndpoint of
CodebaseApi.Find { perspective, withinFqn, limit, sourceWidth, query } ->
let
params =
case withinFqn of
Just fqn ->
[ rootBranch (Perspective.codebaseHash perspective), relativeTo fqn ]

Nothing ->
perspectiveToQueryParams perspective

width =
case sourceWidth of
Syntax.Width w ->
w
in
EndpointUrl
[ "find" ]
([ int "limit" limit
, int "renderWidth" width
, string "query" query
]
++ params
)

CodebaseApi.Browse { perspective, namespaceId } ->
let
namespace_ =
namespaceId |> Maybe.map EntityId.toString |> Maybe.withDefault "."
in
EndpointUrl [ "list" ] (string "namespace" namespace_ :: perspectiveToQueryParams perspective)

CodebaseApi.Definition { perspective, definitionId } ->
let
re =
Maybe.withDefault Regex.never (Regex.fromString "#[d|a|](\\d+)$")

stripConstructorPositionFromHash =
Regex.replace re (always "")
in
[ EntityId.toString definitionId ]
|> List.map stripConstructorPositionFromHash
|> List.map (string "names")
|> (\names -> EndpointUrl [ "getDefinition" ] (names ++ perspectiveToQueryParams perspective))



-- QUERY PARAMS ---------------------------------------------------------------


perspectiveToQueryParams : Perspective -> List QueryParameter
perspectiveToQueryParams perspective =
case perspective of
Codebase h ->
[ rootBranch h ]

Namespace d ->
[ rootBranch d.codebaseHash, relativeTo d.fqn ]


rootBranch : Hash -> QueryParameter
rootBranch hash =
string "rootBranch" (hash |> Hash.toString)


relativeTo : FQN -> QueryParameter
relativeTo fqn =
string "relativeTo" (fqn |> FQN.toString)
Loading

0 comments on commit e7ec23d

Please sign in to comment.