diff --git a/packages/bruno-app/src/utils/common/index.js b/packages/bruno-app/src/utils/common/index.js index 191fefc2..f5b3b2df 100644 --- a/packages/bruno-app/src/utils/common/index.js +++ b/packages/bruno-app/src/utils/common/index.js @@ -49,3 +49,15 @@ export const safeStringifyJSON = (obj, indent=false) => { return obj; } } + +// Remove any characters that are not alphanumeric, spaces, hyphens, or underscores +export const normalizeFileName = (name) => { + if (!name) { + return name; + } + + const validChars = /[^\w\s-]/g; + const formattedName = name.replace(validChars, '-'); + + return formattedName; +} diff --git a/packages/bruno-app/src/utils/common/index.spec.js b/packages/bruno-app/src/utils/common/index.spec.js new file mode 100644 index 00000000..30dd185e --- /dev/null +++ b/packages/bruno-app/src/utils/common/index.spec.js @@ -0,0 +1,19 @@ +const { describe, it, expect } = require("@jest/globals"); + +import { normalizeFileName } from './index'; + +describe("common utils", () => { + describe("normalizeFileName", () => { + it("should remove special characters", () => { + expect(normalizeFileName("hello world")).toBe("hello world"); + expect(normalizeFileName("hello-world")).toBe("hello-world"); + expect(normalizeFileName("hello_world")).toBe("hello_world"); + expect(normalizeFileName("hello_world-")).toBe("hello_world-"); + expect(normalizeFileName("hello_world-123")).toBe("hello_world-123"); + expect(normalizeFileName("hello_world-123!@#$%^&*()")).toBe("hello_world-123----------"); + expect(normalizeFileName("hello_world?")).toBe("hello_world-"); + expect(normalizeFileName("foo/bar/")).toBe("foo-bar-"); + expect(normalizeFileName("foo\\bar\\")).toBe("foo-bar-"); + }); + }); +}); \ No newline at end of file diff --git a/packages/bruno-app/src/utils/importers/common.js b/packages/bruno-app/src/utils/importers/common.js index 558d450a..47a20a00 100644 --- a/packages/bruno-app/src/utils/importers/common.js +++ b/packages/bruno-app/src/utils/importers/common.js @@ -3,7 +3,7 @@ import each from 'lodash/each'; import get from 'lodash/get'; import cloneDeep from 'lodash/cloneDeep'; -import { uuid } from 'utils/common'; +import { uuid, normalizeFileName } from 'utils/common'; import { isItemARequest } from 'utils/collections'; import { collectionSchema } from '@usebruno/schema'; import { BrunoError } from 'utils/common/error'; @@ -63,6 +63,8 @@ export const updateUidsInCollection = (_collection) => { export const transformItemsInCollection = (collection) => { const transformItems = (items = []) => { each(items, (item) => { + item.name = normalizeFileName(item.name); + if (['http', 'graphql'].includes(item.type)) { item.type = `${item.type}-request`; if(item.request.query) {