refactor: Add IRequestOptions type to helpers.request for more type safety (no-changelog) (#8563)

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
Elias Meire
2024-02-14 16:29:09 +01:00
committed by GitHub
parent 24859cfef5
commit 100d9bc087
330 changed files with 1682 additions and 1492 deletions

View File

@@ -1,5 +1,9 @@
import type { IDataObject, INodeExecutionData, IOAuth2Options } from 'n8n-workflow';
import type { OptionsWithUri } from 'request-promise-native';
import type {
IDataObject,
INodeExecutionData,
IOAuth2Options,
IRequestOptions,
} from 'n8n-workflow';
import set from 'lodash/set';
@@ -16,14 +20,16 @@ export const replaceNullValues = (item: INodeExecutionData) => {
return item;
};
export function sanitizeUiMessage(request: OptionsWithUri, authDataKeys: IAuthDataSanitizeKeys) {
export function sanitizeUiMessage(request: IRequestOptions, authDataKeys: IAuthDataSanitizeKeys) {
let sendRequest = request as unknown as IDataObject;
// Protect browser from sending large binary data
if (Buffer.isBuffer(sendRequest.body) && sendRequest.body.length > 250000) {
sendRequest = {
...request,
body: `Binary data got replaced with this text. Original was a Buffer with a size of ${request.body.length} byte.`,
body: `Binary data got replaced with this text. Original was a Buffer with a size of ${
(request.body as string).length
} byte.`,
};
}

View File

@@ -8,10 +8,11 @@ import type {
INodeTypeBaseDescription,
INodeTypeDescription,
JsonObject,
IHttpRequestMethods,
IRequestOptions,
} from 'n8n-workflow';
import { NodeApiError, NodeOperationError, sleep, removeCircularRefs } from 'n8n-workflow';
import type { OptionsWithUri } from 'request';
import type { IAuthDataSanitizeKeys } from '../GenericFunctions';
import { replaceNullValues, sanitizeUiMessage } from '../GenericFunctions';
interface OptionData {
@@ -23,7 +24,7 @@ interface OptionDataParameters {
[key: string]: OptionData;
}
type OptionsWithUriKeys = keyof OptionsWithUri;
type IRequestOptionsKeys = keyof IRequestOptions;
export class HttpRequestV1 implements INodeType {
description: INodeTypeDescription;
@@ -635,7 +636,7 @@ export class HttpRequestV1 implements INodeType {
oAuth2Api = await this.getCredentials('oAuth2Api');
} catch {}
let requestOptions: OptionsWithUri & { useStream?: boolean };
let requestOptions: IRequestOptions;
let setUiParameter: IDataObject;
const uiParameters: IDataObject = {
@@ -661,7 +662,10 @@ export class HttpRequestV1 implements INodeType {
let returnItems: INodeExecutionData[] = [];
const requestPromises = [];
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
const requestMethod = this.getNodeParameter('requestMethod', itemIndex) as string;
const requestMethod = this.getNodeParameter(
'requestMethod',
itemIndex,
) as IHttpRequestMethods;
const parametersAreJson = this.getNodeParameter('jsonParameters', itemIndex);
const options = this.getNodeParameter('options', itemIndex, {});
@@ -688,7 +692,7 @@ export class HttpRequestV1 implements INodeType {
uri: url,
gzip: true,
rejectUnauthorized: !this.getNodeParameter('allowUnauthorizedCerts', itemIndex, false),
};
} satisfies IRequestOptions;
if (fullResponse) {
// @ts-ignore
@@ -815,7 +819,7 @@ export class HttpRequestV1 implements INodeType {
try {
// @ts-ignore
requestOptions[optionData.name] = JSON.parse(
requestOptions[optionData.name as OptionsWithUriKeys] as string,
requestOptions[optionData.name as IRequestOptionsKeys] as string,
);
} catch (error) {
throw new NodeOperationError(
@@ -848,8 +852,8 @@ export class HttpRequestV1 implements INodeType {
return newValue;
}
};
requestOptions[optionName][parameterDataName] = computeNewValue(
requestOptions[optionName][parameterDataName],
requestOptions[optionName]![parameterDataName] = computeNewValue(
requestOptions[optionName]![parameterDataName],
);
} else if (optionName === 'headers') {
// @ts-ignore

View File

@@ -3,15 +3,16 @@ import type { Readable } from 'stream';
import type {
IDataObject,
IExecuteFunctions,
IHttpRequestMethods,
INodeExecutionData,
INodeType,
INodeTypeBaseDescription,
INodeTypeDescription,
IRequestOptions,
JsonObject,
} from 'n8n-workflow';
import { NodeApiError, NodeOperationError, sleep, removeCircularRefs } from 'n8n-workflow';
import type { OptionsWithUri } from 'request';
import type { IAuthDataSanitizeKeys } from '../GenericFunctions';
import {
getOAuth2AdditionalParameters,
@@ -28,7 +29,7 @@ interface OptionDataParameters {
[key: string]: OptionData;
}
type OptionsWithUriKeys = keyof OptionsWithUri;
type IRequestOptionsKeys = keyof IRequestOptions;
export class HttpRequestV2 implements INodeType {
description: INodeTypeDescription;
@@ -675,7 +676,7 @@ export class HttpRequestV2 implements INodeType {
} catch {}
}
let requestOptions: OptionsWithUri & { useStream?: boolean };
let requestOptions: IRequestOptions & { useStream?: boolean };
let setUiParameter: IDataObject;
const uiParameters: IDataObject = {
@@ -701,7 +702,10 @@ export class HttpRequestV2 implements INodeType {
let returnItems: INodeExecutionData[] = [];
const requestPromises = [];
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
const requestMethod = this.getNodeParameter('requestMethod', itemIndex) as string;
const requestMethod = this.getNodeParameter(
'requestMethod',
itemIndex,
) as IHttpRequestMethods;
const parametersAreJson = this.getNodeParameter('jsonParameters', itemIndex);
const options = this.getNodeParameter('options', itemIndex, {});
@@ -855,7 +859,7 @@ export class HttpRequestV2 implements INodeType {
try {
// @ts-ignore
requestOptions[optionData.name] = JSON.parse(
requestOptions[optionData.name as OptionsWithUriKeys] as string,
requestOptions[optionData.name as IRequestOptionsKeys] as string,
);
} catch (error) {
throw new NodeOperationError(
@@ -888,8 +892,8 @@ export class HttpRequestV2 implements INodeType {
return newValue;
}
};
requestOptions[optionName][parameterDataName] = computeNewValue(
requestOptions[optionName][parameterDataName],
requestOptions[optionName]![parameterDataName] = computeNewValue(
requestOptions[optionName]![parameterDataName],
);
} else if (optionName === 'headers') {
// @ts-ignore

View File

@@ -11,6 +11,8 @@ import type {
IRequestOptionsSimplified,
PaginationOptions,
JsonObject,
IRequestOptions,
IHttpRequestMethods,
} from 'n8n-workflow';
import {
@@ -22,8 +24,6 @@ import {
sleep,
} from 'n8n-workflow';
import type { OptionsWithUri } from 'request-promise-native';
import type { BodyParameter, IAuthDataSanitizeKeys } from '../GenericFunctions';
import {
binaryContentTypes,
@@ -1223,8 +1223,7 @@ export class HttpRequestV3 implements INodeType {
let nodeCredentialType: string | undefined;
let genericCredentialType: string | undefined;
type RequestOptions = OptionsWithUri & { useStream?: boolean };
let requestOptions: RequestOptions = {
let requestOptions: IRequestOptions = {
uri: '',
};
@@ -1279,7 +1278,7 @@ export class HttpRequestV3 implements INodeType {
nodeCredentialType = this.getNodeParameter('nodeCredentialType', itemIndex) as string;
}
const requestMethod = this.getNodeParameter('method', itemIndex) as string;
const requestMethod = this.getNodeParameter('method', itemIndex) as IHttpRequestMethods;
const sendQuery = this.getNodeParameter('sendQuery', itemIndex, false) as boolean;
const queryParameters = this.getNodeParameter(
@@ -1465,10 +1464,10 @@ export class HttpRequestV3 implements INodeType {
// Change the way data get send in case a different content-type than JSON got selected
if (sendBody && ['PATCH', 'POST', 'PUT', 'GET'].includes(requestMethod)) {
if (bodyContentType === 'multipart-form-data') {
requestOptions.formData = requestOptions.body;
requestOptions.formData = requestOptions.body as IDataObject;
delete requestOptions.body;
} else if (bodyContentType === 'form-urlencoded') {
requestOptions.form = requestOptions.body;
requestOptions.form = requestOptions.body as IDataObject;
delete requestOptions.body;
} else if (bodyContentType === 'binaryData') {
const inputDataFieldName = this.getNodeParameter(
@@ -1608,7 +1607,7 @@ export class HttpRequestV3 implements INodeType {
authDataKeys.headers = Object.keys(customAuth.headers);
}
if (customAuth.body) {
requestOptions.body = { ...requestOptions.body, ...customAuth.body };
requestOptions.body = { ...(requestOptions.body as IDataObject), ...customAuth.body };
authDataKeys.body = Object.keys(customAuth.body);
}
if (customAuth.qs) {