servant-js
servant-js copied to clipboard
Fetch Api support
I like how the project is supporting multiple frameworks and plain javascript with xhr. The ˋfetchˋ api is missing. It returns promises and could in general be favored over the other implementations.
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
Is support planned, discussed or wanted?
wanted.
Not complete, but works for simple cases:
import Servant.JS
import Servant.Foreign
import Servant.JS.Internal
react :: JavaScriptGenerator
react = reactWith defCommonGeneratorOptions
reactWith :: CommonGeneratorOptions -> JavaScriptGenerator
reactWith opts = mconcat . map (generateReactJSWith opts)
generateReactJSWith :: CommonGeneratorOptions -> AjaxReq -> T.Text
generateReactJSWith opts req =
"export function " <> fname <> "(" <> argsStr <> ") {\n" <>
" return fetch(" <> url <> ");\n" <>
"}\n\n"
where
argsStr = T.intercalate ", " args
args = captures
++ map (view $ queryArgName . argPath) queryparams
-- ++ body
++ map ( toValidFunctionName
. (<>) "header"
. view (headerArg . argPath)
) hs
captures = map (view argPath . captureArg)
. filter isCapture
$ req ^. reqUrl.path
hs = req ^. reqHeaders
--body = if isJust(req ^. reqBody)
-- then [requestBody opts]
-- else []
fname = (functionNameBuilder opts $ req ^. reqFuncName)
method = req ^. reqMethod
queryparams = req ^.. reqUrl.queryStr.traverse
url = if url' == "'" then "'/'" else url'
url' = "'"
<> urlPrefix opts
<> urlArgs
<> queryArgs
urlArgs = jsSegments
$ req ^.. reqUrl.path.traverse
queryArgs = if null queryparams
then ""
else " + '?" <> jsParams queryparams
i made a PR adding basic support for fetch
What's the current status of this issue? I noticed the PR (#28) was closed...