2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
2022-01-08 01:53:10 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IDataObject } from 'n8n-workflow';
|
|
|
|
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2022-01-08 01:53:10 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2022-01-08 01:53:10 -08:00
|
|
|
|
|
|
|
export async function msGraphSecurityApiRequest(
|
|
|
|
this: IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
headers: IDataObject = {},
|
|
|
|
) {
|
|
|
|
const {
|
2022-12-02 06:25:21 -08:00
|
|
|
oauthTokenData: { access_token },
|
2022-08-17 08:50:24 -07:00
|
|
|
} = (await this.getCredentials('microsoftGraphSecurityOAuth2Api')) as {
|
2022-01-08 01:53:10 -08:00
|
|
|
oauthTokenData: {
|
|
|
|
access_token: string;
|
2022-08-17 08:50:24 -07:00
|
|
|
};
|
2022-01-08 01:53:10 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${access_token}`,
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
uri: `https://graph.microsoft.com/v1.0/security${endpoint}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.keys(qs).length) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(headers).length) {
|
|
|
|
options.headers = { ...options.headers, ...headers };
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.request(options);
|
|
|
|
} catch (error) {
|
|
|
|
const nestedMessage = error?.error?.error?.message;
|
|
|
|
|
|
|
|
if (nestedMessage.startsWith('{"')) {
|
|
|
|
error = JSON.parse(nestedMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nestedMessage.startsWith('Http request failed with statusCode=BadRequest')) {
|
|
|
|
error.error.error.message = 'Request failed with bad request';
|
|
|
|
} else if (nestedMessage.startsWith('Http request failed with')) {
|
|
|
|
const stringified = nestedMessage.split(': ').pop();
|
|
|
|
if (stringified) {
|
|
|
|
error = JSON.parse(stringified);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (['Invalid filter clause', 'Invalid ODATA query filter'].includes(nestedMessage)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
error.error.error.message +=
|
|
|
|
' - Please check that your query parameter syntax is correct: https://docs.microsoft.com/en-us/graph/query-parameters#filter-parameter';
|
2022-01-08 01:53:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new NodeApiError(this.getNode(), error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function tolerateDoubleQuotes(filterQueryParameter: string) {
|
2022-12-29 03:20:43 -08:00
|
|
|
return filterQueryParameter.replace(/"/g, "'");
|
2022-01-08 01:53:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function throwOnEmptyUpdate(this: IExecuteFunctions) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Please enter at least one field to update');
|
2022-07-12 08:51:01 -07:00
|
|
|
}
|