n8n/packages/nodes-base/nodes/Discord/GenericFunctions.ts

62 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-03-04 11:28:41 -08:00
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
export async function discordApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions,
method: string,
resource: string,
body: IDataObject = {},
qs: IDataObject = {},
) {
const { oauthTokenData } = this.getCredentials('discordOAuth2Api') as {
oauthTokenData: { access_token: string }
};
2021-03-04 11:28:41 -08:00
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
2021-03-10 15:00:32 -08:00
'user-agent': 'n8n',
Authorization: `Bearer ${oauthTokenData.access_token}`,
2021-03-04 11:28:41 -08:00
},
method,
body,
qs,
2021-03-10 15:00:32 -08:00
uri: `https://discord.com/api${resource}`,
2021-03-04 11:28:41 -08:00
json: true,
};
2021-03-10 15:00:32 -08:00
if (!Object.keys(body).length) {
delete options.body;
2021-03-04 11:28:41 -08:00
}
if (!Object.keys(qs).length) {
delete options.qs;
}
const oAuth2Options = {
includeCredentialsOnRefreshOnBody: true,
};
try {
2021-03-10 15:00:32 -08:00
console.log(options);
2021-03-04 11:28:41 -08:00
return await this.helpers.requestOAuth2!.call(this, 'discordOAuth2Api', options, oAuth2Options);
} catch (error) {
2021-03-10 15:00:32 -08:00
// TODO: Prettify error
2021-03-04 11:28:41 -08:00
throw error;
}
}