n8n/packages/nodes-base/utils/url.ts
n8n-assistant[bot] cae5e093d3
chore: Bundle 2.x (#37586)
Co-authored-by: Matsu <matias.huhta@n8n.io>
2026-09-02 08:16:39 +03:00

20 lines
742 B
TypeScript

import { UserError } from 'n8n-workflow';
/**
* Encode an id as a single URL path segment. `.`, `..`, and the empty string
* are rejected: they survive encodeURIComponent but are removed during path
* normalisation, so they cannot be encoded safely. Nullish values are
* rejected before encoding, since they would otherwise become the literal
* string "null" or "undefined".
*/
export function toPathSegment(id: unknown): string {
if (id === null || id === undefined) {
throw new UserError('Invalid identifier: a value is required');
}
const value = String(id);
if (value === '' || value === '.' || value === '..') {
throw new UserError(`Invalid identifier: "${value}" is not allowed`);
}
return encodeURIComponent(value);
}