mirror of
https://github.com/n8n-io/n8n.git
synced 2026-10-17 05:29:44 +02:00
fix: normalize multiOptions fields in Hunter node to handle string coercion
This commit is contained in:
parent
e8089b4ec7
commit
ff107a2564
|
|
@ -9,6 +9,28 @@ import type {
|
|||
} from 'n8n-workflow';
|
||||
import { NodeApiError } from 'n8n-workflow';
|
||||
|
||||
// A multiOptions parameter normally resolves to an array. When its value comes
|
||||
// from an expression that is wrapped in surrounding text/whitespace, n8n
|
||||
// switches to string interpolation and the array is coerced to a comma-joined
|
||||
// string. Accept both shapes so the node degrades gracefully instead of
|
||||
// throwing a low-level "join is not a function" TypeError.
|
||||
export function toMultiOptionsCsv(value: unknown): string {
|
||||
if (Array.isArray(value)) {
|
||||
return value
|
||||
.map((entry) => String(entry).trim())
|
||||
.filter((entry) => entry.length > 0)
|
||||
.join(',');
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
return value
|
||||
.split(',')
|
||||
.map((entry) => entry.trim())
|
||||
.filter((entry) => entry.length > 0)
|
||||
.join(',');
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
export async function hunterApiRequest(
|
||||
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
||||
method: IHttpRequestMethods,
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import type {
|
|||
} from 'n8n-workflow';
|
||||
import { NodeConnectionTypes } from 'n8n-workflow';
|
||||
|
||||
import { hunterApiRequest, hunterApiRequestAllItems } from './GenericFunctions';
|
||||
import { hunterApiRequest, hunterApiRequestAllItems, toMultiOptionsCsv } from './GenericFunctions';
|
||||
|
||||
export class Hunter implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
|
|
@ -295,10 +295,10 @@ export class Hunter implements INodeType {
|
|||
qs.type = filters.type;
|
||||
}
|
||||
if (filters.seniority) {
|
||||
qs.seniority = (filters.seniority as string[]).join(',');
|
||||
qs.seniority = toMultiOptionsCsv(filters.seniority);
|
||||
}
|
||||
if (filters.department) {
|
||||
qs.department = (filters.department as string[]).join(',');
|
||||
qs.department = toMultiOptionsCsv(filters.department);
|
||||
}
|
||||
if (returnAll) {
|
||||
responseData = await hunterApiRequestAllItems.call(
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
import { toMultiOptionsCsv } from '../GenericFunctions';
|
||||
|
||||
describe('Hunter > GenericFunctions', () => {
|
||||
describe('toMultiOptionsCsv', () => {
|
||||
it('joins array values', () => {
|
||||
expect(toMultiOptionsCsv(['junior', 'senior'])).toBe('junior,senior');
|
||||
});
|
||||
|
||||
it('trims entries inside an array (interpolated array elements)', () => {
|
||||
expect(toMultiOptionsCsv(['junior ', ' senior'])).toBe('junior,senior');
|
||||
});
|
||||
|
||||
it('coerces non-string array entries via String()', () => {
|
||||
expect(toMultiOptionsCsv([1, 2, 3])).toBe('1,2,3');
|
||||
});
|
||||
|
||||
it('accepts a comma-joined string (the whitespace-expression coercion case)', () => {
|
||||
expect(toMultiOptionsCsv('junior,senior')).toBe('junior,senior');
|
||||
});
|
||||
|
||||
it('trims whitespace around each entry in a comma-string', () => {
|
||||
expect(toMultiOptionsCsv(' junior , senior ')).toBe('junior,senior');
|
||||
});
|
||||
|
||||
it('drops empty entries from an array', () => {
|
||||
expect(toMultiOptionsCsv(['junior', '', ' ', 'senior'])).toBe('junior,senior');
|
||||
});
|
||||
|
||||
it('drops empty entries from a string', () => {
|
||||
expect(toMultiOptionsCsv('junior,,senior')).toBe('junior,senior');
|
||||
});
|
||||
|
||||
it('returns an empty string for an empty array', () => {
|
||||
expect(toMultiOptionsCsv([])).toBe('');
|
||||
});
|
||||
|
||||
it('returns an empty string for an empty string', () => {
|
||||
expect(toMultiOptionsCsv('')).toBe('');
|
||||
});
|
||||
|
||||
it('returns an empty string for non-string non-array values', () => {
|
||||
expect(toMultiOptionsCsv(undefined)).toBe('');
|
||||
expect(toMultiOptionsCsv(null)).toBe('');
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user