2020-08-26 00:09:07 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
2019-11-05 07:17:06 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2020-08-26 00:09:07 -07:00
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError,
|
2020-08-26 00:09:07 -07:00
|
|
|
} from 'n8n-workflow';
|
2020-08-17 13:41:05 -07:00
|
|
|
|
|
|
|
export async function todoistApiRequest(
|
|
|
|
this:
|
|
|
|
| IHookFunctions
|
|
|
|
| IExecuteFunctions
|
|
|
|
| ILoadOptionsFunctions,
|
|
|
|
method: string,
|
2020-08-26 00:09:07 -07:00
|
|
|
resource: string,
|
2020-08-26 00:28:39 -07:00
|
|
|
body: any = {}, // tslint:disable-line:no-any
|
2020-10-22 09:00:28 -07:00
|
|
|
qs: IDataObject = {},
|
2020-08-26 00:09:07 -07:00
|
|
|
): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const authentication = this.getNodeParameter('authentication', 0, 'apiKey');
|
2019-11-05 07:17:06 -08:00
|
|
|
|
2019-11-05 12:56:10 -08:00
|
|
|
const endpoint = 'api.todoist.com/rest/v1';
|
2019-11-05 07:17:06 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
2020-08-26 00:09:07 -07:00
|
|
|
headers: {},
|
2019-11-05 07:17:06 -08:00
|
|
|
method,
|
2020-08-26 00:09:07 -07:00
|
|
|
qs,
|
2019-11-05 12:56:10 -08:00
|
|
|
uri: `https://${endpoint}${resource}`,
|
2020-08-26 00:09:07 -07:00
|
|
|
json: true,
|
2019-11-05 07:17:06 -08:00
|
|
|
};
|
|
|
|
|
2019-11-06 23:40:12 -08:00
|
|
|
if (Object.keys(body).length !== 0) {
|
|
|
|
options.body = body;
|
2019-11-05 12:56:10 -08:00
|
|
|
}
|
2019-11-05 07:17:06 -08:00
|
|
|
|
|
|
|
try {
|
2020-08-26 00:09:07 -07:00
|
|
|
if (authentication === 'apiKey') {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('todoistApi') as IDataObject;
|
2020-08-26 00:09:07 -07:00
|
|
|
|
|
|
|
//@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);
|
|
|
|
}
|
|
|
|
|
2019-11-05 07:17:06 -08:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2019-11-05 07:17:06 -08:00
|
|
|
}
|
|
|
|
}
|