Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP Function Refactor #641

Open
wants to merge 10 commits into
base: v6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
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
13 changes: 13 additions & 0 deletions src/functions/httpAddHeader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = async (d) => {
const data = d.util.aoiFunc(d);
if (data.err) return d.error(data.err);

let [name, value] = data.inside.splits;

((d.data.http ??= {})["headers"] ??= {})[name] = value;

return {
code: d.util.setCode(data),
data: d.data
}
}
10 changes: 10 additions & 0 deletions src/functions/httpClearOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = async (d) => {
const data = d.util.aoiFunc(d);

d.data.http = {};

return {
code: d.util.setCode(data),
data: d.data
}
}
10 changes: 10 additions & 0 deletions src/functions/httpGetContentType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = async (d) => {
const data = d.util.aoiFunc(d);

data.result = d.data.http?.contentType;

return {
code: d.util.setCode(data),
data: d.data
}
}
13 changes: 13 additions & 0 deletions src/functions/httpGetHeader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = async (d) => {
const data = d.util.aoiFunc(d);
if (data.err) return d.error(data.err);

let [name] = data.inside.splits;

data.result = d.data.http?.headers[name.addBrackets()]

return {
code: d.util.setCode(data),
data: d.data
}
}
10 changes: 10 additions & 0 deletions src/functions/httpOk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = async (d) => {
const data = d.util.aoiFunc(d);

data.result = d.data.http?.ok;

return {
code: d.util.setCode(data),
data: d.data
}
}
16 changes: 16 additions & 0 deletions src/functions/httpRemoveHeader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = async (d) => {
const data = d.util.aoiFunc(d);
if (data.err) return d.error(data.err);

let [name] = data.inside.splits;
if (!name) return d.aoiError.fnError(d, "custom", {
inside: data.inside
}, "Missing header name!");

delete d.data.http?.headers[name];

return {
code: d.util.setCode(data),
data: d.data
}
}
110 changes: 47 additions & 63 deletions src/functions/httpRequest.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,62 @@
const { Agent, fetch } = require('undici');

module.exports = async (d) => {
const data = d.util.aoiFunc(d);
if (data.err) return d.error(data.err);


let [
url,
method = 'get',
body = '',
property,
error = 'default',
...header
body = ''
] = data.inside.splits;

body = body?.trim() === '' ? undefined : body;

let headers = {};
if (header.length === 1) {
try {
headers = JSON.parse(header);
} catch (e) {
header.forEach((x) => {
const split = x.split(':');
headers[split[0]] = split[1];
});
}
} else if (header.length > 1) {
header.forEach((x) => {
const split = x.split(':');
headers[split[0]] = split[1];
});
}
body = body !== '' ? body.trim() : body;

try {
const response = await fetch(url.addBrackets(), {
method,
headers,
body,
agent: new Agent(),
// Getting the user-defined headers and content type.
const headers = {};
if (d.data.http?.contentType) {
Object.assign(headers, {
['Content-Type']: d.data.http.contentType
});
}
if (d.data.http?.headers) {
Object.assign(headers, d.data.http.headers);
}

const responseBody = await response.text();
const contentType = response.headers.get("content-type")?.split(/;/)[0];
// Making the request.
const response = await fetch(url.addBrackets(), {
body: body === '' ? undefined : body.addBrackets(),
headers,
method
});

if (property && /content(-|\s)?type/gi.test(property)) {
data.result = contentType;
} else if (property && contentType.includes("json")) {
data.result = eval(`JSON.parse(responseBody)?.${property}`)
} else {
data.result = responseBody;
}
} catch (err) {
console.error(err);
if (error === 'default') {
return d.aoiError.makeMessageError(
d.client,
d.channel,
{ content: err },
{},
d
);
} else {
const parsedError = await d.util.errorParser(error, d);
return d.aoiError.makeMessageError(
d.client,
d.channel,
parsedError.data ?? parsedError,
parsedError.options,
d
);
}
// Defining response headers.
(d.data.http??={})["headers"] = {};
for (const [name, value] of response.headers.entries()) {
if (/content-type/.test(name)) continue;
d.data.http.headers[name] = value;
}

// Get the response content type.
const contentType = response.headers.get("content-type")?.split(/;/)[0];

// Defining response content type.
(d.data.http ??= {})['contentType'] = contentType;
d.data.http.statusCode = response.status;

// Additional response data.
d.data.http.redirected = response.redirected;
d.data.http.ok = response.ok;

// Saving the result.
if (contentType.match(/application\/json/)) {
d.data.http.result = await response.json()
} else if (contentType.match(/image\//)) {
d.data.http.result = await response.arrayBuffer()
.then(a => Buffer.from(a).toString("base64"));
} else {
d.data.http.result = await response.text()
}

return {
code: d.util.setCode(data),
};
};
data: d.data
}
}
32 changes: 32 additions & 0 deletions src/functions/httpResult.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = async (d) => {
const data = d.util.aoiFunc(d);

let properties = data.inside.splits;
let isObject = (d.data.http?.contentType.includes("json") && properties.length) ?? false;

if (isObject) {
let response = d.data.http.result;
for (const property of properties) {
if (!Object.prototype.hasOwnProperty.call(response, property)) {
return d.aoiError.fnError(d, "custom", {
inside: data.inside
}, `Invalid property "${property}" in HTTP result!`)
}

response = response?.[property];
}

data.result = typeof response !== "string"
? JSON.stringify(response)
: String(response);
} else {
data.result = typeof d.data.http?.result === "object"
? JSON.stringify(d.data.http?.result)
: d.data.http?.result;
}

return {
code: d.util.setCode(data),
data: d.data
}
}
10 changes: 10 additions & 0 deletions src/functions/httpStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = async (d) => {
const data = d.util.aoiFunc(d);

data.result = d.data.http?.statusCode ?? Infinity;
Asayukiii marked this conversation as resolved.
Show resolved Hide resolved

return {
code: d.util.setCode(data),
data: d.data
}
}
10 changes: 10 additions & 0 deletions src/functions/httpWasRedirected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = async (d) => {
const data = d.util.aoiFunc(d);

data.result = d.data.http?.redirected;

return {
code: d.util.setCode(data),
data: d.data
}
}