n8n-nodes-base
Version:
Base nodes of n8n
71 lines • 2.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.discordApiRequest = discordApiRequest;
exports.discordApiMultiPartRequest = discordApiMultiPartRequest;
const n8n_workflow_1 = require("n8n-workflow");
const helpers_1 = require("./helpers");
async function discordApiRequest(method, endpoint, body, qs) {
const authentication = this.getNodeParameter('authentication', 0, 'webhook');
const headers = {};
const credentialType = (0, helpers_1.getCredentialsType)(authentication);
const options = {
headers,
method,
qs,
body,
url: `https://discord.com/api/v10${endpoint}`,
json: true,
};
if (credentialType === 'discordWebhookApi') {
const credentials = await this.getCredentials('discordWebhookApi');
options.url = credentials.webhookUri;
}
try {
const response = await helpers_1.requestApi.call(this, options, credentialType, endpoint);
const resetAfter = Number(response.headers['x-ratelimit-reset-after']);
const remaining = Number(response.headers['x-ratelimit-remaining']);
if (remaining === 0) {
await (0, n8n_workflow_1.sleep)(resetAfter);
}
else {
await (0, n8n_workflow_1.sleep)(20); //prevent exceeding global rate limit of 50 requests per second
}
return response.body || { success: true };
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
async function discordApiMultiPartRequest(method, endpoint, formData) {
const headers = {
'content-type': 'multipart/form-data; charset=utf-8',
};
const authentication = this.getNodeParameter('authentication', 0, 'webhook');
const credentialType = (0, helpers_1.getCredentialsType)(authentication);
const options = {
headers,
method,
formData,
url: `https://discord.com/api/v10${endpoint}`,
};
if (credentialType === 'discordWebhookApi') {
const credentials = await this.getCredentials('discordWebhookApi');
options.url = credentials.webhookUri;
}
try {
const response = await helpers_1.requestApi.call(this, options, credentialType, endpoint);
const resetAfter = Number(response.headers['x-ratelimit-reset-after']);
const remaining = Number(response.headers['x-ratelimit-remaining']);
if (remaining === 0) {
await (0, n8n_workflow_1.sleep)(resetAfter);
}
else {
await (0, n8n_workflow_1.sleep)(20); //prevent exceeding global rate limit of 50 requests per second
}
return (0, n8n_workflow_1.jsonParse)(response.body);
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
//# sourceMappingURL=discord.api.js.map