-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathAPI.purs
147 lines (119 loc) · 4.26 KB
/
API.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
module Try.API
( ErrorPosition(..)
, CompilerError(..)
, CompileError(..)
, CompileWarning(..)
, Suggestion(..)
, SuccessResult(..)
, FailedResult(..)
, CompileResult(..)
, get
, compile
) where
import Prelude
import Affjax (URL, printError)
import Affjax as AX
import Affjax.RequestBody as AXRB
import Affjax.ResponseFormat as AXRF
import Affjax.StatusCode (StatusCode(..))
import Control.Alt ((<|>))
import Control.Monad.Except (ExceptT(..), runExcept)
import Data.Either (Either(..))
import Data.Generic.Rep (class Generic)
import Data.List.NonEmpty (NonEmptyList)
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Foreign (ForeignError, unsafeToForeign)
import Foreign.Class (class Decode, decode)
import Foreign.Generic (defaultOptions, genericDecode)
import Foreign.Generic.Class (Options, SumEncoding(..))
decodingOptions :: Options
decodingOptions = defaultOptions { unwrapSingleConstructors = true }
-- | The range of text associated with an error
newtype ErrorPosition = ErrorPosition
{ startLine :: Int
, endLine :: Int
, startColumn :: Int
, endColumn :: Int
}
derive instance genericErrorPosition :: Generic ErrorPosition _
instance decodeErrorPosition :: Decode ErrorPosition where
decode = genericDecode decodingOptions
newtype CompilerError = CompilerError
{ message :: String
, position :: Maybe ErrorPosition
}
derive instance genericCompilerError :: Generic CompilerError _
instance decodeCompilerError :: Decode CompilerError where
decode = genericDecode decodingOptions
-- | An error reported from the compile API.
data CompileError
= CompilerErrors (Array CompilerError)
| OtherError String
derive instance genericCompileError :: Generic CompileError _
instance decodeCompileError :: Decode CompileError where
decode = genericDecode
(defaultOptions
{ sumEncoding =
TaggedObject
{ tagFieldName: "tag"
, contentsFieldName: "contents"
, constructorTagTransform: identity
}
})
newtype Suggestion = Suggestion
{ replacement :: String
, replaceRange :: Maybe ErrorPosition
}
derive instance genericSuggestion :: Generic Suggestion _
instance decodeSuggestion :: Decode Suggestion where
decode = genericDecode decodingOptions
newtype CompileWarning = CompileWarning
{ errorCode :: String
, message :: String
, position :: Maybe ErrorPosition
, suggestion :: Maybe Suggestion
}
derive instance genericCompileWarning :: Generic CompileWarning _
instance decodeCompileWarning :: Decode CompileWarning where
decode = genericDecode decodingOptions
newtype SuccessResult = SuccessResult
{ js :: String
, warnings :: Maybe (Array CompileWarning)
}
derive instance genericSuccessResult :: Generic SuccessResult _
instance decodeSuccessResult :: Decode SuccessResult where
decode = genericDecode decodingOptions
newtype FailedResult = FailedResult
{ error :: CompileError }
derive instance genericFailedResult :: Generic FailedResult _
instance decodeFailedResult :: Decode FailedResult where
decode = genericDecode decodingOptions
-- | The result of calling the compile API.
data CompileResult
= CompileSuccess SuccessResult
| CompileFailed FailedResult
-- | Parse the result from the compile API and verify it
instance decodeCompileResult :: Decode CompileResult where
decode f =
CompileSuccess <$> genericDecode decodingOptions f
<|> CompileFailed <$> genericDecode decodingOptions f
get :: URL -> ExceptT String Aff String
get url = ExceptT $ AX.get AXRF.string url >>= case _ of
Left e ->
pure $ Left $ printError e
Right { status } | status >= StatusCode 400 ->
pure $ Left $ "Received error status code: " <> show status
Right { body } ->
pure $ Right body
-- | POST the specified code to the Try PureScript API, and wait for a response.
compile :: String -> String -> ExceptT String Aff (Either (NonEmptyList ForeignError) CompileResult)
compile endpoint code = ExceptT $ AX.post AXRF.json (endpoint <> "/compile") (Just requestBody) >>= case _ of
Left e ->
pure $ Left $ printError e
Right { status } | status >= StatusCode 400 ->
pure $ Left $ "Received error status code: " <> show status
Right { body } ->
pure $ Right $ runExcept (decode (unsafeToForeign body))
where
requestBody = AXRB.String code