git.iko.soy/rowtype-yoga/purescript-yoga-fetch
on main help sign in
Initial commit
commit8b2d582f3006c636448840fc1487780405fe834d authorMark Eibes <mark.eibes@gmail.com> dateTue 10 May 2022, 11:38 (4 years ago)
21 files changed+636
.github/workflows/ci.yml+37 .gitignore+9 .tidyrc.json+10 LICENCE/purescript-milkis.LICENSE+21 LICENSE+21 README.md+16 bower.json+26 package-lock.json+144 package.json+17 packages.dhall+5 spago.dhall+18 src/Fetch.js+25 src/Fetch.purs+173 src/Fetch/Impl/Impl.purs+3 src/Fetch/Impl/Node.js+1 src/Fetch/Impl/Node.purs+7 src/Fetch/Impl/Window.js+1 src/Fetch/Impl/Window.purs+7 test.dhall+9 test/Main.js+3 test/Main.purs+83
.github/workflows/ci.ymlnew file+37
@@ -0,0 +1,37 @@
1+name: CI
2+
3+on:
4+ push:
5+ branches: [main]
6+ pull_request:
7+ branches: [main]
8+
9+jobs:
10+ build:
11+ runs-on: ubuntu-latest
12+ steps:
13+ - uses: actions/checkout@v2
14+
15+ - uses: purescript-contrib/setup-purescript@main
16+
17+ - uses: actions/setup-node@v2
18+ with:
19+ node-version: "14"
20+
21+ - name: Cache PureScript dependencies
22+ uses: actions/cache@v2
23+ # This cache uses the .dhall files to know when it should reinstall
24+ # and rebuild packages. It caches both the installed packages from
25+ # the `.spago` directory and compilation artifacts from the `output`
26+ # directory. When restored the compiler will rebuild any files that
27+ # have changed. If you do not want to cache compiled output, remove
28+ # the `output` path.
29+ with:
30+ key: ${{ runner.os }}-spago-${{ hashFiles('**/*.dhall') }}
31+ path: |
32+ .spago
33+ output
34+
35+ - run: spago build
36+
37+ - run: spago -x test.dhall test
.gitignorenew file+9
@@ -0,0 +1,9 @@
1+/bower_components/
2+/node_modules/
3+/output/
4+/.psc-package/
5+/.psc*
6+/.purs*
7+/.psa*
8+/.spago/
9+.psc-ide-port
.tidyrc.jsonnew file+10
@@ -0,0 +1,10 @@
1+{
2+ "importSort": "source",
3+ "importWrap": "source",
4+ "indent": 2,
5+ "operatorsFile": null,
6+ "ribbon": 1,
7+ "typeArrowPlacement": "last",
8+ "unicode": "always",
9+ "width": 80
10+}
LICENCE/purescript-milkis.LICENSEnew file+21
@@ -0,0 +1,21 @@
1+The MIT License (MIT)
2+
3+Copyright (c) 2017 Justin Woo, Vincent, Welkin, sterd, Alex Kelley, Rightfold
4+
5+Permission is hereby granted, free of charge, to any person obtaining a copy
6+of this software and associated documentation files (the "Software"), to deal
7+in the Software without restriction, including without limitation the rights
8+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+copies of the Software, and to permit persons to whom the Software is
10+furnished to do so, subject to the following conditions:
11+
12+The above copyright notice and this permission notice shall be included in all
13+copies or substantial portions of the Software.
14+
15+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+SOFTWARE.
LICENSEnew file+21
@@ -0,0 +1,21 @@
1+The MIT License (MIT)
2+
3+Copyright (c) 2022 Mark Eibes
4+
5+Permission is hereby granted, free of charge, to any person obtaining a copy
6+of this software and associated documentation files (the "Software"), to deal
7+in the Software without restriction, including without limitation the rights
8+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+copies of the Software, and to permit persons to whom the Software is
10+furnished to do so, subject to the following conditions:
11+
12+The above copyright notice and this permission notice shall be included in all
13+copies or substantial portions of the Software.
14+
15+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+SOFTWARE.
README.mdnew file+16
@@ -0,0 +1,16 @@
1+# 📬 purescript-fetch
2+
3+A simple wrapper for the JavaScript fetch API.
4+
5+**Note**: This is a fork of [milkis](https://github.com/justinwoo/purescript-milkis) ([MIT Licence](./LICENSE/milkis.LICENSE)).
6+
7+## Table of Contents
8+* [usage](#usage)
9+* [migrate from `milkis`](#migrate-from-purescript-milkis)
10+
11+## Usage
12+
13+
14+## Migrate from `purescript-milkis`
15+
16+`purescript-fetch` is a drop-in replacement for `purescript-milkis`. Just change the imports from `Milkis` to `Fetch`.
bower.jsonnew file+26
@@ -0,0 +1,26 @@
1+{
2+ "name": "purescript-fetch",
3+ "ignore": [
4+ "**/.*",
5+ "node_modules",
6+ "bower_components",
7+ "output",
8+ "test",
9+ "tests"
10+ ],
11+ "dependencies": {
12+ "purescript-prelude": "^6.0.0",
13+ "purescript-aff-promise": "^4.0.0",
14+ "purescript-typelevel-prelude": "^7.0.0",
15+ "purescript-foreign-object": "^4.0.0",
16+ "purescript-arraybuffer-types": "^4.0.0"
17+ },
18+ "devDependencies": {
19+ "purescript-spec": "^7.0.0"
20+ },
21+ "repository": {
22+ "type": "git",
23+ "url": "git://github.com/rowtype-yoga/purescript-fetch.git"
24+ },
25+ "license": "MIT"
26+}
package-lock.jsonnew file+144
@@ -0,0 +1,144 @@
1+{
2+ "name": "purescript-fetch",
3+ "version": "1.0.0",
4+ "lockfileVersion": 2,
5+ "requires": true,
6+ "packages": {
7+ "": {
8+ "name": "purescript-fetch",
9+ "version": "1.0.0",
10+ "license": "MIT",
11+ "dependencies": {
12+ "node-fetch": "^3.2.4"
13+ }
14+ },
15+ "node_modules/data-uri-to-buffer": {
16+ "version": "4.0.0",
17+ "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.0.tgz",
18+ "integrity": "sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA==",
19+ "engines": {
20+ "node": ">= 12"
21+ }
22+ },
23+ "node_modules/fetch-blob": {
24+ "version": "3.1.5",
25+ "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.1.5.tgz",
26+ "integrity": "sha512-N64ZpKqoLejlrwkIAnb9iLSA3Vx/kjgzpcDhygcqJ2KKjky8nCgUQ+dzXtbrLaWZGZNmNfQTsiQ0weZ1svglHg==",
27+ "funding": [
28+ {
29+ "type": "github",
30+ "url": "https://github.com/sponsors/jimmywarting"
31+ },
32+ {
33+ "type": "paypal",
34+ "url": "https://paypal.me/jimmywarting"
35+ }
36+ ],
37+ "dependencies": {
38+ "node-domexception": "^1.0.0",
39+ "web-streams-polyfill": "^3.0.3"
40+ },
41+ "engines": {
42+ "node": "^12.20 || >= 14.13"
43+ }
44+ },
45+ "node_modules/formdata-polyfill": {
46+ "version": "4.0.10",
47+ "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
48+ "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==",
49+ "dependencies": {
50+ "fetch-blob": "^3.1.2"
51+ },
52+ "engines": {
53+ "node": ">=12.20.0"
54+ }
55+ },
56+ "node_modules/node-domexception": {
57+ "version": "1.0.0",
58+ "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
59+ "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==",
60+ "funding": [
61+ {
62+ "type": "github",
63+ "url": "https://github.com/sponsors/jimmywarting"
64+ },
65+ {
66+ "type": "github",
67+ "url": "https://paypal.me/jimmywarting"
68+ }
69+ ],
70+ "engines": {
71+ "node": ">=10.5.0"
72+ }
73+ },
74+ "node_modules/node-fetch": {
75+ "version": "3.2.4",
76+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.2.4.tgz",
77+ "integrity": "sha512-WvYJRN7mMyOLurFR2YpysQGuwYrJN+qrrpHjJDuKMcSPdfFccRUla/kng2mz6HWSBxJcqPbvatS6Gb4RhOzCJw==",
78+ "dependencies": {
79+ "data-uri-to-buffer": "^4.0.0",
80+ "fetch-blob": "^3.1.4",
81+ "formdata-polyfill": "^4.0.10"
82+ },
83+ "engines": {
84+ "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
85+ },
86+ "funding": {
87+ "type": "opencollective",
88+ "url": "https://opencollective.com/node-fetch"
89+ }
90+ },
91+ "node_modules/web-streams-polyfill": {
92+ "version": "3.2.1",
93+ "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz",
94+ "integrity": "sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==",
95+ "engines": {
96+ "node": ">= 8"
97+ }
98+ }
99+ },
100+ "dependencies": {
101+ "data-uri-to-buffer": {
102+ "version": "4.0.0",
103+ "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.0.tgz",
104+ "integrity": "sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA=="
105+ },
106+ "fetch-blob": {
107+ "version": "3.1.5",
108+ "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.1.5.tgz",
109+ "integrity": "sha512-N64ZpKqoLejlrwkIAnb9iLSA3Vx/kjgzpcDhygcqJ2KKjky8nCgUQ+dzXtbrLaWZGZNmNfQTsiQ0weZ1svglHg==",
110+ "requires": {
111+ "node-domexception": "^1.0.0",
112+ "web-streams-polyfill": "^3.0.3"
113+ }
114+ },
115+ "formdata-polyfill": {
116+ "version": "4.0.10",
117+ "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
118+ "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==",
119+ "requires": {
120+ "fetch-blob": "^3.1.2"
121+ }
122+ },
123+ "node-domexception": {
124+ "version": "1.0.0",
125+ "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
126+ "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ=="
127+ },
128+ "node-fetch": {
129+ "version": "3.2.4",
130+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.2.4.tgz",
131+ "integrity": "sha512-WvYJRN7mMyOLurFR2YpysQGuwYrJN+qrrpHjJDuKMcSPdfFccRUla/kng2mz6HWSBxJcqPbvatS6Gb4RhOzCJw==",
132+ "requires": {
133+ "data-uri-to-buffer": "^4.0.0",
134+ "fetch-blob": "^3.1.4",
135+ "formdata-polyfill": "^4.0.10"
136+ }
137+ },
138+ "web-streams-polyfill": {
139+ "version": "3.2.1",
140+ "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz",
141+ "integrity": "sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q=="
142+ }
143+ }
144+}
package.jsonnew file+17
@@ -0,0 +1,17 @@
1+{
2+ "name": "purescript-fetch",
3+ "version": "1.0.0",
4+ "description": "",
5+ "main": "index.js",
6+ "directories": {
7+ "test": "test"
8+ },
9+ "scripts": {
10+ "build": "spago build",
11+ "test": "spago -x test.dhall test"
12+ },
13+ "license": "MIT",
14+ "dependencies": {
15+ "node-fetch": "^3.2.4"
16+ }
17+}
packages.dhallnew file+5
@@ -0,0 +1,5 @@
1+let upstream =
2+ https://github.com/purescript/package-sets/releases/download/psc-0.15.0-20220429/packages.dhall
3+ sha256:03c682bff56fc8f9d8c495ffcc6f524cbd3c89fe04778f965265c08757de8c9d
4+
5+in upstream
spago.dhallnew file+18
@@ -0,0 +1,18 @@
1+{ name = "purescript-fetch"
2+, dependencies =
3+ [ "aff"
4+ , "aff-promise"
5+ , "arraybuffer-types"
6+ , "effect"
7+ , "either"
8+ , "foreign"
9+ , "foreign-object"
10+ , "newtype"
11+ , "prelude"
12+ , "strings"
13+ , "typelevel-prelude"
14+ , "unsafe-coerce"
15+ ]
16+, packages = ./packages.dhall
17+, sources = [ "src/**/*.purs" ]
18+}
src/Fetch.jsnew file+25
@@ -0,0 +1,25 @@
1+export const _fetch = fetchImpl => url => options => () => {
2+ return fetchImpl(url, options).catch(function(e) {
3+ // we have to wrap node-fetch's non-Error error :(
4+ throw new Error(e);
5+ });
6+};
7+
8+export const jsonImpl = response => () => {
9+ return response.json().catch(function(e) {
10+ throw new Error(e);
11+ });
12+};
13+
14+export const textImpl = response => () => response.text()
15+
16+export const headersImpl = response => {
17+ var result = {};
18+ for (const [key, value] of response.headers.entries()) {
19+ result[key] = value
20+ }
21+ return result;
22+};
23+
24+
25+export const arrayBufferImpl = response => () => response.arrayBuffer()
src/Fetch.pursnew file+173
@@ -0,0 +1,173 @@
1+module Fetch
2+ ( URL(..)
3+ , Fetch
4+ , Response
5+ , Options
6+ , Method
7+ , Headers
8+ , Redirect
9+ , Credentials
10+ , defaultFetchOptions
11+ , getMethod
12+ , postMethod
13+ , putMethod
14+ , patchMethod
15+ , deleteMethod
16+ , headMethod
17+ , redirectError
18+ , redirectFollow
19+ , redirectManual
20+ , omitCredentials
21+ , sameOriginCredentials
22+ , includeCredentials
23+ , fetch
24+ , json
25+ , text
26+ , headers
27+ , arrayBuffer
28+ , makeHeaders
29+ , statusCode
30+ , url
31+ ) where
32+
33+import Type.Row.Homogeneous
34+
35+import Control.Promise (Promise, toAffE)
36+import Data.ArrayBuffer.Types (ArrayBuffer)
37+import Data.Newtype (class Newtype)
38+import Effect (Effect)
39+import Effect.Aff (Aff)
40+import Foreign (Foreign)
41+import Foreign.Object (Object)
42+import Foreign.Object as Object
43+import Fetch.Impl (FetchImpl)
44+import Prelude (class Eq, class Show, ($))
45+import Type.Row (class Union)
46+import Unsafe.Coerce (unsafeCoerce)
47+
48+-- | Create a map from a homogeneous record (all attributes have the same type).
49+fromRecord ∷ ∀ r t. Homogeneous r t ⇒ Record r → Object.Object t
50+fromRecord = unsafeCoerce
51+
52+newtype URL = URL String
53+
54+derive instance Newtype URL _
55+derive newtype instance Show URL
56+derive instance Eq URL
57+
58+type Fetch =
59+ ∀ options trash.
60+ Union options trash Options ⇒
61+ URL →
62+ Record (method ∷ Method | options) →
63+ Aff Response
64+
65+type Options =
66+ ( method ∷ Method
67+ , body ∷ String
68+ , headers ∷ Headers
69+ , credentials ∷ Credentials
70+ , follow ∷ Int
71+ , redirect ∷ Redirect
72+ )
73+
74+-- | See <https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials>.
75+foreign import data Credentials ∷ Type
76+
77+omitCredentials ∷ Credentials
78+omitCredentials = unsafeCoerce "omit"
79+
80+sameOriginCredentials ∷ Credentials
81+sameOriginCredentials = unsafeCoerce "same-origin"
82+
83+includeCredentials ∷ Credentials
84+includeCredentials = unsafeCoerce "include"
85+
86+foreign import data Method ∷ Type
87+
88+getMethod ∷ Method
89+getMethod = unsafeCoerce "GET"
90+
91+postMethod ∷ Method
92+postMethod = unsafeCoerce "POST"
93+
94+putMethod ∷ Method
95+putMethod = unsafeCoerce "PUT"
96+
97+patchMethod ∷ Method
98+patchMethod = unsafeCoerce "PATCH"
99+
100+deleteMethod ∷ Method
101+deleteMethod = unsafeCoerce "DELETE"
102+
103+headMethod ∷ Method
104+headMethod = unsafeCoerce "HEAD"
105+
106+foreign import data Redirect ∷ Type
107+
108+redirectError ∷ Redirect
109+redirectError = unsafeCoerce "error"
110+
111+redirectFollow ∷ Redirect
112+redirectFollow = unsafeCoerce "follow"
113+
114+redirectManual ∷ Redirect
115+redirectManual = unsafeCoerce "manual"
116+
117+type Headers = Object.Object String
118+
119+makeHeaders ∷
120+ ∀ r.
121+ Homogeneous r String ⇒
122+ Record r →
123+ Headers
124+makeHeaders = fromRecord
125+
126+defaultFetchOptions ∷ { method ∷ Method }
127+defaultFetchOptions =
128+ { method: getMethod
129+ }
130+
131+fetch ∷ FetchImpl → Fetch
132+fetch impl url' opts = toAffE $ _fetch impl url' opts
133+
134+json ∷ Response → Aff Foreign
135+json res = toAffE (jsonImpl res)
136+
137+text ∷ Response → Aff String
138+text res = toAffE (textImpl res)
139+
140+headers ∷ Response → Object String
141+headers = headersImpl
142+
143+arrayBuffer ∷ Response → Aff ArrayBuffer
144+arrayBuffer res = toAffE (arrayBufferImpl res)
145+
146+statusCode ∷ Response → Int
147+statusCode response = response'.status
148+ where
149+ response' ∷ { status ∷ Int }
150+ response' = unsafeCoerce response
151+
152+url ∷ Response → URL
153+url response = URL response'.url
154+ where
155+ response' ∷ { url ∷ String }
156+ response' = unsafeCoerce response
157+
158+foreign import data Response ∷ Type
159+
160+foreign import _fetch ∷
161+ ∀ options.
162+ FetchImpl →
163+ URL →
164+ Record options →
165+ Effect (Promise Response)
166+
167+foreign import jsonImpl ∷ Response → Effect (Promise Foreign)
168+
169+foreign import textImpl ∷ Response → Effect (Promise String)
170+
171+foreign import headersImpl ∷ Response → Object String
172+
173+foreign import arrayBufferImpl ∷ Response → Effect (Promise ArrayBuffer)
src/Fetch/Impl/Impl.pursnew file+3
@@ -0,0 +1,3 @@
1+module Fetch.Impl (FetchImpl) where
2+
3+foreign import data FetchImpl ∷ Type
src/Fetch/Impl/Node.jsnew file+1
@@ -0,0 +1 @@
1+export { default as nodeFetch } from 'node-fetch';
src/Fetch/Impl/Node.pursnew file+7
@@ -0,0 +1,7 @@
1+module Fetch.Impl.Node
2+ ( nodeFetch
3+ ) where
4+
5+import Fetch.Impl (FetchImpl)
6+
7+foreign import nodeFetch ∷ FetchImpl
src/Fetch/Impl/Window.jsnew file+1
@@ -0,0 +1 @@
1+export const windowFetch = window.fetch;
src/Fetch/Impl/Window.pursnew file+7
@@ -0,0 +1,7 @@
1+module Fetch.Impl.Window
2+ ( windowFetch
3+ ) where
4+
5+import Fetch.Impl (FetchImpl)
6+
7+foreign import windowFetch ∷ FetchImpl
test.dhallnew file+9
@@ -0,0 +1,9 @@
1+let conf = ./spago.dhall
2+
3+in conf
4+ // { sources = conf.sources # [ "test/**/*.purs" ]
5+ , dependencies =
6+ conf.dependencies
7+ # [ "spec"
8+ ]
9+ }
test/Main.jsnew file+3
@@ -0,0 +1,3 @@
1+export const byteLength = function(arrayBuffer) {
2+ return arrayBuffer.byteLength;
3+};
test/Main.pursnew file+83
@@ -0,0 +1,83 @@
1+module Test.Main where
2+
3+import Prelude
4+
5+import Data.ArrayBuffer.Types (ArrayBuffer)
6+import Data.Either (Either(..), isRight)
7+import Data.String (null)
8+import Effect (Effect)
9+import Effect.Aff (attempt, launchAff_)
10+import Foreign.Object as Object
11+import Fetch as Fetch
12+import Fetch.Impl.Node (nodeFetch)
13+import Test.Spec (describe, it)
14+import Test.Spec.Assertions (fail, shouldEqual, shouldContain)
15+import Test.Spec.Reporter.Console (consoleReporter)
16+import Test.Spec.Runner (runSpec)
17+
18+fetch ∷ Fetch.Fetch
19+fetch = Fetch.fetch nodeFetch
20+
21+main ∷ Effect Unit
22+main = launchAff_ $ runSpec [ consoleReporter ] do
23+ describe "purescript-fetch" do
24+ it "get works and gets a body" do
25+ _response ← attempt $ fetch (Fetch.URL "https://www.google.com")
26+ Fetch.defaultFetchOptions
27+ case _response of
28+ Left e → do
29+ fail $ "failed with " <> show e
30+ Right response → do
31+ stuff ← Fetch.text response
32+ let code = Fetch.statusCode response
33+ code `shouldEqual` 200
34+ null stuff `shouldEqual` false
35+ let headers = Fetch.headers response
36+ Object.keys headers `shouldContain` "content-type"
37+ Object.keys headers `shouldContain` "content-encoding"
38+
39+ it "get works and gets a body" do
40+ _response ← attempt $ fetch (Fetch.URL "https://www.google.com")
41+ Fetch.defaultFetchOptions
42+ case _response of
43+ Left e → do
44+ fail $ "failed with " <> show e
45+ Right response → do
46+ arrayBuffer ← Fetch.arrayBuffer response
47+ let code = Fetch.statusCode response
48+ code `shouldEqual` 200
49+ (byteLength arrayBuffer > 0) `shouldEqual` true
50+
51+ it "gets an url" do
52+ _response ← attempt $ fetch (Fetch.URL "https://www.google.com")
53+ Fetch.defaultFetchOptions
54+ case _response of
55+ Left e → do
56+ fail $ "failed with " <> show e
57+ Right response → do
58+ let url = Fetch.url response
59+ url `shouldEqual` (Fetch.URL "https://www.google.com/")
60+
61+ it "post works" do
62+ let
63+ opts =
64+ { method: Fetch.postMethod
65+ , body: "{}"
66+ , headers: Fetch.makeHeaders { "Content-Type": "application/json" }
67+ }
68+ result ← attempt $ fetch (Fetch.URL "https://www.google.com") opts
69+ isRight result `shouldEqual` true
70+ it "put works" do
71+ let opts = { method: Fetch.putMethod }
72+ result ← attempt $ fetch (Fetch.URL "https://www.google.com") opts
73+ isRight result `shouldEqual` true
74+ it "patch works" do
75+ let opts = { method: Fetch.patchMethod }
76+ result ← attempt $ fetch (Fetch.URL "https://www.google.com") opts
77+ isRight result `shouldEqual` true
78+ it "delete works" do
79+ let opts = { method: Fetch.deleteMethod }
80+ result ← attempt $ fetch (Fetch.URL "https://www.google.com") opts
81+ isRight result `shouldEqual` true
82+
83+foreign import byteLength ∷ ArrayBuffer → Int