mirror of
https://github.com/Steffo99/bluelib.git
synced 2024-12-22 19:44:21 +00:00
0.11.0
This commit is contained in:
parent
52b47ed349
commit
8f3f73fbe6
6 changed files with 150 additions and 2 deletions
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"private": false,
|
"private": false,
|
||||||
"name": "bluelib",
|
"name": "bluelib",
|
||||||
"version": "0.10.4",
|
"version": "0.11.0",
|
||||||
"license": "AGPL-3.0-or-later",
|
"license": "AGPL-3.0-or-later",
|
||||||
"source": "src/index.js",
|
"source": "src/index.js",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
@ -10,7 +10,7 @@
|
||||||
"files": ["dist/*"],
|
"files": ["dist/*"],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "preact watch",
|
"start": "preact watch",
|
||||||
"dist": "microbundle && npm publish"
|
"dist": "git add . && cross-env-shell git commit -m \"$npm_package_version\" && git push && microbundle && npm publish && cross-env-shell hub release create -m \"$npm_package_version\" \"$npm_package_version\""
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"cross-env": "^7.0.2",
|
"cross-env": "^7.0.2",
|
||||||
|
|
3
src/contexts/RoyalnetInstanceUrl.js
Normal file
3
src/contexts/RoyalnetInstanceUrl.js
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import {createContext} from "preact";
|
||||||
|
|
||||||
|
export default createContext(undefined);
|
14
src/hooks/useFormValidator.js
Normal file
14
src/hooks/useFormValidator.js
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import { useEffect, useState } from 'preact/hooks';
|
||||||
|
|
||||||
|
export default function(value, validator) {
|
||||||
|
const [status, setStatus] = useState({
|
||||||
|
validity: null,
|
||||||
|
message: ""
|
||||||
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
validator(value, setStatus);
|
||||||
|
}, [value]);
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
15
src/hooks/useRoyalnetData.js
Normal file
15
src/hooks/useRoyalnetData.js
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import RoyalnetInstanceUrl from '../contexts/RoyalnetInstanceUrl';
|
||||||
|
import {useContext, useEffect, useState} from 'preact/hooks';
|
||||||
|
import {royalnetApiRequest} from '../utils/royalnetApiRequest';
|
||||||
|
|
||||||
|
|
||||||
|
export default function(method, path, body) {
|
||||||
|
const instanceUrl = useContext(RoyalnetInstanceUrl);
|
||||||
|
const [data, setData] = useState(undefined);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
royalnetApiRequest(instanceUrl, method, path, body).then(d => setData(d));
|
||||||
|
}, [instanceUrl, method, path, body]);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
61
src/hooks/useRoyalnetInstanceValidator.js
Normal file
61
src/hooks/useRoyalnetInstanceValidator.js
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
import { useContext, useState } from 'preact/hooks';
|
||||||
|
import RoyalnetInstanceUrl from '../contexts/RoyalnetInstanceUrl';
|
||||||
|
import useFormValidator from "./useFormValidator";
|
||||||
|
import apiRequest from '../utils/apiRequest';
|
||||||
|
|
||||||
|
const instanceUrlRegex = /^https?:\/\/.*?[^/]$/;
|
||||||
|
|
||||||
|
export default function() {
|
||||||
|
const defaultInstanceUrl = useContext(RoyalnetInstanceUrl);
|
||||||
|
const [instanceUrl, setInstanceUrl] = useState(defaultInstanceUrl);
|
||||||
|
const [instanceTesterAbort, setInstanceTesterAbort] = useState(null);
|
||||||
|
|
||||||
|
const instanceUrlStatus = useFormValidator(instanceUrl, (value, setStatus) => {
|
||||||
|
if(value.length === 0) {
|
||||||
|
setStatus({
|
||||||
|
validity: null,
|
||||||
|
message: ""
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!Boolean(instanceUrlRegex.test(value))) {
|
||||||
|
setStatus({
|
||||||
|
validity: false,
|
||||||
|
message: "Invalid URL"
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(instanceTesterAbort !== null) {
|
||||||
|
instanceTesterAbort.abort();
|
||||||
|
}
|
||||||
|
let abort = new AbortController();
|
||||||
|
setInstanceTesterAbort(abort);
|
||||||
|
|
||||||
|
apiRequest(value, "GET", "/api/royalnet/version/v1", undefined, abort.signal).then((data) => {
|
||||||
|
if(value === instanceUrl) {
|
||||||
|
setStatus({
|
||||||
|
validity: true,
|
||||||
|
message: `Royalnet ${data["semantic"]}`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.log("wtf?")
|
||||||
|
}
|
||||||
|
}).catch((err) => {
|
||||||
|
if(value === instanceUrl) {
|
||||||
|
setStatus({
|
||||||
|
validity: false,
|
||||||
|
message: "Royalnet not found"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
setStatus({
|
||||||
|
validity: null,
|
||||||
|
message: ""
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return [instanceUrl, setInstanceUrl, instanceUrlStatus];
|
||||||
|
}
|
55
src/utils/royalnetApiRequest.js
Normal file
55
src/utils/royalnetApiRequest.js
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||||
|
class RoyalnetApiError extends Error {
|
||||||
|
constructor(errorCode, errorType, errorArgs, ...params) {
|
||||||
|
// noinspection JSCheckFunctionSignatures
|
||||||
|
super(...params);
|
||||||
|
if(Error.captureStackTrace) Error.captureStackTrace(this, RoyalnetApiError);
|
||||||
|
|
||||||
|
this.name = "RoyalnetApiError";
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
this.errorType = errorType;
|
||||||
|
this.errorArgs = errorArgs;
|
||||||
|
this.message = `${errorCode} | ${errorType} | ${errorArgs.join("|")}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function royalnetApiRequest(baseUrl, method, path, args, abortSignal) {
|
||||||
|
if(args === undefined || args === null) {
|
||||||
|
args = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
let body;
|
||||||
|
let url;
|
||||||
|
if(method === "GET") {
|
||||||
|
body = undefined;
|
||||||
|
//Create a query string
|
||||||
|
let params = new URLSearchParams();
|
||||||
|
//Use the items in the args object as key-value pairs for the query string
|
||||||
|
Object.keys(args).forEach(key => {
|
||||||
|
let arg = args[key];
|
||||||
|
params.append(key, arg);
|
||||||
|
});
|
||||||
|
url = `${baseUrl}${path}?${params.toString()}`;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
body = JSON.stringify(args);
|
||||||
|
url = `${baseUrl}${path}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Make the request
|
||||||
|
let response = await fetch(url, {
|
||||||
|
method: method,
|
||||||
|
body: body,
|
||||||
|
signal: abortSignal,
|
||||||
|
});
|
||||||
|
//Parse the response as JSON
|
||||||
|
let json = await response.json();
|
||||||
|
//Check if the request was a success
|
||||||
|
if(json["success"] === false) {
|
||||||
|
throw new RoyalnetApiError(json["error_code"], json["error_type"], json["error_args"])
|
||||||
|
}
|
||||||
|
return json["data"]
|
||||||
|
}
|
||||||
|
|
||||||
|
export {royalnetApiRequest, RoyalnetApiError};
|
Loading…
Reference in a new issue