mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 22:54:05 -08:00
64 lines
1.3 KiB
TypeScript
64 lines
1.3 KiB
TypeScript
import {
|
|
OptionsWithUri,
|
|
} from 'request';
|
|
|
|
import {
|
|
IExecuteFunctions,
|
|
IHookFunctions,
|
|
ILoadOptionsFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
} from 'n8n-workflow';
|
|
|
|
export async function todoistApiRequest(
|
|
this:
|
|
| IHookFunctions
|
|
| IExecuteFunctions
|
|
| ILoadOptionsFunctions,
|
|
method: string,
|
|
resource: string,
|
|
body: any = {}, // tslint:disable-line:no-any
|
|
qs: IDataObject = {},
|
|
): Promise<any> { // tslint:disable-line:no-any
|
|
const authentication = this.getNodeParameter('authentication', 0, 'apiKey');
|
|
|
|
const endpoint = 'api.todoist.com/rest/v1';
|
|
|
|
const options: OptionsWithUri = {
|
|
headers: {},
|
|
method,
|
|
qs,
|
|
uri: `https://${endpoint}${resource}`,
|
|
json: true,
|
|
};
|
|
|
|
if (Object.keys(body).length !== 0) {
|
|
options.body = body;
|
|
}
|
|
|
|
try {
|
|
if (authentication === 'apiKey') {
|
|
const credentials = this.getCredentials('todoistApi') as IDataObject;
|
|
|
|
//@ts-ignore
|
|
options.headers['Authorization'] = `Bearer ${credentials.apiKey}`;
|
|
|
|
return this.helpers.request!(options);
|
|
} else {
|
|
//@ts-ignore
|
|
return await this.helpers.requestOAuth2.call(this, 'todoistOAuth2Api', options);
|
|
}
|
|
|
|
} catch (error) {
|
|
const errorMessage = error.response.body;
|
|
|
|
if (errorMessage !== undefined) {
|
|
throw new Error(errorMessage);
|
|
}
|
|
|
|
throw errorMessage;
|
|
}
|
|
}
|