Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch Api support #26

Open
andys8 opened this issue Mar 29, 2018 · 4 comments
Open

Fetch Api support #26

andys8 opened this issue Mar 29, 2018 · 4 comments

Comments

@andys8
Copy link
Contributor

andys8 commented Mar 29, 2018

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?

@phadej
Copy link
Contributor

phadej commented Mar 30, 2018

wanted.

@xaviershay
Copy link

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

@shrynx
Copy link

shrynx commented Jul 30, 2018

i made a PR adding basic support for fetch

@declension
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants