Don't add duplicate body headers for Postman

This commit is contained in:
Gregory Schier
2024-07-23 12:41:01 -07:00
parent afaf4e62d8
commit bb3f948596

View File

@@ -1,4 +1,5 @@
import { Environment, Folder, HttpRequest, Model, Workspace } from '../../../types/models'; import { root } from 'postcss';
import { Environment, Folder, HttpHeader, HttpRequest, Model, Workspace } from '../../../types/models';
const POSTMAN_2_1_0_SCHEMA = 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'; const POSTMAN_2_1_0_SCHEMA = 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json';
const POSTMAN_2_0_0_SCHEMA = 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json'; const POSTMAN_2_0_0_SCHEMA = 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json';
@@ -66,6 +67,24 @@ export function pluginHookImport(
const bodyPatch = importBody(r.body); const bodyPatch = importBody(r.body);
const requestAuthPath = importAuth(r.auth); const requestAuthPath = importAuth(r.auth);
const authPatch = requestAuthPath.authenticationType == null ? globalAuth : requestAuthPath; const authPatch = requestAuthPath.authenticationType == null ? globalAuth : requestAuthPath;
const headers: HttpHeader[] = toArray(r.header).map((h) => {
return {
name: h.key,
value: h.value,
enabled: !h.disabled,
};
});
// Add body headers only if they don't already exist
for (const bodyPatchHeader of bodyPatch.headers) {
const existingHeader = headers.find(h => h.name.toLowerCase() === bodyPatchHeader.name.toLowerCase());
if (existingHeader) {
continue;
}
headers.push(bodyPatchHeader);
}
const request: ExportResources['httpRequests'][0] = { const request: ExportResources['httpRequests'][0] = {
model: 'http_request', model: 'http_request',
id: generateId('http_request'), id: generateId('http_request'),
@@ -78,17 +97,7 @@ export function pluginHookImport(
bodyType: bodyPatch.bodyType, bodyType: bodyPatch.bodyType,
authentication: authPatch.authentication, authentication: authPatch.authentication,
authenticationType: authPatch.authenticationType, authenticationType: authPatch.authenticationType,
headers: [ headers,
...bodyPatch.headers,
...authPatch.headers,
...toArray(r.header).map((h) => {
return {
name: h.key,
value: h.value,
enabled: !h.disabled,
};
}),
],
}; };
exportResources.httpRequests.push(request); exportResources.httpRequests.push(request);
} else { } else {
@@ -142,11 +151,10 @@ function convertUrl(url: Record<string, any>) {
function importAuth( function importAuth(
rawAuth: any, rawAuth: any,
): Pick<HttpRequest, 'authentication' | 'authenticationType' | 'headers'> { ): Pick<HttpRequest, 'authentication' | 'authenticationType'> {
const auth = toRecord(rawAuth); const auth = toRecord(rawAuth);
if ('basic' in auth) { if ('basic' in auth) {
return { return {
headers: [],
authenticationType: 'basic', authenticationType: 'basic',
authentication: { authentication: {
username: auth.basic.username || '', username: auth.basic.username || '',
@@ -155,14 +163,13 @@ function importAuth(
}; };
} else if ('bearer' in auth) { } else if ('bearer' in auth) {
return { return {
headers: [],
authenticationType: 'bearer', authenticationType: 'bearer',
authentication: { authentication: {
token: auth.bearer.token || '', token: auth.bearer.token || '',
}, },
}; };
} else { } else {
return { headers: [], authenticationType: null, authentication: {} }; return { authenticationType: null, authentication: {} };
} }
} }
@@ -250,9 +257,9 @@ function importBody(rawBody: any): Pick<HttpRequest, 'body' | 'bodyType' | 'head
headers: [], headers: [],
bodyType: 'binary', bodyType: 'binary',
body: { body: {
filePath: body.file?.src filePath: body.file?.src,
} },
} };
} else { } else {
return { headers: [], bodyType: null, body: {} }; return { headers: [], bodyType: null, body: {} };
} }