fix: normalize json payload fields across nodes

This commit is contained in:
Jesse Freitas 2026-03-16 09:14:03 -03:00
parent 6bb07609a3
commit 381e361814
4 changed files with 127 additions and 23 deletions

View file

@ -424,6 +424,38 @@ export class MegaClient implements INodeType {
const returnData: INodeExecutionData[] = [];
const credentials = await this.getCredentials('megaClientApi');
const inboxIdentifier = credentials.inboxIdentifier as string;
const parseOptionalObject = (
value: unknown,
itemIndex: number,
parameterName: string,
): IDataObject => {
if (value === undefined || value === null || value === '') {
return {};
}
let parsedValue = value;
if (typeof parsedValue === 'string') {
try {
parsedValue = JSON.parse(parsedValue);
} catch {
throw new NodeOperationError(
this.getNode(),
`${parameterName} must be a valid JSON object`,
{ itemIndex },
);
}
}
if (typeof parsedValue !== 'object' || Array.isArray(parsedValue)) {
throw new NodeOperationError(
this.getNode(),
`${parameterName} must be a JSON object`,
{ itemIndex },
);
}
return parsedValue as IDataObject;
};
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
@ -442,14 +474,19 @@ export class MegaClient implements INodeType {
'clientContactCustomAttributes',
itemIndex,
{},
) as IDataObject;
);
const parsedCustomAttributes = parseOptionalObject(
customAttributes,
itemIndex,
'clientContactCustomAttributes',
);
if (identifier.trim()) body.identifier = identifier;
if (identifierHash.trim()) body.identifier_hash = identifierHash;
if (email.trim()) body.email = email;
if (name.trim()) body.name = name;
if (phoneNumber.trim()) body.phone_number = phoneNumber;
if (Object.keys(customAttributes).length > 0) body.custom_attributes = customAttributes;
if (Object.keys(parsedCustomAttributes).length > 0) body.custom_attributes = parsedCustomAttributes;
response = (await megaClientApiRequest.call(
this,
@ -476,14 +513,19 @@ export class MegaClient implements INodeType {
'clientContactCustomAttributes',
itemIndex,
{},
) as IDataObject;
);
const parsedCustomAttributes = parseOptionalObject(
customAttributes,
itemIndex,
'clientContactCustomAttributes',
);
if (identifier.trim()) body.identifier = identifier;
if (identifierHash.trim()) body.identifier_hash = identifierHash;
if (email.trim()) body.email = email;
if (name.trim()) body.name = name;
if (phoneNumber.trim()) body.phone_number = phoneNumber;
if (Object.keys(customAttributes).length > 0) body.custom_attributes = customAttributes;
if (Object.keys(parsedCustomAttributes).length > 0) body.custom_attributes = parsedCustomAttributes;
response = (await megaClientApiRequest.call(
this,
@ -504,12 +546,17 @@ export class MegaClient implements INodeType {
'clientConversationCustomAttributes',
itemIndex,
{},
) as IDataObject;
);
const parsedCustomAttributes = parseOptionalObject(
customAttributes,
itemIndex,
'clientConversationCustomAttributes',
);
response = (await megaClientApiRequest.call(
this,
'POST',
`/public/api/v1/inboxes/${inboxIdentifier}/contacts/${contactIdentifier}/conversations`,
{ custom_attributes: customAttributes },
{ custom_attributes: parsedCustomAttributes },
)) as IDataObject;
} else if (resource === 'conversation' && operation === 'get') {
const contactIdentifier = this.getNodeParameter('clientContactIdentifier', itemIndex) as string;

View file

@ -791,6 +791,38 @@ export class MegaPlatform implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
const parseOptionalObject = (
value: unknown,
itemIndex: number,
parameterName: string,
): IDataObject => {
if (value === undefined || value === null || value === '') {
return {};
}
let parsedValue = value;
if (typeof parsedValue === 'string') {
try {
parsedValue = JSON.parse(parsedValue);
} catch {
throw new NodeOperationError(
this.getNode(),
`${parameterName} must be a valid JSON object`,
{ itemIndex },
);
}
}
if (typeof parsedValue !== 'object' || Array.isArray(parsedValue)) {
throw new NodeOperationError(
this.getNode(),
`${parameterName} must be a JSON object`,
{ itemIndex },
);
}
return parsedValue as IDataObject;
};
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
@ -799,6 +831,16 @@ export class MegaPlatform implements INodeType {
let response: IDataObject;
if (resource === 'account' && operation === 'create') {
const limits = parseOptionalObject(
this.getNodeParameter('platformAccountLimits', itemIndex, {}),
itemIndex,
'platformAccountLimits',
);
const customAttributes = parseOptionalObject(
this.getNodeParameter('platformAccountCustomAttributes', itemIndex, {}),
itemIndex,
'platformAccountCustomAttributes',
);
response = (await megaPlatformApiRequest.call(this, 'POST', '/platform/api/v1/accounts', {
name: this.getNodeParameter('platformAccountName', itemIndex) as string,
locale: this.getNodeParameter('platformAccountLocale', itemIndex, 'en') as string,
@ -809,12 +851,8 @@ export class MegaPlatform implements INodeType {
'',
) as string,
status: this.getNodeParameter('platformAccountStatus', itemIndex, 'active') as string,
limits: this.getNodeParameter('platformAccountLimits', itemIndex, {}) as IDataObject,
custom_attributes: this.getNodeParameter(
'platformAccountCustomAttributes',
itemIndex,
{},
) as IDataObject,
limits,
custom_attributes: customAttributes,
})) as IDataObject;
} else if (resource === 'account' && operation === 'get') {
const accountId = this.getNodeParameter('platformAccountId', itemIndex) as number;
@ -832,9 +870,21 @@ export class MegaPlatform implements INodeType {
) as IDataObject;
const body: IDataObject = {};
if (updateFields.customAttributes !== undefined) body.custom_attributes = updateFields.customAttributes;
if (updateFields.customAttributes !== undefined) {
body.custom_attributes = parseOptionalObject(
updateFields.customAttributes,
itemIndex,
'platformAccountUpdateFields.values.customAttributes',
);
}
if (updateFields.domain !== undefined) body.domain = updateFields.domain;
if (updateFields.limits !== undefined) body.limits = updateFields.limits;
if (updateFields.limits !== undefined) {
body.limits = parseOptionalObject(
updateFields.limits,
itemIndex,
'platformAccountUpdateFields.values.limits',
);
}
if (updateFields.locale !== undefined) body.locale = updateFields.locale;
if (updateFields.name !== undefined) body.name = updateFields.name;
if (updateFields.status !== undefined) body.status = updateFields.status;
@ -946,16 +996,17 @@ export class MegaPlatform implements INodeType {
);
response = { success: true, id: agentBotId };
} else if (resource === 'user' && operation === 'create') {
const customAttributes = parseOptionalObject(
this.getNodeParameter('platformUserCustomAttributes', itemIndex, {}),
itemIndex,
'platformUserCustomAttributes',
);
response = (await megaPlatformApiRequest.call(this, 'POST', '/platform/api/v1/users', {
name: this.getNodeParameter('platformUserName', itemIndex) as string,
display_name: this.getNodeParameter('platformUserDisplayName', itemIndex, '') as string,
email: this.getNodeParameter('platformUserEmail', itemIndex) as string,
password: this.getNodeParameter('platformUserPassword', itemIndex) as string,
custom_attributes: this.getNodeParameter(
'platformUserCustomAttributes',
itemIndex,
{},
) as IDataObject,
custom_attributes: customAttributes,
})) as IDataObject;
} else if (resource === 'user' && operation === 'get') {
const userId = this.getNodeParameter('platformUserId', itemIndex) as number;
@ -969,7 +1020,13 @@ export class MegaPlatform implements INodeType {
const updateFields = this.getNodeParameter('platformUserUpdateFields.values', itemIndex, {}) as IDataObject;
const body: IDataObject = {};
if (updateFields.customAttributes !== undefined) body.custom_attributes = updateFields.customAttributes;
if (updateFields.customAttributes !== undefined) {
body.custom_attributes = parseOptionalObject(
updateFields.customAttributes,
itemIndex,
'platformUserUpdateFields.values.customAttributes',
);
}
if (updateFields.displayName !== undefined) body.display_name = updateFields.displayName;
if (updateFields.email !== undefined) body.email = updateFields.email;
if (updateFields.name !== undefined) body.name = updateFields.name;

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "@jessefreitas/n8n-nodes-mega",
"version": "0.4.10",
"version": "0.4.11",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@jessefreitas/n8n-nodes-mega",
"version": "0.4.10",
"version": "0.4.11",
"license": "MIT",
"devDependencies": {
"@n8n/node-cli": "0.23.0",

View file

@ -1,6 +1,6 @@
{
"name": "@jessefreitas/n8n-nodes-mega",
"version": "0.4.10",
"version": "0.4.11",
"description": "Trabalhe com a API do Mega",
"license": "MIT",
"homepage": "https://github.com/jessefreitas/n8n_community_mega",