This commit is contained in:
syuilo 2018-03-03 14:42:25 +09:00
parent 12f472b3fc
commit bec7ed723b
3 changed files with 56 additions and 50 deletions

View File

@ -66,6 +66,10 @@ export default async function(request: websocket.request, connection: websocket.
const msg = JSON.parse(data.utf8Data); const msg = JSON.parse(data.utf8Data);
switch (msg.type) { switch (msg.type) {
case 'api':
// TODO
break;
case 'alive': case 'alive':
// Update lastUsedAt // Update lastUsedAt
User.update({ _id: user._id }, { User.update({ _id: user._id }, {

View File

@ -2,7 +2,6 @@ import Vue from 'vue';
import { EventEmitter } from 'eventemitter3'; import { EventEmitter } from 'eventemitter3';
import { host, apiUrl, swPublickey, version, lang } from '../config'; import { host, apiUrl, swPublickey, version, lang } from '../config';
import api from './scripts/api';
import Progress from './scripts/loading'; import Progress from './scripts/loading';
import HomeStreamManager from './scripts/streaming/home-stream-manager'; import HomeStreamManager from './scripts/streaming/home-stream-manager';
import DriveStreamManager from './scripts/streaming/drive-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'; import Err from '../common/views/components/connect-failed.vue';
//#region api requests
let spinner = null;
let pending = 0;
//#endregion
export type API = { export type API = {
chooseDriveFile: (opts: { chooseDriveFile: (opts: {
title?: string; title?: string;
@ -365,8 +369,53 @@ export default class MiOS extends EventEmitter {
* @param endpoint * @param endpoint
* @param data * @param data
*/ */
public api(endpoint: string, data?: { [x: string]: any }) { public api(endpoint: string, data: { [x: string]: any } = {}): Promise<{ [x: string]: any }> {
return api(this.i, endpoint, data); 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);
/*}*/
});
} }
/** /**

View File

@ -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);
});
};