n8n/packages/nodes-base/nodes/Mattermost/v1/transport/index.ts
Marcus 25a386dd70
fix(Mattermost Node): Fix base url trailing slash error (#6097)
* 🐛 Fix credential base url with trailing slash

* Fix credential test base url with trailing slash
2023-04-27 11:42:37 +02:00

59 lines
1.5 KiB
TypeScript

import type {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
GenericValue,
IDataObject,
IHttpRequestMethods,
IHttpRequestOptions,
} from 'n8n-workflow';
/**
* Make an API request to Mattermost
*/
export async function apiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: IHttpRequestMethods,
endpoint: string,
body: IDataObject | GenericValue | GenericValue[] = {},
query: IDataObject = {},
) {
const credentials = await this.getCredentials('mattermostApi');
const baseUrl = (credentials.baseUrl as string).replace(/\/$/, '');
const options: IHttpRequestOptions = {
method,
body,
qs: query,
url: `${baseUrl}/api/v4/${endpoint}`,
headers: {
'content-type': 'application/json; charset=utf-8',
},
skipSslCertificateValidation: credentials.allowUnauthorizedCerts as boolean,
};
return this.helpers.httpRequestWithAuthentication.call(this, 'mattermostApi', options);
}
export async function apiRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions,
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD',
endpoint: string,
body: IDataObject = {},
query: IDataObject = {},
) {
const returnData: IDataObject[] = [];
let responseData;
query.page = 0;
query.per_page = 100;
do {
responseData = await apiRequest.call(this, method, endpoint, body, query);
query.page++;
returnData.push.apply(returnData, responseData as IDataObject[]);
} while (responseData.length !== 0);
return returnData;
}