servant-js icon indicating copy to clipboard operation
servant-js copied to clipboard

Fetch Api support

Open andys8 opened this issue 7 years ago • 4 comments

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?

andys8 avatar Mar 29 '18 00:03 andys8

wanted.

phadej avatar Mar 30 '18 18:03 phadej

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

xaviershay avatar Apr 26 '18 21:04 xaviershay

i made a PR adding basic support for fetch

shrynx avatar Jul 30 '18 20:07 shrynx

What's the current status of this issue? I noticed the PR (#28) was closed...

declension avatar Mar 25 '19 09:03 declension