use internal http request

This commit is contained in:
pypy
2020-11-07 20:28:13 +09:00
parent c2b9345a59
commit d321354b42
+38 -36
View File
@@ -11,9 +11,11 @@ import locale from 'element-ui/lib/locale/lang/en';
import sharedRepository from './repository/shared.js'; import sharedRepository from './repository/shared.js';
import configRepository from './repository/config.js'; import configRepository from './repository/config.js';
import webApiService from './service/webapi.js';
(async function () { (async function () {
await CefSharp.BindObjectAsync( await CefSharp.BindObjectAsync(
'WebApi',
'VRCX', 'VRCX',
'SharedVariable', 'SharedVariable',
'SQLite' 'SQLite'
@@ -151,30 +153,25 @@ import configRepository from './repository/config.js';
API.pendingGetRequests = new Map(); API.pendingGetRequests = new Map();
API.call = function (endpoint, options) { API.call = function (endpoint, options) {
var resource = `https://api.vrchat.cloud/api/1/${endpoint}`;
var init = { var init = {
url: `https://api.vrchat.cloud/api/1/${endpoint}`,
method: 'GET', method: 'GET',
mode: 'cors',
credentials: 'include',
cache: 'no-cache',
redirect: 'follow',
referrerPolicy: 'no-referrer',
...options ...options
}; };
var { params } = init; var { params } = init;
var isGetRequest = init.method === 'GET'; var isGetRequest = init.method === 'GET';
if (isGetRequest) { if (isGetRequest === true) {
// transform body to url // transform body to url
if (params === Object(params)) { if (params === Object(params)) {
var url = new URL(resource); var url = new URL(init.url);
var { searchParams } = url; var { searchParams } = url;
for (var key in params) { for (var key in params) {
searchParams.set(key, params[key]); searchParams.set(key, params[key]);
} }
resource = url.toString(); init.url = url.toString();
} }
// merge requests // merge requests
var req = this.pendingGetRequests.get(resource); var req = this.pendingGetRequests.get(init.url);
if (req !== undefined) { if (req !== undefined) {
return req; return req;
} }
@@ -187,45 +184,50 @@ import configRepository from './repository/config.js';
? JSON.stringify(params) ? JSON.stringify(params)
: '{}'; : '{}';
} }
var req = fetch(resource, init).catch((err) => { var req = webApiService.execute(init).catch((err) => {
this.$throw(0, err); this.$throw(0, err);
}).then((res) => res.json().catch(() => { }).then((response) => {
if (res.ok) { try {
response.data = JSON.parse(response.data);
return response;
} catch (e) {
}
if (response.status === 200) {
this.$throw(0, 'Invalid JSON response'); this.$throw(0, 'Invalid JSON response');
} }
this.$throw(res.status); this.$throw(res.status);
}).then((json) => { }).then(({ data, status }) => {
if (res.ok) { if (data === Object(data)) {
if (json.success === Object(json.success)) { if (status === 200) {
new Noty({ if (data.success === Object(data.success)) {
type: 'success', new Noty({
text: escapeTag(json.success.message) type: 'success',
}).show(); text: escapeTag(data.success.message)
}).show();
}
return data;
} }
return json; if (data.error === Object(data.error)) {
}
if (json === Object(json)) {
if (json.error === Object(json.error)) {
this.$throw( this.$throw(
json.error.status_code || res.status, data.error.status_code || status,
json.error.message, data.error.message,
json.error.data data.error.data
); );
} else if (typeof json.error === 'string') { } else if (typeof data.error === 'string') {
this.$throw( this.$throw(
json.status_code || res.status, data.status_code || status,
json.error data.error
); );
} }
} }
this.$throw(res.status, json); this.$throw(status, data);
return json; return data;
})); });
if (isGetRequest) { if (isGetRequest === true) {
req.finally(() => { req.finally(() => {
this.pendingGetRequests.delete(resource); this.pendingGetRequests.delete(init.url);
}); });
this.pendingGetRequests.set(resource, req); this.pendingGetRequests.set(init.url, req);
} }
return req; return req;
}; };