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 {
|
|
|
|
IDataObject,
|
|
|
|
} 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-08-26 00:09:07 -07:00
|
|
|
qs: IDataObject = {},
|
|
|
|
): 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') {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-11-05 07:17:06 -08:00
|
|
|
} catch (error) {
|
2020-08-26 00:09:07 -07:00
|
|
|
const errorMessage = error.response.body;
|
2019-11-05 07:17:06 -08:00
|
|
|
|
|
|
|
if (errorMessage !== undefined) {
|
2020-08-26 00:09:07 -07:00
|
|
|
throw new Error(errorMessage);
|
2019-11-05 07:17:06 -08:00
|
|
|
}
|
2020-08-26 00:09:07 -07:00
|
|
|
|
|
|
|
throw errorMessage;
|
2019-11-05 07:17:06 -08:00
|
|
|
}
|
|
|
|
}
|