summaryrefslogtreecommitdiff
path: root/src/web/app
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-03-03 14:42:25 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-03-03 14:42:25 +0900
commitbec7ed723be235b94daf1d5e5aa7f595bf7d9815 (patch)
treef1959261337e300b0c60dcb066e5d54c22551a0c /src/web/app
parent:v: (diff)
downloadsharkey-bec7ed723be235b94daf1d5e5aa7f595bf7d9815.tar.gz
sharkey-bec7ed723be235b94daf1d5e5aa7f595bf7d9815.tar.bz2
sharkey-bec7ed723be235b94daf1d5e5aa7f595bf7d9815.zip
:v:
Diffstat (limited to 'src/web/app')
-rw-r--r--src/web/app/common/mios.ts55
-rw-r--r--src/web/app/common/scripts/api.ts47
2 files changed, 52 insertions, 50 deletions
diff --git a/src/web/app/common/mios.ts b/src/web/app/common/mios.ts
index 3701a24c30..bc83ec0bbb 100644
--- a/src/web/app/common/mios.ts
+++ b/src/web/app/common/mios.ts
@@ -2,7 +2,6 @@ import Vue from 'vue';
import { EventEmitter } from 'eventemitter3';
import { host, apiUrl, swPublickey, version, lang } from '../config';
-import api from './scripts/api';
import Progress from './scripts/loading';
import HomeStreamManager from './scripts/streaming/home-stream-manager';
import DriveStreamManager from './scripts/streaming/drive-stream-manager';
@@ -12,6 +11,11 @@ import MessagingIndexStreamManager from './scripts/streaming/messaging-index-str
import Err from '../common/views/components/connect-failed.vue';
+//#region api requests
+let spinner = null;
+let pending = 0;
+//#endregion
+
export type API = {
chooseDriveFile: (opts: {
title?: string;
@@ -365,8 +369,53 @@ export default class MiOS extends EventEmitter {
* @param endpoint エンドポイント名
* @param data パラメータ
*/
- public api(endpoint: string, data?: { [x: string]: any }) {
- return api(this.i, endpoint, data);
+ public api(endpoint: string, data: { [x: string]: any } = {}): Promise<{ [x: string]: any }> {
+ if (++pending === 1) {
+ spinner = document.createElement('div');
+ spinner.setAttribute('id', 'wait');
+ document.body.appendChild(spinner);
+ }
+
+ // Append a credential
+ if (this.isSignedIn) (data as any).i = this.i.token;
+
+ // TODO
+ //const viaStream = localStorage.getItem('enableExperimental') == 'true';
+
+ return new Promise((resolve, reject) => {
+ /*if (viaStream) {
+ const stream = this.stream.borrow();
+ const id = Math.random().toString();
+ stream.once(`api-res:${id}`, res => {
+ resolve(res);
+ });
+ stream.send({
+ type: 'api',
+ id,
+ endpoint,
+ data
+ });
+ } else {*/
+ // Send request
+ fetch(endpoint.indexOf('://') > -1 ? endpoint : `${apiUrl}/${endpoint}`, {
+ method: 'POST',
+ body: JSON.stringify(data),
+ credentials: endpoint === 'signin' ? 'include' : 'omit',
+ cache: 'no-cache'
+ }).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);
+ }, reject);
+ }
+ }).catch(reject);
+ /*}*/
+ });
}
/**
diff --git a/src/web/app/common/scripts/api.ts b/src/web/app/common/scripts/api.ts
deleted file mode 100644
index bba838f56b..0000000000
--- a/src/web/app/common/scripts/api.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-/**
- * API Request
- */
-
-declare const _API_URL_: string;
-
-let spinner = null;
-let pending = 0;
-
-/**
- * Send a request to API
- * @param {string|Object} i Credential
- * @param {string} endpoint Endpoint
- * @param {any} [data={}] Data
- * @return {Promise<any>} Response
- */
-export default (i, endpoint, data = {}): Promise<{ [x: string]: any }> => {
- if (++pending === 1) {
- spinner = document.createElement('div');
- spinner.setAttribute('id', 'wait');
- document.body.appendChild(spinner);
- }
-
- // Append the credential
- if (i != null) (data as any).i = typeof i === 'object' ? i.token : i;
-
- return new Promise((resolve, reject) => {
- // Send request
- fetch(endpoint.indexOf('://') > -1 ? endpoint : `${_API_URL_}/${endpoint}`, {
- method: 'POST',
- body: JSON.stringify(data),
- credentials: endpoint === 'signin' ? 'include' : 'omit',
- cache: 'no-cache'
- }).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);
- }, reject);
- }
- }).catch(reject);
- });
-};