summaryrefslogtreecommitdiff
path: root/src/web/app/common
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-02-13 07:39:54 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-02-13 07:39:54 +0900
commit96cd073bda6ecffb4e9b2489af512f05c9f8dd8c (patch)
treec5b6dbd035460297f059e8ff24a9fc620c84547c /src/web/app/common
parent[Test] :v: (diff)
downloadsharkey-96cd073bda6ecffb4e9b2489af512f05c9f8dd8c.tar.gz
sharkey-96cd073bda6ecffb4e9b2489af512f05c9f8dd8c.tar.bz2
sharkey-96cd073bda6ecffb4e9b2489af512f05c9f8dd8c.zip
[Client] LS to JS and some Clean up :v:
Diffstat (limited to 'src/web/app/common')
-rw-r--r--src/web/app/common/mixins.ls2
-rw-r--r--src/web/app/common/scripts/api.js44
-rw-r--r--src/web/app/common/scripts/api.ls51
3 files changed, 45 insertions, 52 deletions
diff --git a/src/web/app/common/mixins.ls b/src/web/app/common/mixins.ls
index 50918c68c4..0a3802e25c 100644
--- a/src/web/app/common/mixins.ls
+++ b/src/web/app/common/mixins.ls
@@ -6,7 +6,7 @@ module.exports = (me) ~>
(require './scripts/i.ls') me
riot.mixin \api do
- api: (require './scripts/api.ls').bind null i
+ api: (require './scripts/api').bind null i
riot.mixin \cropper do
Cropper: require \cropperjs
diff --git a/src/web/app/common/scripts/api.js b/src/web/app/common/scripts/api.js
new file mode 100644
index 0000000000..924d697ebc
--- /dev/null
+++ b/src/web/app/common/scripts/api.js
@@ -0,0 +1,44 @@
+/**
+ * API Request
+ */
+
+let spinner = null;
+let pending = 0;
+
+/**
+ * Send a request to API
+ * @param {string|Object} i Credential
+ * @param {string} endpoint Endpoint
+ * @param {Object} [data={}] Data
+ * @return {Promise<Object>} Response
+ */
+module.exports = (i, endpoint, data = {}) => {
+ if (++pending === 1) {
+ spinner = document.createElement('div');
+ spinner.setAttribute('id', 'wait');
+ document.body.appendChild(spinner);
+ }
+
+ // Append the credential
+ if (i != null) data.i = typeof i === 'object' ? i.token : i;
+
+ return new Promise((resolve, reject) => {
+ // Send request
+ fetch(endpoint.indexOf('://') > -1 ? endpoint : `${CONFIG.api.url}/${endpoint}`, {
+ method: 'POST',
+ body: JSON.stringify(data),
+ credentials: endpoint === 'signin' ? 'include' : 'omit'
+ }).then(res => {
+ if (--pending === 0) spinner.parentNode.removeChild(spinner);
+ if (res.status === 200) {
+ res.json().then(resolve);
+ } else if (res.status === 204) {
+ resolve();
+ } else {
+ res.json().then(err => {
+ reject(err.error);
+ });
+ }
+ }).catch(reject);
+ });
+};
diff --git a/src/web/app/common/scripts/api.ls b/src/web/app/common/scripts/api.ls
deleted file mode 100644
index 47182b6a5a..0000000000
--- a/src/web/app/common/scripts/api.ls
+++ /dev/null
@@ -1,51 +0,0 @@
-riot = require \riot
-
-spinner = null
-pending = 0
-
-net = riot.observable!
-
-riot.mixin \net do
- net: net
-
-module.exports = (i, endpoint, data = {}) ->
- if ++pending == 1
- spinner := document.create-element \div
- ..set-attribute \id \wait
- document.body.append-child spinner
-
- if i? and typeof i == \object then i = i.token
-
- # append user token when signed in
- if i? then data.i = i
-
- opts =
- method: \POST
- body: JSON.stringify data
-
- if endpoint == \signin
- opts.credentials = \include
-
- ep = if (endpoint.index-of '://') > -1
- then endpoint
- else "#{CONFIG.api.url}/#{endpoint}"
-
- new Promise (resolve, reject) ->
- timer = set-timeout ->
- net.trigger \detected-slow-network
- , 5000ms
-
- fetch ep, opts
- .then (res) ->
- clear-timeout timer
- if --pending == 0
- spinner.parent-node.remove-child spinner
-
- if res.status == 200
- res.json!.then resolve
- else if res.status == 204
- resolve!
- else
- res.json!.then (err) ->
- reject err.error
- .catch reject