• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

dariooddenino/purescript-simple-ajax: An opionated library to work with AJAX an ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

dariooddenino/purescript-simple-ajax

开源软件地址:

https://github.com/dariooddenino/purescript-simple-ajax

开源编程语言:

PureScript 90.9%

开源软件介绍:

Purescript-Simple-Ajax

An opinionated library to work with AJAX and JSON, using argonaut and variant.

Thanks to Vladimir Ciobanu for his error handling solution.

Functions

All requests have 4 versions:

  • post: Takes the request URL and some optional content and then tries to parse the response.
  • post_: Takes the request URL and some optional content, but ignores the response payload.
  • postR: Like post, but takes a subset of a SimpleRequest as an additional argument (for example if additional headers are needed).
  • postR_: Like post_, but takes a subset of a SimpleRequest as an additional argument.

POST requests also have variations that includes the response headers. (Tuple (Array ResponseHeader) b) is returned in place of b, where b is just Unit in the _ versions: postH, postH_, postRH, postRH_

Requests payload objects must implement an instance of EncodeJson and responses payload objects must implement an instance of DecodeJson.

Check argonaut-codecs documentation to learn more about this.

Requests

simpleRequest, getR, postR, postRH, putR, deleteR and patchR (and the versions ending with an underscore) accept a subset of a SimpleRequest as an argument.

type SimpleRequest = { headers         :: Array RequestHeader
                     , username        :: Maybe String
                     , password        :: Maybe String
                     , withCredentials :: Boolean
                     }

For example:

getR { withCredentials: true } "http://www.google.it"

Headers and MediaType

The default requests sets the header Accept (MediaType "application/json"). When passing a new headers array, this header should be included again or the request will fail.

Errors

The different types of error (Error, ForeignError and ResponseFormatError) are put together in a Variant.

There are two type alias:

  • HTTPError containing the common http errors and affjax Error
  • AjaxError which extends HTTPError to add json parsing errors

By using Variant's functions, it's possible to match on them:

let error = 
  default "Generic error"
  # on _notFound $ const "Not found"
  # on _badRequest identity
  # on _parseError $ intercalate ", " <<< map renderForeignError
  $ err

Example usage

import Prelude
import Data.Either (Either(..))
import Data.Maybe (Maybe(..))
import Data.Variant (default, on)
import Effect (Effect)
import Effect.Aff (launchAff_)
import Effect.Class.Console (log, logShow)
import Simple.Ajax (_unAuthorized, post)


payload :: { foo :: Int, bar :: String }
payload = { foo: 1, bar: "hello" }

type Baz = { baz :: Boolean }

main = launchAff_ $ do
  res <- post url (Just payload)
  case res of
    Left err -> do
      let error = 
            default "Generic error" 
            # on _unAuthorized (const "Not authorized") 
            $ err
      log error
    Right (res :: Baz) ->
      logShow res

NOTE: To run this in the console you will need to install xhr2 (or similar) with npm.

npm init
npm install xhr2
spago run

Module documentation

Module documentation is published on Pursuit.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap