Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions src/pages/plugins/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,16 @@ export default function PluginsInclude(updates) {

async function searchRemotely(query) {
if (!query) return [];
const encodedQuery = encodeURIComponent(query);
const searchUrl = withSupportedEditor(
`${constants.API_BASE}/plugins?name=${encodedQuery}`,
);
try {
const response = await fetch(
withSupportedEditor(`${constants.API_BASE}/plugins?name=${query}`),
);
const plugins = await response.json();
const plugins = await helpers.requestJson(searchUrl);
// Map the plugins to Item elements and return
return plugins.map((plugin) => <Item {...plugin} />);
} catch (error) {
$list.all.setAttribute("empty-msg", strings["error"]);
window.log("error", "Failed to search remotely:");
window.log("error", error);
Comment thread
Ebola-Chan-bot marked this conversation as resolved.
Outdated
return [];
}
Expand Down Expand Up @@ -421,21 +421,20 @@ export default function PluginsInclude(updates) {
if (filterState.type === "orderBy") {
const page = filterState.nextPage || 1;
try {
let response;
let items;
if (filterState.value === "top_rated") {
response = await fetch(
items = await helpers.requestJson(
withSupportedEditor(
`${constants.API_BASE}/plugins?explore=random&page=${page}&limit=${LIMIT}`,
),
);
} else {
response = await fetch(
items = await helpers.requestJson(
withSupportedEditor(
`${constants.API_BASE}/plugin?orderBy=${filterState.value}&page=${page}&limit=${LIMIT}`,
),
);
}
const items = await response.json();
if (!Array.isArray(items)) {
return { items: [], hasMore: false };
}
Expand Down Expand Up @@ -470,10 +469,9 @@ export default function PluginsInclude(updates) {

try {
const page = filterState.nextPage;
const response = await fetch(
const data = await helpers.requestJson(
withSupportedEditor(`${constants.API_BASE}/plugins?page=${page}&limit=${LIMIT}`),
);
const data = await response.json();
filterState.nextPage = page + 1;

if (!Array.isArray(data) || !data.length) {
Expand Down Expand Up @@ -567,10 +565,9 @@ export default function PluginsInclude(updates) {

$list.all.setAttribute("empty-msg", strings["loading..."]);

const response = await fetch(
const newPlugins = await helpers.requestJson(
withSupportedEditor(`${constants.API_BASE}/plugins?page=${currentPage}&limit=${LIMIT}`),
);
const newPlugins = await response.json();

if (newPlugins.length < LIMIT) {
hasMore = false;
Expand Down
27 changes: 12 additions & 15 deletions src/sidebarApps/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,11 @@ async function loadMorePlugins() {
isLoading = true;
startLoading($explore);

const response = await fetch(
const newPlugins = await helpers.requestJson(
withSupportedEditor(
`${constants.API_BASE}/plugins?page=${currentPage}&limit=${LIMIT}`,
),
);
const newPlugins = await response.json();

if (newPlugins.length < LIMIT) {
hasMore = false;
Expand Down Expand Up @@ -218,7 +217,7 @@ async function searchPlugin() {
$searchResult.onscroll = null;

$searchResult.content = "";
const status = helpers.checkAPIStatus();
const status = await helpers.checkAPIStatus();
if (!status) {
$searchResult.content = (
<span className="error">{strings.api_error}</span>
Expand All @@ -228,14 +227,15 @@ async function searchPlugin() {

const query = this.value;
if (!query) return;
const encodedQuery = encodeURIComponent(query);

try {
$searchResult.classList.add("loading");
const plugins = await fsOperation(
const plugins = await helpers.requestJson(
withSupportedEditor(
Url.join(constants.API_BASE, `plugins?name=${query}`),
Url.join(constants.API_BASE, `plugins?name=${encodedQuery}`),
),
).readFile("json");
);

installedPlugins = await listInstalledPlugins();
$searchResult.content = plugins.map(ListItem);
Expand Down Expand Up @@ -409,7 +409,7 @@ async function loadInstalled() {
async function loadExplore() {
if (this.collapsed) return;

const status = helpers.checkAPIStatus();
const status = await helpers.checkAPIStatus();
if (!status) {
$explore.$ul.content = <span className="error">{strings.api_error}</span>;
return;
Expand All @@ -420,12 +420,11 @@ async function loadExplore() {
currentPage = 1;
hasMore = true;

const response = await fetch(
const plugins = await helpers.requestJson(
withSupportedEditor(
`${constants.API_BASE}/plugins?page=${currentPage}&limit=${LIMIT}`,
),
);
const plugins = await response.json();

if (plugins.length < LIMIT) {
hasMore = false;
Expand Down Expand Up @@ -463,21 +462,20 @@ async function getFilteredPlugins(filterState) {
if (filterState.type === "orderBy") {
const page = filterState.nextPage || 1;
try {
let response;
let items;
if (filterState.value === "top_rated") {
response = await fetch(
items = await helpers.requestJson(
withSupportedEditor(
`${constants.API_BASE}/plugins?explore=random&page=${page}&limit=${LIMIT}`,
),
);
} else {
response = await fetch(
items = await helpers.requestJson(
withSupportedEditor(
`${constants.API_BASE}/plugin?orderBy=${filterState.value}&page=${page}&limit=${LIMIT}`,
),
);
}
const items = await response.json();
if (!Array.isArray(items)) {
return { items: [], hasMore: false };
}
Expand Down Expand Up @@ -512,12 +510,11 @@ async function getFilteredPlugins(filterState) {

try {
const page = filterState.nextPage;
const response = await fetch(
const data = await helpers.requestJson(
withSupportedEditor(
`${constants.API_BASE}/plugins?page=${page}&limit=${LIMIT}`,
),
);
const data = await response.json();
filterState.nextPage = page + 1;

if (!Array.isArray(data) || !data.length) {
Expand Down
24 changes: 23 additions & 1 deletion src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,31 @@ export default {
func(...args, resolve, reject);
});
},
// Plugin API requests are all remote HTTPS calls. For this workload, the
// JS-to-native bridge cost is negligible compared to network latency, so we
// use native HTTP unconditionally instead of keeping a fetch fast path.
// This also avoids the WebView CORS mismatch that caused false
// "API unavailable" failures against the plugin API.
async requestJson(url, headers = {}) {
const nativeHttp = window.cordova?.plugin?.http;
if (typeof nativeHttp?.get !== "function") {
throw new Error("Native HTTP plugin is unavailable");
}

const response = await new Promise((resolve, reject) => {
nativeHttp.get(url, {}, headers, resolve, reject);
});

if (typeof response?.data === "string") {
return JSON.parse(response.data);
Comment thread
Ebola-Chan-bot marked this conversation as resolved.
Outdated
}

return response?.data;
Comment thread
Ebola-Chan-bot marked this conversation as resolved.
Comment thread
Ebola-Chan-bot marked this conversation as resolved.
},
async checkAPIStatus() {
const statusUrl = Url.join(constants.API_BASE, "status");
try {
const { status } = await ajax.get(Url.join(constants.API_BASE, "status"));
const { status } = await this.requestJson(statusUrl);
return status === "ok";
} catch (error) {
window.log("error", error);
Expand Down
Loading