From bec7ed723be235b94daf1d5e5aa7f595bf7d9815 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 3 Mar 2018 14:42:25 +0900 Subject: [PATCH] :v: --- src/api/stream/home.ts | 4 +++ src/web/app/common/mios.ts | 55 +++++++++++++++++++++++++++++-- src/web/app/common/scripts/api.ts | 47 -------------------------- 3 files changed, 56 insertions(+), 50 deletions(-) delete mode 100644 src/web/app/common/scripts/api.ts diff --git a/src/api/stream/home.ts b/src/api/stream/home.ts index 10078337c..cc3fb885e 100644 --- a/src/api/stream/home.ts +++ b/src/api/stream/home.ts @@ -66,6 +66,10 @@ export default async function(request: websocket.request, connection: websocket. const msg = JSON.parse(data.utf8Data); switch (msg.type) { + case 'api': + // TODO + break; + case 'alive': // Update lastUsedAt User.update({ _id: user._id }, { diff --git a/src/web/app/common/mios.ts b/src/web/app/common/mios.ts index 3701a24c3..bc83ec0bb 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 bba838f56..000000000 --- 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} 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); - }); -};