2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions, IHookFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2022-01-07 09:19:24 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2022-01-07 09:19:24 -08:00
|
|
|
GenericValue,
|
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
ICredentialTestFunctions,
|
|
|
|
IDataObject,
|
|
|
|
IHttpRequestOptions,
|
2023-02-27 19:39:43 -08:00
|
|
|
JsonObject,
|
2022-01-07 09:19:24 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2022-01-07 09:19:24 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to Mattermost
|
|
|
|
*/
|
|
|
|
export async function apiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD',
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject | GenericValue | GenericValue[] = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
) {
|
|
|
|
const credentials = await this.getCredentials('syncroMspApi');
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
query.api_key = credentials.apiKey;
|
2022-01-07 09:19:24 -08:00
|
|
|
|
|
|
|
const options: IHttpRequestOptions = {
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs: query,
|
|
|
|
url: `https://${credentials.subdomain}.syncromsp.com/api/v1/${endpoint}`,
|
|
|
|
headers: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.httpRequest(options);
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2022-01-07 09:19:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function apiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD',
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
) {
|
|
|
|
let returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
query.page = 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await apiRequest.call(this, method, endpoint, body, query);
|
|
|
|
query.page++;
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData = returnData.concat(responseData[endpoint] as IDataObject[]);
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (responseData[endpoint].length !== 0);
|
2022-01-07 09:19:24 -08:00
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function validateCredentials(
|
|
|
|
this: ICredentialTestFunctions,
|
|
|
|
decryptedCredentials: ICredentialDataDecryptedObject,
|
|
|
|
): Promise<any> {
|
2022-01-07 09:19:24 -08:00
|
|
|
const credentials = decryptedCredentials;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const { subdomain, apiKey } = credentials as {
|
|
|
|
subdomain: string;
|
|
|
|
apiKey: string;
|
2022-01-07 09:19:24 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const options: IHttpRequestOptions = {
|
|
|
|
method: 'GET',
|
|
|
|
qs: {
|
|
|
|
api_key: apiKey,
|
|
|
|
},
|
|
|
|
url: `https://${subdomain}.syncromsp.com/api/v1//me`,
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.helpers.request(options);
|
|
|
|
}
|