Skip to content

Commit 75372f8

Browse files
Update to PureScript v0.15.0 (#171)
* Migrated FFI to ES modules via 'lebab' * Replaced 'export var' with 'export const' * Removed '"use strict";' in FFI files * Update to CI to use 'unstable' purescript * Add CI test: verify 'bower.json' file works via pulp * Ignore spago-based tests (temporarily) * Update Bower dependencies to master or main * Update packages.dhall to 'prepare-0.15' package set * Removed unneeded 'psci-support' package * Update psa to 0.8.2 * Fix type var shadowing * Update eslint to ES6 * Fix linting issue * Move psci-support to dev dep * Replace _ajax with driver arg * Drop tests * Export AffjaxDriver type * Add affjax driver to each request * Fix formatting issues * Add changelog entry * Fix usages of `F` alias * Instruct new envs to re-export Affjax things * Clarify how to fix broken code
1 parent a3a5e93 commit 75372f8

File tree

12 files changed

+151
-369
lines changed

12 files changed

+151
-369
lines changed

.eslintrc.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
2-
"env": { "commonjs": true },
32
"extends": "eslint:recommended",
4-
"parserOptions": { "ecmaVersion": 5 },
3+
"parserOptions": { "ecmaVersion": 6, "sourceType": "module" },
54
"rules": {
65
"block-scoped-var": "error",
76
"consistent-return": "error",

.github/workflows/ci.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
- name: Set up PureScript toolchain
1717
uses: purescript-contrib/setup-purescript@main
1818
with:
19+
purescript: "unstable"
1920
purs-tidy: "latest"
2021

2122
- name: Cache PureScript dependencies
@@ -49,8 +50,17 @@ jobs:
4950
- name: Build the project
5051
run: npm run build
5152

52-
- name: Run tests
53-
run: npm run test
53+
# - name: Run tests
54+
# run: npm run test
5455

5556
- name: Check formatting
5657
run: purs-tidy check src test
58+
59+
- name: Verify Bower & Pulp
60+
run: |
61+
npm install bower [email protected]
62+
npx bower install
63+
npx pulp build -- --censor-lib --strict
64+
if [ -d "test" ]; then
65+
npx pulp test
66+
fi

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ Notable changes to this project are documented in this file. The format is based
55
## [Unreleased]
66

77
Breaking changes:
8+
- Update project and deps to PureScript v0.15.0 (#171 by @JordanMartinez)
9+
- Update all request functions to take a driver arg (#171 by @JordanMartinez)
10+
11+
Affjax works on the Node.js and browser environments by relying on a `require`
12+
statement within a function. Depending on the environment detected,
13+
either `XHR` or `XmlHttpRequest` is used. Since ES modules do not allow
14+
one to call `import` within a function in a _synchronous_ way,
15+
we cannot continue to use this approach.
16+
17+
Rather, all request-related functions (e.g. `request`, `get`, etc.) now take
18+
as their first argument an `AffjaxDriver` value. Different environments
19+
will pass in their implementation for that driver and re-export
20+
the functionality defined in `affjax`.
21+
22+
To fix your code, depend on the corresponding library below and update the imported
23+
module from `Affjax` to `Affjax.Node`/`Affjax.Web`:
24+
- If on Node.js, use [`purescript-affjax-node`](https://github.com/purescript-contrib/purescript-affjax-node/).
25+
- If on the brower, use [`purescript-affjax-web`](https://github.com/purescript-contrib/purescript-affjax-web/).
826

927
New features:
1028

bower.json

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,34 @@
2323
"package.json"
2424
],
2525
"dependencies": {
26-
"purescript-aff": "^v6.0.0",
27-
"purescript-argonaut-core": "^v6.0.0",
28-
"purescript-arraybuffer-types": "^v3.0.0",
29-
"purescript-arrays": "^v6.0.0",
30-
"purescript-console": "^v5.0.0",
31-
"purescript-control": "^v5.0.0",
32-
"purescript-datetime": "^v5.0.0",
33-
"purescript-effect": "^v3.0.0",
34-
"purescript-either": "^v5.0.0",
35-
"purescript-exceptions": "^v5.0.0",
36-
"purescript-foldable-traversable": "^v5.0.0",
37-
"purescript-foreign": "^v6.0.0",
38-
"purescript-foreign-object": "^v3.0.0",
39-
"purescript-form-urlencoded": "^v6.0.0",
40-
"purescript-functions": "^v5.0.0",
41-
"purescript-http-methods": "^v5.0.0",
42-
"purescript-lists": "^v6.0.0",
43-
"purescript-maybe": "^v5.0.0",
44-
"purescript-media-types": "^v5.0.0",
45-
"purescript-newtype": "^v4.0.0",
46-
"purescript-nullable": "^v5.0.0",
47-
"purescript-prelude": "^v5.0.0",
48-
"purescript-psci-support": "^v5.0.0",
49-
"purescript-transformers": "^v5.0.0",
50-
"purescript-web-dom": "^v5.0.0",
51-
"purescript-web-file": "^v3.0.0",
52-
"purescript-web-xhr": "^v4.0.0"
26+
"purescript-aff": "main",
27+
"purescript-argonaut-core": "main",
28+
"purescript-arraybuffer-types": "main",
29+
"purescript-arrays": "master",
30+
"purescript-console": "master",
31+
"purescript-control": "master",
32+
"purescript-datetime": "master",
33+
"purescript-effect": "master",
34+
"purescript-either": "master",
35+
"purescript-exceptions": "master",
36+
"purescript-foldable-traversable": "master",
37+
"purescript-foreign": "master",
38+
"purescript-foreign-object": "master",
39+
"purescript-form-urlencoded": "main",
40+
"purescript-functions": "master",
41+
"purescript-http-methods": "main",
42+
"purescript-lists": "master",
43+
"purescript-maybe": "master",
44+
"purescript-media-types": "main",
45+
"purescript-newtype": "master",
46+
"purescript-nullable": "main",
47+
"purescript-prelude": "master",
48+
"purescript-transformers": "master",
49+
"purescript-web-dom": "master",
50+
"purescript-web-file": "master",
51+
"purescript-web-xhr": "master"
52+
},
53+
"devDependencies": {
54+
"purescript-psci-support": "master"
5355
}
5456
}

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
{
22
"private": true,
33
"scripts": {
4-
"build": "eslint src && spago build --purs-args '--censor-lib --strict'",
5-
"test": "spago test --no-install"
4+
"build": "eslint src && spago build --purs-args '--censor-lib --strict'"
65
},
76
"devDependencies": {
87
"body-parser": "^1.19.0",
98
"eslint": "^7.10.0",
109
"express": "^4.17.1",
11-
"purescript-psa": "^0.8.0",
10+
"purescript-psa": "^0.8.2",
1211
"xhr2": "^0.2.0"
1312
}
1413
}

packages.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
let upstream =
2-
https://github.com/purescript/package-sets/releases/download/psc-0.14.3-20210722/packages.dhall sha256:1ceb43aa59436bf5601bac45f6f3781c4e1f0e4c2b8458105b018e5ed8c30f8c
2+
https://raw.githubusercontent.com/purescript/package-sets/prepare-0.15/src/packages.dhall
33

44
in upstream

spago.dhall

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
, "newtype"
2323
, "nullable"
2424
, "prelude"
25-
, "psci-support"
2625
, "transformers"
2726
, "web-dom"
2827
, "web-file"
2928
, "web-xhr"
3029
]
3130
, packages = ./packages.dhall
32-
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
31+
, sources = [ "src/**/*.purs" ]
3332
}

src/Affjax.js

Lines changed: 45 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,52 @@
1-
/* global XMLHttpRequest */
2-
/* global process */
3-
"use strict";
4-
5-
exports._ajax = function () {
6-
var platformSpecific = { };
7-
if (typeof module !== "undefined" && module.require && !(typeof process !== "undefined" && process.versions["electron"])) {
8-
// We are on node.js
9-
platformSpecific.newXHR = function () {
10-
var XHR = module.require("xhr2");
11-
return new XHR();
12-
};
13-
14-
platformSpecific.fixupUrl = function (url, xhr) {
15-
if (xhr.nodejsBaseUrl === null) {
16-
var urllib = module.require("url");
17-
var u = urllib.parse(url);
18-
u.protocol = u.protocol || "http:";
19-
u.hostname = u.hostname || "localhost";
20-
return urllib.format(u);
21-
} else {
22-
return url || "/";
1+
export function _ajax(platformSpecificDriver, timeoutErrorMessageIdent, requestFailedMessageIdent, mkHeader, options) {
2+
return function (errback, callback) {
3+
var xhr = platformSpecificDriver.newXHR();
4+
var fixedUrl = platformSpecificDriver.fixupUrl(options.url, xhr);
5+
xhr.open(options.method || "GET", fixedUrl, true, options.username, options.password);
6+
if (options.headers) {
7+
try {
8+
// eslint-disable-next-line no-eq-null,eqeqeq
9+
for (var i = 0, header; (header = options.headers[i]) != null; i++) {
10+
xhr.setRequestHeader(header.field, header.value);
11+
}
12+
} catch (e) {
13+
errback(e);
2314
}
15+
}
16+
var onerror = function (msgIdent) {
17+
return function () {
18+
errback(new Error(msgIdent));
19+
};
2420
};
25-
26-
platformSpecific.getResponse = function (xhr) {
27-
return xhr.response;
28-
};
29-
} else {
30-
// We are in the browser
31-
platformSpecific.newXHR = function () {
32-
return new XMLHttpRequest();
33-
};
34-
35-
platformSpecific.fixupUrl = function (url) {
36-
return url || "/";
37-
};
38-
39-
platformSpecific.getResponse = function (xhr) {
40-
return xhr.response;
21+
xhr.onerror = onerror(requestFailedMessageIdent);
22+
xhr.ontimeout = onerror(timeoutErrorMessageIdent);
23+
xhr.onload = function () {
24+
callback({
25+
status: xhr.status,
26+
statusText: xhr.statusText,
27+
headers: xhr.getAllResponseHeaders().split("\r\n")
28+
.filter(function (header) {
29+
return header.length > 0;
30+
})
31+
.map(function (header) {
32+
var i = header.indexOf(":");
33+
return mkHeader(header.substring(0, i))(header.substring(i + 2));
34+
}),
35+
body: xhr.response
36+
});
4137
};
42-
}
38+
xhr.responseType = options.responseType;
39+
xhr.withCredentials = options.withCredentials;
40+
xhr.timeout = options.timeout;
41+
xhr.send(options.content);
4342

44-
return function (timeoutErrorMessageIdent, requestFailedMessageIdent, mkHeader, options) {
45-
return function (errback, callback) {
46-
var xhr = platformSpecific.newXHR();
47-
var fixedUrl = platformSpecific.fixupUrl(options.url, xhr);
48-
xhr.open(options.method || "GET", fixedUrl, true, options.username, options.password);
49-
if (options.headers) {
50-
try {
51-
// eslint-disable-next-line no-eq-null,eqeqeq
52-
for (var i = 0, header; (header = options.headers[i]) != null; i++) {
53-
xhr.setRequestHeader(header.field, header.value);
54-
}
55-
} catch (e) {
56-
errback(e);
57-
}
43+
return function (error, cancelErrback, cancelCallback) {
44+
try {
45+
xhr.abort();
46+
} catch (e) {
47+
return cancelErrback(e);
5848
}
59-
var onerror = function (msgIdent) {
60-
return function () {
61-
errback(new Error(msgIdent));
62-
};
63-
};
64-
xhr.onerror = onerror(requestFailedMessageIdent);
65-
xhr.ontimeout = onerror(timeoutErrorMessageIdent);
66-
xhr.onload = function () {
67-
callback({
68-
status: xhr.status,
69-
statusText: xhr.statusText,
70-
headers: xhr.getAllResponseHeaders().split("\r\n")
71-
.filter(function (header) {
72-
return header.length > 0;
73-
})
74-
.map(function (header) {
75-
var i = header.indexOf(":");
76-
return mkHeader(header.substring(0, i))(header.substring(i + 2));
77-
}),
78-
body: platformSpecific.getResponse(xhr)
79-
});
80-
};
81-
xhr.responseType = options.responseType;
82-
xhr.withCredentials = options.withCredentials;
83-
xhr.timeout = options.timeout;
84-
xhr.send(options.content);
85-
86-
return function (error, cancelErrback, cancelCallback) {
87-
try {
88-
xhr.abort();
89-
} catch (e) {
90-
return cancelErrback(e);
91-
}
92-
return cancelCallback();
93-
};
49+
return cancelCallback();
9450
};
9551
};
96-
}();
52+
}

0 commit comments

Comments
 (0)