|
7 | 7 |
|
8 | 8 | A library taking advantage of [`aff`](https://github.com/purescript-contrib/purescript-aff) to enable pain-free asynchronous AJAX requests and response handling. |
9 | 9 |
|
10 | | -## Installation |
11 | | - |
12 | | -Install `affjax` with [Spago](https://github.com/purescript/spago): |
13 | | - |
14 | | -```sh |
15 | | -spago install affjax |
16 | | -``` |
17 | | - |
18 | | -If you are using `affjax` in a Node.js setting you will also need to install an additional NPM dependency: |
19 | | - |
20 | | -``` |
21 | | -npm install xhr2 |
22 | | -``` |
23 | | - |
24 | | -## Quick start |
25 | | - |
26 | | -You can construct requests with the `request` function: |
27 | | - |
28 | | -```purescript |
29 | | -module Main where |
30 | | -
|
31 | | -import Prelude |
32 | | -
|
33 | | -import Affjax as AX |
34 | | -import Affjax.ResponseFormat as ResponseFormat |
35 | | -import Data.Argonaut.Core (stringify, fromString) |
36 | | -import Data.Either (Either(..)) |
37 | | -import Data.HTTP.Method (Method(..)) |
38 | | -import Effect.Aff (launchAff) |
39 | | -import Effect.Class.Console (log) |
40 | | -
|
41 | | -main = void $ launchAff $ do |
42 | | - result <- AX.request (AX.defaultRequest { url = "/api", method = Left GET, responseFormat = ResponseFormat.json }) |
43 | | - case result of |
44 | | - Left err -> log $ "GET /api response failed to decode: " <> AX.printError err |
45 | | - Right response -> log $ "GET /api response: " <> stringify response.body |
46 | | -``` |
47 | | - |
48 | | -(`defaultRequest` is a record value that has all the required fields pre-set for convenient overriding when making a request.) |
49 | | - |
50 | | -There are also a number of helpers for common `get`, `post`, `put`, `delete`, and `patch` cases: |
51 | | - |
52 | | -```purescript |
53 | | -import Affjax.RequestBody as RequestBody |
54 | | -import Data.Maybe (Maybe(..)) |
55 | | -import Effect.Aff (launchAff_) |
56 | | -
|
57 | | -main = launchAff_ do |
58 | | - result1 <- AX.get ResponseFormat.json "/api" |
59 | | - case result1 of |
60 | | - Left err -> log $ "GET /api response failed to decode: " <> AX.printError err |
61 | | - Right response -> log $ "GET /api response: " <> stringify response.body |
62 | | -
|
63 | | - result2 <- AX.post ResponseFormat.json "/api" (Just (RequestBody.json (fromString "test"))) |
64 | | - case result2 of |
65 | | - Left err -> log $ "POST /api response failed to decode: " <> AX.printError err |
66 | | - Right response -> log $ "POST /api response: " <> stringify response.body |
67 | | -``` |
| 10 | +This library provides types and common functionality that work across environments (e.g. Node, browser), but **it does not work out-of-box**. Rather, use the environment-specific library instead: |
| 11 | +- Browser environment: [`purescript-affjax-web`](https://github.com/purescript-contrib/purescript-affjax-web) |
| 12 | +- Node environment: [`purescript-affjax-node`](https://github.com/purescript-contrib/purescript-affjax-node) |
68 | 13 |
|
69 | 14 | ## Documentation |
70 | 15 |
|
|
0 commit comments