added customRequest to api core

This commit is contained in:
srgooglo 2022-09-13 17:49:23 +02:00
parent 8e701baef0
commit bb053bf10e

View File

@ -15,6 +15,43 @@ export default class ApiCore extends Core {
this.ctx.registerPublicMethod("api", this)
}
async customRequest(
namepace = undefined,
payload = {
method: "GET",
},
...args
) {
if (typeof namepace === "undefined") {
throw new Error("Namespace must be defined")
}
if (typeof this.namespaces[namepace] === "undefined") {
throw new Error("Namespace not found")
}
if (typeof payload === "string") {
payload = {
url: payload,
}
}
if (typeof payload.headers !== "object") {
payload.headers = {}
}
const sessionToken = await Session.token
if (sessionToken) {
payload.headers["Authorization"] = `Bearer ${sessionToken}`
} else {
console.warn("Making a request with no session token")
}
return await this.namespaces[namepace].httpInterface(payload, ...args)
}
request = (namespace = "main", method, endpoint, ...args) => {
if (!this.namespaces[namespace]) {
throw new Error(`Namespace ${namespace} not found`)