2020-10-13 04:00:14 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-10-13 04:00:14 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
IDataObject,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
|
|
|
NodeOperationError,
|
2020-10-13 04:00:14 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
export async function getAuthorization(
|
2022-08-17 08:50:24 -07:00
|
|
|
this:
|
|
|
|
| IHookFunctions
|
|
|
|
| IExecuteFunctions
|
|
|
|
| IExecuteSingleFunctions
|
|
|
|
| ILoadOptionsFunctions
|
|
|
|
| IWebhookFunctions,
|
2020-10-22 09:00:28 -07:00
|
|
|
credentials?: ICredentialDataDecryptedObject,
|
2020-10-13 04:00:14 -07:00
|
|
|
): Promise<IDataObject> {
|
|
|
|
if (credentials === undefined) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
2020-10-13 04:00:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const { password, username } = credentials;
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method: 'POST',
|
|
|
|
form: {
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
},
|
|
|
|
uri: `${credentials.url}/users/login`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await this.helpers.request!(options);
|
|
|
|
|
|
|
|
return { token: response.token, userId: response.id };
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-10-13 04:00:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function apiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: object,
|
|
|
|
query?: IDataObject,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('wekanApi');
|
2020-10-13 04:00:14 -07:00
|
|
|
|
|
|
|
query = query || {};
|
|
|
|
|
|
|
|
const { token } = await getAuthorization.call(this, credentials);
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
2022-08-17 08:50:24 -07:00
|
|
|
Accept: 'application/json',
|
|
|
|
Authorization: `Bearer ${token}`,
|
2020-10-13 04:00:14 -07:00
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs: query,
|
|
|
|
uri: `${credentials.url}/api/${endpoint}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.statusCode === 401) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'The Wekan credentials are not valid!');
|
2020-10-13 04:00:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|