Skip to content

Commit 375baae

Browse files
committed
Initial commit
0 parents  commit 375baae

23 files changed

+646
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/bower_components/
2+
/node_modules/
3+
/.pulp-cache/
4+
/output/
5+
/generated-docs/
6+
/.psc-package/
7+
/.psc*
8+
/.purs*
9+
/.psa*

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Awake Security
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.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# purescript-web-fetch

bower.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "purescript-web-fetch",
3+
"homepage": "https://github.com/purescript-web/purescript-web-fetch",
4+
"license": "MIT",
5+
"repository": {
6+
"type": "git",
7+
"url": "git://github.com/purescript-web/purescript-web-fetch.git"
8+
},
9+
"ignore": [
10+
"**/.*",
11+
"bower_components",
12+
"node_modules",
13+
"output",
14+
"bower.json",
15+
"package.json"
16+
],
17+
"dependencies": {
18+
"purescript-prelude": "^4.0.0",
19+
"purescript-effect": "^2.0.0",
20+
"purescript-http-methods": "^4.0.0",
21+
"purescript-typelevel-prelude": "^5.0.0",
22+
"purescript-web-file": "^2.3.0",
23+
"purescript-record": "^2.0.0",
24+
"purescript-foreign-object": "^2.0.0",
25+
"purescript-web-promise": "https://github.com/purescript-web/purescript-web-promise.git#^1.0.0",
26+
"purescript-web-streams": "https://github.com/purescript-web/purescript-web-streams.git#^1.0.0"
27+
}
28+
}

src/Web/Fetch.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports._fetch = fetch;

src/Web/Fetch.purs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Web.Fetch
2+
( FetchOptions
3+
, fetch
4+
, fetchWithOptions
5+
) where
6+
7+
import Effect (Effect)
8+
import Effect.Uncurried (EffectFn2, runEffectFn2)
9+
import Prim.Row as Row
10+
import Web.Fetch.AbortController (AbortSignal)
11+
import Web.Fetch.Request (Request)
12+
import Web.Fetch.Response (Response)
13+
import Web.Promise (Promise)
14+
15+
type FetchOptions =
16+
( keepalive :: Boolean
17+
, signal :: AbortSignal
18+
)
19+
20+
foreign import _fetch :: forall r. EffectFn2 Request { | r } (Promise Response)
21+
22+
fetch :: Request -> Effect (Promise Response)
23+
fetch req = runEffectFn2 _fetch req {}
24+
25+
fetchWithOptions :: forall r rx. Row.Union r rx FetchOptions => Request -> { | r } -> Effect (Promise Response)
26+
fetchWithOptions = runEffectFn2 _fetch

src/Web/Fetch/AbortController.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
exports.new = function() {
2+
return new AbortController();
3+
};
4+
5+
exports.abort = function(controller) {
6+
return function() {
7+
return controller.abort();
8+
};
9+
};
10+
11+
exports.signal = function(controller) {
12+
return controller.signal;
13+
};

src/Web/Fetch/AbortController.purs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Web.Fetch.AbortController where
2+
3+
import Effect (Effect)
4+
import Prelude (Unit)
5+
6+
foreign import data AbortController :: Type
7+
8+
foreign import data AbortSignal :: Type
9+
10+
foreign import new :: Effect AbortController
11+
12+
foreign import abort :: AbortController -> Effect Unit
13+
14+
foreign import signal :: AbortController -> AbortSignal

src/Web/Fetch/Headers.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
exports.unsafeNew = function() {
2+
return new Headers();
3+
};
4+
5+
exports.unsafeAppend = function(key, value, headers) {
6+
return headers.append(key, value);
7+
};
8+
9+
exports.unsafeFromRecord = function(r) {
10+
return new Headers(r);
11+
};
12+
13+
exports._toArray = function(tuple, headers) {
14+
var arr = [];
15+
for (var pair of headers.entries()) {
16+
arr.push(tuple(pair[0])(pair[1]));
17+
}
18+
return arr;
19+
}
20+
21+
exports.fromObject = function(obj) {
22+
return new Headers(obj);
23+
};

src/Web/Fetch/Headers.purs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module Web.Fetch.Headers
2+
( Headers
3+
, fromFoldable
4+
, fromRecord
5+
, fromObject
6+
, toArray
7+
, toUnfoldable
8+
, empty
9+
) where
10+
11+
import Prelude
12+
13+
import Data.Array as Array
14+
import Data.Foldable (class Foldable, foldM)
15+
import Data.Function.Uncurried (Fn2, runFn2)
16+
import Data.Tuple (Tuple(..))
17+
import Data.Unfoldable (class Unfoldable)
18+
import Effect (Effect)
19+
import Effect.Uncurried (EffectFn3, runEffectFn3)
20+
import Effect.Unsafe (unsafePerformEffect)
21+
import Foreign.Object (Object)
22+
import Type.Row.Homogeneous (class Homogeneous)
23+
24+
foreign import data Headers :: Type
25+
26+
foreign import unsafeNew :: Effect Headers
27+
28+
foreign import unsafeAppend :: EffectFn3 String String Headers Unit
29+
30+
foreign import unsafeFromRecord :: forall r. { | r } -> Headers
31+
32+
foreign import _toArray :: Fn2 (forall a b. a -> b -> Tuple a b) Headers (Array (Tuple String String))
33+
34+
foreign import fromObject :: Object String -> Headers
35+
36+
fromFoldable :: forall f. Foldable f => f (Tuple String String) -> Headers
37+
fromFoldable f = unsafePerformEffect do
38+
init <- unsafeNew
39+
foldM (\headers (Tuple key value) -> do
40+
runEffectFn3 unsafeAppend key value headers
41+
pure headers) init f
42+
43+
fromRecord :: forall r. Homogeneous r String => { | r } -> Headers
44+
fromRecord = unsafeFromRecord
45+
46+
toArray :: Headers -> Array (Tuple String String)
47+
toArray = runFn2 _toArray Tuple
48+
49+
toUnfoldable :: forall f. Unfoldable f => Headers -> f (Tuple String String)
50+
toUnfoldable = Array.toUnfoldable <<< toArray
51+
52+
empty :: Headers
53+
empty = fromFoldable []

0 commit comments

Comments
 (0)