Skip to content

Commit

Permalink
feat(code-export): add support to grapqhl (#1288)
Browse files Browse the repository at this point in the history
Co-authored-by: Anoop M D <anoop.md1421@gmail.com>
  • Loading branch information
ajubin and helloanoop authored Sep 25, 2024
1 parent 3a58c6d commit 5889e11
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ const CodeView = ({ language, item }) => {

let snippet = '';
try {
snippet = new HTTPSnippet(buildHarRequest({ request: item.request, headers })).convert(target, client);
snippet = new HTTPSnippet(buildHarRequest({ request: item.request, headers, type: item.type })).convert(
target,
client
);
} catch (e) {
console.error(e);
snippet = 'Error generating code snippet';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ const CollectionItem = ({ item, collection, searchText }) => {
Run
</div>
)}
{!isFolder && item.type === 'http-request' && (
{!isFolder && (
<div
className="dropdown-item"
onClick={(e) => {
Expand Down
14 changes: 11 additions & 3 deletions packages/bruno-app/src/utils/codegenerator/har.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const createHeaders = (request, headers) => {
if (contentType !== '') {
enabledHeaders.push({ name: 'content-type', value: contentType });
}

return enabledHeaders;
};

Expand All @@ -43,7 +44,14 @@ const createQuery = (queryParams = []) => {
}));
};

const createPostData = (body) => {
const createPostData = (body, type) => {
if (type === 'graphql-request') {
return {
mimeType: 'application/json',
text: JSON.stringify(body[body.mode])
};
}

const contentType = createContentType(body.mode);
if (body.mode === 'formUrlEncoded' || body.mode === 'multipartForm') {
return {
Expand All @@ -64,15 +72,15 @@ const createPostData = (body) => {
}
};

export const buildHarRequest = ({ request, headers }) => {
export const buildHarRequest = ({ request, headers, type }) => {
return {
method: request.method,
url: encodeURI(request.url),
httpVersion: 'HTTP/1.1',
cookies: [],
headers: createHeaders(request, headers),
queryString: createQuery(request.params),
postData: createPostData(request.body),
postData: createPostData(request.body, type),
headersSize: 0,
bodySize: 0
};
Expand Down

0 comments on commit 5889e11

Please sign in to comment.