refactor(core): fix for no-uncaught-json-parse warnings

This commit is contained in:
Michael Kret
2022-10-21 21:52:43 +03:00
committed by GitHub
parent ca9eca9ae9
commit 1d57b10942
36 changed files with 150 additions and 93 deletions

View File

@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import curlconverter from 'curlconverter';
import get from 'lodash.get';
import { jsonParse } from 'n8n-workflow';
interface CurlJson {
url: string;
@@ -109,8 +110,7 @@ const DOWNLOAD_FILE_FLAGS = ['-O', '-o'];
const IGNORE_SSL_ISSUES_FLAGS = ['-k', '--insecure'];
const curlToJson = (curlCommand: string): CurlJson => {
// eslint-disable-next-line n8n-local-rules/no-uncaught-json-parse
return JSON.parse(curlconverter.toJsonString(curlCommand)) as CurlJson;
return jsonParse(curlconverter.toJsonString(curlCommand));
};
const isContentType = (headers: CurlJson['headers'], contentType: ContentTypes): boolean => {
@@ -196,8 +196,7 @@ const extractQueries = (queries: CurlJson['queries'] = {}): HttpNodeQueries => {
const extractJson = (body: CurlJson['data']) =>
//@ts-ignore
// eslint-disable-next-line n8n-local-rules/no-uncaught-json-parse
JSON.parse(Object.keys(body)[0]) as { [key: string]: string };
jsonParse<{ [key: string]: string }>(Object.keys(body)[0]);
const jsonBodyToNodeParameters = (body: CurlJson['data'] = {}): Parameter[] | [] => {
const data = extractJson(body);

View File

@@ -13,6 +13,7 @@ import {
IDataObject,
INode,
IRunExecutionData,
jsonParse,
Workflow,
WorkflowExecuteMode,
} from 'n8n-workflow';
@@ -28,6 +29,7 @@ import {
IPackageVersions,
IWorkflowDb,
ResponseHelper,
IN8nNodePackageJson,
} from '.';
// eslint-disable-next-line import/order
import { Like } from 'typeorm';
@@ -74,7 +76,7 @@ export async function getVersions(): Promise<IPackageVersions> {
const packageFile = await fsReadFile(pathJoin(__dirname, '../../package.json'), 'utf8');
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const packageData = JSON.parse(packageFile);
const packageData = jsonParse<IN8nNodePackageJson>(packageFile);
versionCache = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment

View File

@@ -21,6 +21,7 @@ import {
INodeTypeNameVersion,
INodeVersionedType,
LoggerProxy,
jsonParse,
} from 'n8n-workflow';
import {
@@ -509,7 +510,7 @@ class LoadNodesAndCredentialsClass {
async readPackageJson(packagePath: string): Promise<IN8nNodePackageJson> {
// Get the absolute path of the package
const packageFileString = await fsReadFile(path.join(packagePath, 'package.json'), 'utf8');
return JSON.parse(packageFileString) as IN8nNodePackageJson;
return jsonParse(packageFileString);
}
/**

View File

@@ -1,3 +1,4 @@
import { jsonParse } from 'n8n-workflow';
import {
CursorPagination,
OffsetPagination,
@@ -6,9 +7,7 @@ import {
} from '../../../types';
export const decodeCursor = (cursor: string): PaginationOffsetDecoded | PaginationCursorDecoded => {
return JSON.parse(Buffer.from(cursor, 'base64').toString()) as
| PaginationCursorDecoded
| PaginationOffsetDecoded;
return jsonParse(Buffer.from(cursor, 'base64').toString());
};
const encodeOffSetPagination = (pagination: OffsetPagination): string | null => {

View File

@@ -67,6 +67,7 @@ import {
ITelemetrySettings,
LoggerProxy,
NodeHelpers,
jsonParse,
WebhookHttpMethod,
WorkflowExecuteMode,
} from 'n8n-workflow';
@@ -787,20 +788,20 @@ class App {
`/${this.restEndpoint}/node-parameter-options`,
ResponseHelper.send(
async (req: NodeParameterOptionsRequest): Promise<INodePropertyOptions[]> => {
const nodeTypeAndVersion = JSON.parse(
const nodeTypeAndVersion = jsonParse(
req.query.nodeTypeAndVersion,
) as INodeTypeNameVersion;
const { path, methodName } = req.query;
const currentNodeParameters = JSON.parse(
const currentNodeParameters = jsonParse(
req.query.currentNodeParameters,
) as INodeParameters;
let credentials: INodeCredentials | undefined;
if (req.query.credentials) {
credentials = JSON.parse(req.query.credentials);
credentials = jsonParse(req.query.credentials);
}
const loadDataInstance = new LoadNodeParameterOptions(
@@ -823,7 +824,7 @@ class App {
if (req.query.loadOptions) {
return loadDataInstance.getOptionsViaRequestProperty(
// @ts-ignore
JSON.parse(req.query.loadOptions as string),
jsonParse(req.query.loadOptions as string),
additionalData,
);
}
@@ -842,7 +843,7 @@ class App {
req: NodeListSearchRequest,
res: express.Response,
): Promise<INodeListSearchResult | undefined> => {
const nodeTypeAndVersion = JSON.parse(
const nodeTypeAndVersion = jsonParse(
req.query.nodeTypeAndVersion,
) as INodeTypeNameVersion;
@@ -852,14 +853,14 @@ class App {
throw new ResponseError('Parameter currentNodeParameters is required.', undefined, 400);
}
const currentNodeParameters = JSON.parse(
const currentNodeParameters = jsonParse(
req.query.currentNodeParameters,
) as INodeParameters;
let credentials: INodeCredentials | undefined;
if (req.query.credentials) {
credentials = JSON.parse(req.query.credentials);
credentials = jsonParse(req.query.credentials);
}
const listSearchInstance = new LoadNodeListSearch(
@@ -1454,7 +1455,7 @@ class App {
if (!sharedWorkflowIds.length) return [];
if (req.query.filter) {
const { workflowId } = JSON.parse(req.query.filter);
const { workflowId } = jsonParse<any>(req.query.filter);
if (workflowId && sharedWorkflowIds.includes(workflowId)) {
Object.assign(findOptions.where!, { workflowId });
}
@@ -1481,7 +1482,7 @@ class App {
const returnData: IExecutionsSummary[] = [];
const filter = req.query.filter ? JSON.parse(req.query.filter) : {};
const filter = req.query.filter ? jsonParse<any>(req.query.filter) : {};
const sharedWorkflowIds = await getSharedWorkflowIds(req.user).then((ids) =>
ids.map((id) => id.toString()),

View File

@@ -11,7 +11,7 @@
import express from 'express';
import _, { cloneDeep } from 'lodash';
import { BinaryDataManager } from 'n8n-core';
import { IDataObject, IWorkflowBase, LoggerProxy, Workflow } from 'n8n-workflow';
import { IDataObject, IWorkflowBase, LoggerProxy, jsonParse, Workflow } from 'n8n-workflow';
import { FindManyOptions, In, IsNull, LessThanOrEqual, Not, Raw } from 'typeorm';
import {
@@ -111,7 +111,7 @@ async function getExecutionsCount(
executionsController.get(
'/',
ResponseHelper.send(async (req: ExecutionRequest.GetAll): Promise<IExecutionsListResponse> => {
const filter = req.query.filter ? JSON.parse(req.query.filter) : {};
const filter = req.query.filter ? jsonParse<IDataObject>(req.query.filter) : {};
const limit = req.query.limit
? parseInt(req.query.limit, 10)

View File

@@ -1,3 +1,4 @@
import { jsonParse } from 'n8n-workflow';
import { ValueTransformer } from 'typeorm';
import config from '../../../config';
@@ -16,8 +17,7 @@ export const lowerCaser = {
*/
export const objectRetriever: ValueTransformer = {
to: (value: object): object => value,
from: (value: string | object): object =>
typeof value === 'string' ? (JSON.parse(value) as object) : value,
from: (value: string | object): object => (typeof value === 'string' ? jsonParse(value) : value),
};
/**
@@ -27,8 +27,7 @@ export const objectRetriever: ValueTransformer = {
const jsonColumn: ValueTransformer = {
to: (value: object): string | object =>
config.getEnv('database.type') === 'sqlite' ? JSON.stringify(value) : value,
from: (value: string | object): object =>
typeof value === 'string' ? (JSON.parse(value) as object) : value,
from: (value: string | object): object => (typeof value === 'string' ? jsonParse(value) : value),
};
export const sqlite = { jsonColumn };