mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-12-24 20:03:08 +02:00
✌️
This commit is contained in:
parent
12f472b3fc
commit
bec7ed723b
3 changed files with 56 additions and 50 deletions
|
@ -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 }, {
|
||||
|
|
|
@ -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);
|
||||
/*}*/
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
};
|
Loading…
Reference in a new issue