mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Add generic functions stub
This commit is contained in:
parent
0acc90043f
commit
148e48247e
|
@ -12,16 +12,12 @@ import {
|
|||
IDataObject,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import WebSocket = require('ws');
|
||||
|
||||
export async function discordApiRequest(
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions,
|
||||
method: string,
|
||||
resource: string,
|
||||
body: IDataObject = {},
|
||||
qs: IDataObject = {},
|
||||
uri?: string,
|
||||
option: IDataObject = {},
|
||||
) {
|
||||
|
||||
const { oauthTokenData } = this.getCredentials('discordOAuth2Api') as {
|
||||
|
@ -31,50 +27,35 @@ export async function discordApiRequest(
|
|||
const options: OptionsWithUri = {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'user-agent': 'n8n',
|
||||
Authorization: `Bearer ${oauthTokenData.access_token}`,
|
||||
},
|
||||
method,
|
||||
body,
|
||||
qs,
|
||||
uri: uri || `https://discord.com/api${resource}`,
|
||||
uri: `https://discord.com/api${resource}`,
|
||||
json: true,
|
||||
};
|
||||
|
||||
if (Object.keys(option).length) {
|
||||
Object.assign(options, option);
|
||||
if (!Object.keys(body).length) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
if (!Object.keys(qs).length) {
|
||||
delete options.qs;
|
||||
}
|
||||
|
||||
if (!Object.keys(body).length) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
const oAuth2Options = {
|
||||
includeCredentialsOnRefreshOnBody: true,
|
||||
};
|
||||
|
||||
try {
|
||||
// console.log(options);
|
||||
console.log(options);
|
||||
return await this.helpers.requestOAuth2!.call(this, 'discordOAuth2Api', options, oAuth2Options);
|
||||
// return await this.helpers.request!.call(this, options);
|
||||
} catch (error) {
|
||||
|
||||
// TODO
|
||||
// TODO: Prettify error
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export function setHeartbeatInterval(ws: WebSocket, data: string) {
|
||||
const interval = JSON.parse(data).d.heartbeat_interval;
|
||||
|
||||
const payload = JSON.stringify({
|
||||
op: 1, // opcode
|
||||
d: 251, // last sequence number `s` received by client
|
||||
});
|
||||
|
||||
return setInterval(() => ws.send(payload), interval);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue