Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/bruno-cli/src/utils/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const FORMAT_CONFIG = {
yml: { ext: '.yml', collectionFile: 'opencollection.yml', folderFile: 'folder.yml' },
bru: { ext: '.bru', collectionFile: 'collection.bru', folderFile: 'folder.bru' }
};
const REQUEST_ITEM_TYPES = ['http-request', 'graphql-request'];

const getCollectionFormat = (collectionPath) => {
if (fs.existsSync(path.join(collectionPath, 'opencollection.yml'))) return 'yml';
Expand Down Expand Up @@ -499,25 +500,24 @@ const processCollectionItems = async (items = [], currentPath) => {
if (item.seq) {
item.root.meta.seq = item.seq;
}
const folderContent = await stringifyFolder(item.root);
const folderContent = stringifyFolder(item.root, { format: 'bru' });
safeWriteFileSync(folderBruFilePath, folderContent);
}

// Process folder items recursively
if (item.items && item.items.length) {
await processCollectionItems(item.items, folderPath);
}
} else if (['http-request', 'graphql-request'].includes(item.type)) {
} else if (REQUEST_ITEM_TYPES.includes(item.type)) {
// Create request file
let sanitizedFilename = sanitizeName(item?.filename || `${item.name}.bru`);
if (!sanitizedFilename.endsWith('.bru')) {
sanitizedFilename += '.bru';
}

// Convert JSON to BRU format based on the item type
let type = item.type === 'http-request' ? 'http' : 'graphql';
const bruJson = {
type: type,
// Keep schema item type so filestore can stringify request correctly
type: item.type,
name: item.name,
seq: typeof item.seq === 'number' ? item.seq : 1,
tags: item.tags || [],
Expand All @@ -538,8 +538,10 @@ const processCollectionItems = async (items = [], currentPath) => {
};

// Convert to BRU format and write to file
const content = await stringifyRequest(bruJson);
const content = stringifyRequest(bruJson, { format: 'bru' });
safeWriteFileSync(path.join(currentPath, sanitizedFilename), content);
} else {
throw new Error(`Unsupported item type: ${item.type}`);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { describe, it, expect, afterEach } = require('@jest/globals');
const { parseRequest, parseFolder } = require('@usebruno/filestore');
const { createCollectionFromBrunoObject } = require('../../../src/utils/collection');

describe('createCollectionFromBrunoObject', () => {
let outputDir;

afterEach(() => {
if (outputDir && fs.existsSync(outputDir)) {
fs.rmSync(outputDir, { recursive: true, force: true });
}
});

it('writes and parses http request from imported collection items', async () => {
outputDir = fs.mkdtempSync(path.join(os.tmpdir(), 'bruno-cli-import-'));

await createCollectionFromBrunoObject(
{
name: 'imported-http-collection',
items: [
{
type: 'http-request',
name: 'Get Users',
filename: 'get-users.bru',
seq: 1,
request: {
method: 'GET',
url: 'https://api.example.com/users'
}
}
]
},
outputDir
);

const httpPath = path.join(outputDir, 'get-users.bru');
expect(fs.existsSync(httpPath)).toBe(true);

const httpRequest = parseRequest(fs.readFileSync(httpPath, 'utf8'), { format: 'bru' });
expect(httpRequest).toHaveProperty('request.method', 'GET');
});

it('writes and parses graphql request from imported collection items', async () => {
outputDir = fs.mkdtempSync(path.join(os.tmpdir(), 'bruno-cli-import-'));

await createCollectionFromBrunoObject(
{
name: 'imported-graphql-collection',
items: [
{
type: 'graphql-request',
name: 'Get Viewer',
filename: 'get-viewer.bru',
seq: 1,
request: {
method: 'POST',
url: 'https://api.example.com/graphql',
body: {
mode: 'graphql',
graphql: {
query: 'query { viewer { id } }',
variables: '{}'
}
}
}
}
]
},
outputDir
);

const graphqlPath = path.join(outputDir, 'get-viewer.bru');
expect(fs.existsSync(graphqlPath)).toBe(true);

const graphqlRequest = parseRequest(fs.readFileSync(graphqlPath, 'utf8'), { format: 'bru' });
expect(graphqlRequest).toHaveProperty('request.method', 'POST');
});

it('writes folder.bru in BRU format for folder items', async () => {
outputDir = fs.mkdtempSync(path.join(os.tmpdir(), 'bruno-cli-import-'));

await createCollectionFromBrunoObject(
{
name: 'folder-collection',
items: [
{
type: 'folder',
name: 'Users',
seq: 3,
root: {
meta: {
name: 'Users'
}
},
items: []
}
]
},
outputDir
);

const folderBruPath = path.join(outputDir, 'Users', 'folder.bru');
expect(fs.existsSync(folderBruPath)).toBe(true);

const folderRoot = parseFolder(fs.readFileSync(folderBruPath, 'utf8'), { format: 'bru' });
expect(folderRoot).toHaveProperty('meta.name', 'Users');
expect(folderRoot).toHaveProperty('meta.seq', 3);
});

it('throws for unsupported item types', async () => {
outputDir = fs.mkdtempSync(path.join(os.tmpdir(), 'bruno-cli-import-'));

await expect(
createCollectionFromBrunoObject(
{
name: 'invalid-type-collection',
items: [
{
type: 'htttttppp',
name: 'Invalid Request',
filename: 'invalid-request.bru',
request: {
method: 'GET',
url: 'https://api.example.com'
}
}
]
},
outputDir
)
).rejects.toThrow('Unsupported item type: htttttppp');
});
});